1   /*
2    * Copyright (c) 2004-2005, University Health Network.  All rights reserved. Distributed under the BSD 
3    * license (see http://opensource.org/licenses/bsd-license.php).
4    *  
5    * Created on 18-Jan-2005
6    */
7   package ca.uhn.cache.util;
8   
9   import java.util.Date;
10  
11  import org.jmock.Mock;
12  import org.jmock.MockObjectTestCase;
13  import org.jmock.core.Constraint;
14  
15  import ca.uhn.cache.IDataItem;
16  import ca.uhn.cache.IQuery;
17  import ca.uhn.cache.IQueryResult;
18  import ca.uhn.cache.ISemanticCache;
19  import ca.uhn.cache.VolatilityEnum;
20  import ca.uhn.cache.exception.CacheException;
21  import ca.uhn.cache.impl.DataItem;
22  import ca.uhn.cache.impl.Query;
23  import ca.uhn.cache.impl.QueryResult;
24  
25  /***
26   * Unit tests for UnstationaryQueryProcessor. 
27   * 
28   * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
29   * @version $Revision: 1.2 $ updated on $Date: 2005/01/25 22:04:19 $ by $Author: aguevara $
30   */
31  public class UnstationaryQueryProcessorTest extends MockObjectTestCase {
32  
33      private Mock myCacheMock;
34      private ISemanticCache myCache;    
35  
36      /***
37       * @see TestCase#setUp()
38       */
39      protected void setUp() throws Exception {
40          super.setUp();
41          
42          myCacheMock = mock( ISemanticCache.class );
43          myCache = (ISemanticCache) myCacheMock.proxy();
44      }
45  
46      /***
47       * Constructor for UnstationaryQueryProcessorTest.
48       * @param theName ...
49       */
50      public UnstationaryQueryProcessorTest(String theName) {
51          super(theName);
52      }
53  
54      /***
55       * @throws CacheException ...
56       */
57      public void testGetUpdateBoundary() throws CacheException {
58          Date now = new Date();
59          
60          IQuery dummyQuery = new Query();
61          myCacheMock.expects( once() ).method( "remainder" ).will(returnValue(new IQuery[] {dummyQuery}));
62          UnstationaryQueryProcessor processor = new UnstationaryQueryProcessor(dummyQuery, myCache, 3, null);
63  
64          myCacheMock.expects( once() ).method( "getEarliestCacheTime" ).will(returnValue(now));
65          assertEquals(now, processor.getUpdateBoundary());
66      }
67  
68      /***
69       * @throws InterruptedException ...
70       * @throws CacheException ...
71       */
72      public void testBasicFlow() throws CacheException, InterruptedException {
73          IQuery dummyQuery = new Query();
74          Date date = new Date();        
75  
76          myCacheMock.expects( once() ).method( "remainder" ).will(returnValue(new IQuery[] {dummyQuery}));
77          UnstationaryQueryProcessor processor = new UnstationaryQueryProcessor(dummyQuery, myCache, 3, null);
78          
79          IQueryResult dummyResult = new QueryResult();
80          dummyResult.add(new DataItem(new String("a"), new String("a"), dummyQuery, VolatilityEnum.STABLE, date));
81          IQueryResult dummyUpdate = new QueryResult();
82  
83          myCacheMock.expects( once() ).method( "get" ).will( returnValue(dummyResult));
84          myCacheMock.expects( once() ).method( "update" )
85              .with(new Constraint[] {eq(dummyUpdate)});
86          processor.setUpdate(dummyUpdate);
87          
88          assertEquals(1, processor.getCombinedResult().size());
89          assertEquals("a", ((IDataItem) processor.getCombinedResult().iterator().next()).getValue());
90  
91          IQueryResult dummySourceResult = new QueryResult();
92          dummySourceResult.add(new DataItem(new String("b"), new String("b"), dummyQuery, VolatilityEnum.STABLE, date));
93          
94          myCacheMock.expects( once() ).method( "put" )
95              .with(new Constraint[] {eq(dummyQuery), eq(dummySourceResult)});
96          processor.setSourceResult(dummyQuery, dummySourceResult);
97          
98          //TODO: remove sleep when QueryResult can handle dynamic updates 
99          Thread.sleep(100);
100 
101         assertEquals(2, processor.getCombinedResult().size());
102     }
103 
104     /***
105      * @throws InterruptedException ...
106      * @throws CacheException ... 
107      */
108     public void testUnusualOrdering() throws CacheException, InterruptedException {
109         IQuery dummyQuery = new Query();
110         Date date = new Date();
111         myCacheMock.expects( once() ).method( "remainder" ).will(returnValue(new IQuery[] {dummyQuery}));
112         UnstationaryQueryProcessor processor = new UnstationaryQueryProcessor(dummyQuery, myCache, 3, null);
113         
114         IQueryResult dummySourceResult = new QueryResult();
115         dummySourceResult.add(new DataItem(new String("b"), new String("b"), dummyQuery, VolatilityEnum.STABLE, date));
116 
117         myCacheMock.expects( once() ).method( "put" )
118             .with(new Constraint[] {eq(dummyQuery), eq(dummySourceResult)});
119         processor.setSourceResult(dummyQuery, dummySourceResult);
120 
121         IQueryResult dummyResult = new QueryResult();
122 
123         dummyResult.add(new DataItem(new String("a"), new String("a"), dummyQuery, VolatilityEnum.STABLE, date));
124         IQueryResult dummyUpdate = new QueryResult();
125 
126         myCacheMock.expects( once() ).method( "get" ).will( returnValue(dummyResult));
127         myCacheMock.expects( once() ).method( "update" )
128             .with(new Constraint[] {eq(dummyUpdate)});
129         processor.setUpdate(dummyUpdate);
130         
131         //TODO: remove sleep when QueryResult can handle dynamic updates 
132         Thread.sleep(100);
133         
134         assertEquals(2, processor.getCombinedResult().size());
135     }
136 }