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    * QueryResultTest.java
6    *
7    * Created on 9-Dec-2004 at 11:40:16 AM
8    */
9   package ca.uhn.cache.impl;
10  
11  import java.util.Calendar;
12  import java.util.Date;
13  import java.util.HashSet;
14  import java.util.Set;
15  
16  import junit.framework.TestCase;
17  import junitx.framework.ArrayAssert;
18  
19  import org.apache.commons.collections.IteratorUtils;
20  import org.jmock.Mock;
21  
22  import ca.uhn.cache.IDataItem;
23  import ca.uhn.cache.VolatilityEnum;
24  
25  /***
26   * TODO complete javadoc for
27   * 
28   * @author <a href="mailto:alexei.guevara@uhn.on.ca">Alexei Guevara </a>
29   * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:51:48 $ by $Author: bryan_tripp $
30   */
31  public class QueryResultTest extends TestCase {
32      
33      //enable assertions
34      static {
35          ClassLoader.getSystemClassLoader().setPackageAssertionStatus( "ca.uhn.cache", true );
36      }
37  
38      private IDataItem myDataItem1;
39      private IDataItem myDataItem2;
40      private IDataItem myDataItem3;
41      private IDataItem myDataItem4;
42      
43      /*
44       * @see TestCase#setUp()
45       */
46      protected void setUp() throws Exception {
47          super.setUp();
48          myDataItem1 = (IDataItem) new Mock( IDataItem.class, "myDataItem1" ).proxy();
49          myDataItem2 = (IDataItem) new Mock( IDataItem.class, "myDataItem2" ).proxy();
50          myDataItem3 = (IDataItem) new Mock( IDataItem.class, "myDataItem3" ).proxy();
51          myDataItem4 = (IDataItem) new Mock( IDataItem.class, "myDataItem4" ).proxy();
52      }
53  
54      /*
55       * @see TestCase#tearDown()
56       */
57      protected void tearDown() throws Exception {
58          super.tearDown();
59      }
60      
61      /***
62       * a newly created query result has no data items.
63       */
64      public void test1() {
65          QueryResult qr = new QueryResult();
66          assertTrue( qr.isEmpty() );
67      }
68  
69      /***
70       */
71      public void test2() {
72          try {
73              QueryResult qr = new QueryResult( null );
74              fail( "AssertionError must be thrown" );
75          }
76          catch ( AssertionError e ) {
77          }
78      }
79  
80      /***
81       * a query result created with an initial set of data items must have them
82       * all (which implies that it won't empty).
83       */
84      public void test3() {
85          Set initialDataItems = new HashSet();
86          initialDataItems.add( myDataItem1 );
87          initialDataItems.add( myDataItem2 );
88  
89          QueryResult qr = new QueryResult( initialDataItems );
90  
91          assertTrue( !qr.isEmpty() );
92          assertEquals( initialDataItems, new HashSet( IteratorUtils.toList( qr.iterator() ) ) );
93      }
94      
95      /***
96       */
97      public void test4() {
98          QueryResult qr = new QueryResult();
99          
100         qr.add( myDataItem1 );
101         qr.add( myDataItem2 );
102         
103         assertTrue( !qr.isEmpty() );
104         ArrayAssert.assertEquivalenceArrays( 
105                 IteratorUtils.toArray( qr.iterator(), IDataItem.class ),
106                 new IDataItem[] { myDataItem1, myDataItem2 } );
107     }
108     
109     /***
110      */
111     public void test5() {
112         QueryResult qr1 = new QueryResult();
113         qr1.add( myDataItem1 );
114         qr1.add( myDataItem2 );
115         
116         QueryResult qr2 = new QueryResult();
117         qr2.add( myDataItem3 );
118         qr2.add( myDataItem4 );
119 
120         QueryResult qr1and2 = new QueryResult();
121         qr1and2.add( myDataItem1 );
122         qr1and2.add( myDataItem2 );
123         qr1and2.add( myDataItem3 );
124         qr1and2.add( myDataItem4 );
125 
126         assertEquals( qr1and2.size(), qr1.size() + qr2.size() );
127         assertEquals( qr1and2, qr1.append( qr2 ) );
128     }
129     
130     /***
131      */
132     public void test6() {
133         Calendar c = Calendar.getInstance();
134         
135         QueryResult qr1 = new QueryResult();
136         Date date = new Date();
137         
138         DataItem di1_1 = new DataItem( "id", "", new Query(), VolatilityEnum.STABLE, date );
139         qr1.add( di1_1 );
140 
141         QueryResult qr2 = new QueryResult();
142         DataItem di2_1 = new DataItem( "id", "", new Query(), VolatilityEnum.STABLE, date );        
143         qr2.add( di2_1 );
144         
145         assertEquals( qr1, qr2 );
146     }
147     
148     
149 
150 }