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    * DateRangeParamTest.java
6    *
7    * Created on 7-Dec-2004 at 12:41:22 PM
8    */
9   package ca.uhn.cache.impl;
10  
11  import java.util.Date;
12  
13  import junit.framework.TestCase;
14  import ca.uhn.cache.IDimension;
15  import ca.uhn.cache.IQueryParam;
16  
17  
18  /***
19   * TODO complete javadoc for 
20   * 
21   * @author <a href="mailto:alexei.guevara@uhn.on.ca">Alexei Guevara</a>
22   * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:51:48 $ by $Author: bryan_tripp $
23   */
24  public class DateParamTest extends TestCase {
25      
26      //enable assertions
27      static {
28          ClassLoader.getSystemClassLoader().setPackageAssertionStatus( "ca.uhn.cache", true );
29      }
30      
31      /***
32       * @param theName The name 
33       */
34      public DateParamTest( String theName ) {
35          super( theName );
36      }
37      
38      private IDimension myDimension1;
39      private IDimension myDimension2;
40      private IDimension myDimension3;
41      
42      /***
43       * @see junit.framework.TestCase#setUp()
44       */
45      protected void setUp() throws Exception {
46          super.setUp();
47          myDimension1 = new Dimension( "dimension1", new Class[] { DateParam.class } );
48          myDimension2 = new Dimension( "dimension2", new Class[] { DateParam.class } );
49          myDimension3 = new Dimension( "dimension3", new Class[] { DateParam.class, DateRangeParam.class });
50      }
51      
52      /***
53       * @see junit.framework.TestCase#tearDown()
54       */
55      protected void tearDown() throws Exception {
56          super.tearDown();
57      }
58      
59      /***
60       */
61      public void test1() {
62          try {
63              DateParam dp = new DateParam( null, new Date() );
64              fail( "AssertionError must be thrown" );
65          }
66          catch (AssertionError e) {
67          }
68      }
69      
70      /***
71       */
72      public void test2() {
73          try {
74              DateParam dp = new DateParam( myDimension1, null );
75              fail( "AssertionError must be thrown" );
76          }
77          catch (AssertionError e) {
78          }
79      }
80      
81      /***
82       */
83      public void test3() {
84          Date dpValue = new Date();
85          
86          DateParam dp = new DateParam( myDimension1, dpValue );
87          
88          assertEquals( myDimension1, dp.getDimension() );
89          assertEquals( dpValue, dp.getValue() );
90      }
91      
92      /***
93       */
94      public void test4() {
95          Date dpValue1 = new Date();
96          Date dpValue2 = dpValue1;
97          
98          DateParam dp1 = new DateParam( myDimension1, dpValue1 );
99          DateParam dp2 = new DateParam( myDimension1, dpValue2 );
100         
101         assertTrue( dp1.intersects(dp2) );
102         assertTrue( dp2.intersects(dp1) );
103     }
104     
105     /***
106      */
107     public void test5() {
108         Date dpValue1 = new Date( 111 );
109         Date dpValue2 = new Date( 222 );
110         
111         DateParam dp1 = new DateParam( myDimension1, dpValue1 );
112         DateParam dp2 = new DateParam( myDimension1, dpValue2 );
113         
114         assertFalse( dp1.intersects(dp2) );
115         assertFalse( dp2.intersects(dp1) );
116         
117     }
118 
119     /***
120      */
121     public void test6() {
122         Date dpValue1 = new Date();
123         Date dpValue2 = dpValue1;
124         
125         DateParam dp1 = new DateParam( myDimension1, dpValue1 );
126         DateParam dp2 = new DateParam( myDimension2, dpValue2 );
127         
128         assertFalse( dp1.intersects(dp2) );
129         assertFalse( dp2.intersects(dp1) );
130     }
131     
132     /***
133      */
134     public void testRangeIntersect() {
135         Date a = new Date(1000);
136         Date b = new Date(2000);
137         Date c = new Date(3000);
138         
139         DateParam point = new DateParam(myDimension3, a);
140         DateRangeParam range = new DateRangeParam(myDimension3, b, c);
141         assertFalse(point.intersects(range));
142         
143         point = new DateParam(myDimension3, b);
144         range = new DateRangeParam(myDimension3, a, c);
145         assertTrue(point.intersects(range));
146     }
147     
148     
149     
150     /***
151      */
152     public void testPointMerge() {
153         Date a = new Date(1000);
154         Date b = new Date(2000);
155         
156         DateParam dp1 = new DateParam(myDimension3, a);
157         DateParam dp2 = new DateParam(myDimension3, b);
158         
159         assertEquals(dp1.merge(dp2), dp2.merge(dp1));
160         
161         IQueryParam result = dp1.merge(dp2);
162         assertEquals(DateRangeParam.class, result.getClass());
163         assertEquals(a, ((DateRangeParam) result).getStart());
164         assertEquals(b, ((DateRangeParam) result).getEnd());
165     }
166 
167     /***
168      */
169     public void testRangeMerge() {
170         Date a = new Date(1000);
171         Date b = new Date(2000);
172         Date c = new Date(3000);
173         
174         //point before range ... 
175         DateParam dp = new DateParam(myDimension3, a);
176         DateRangeParam dr = new DateRangeParam(myDimension3, b, c);
177         DateRangeParam result = (DateRangeParam) dp.merge(dr);
178         assertEquals(a, ((DateRangeParam) result).getStart());
179         assertEquals(c, ((DateRangeParam) result).getEnd());        
180 
181         //point after range ... 
182         dp = new DateParam(myDimension3, c);
183         dr = new DateRangeParam(myDimension3, a, b);
184         result = (DateRangeParam) dp.merge(dr);
185         assertEquals(a, ((DateRangeParam) result).getStart());
186         assertEquals(c, ((DateRangeParam) result).getEnd());        
187 
188         //point within range ... 
189         dp = new DateParam(myDimension3, b);
190         dr = new DateRangeParam(myDimension3, a, c);
191         result = (DateRangeParam) dp.merge(dr);
192         assertEquals(a, ((DateRangeParam) result).getStart());
193         assertEquals(c, ((DateRangeParam) result).getEnd());        
194     }    
195 
196     /***
197      */
198     public void testPointDistance() {
199         DateParam dp1 = new DateParam(myDimension1, new Date(1000));
200         DateParam dp2 = new DateParam(myDimension1, new Date(2000));
201         DateParam dp3 = new DateParam(myDimension1, new Date(3000));
202         DateParam sp = new DateParam(myDimension1, new Date(1500)); //saturation point
203         
204         assertTrue(dp1.getDistance(dp2, sp) - dp2.getDistance(dp1, sp) < .0001);
205         
206         assertTrue(dp1.getDistance(dp2, sp) < .5);
207         assertTrue(dp2.getDistance(dp3, sp) < .5);
208         assertTrue(dp1.getDistance(dp3, sp) > .5);
209     }
210 
211     /***
212      */
213     public void testRangeDistance() {
214         DateRangeParam range = new DateRangeParam(myDimension3, new Date(1500), new Date(2500));
215         DateParam dp1 = new DateParam(myDimension3, new Date(1000));
216         DateParam dp2 = new DateParam(myDimension3, new Date(2000));
217         DateParam dp3 = new DateParam(myDimension3, new Date(3000));
218         DateParam sp = new DateParam(myDimension3, new Date(1500)); //saturation point
219         
220         assertTrue(dp1.getDistance(range, sp) > 0);
221         assertTrue(dp2.getDistance(range, sp) == 0);
222         assertTrue(dp3.getDistance(range, sp) > 0);
223         assertTrue(dp1.getDistance(range, sp) - dp3.getDistance(range, sp) < .0001);        
224     }
225 
226 }