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 junit.framework.TestCase;
12  import ca.uhn.cache.IDimension;
13  
14  
15  /***
16   * TODO complete javadoc for 
17   * 
18   * @author <a href="mailto:alexei.guevara@uhn.on.ca">Alexei Guevara</a>
19   * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:51:48 $ by $Author: bryan_tripp $
20   */
21  public class StringParamTest extends TestCase {
22      
23      //enable assertions
24      static {
25          ClassLoader.getSystemClassLoader().setPackageAssertionStatus( "ca.uhn.cache", true );
26      }
27      
28      /***
29       * @param theName The name 
30       */
31      public StringParamTest( String theName ) {
32          super( theName );
33      }
34      
35      private IDimension myDimension1;
36      private IDimension myDimension2;
37      
38      /***
39       * @see junit.framework.TestCase#setUp()
40       */
41      protected void setUp() throws Exception {
42          super.setUp();
43          myDimension1 = new Dimension( "dimension1", new Class[] { StringParam.class, StringSetParam.class } );
44          myDimension2 = new Dimension( "dimension2", new Class[] { StringParam.class, StringSetParam.class } );
45      }
46      
47      /***
48       * @see junit.framework.TestCase#tearDown()
49       */
50      protected void tearDown() throws Exception {
51          super.tearDown();
52      }
53      
54      /***
55       */
56      public void test1() {
57          try {
58              StringParam drParam = new StringParam( null, "" );
59              fail( "AssertionError must be thrown" );
60          }
61          catch (AssertionError e) {
62          }
63      }
64      
65      /***
66       */
67      public void test2() {
68          try {
69              StringParam drParam = new StringParam( myDimension1, null );
70              fail( "AssertionError must be thrown" );
71          }
72          catch (AssertionError e) {
73          }
74      }
75      
76      /***
77       */
78      public void test3() {
79          String spValue = "value";
80          
81          StringParam drParam = new StringParam( myDimension1, spValue );
82          
83          assertEquals( myDimension1, drParam.getDimension() );
84          assertEquals( spValue, drParam.getValue() );
85      }
86      
87      /***
88       */
89      public void test4() {
90          String spValue1 = "value";
91          String spValue2 = "value";
92          
93          StringParam stringParam1 = new StringParam( myDimension1, spValue1 );
94          StringParam stringParam2 = new StringParam( myDimension1, spValue2 );
95          
96          assertTrue( stringParam1.intersects(stringParam2) );
97          assertTrue( stringParam2.intersects(stringParam1) );
98      }
99      
100     /***
101      */
102     public void test5() {
103         String spValue1 = "value1";
104         String spValue2 = "value2";
105         
106         StringParam stringParam1 = new StringParam( myDimension1, spValue1 );
107         StringParam stringParam2 = new StringParam( myDimension1, spValue2 );
108         
109         assertFalse( stringParam1.intersects(stringParam2) );
110         assertFalse( stringParam2.intersects(stringParam1) );
111         
112     }
113 
114     /***
115      */
116     public void test6() {
117         String spValue1 = "value";
118         String spValue2 = "value";
119         
120         StringParam stringParam1 = new StringParam( myDimension1, spValue1 );
121         StringParam stringParam2 = new StringParam( myDimension2, spValue2 );
122         
123         assertFalse( stringParam1.intersects(stringParam2) );
124         assertFalse( stringParam2.intersects(stringParam1) );
125     }
126     
127     /***
128      */
129     public void testSetIntersect() {
130         StringSetParam set = new StringSetParam(myDimension1, new String[] {"a", "b"});
131         StringParam point = new StringParam(myDimension1, "c");
132         assertFalse(point.intersects(set));
133         
134         set = new StringSetParam(myDimension1, new String[] {"a", "b"});
135         point = new StringParam(myDimension1, "b");
136         assertTrue(point.intersects(set));
137     }
138     
139     /***
140      */
141     public void testPointDistance() {
142         StringParam point1 = new StringParam(myDimension1, "a");
143         StringParam point2 = new StringParam(myDimension1, "b");
144         assertTrue(point1.getDistance(point2, null) - 0.5 < 0.0001);
145 
146         point1 = new StringParam(myDimension1, "a");
147         point2 = new StringParam(myDimension1, "a");
148         assertTrue(point1.getDistance(point2, null) < 0.0001);
149     }
150     
151     /***
152      */
153     public void testSetDistance() {
154         StringSetParam set = new StringSetParam(myDimension1, new String[] {"a", "b"});
155         StringParam point = new StringParam(myDimension1, "c");
156         assertTrue(point.getDistance(set, null) - 0.5 < 0.0001);
157 
158         set = new StringSetParam(myDimension1, new String[] {"a", "b"});
159         point = new StringParam(myDimension1, "b");
160         assertTrue(point.getDistance(set, null) < 0.0001);        
161     }
162 
163     /***
164      */
165     public void testPointMerge() {
166         StringParam point1 = new StringParam(myDimension1, "a");
167         StringParam point2 = new StringParam(myDimension1, "b");
168         StringSetParam result = (StringSetParam) point1.merge(point2);
169         assertTrue(result.getValues().contains("a"));
170         assertTrue(result.getValues().contains("b"));
171     }
172     
173     /***
174      */
175     public void testSetMerge() {
176         StringSetParam set = new StringSetParam(myDimension1, new String[] {"a", "b"});
177         StringParam point = new StringParam(myDimension1, "c");
178         StringSetParam result = (StringSetParam) point.merge(set);
179         assertTrue(result.getValues().contains("a"));
180         assertTrue(result.getValues().contains("c"));
181     }
182 
183 }
184