1   package gate.util;
2   
3   import junit.framework.*;
4   
5   /**
6    * Title:        Gate2
7    * Description:
8    * Copyright:    Copyright (c) 2000
9    * Company:      University Of Sheffield
10   * @version 1.0
11   */
12  
13  public class TestFeatureMap extends TestCase {
14  
15      /** Debug flag */
16      private static final boolean DEBUG = false;
17  
18      /** Construction */
19      public TestFeatureMap(String name) { super(name); }
20  
21      /** Test the testPutAndGet()... methods. */
22      public void testPutAndGet() throws Exception {
23          assertTrue(true);
24          SimpleFeatureMapImpl map = new SimpleFeatureMapImpl();
25          map.put("1", "bala");
26          map.put("1", "bala2");
27          map.put("2", "20");
28          map.put("3", null);
29          map.put(null, "5");
30  
31          Object value = null;
32          /**
33           * test1:
34           *      get replaced value by normal key
35           */
36          value = map.get("1");
37          assertSame(value, "bala2");
38          /**
39           * test 2:
40           *      get normal value by normal key
41           */
42          value = map.get("2");
43          assertSame(value, "20");
44          /**
45           * Test 3:
46           *      get null value by the key
47           */
48          value = map.get("3");
49          assertSame(value, null);
50          /**
51           * test 4:
52           *      try to get value by 'null' key
53           */
54          value = map.get(null);
55          assertSame(value, "5");
56      } // testPutAndGet()
57  
58      public void testSubsume() throws Exception {
59          assertTrue(true);
60          SimpleFeatureMapImpl map = new SimpleFeatureMapImpl();
61          SimpleFeatureMapImpl map2 = new SimpleFeatureMapImpl();
62          map.put("1", "bala");
63          map2.put("1", map.get("1"));
64  
65          map.put("2", "20");
66          /**
67           * test1:
68           *      subsume partially - map1 and map2 has one common element
69           */
70           assertTrue(map.subsumes(map2));
71          /**
72           * test 2:
73           *      map2 do NOT subsumes map1
74           */
75           assertTrue(!map2.subsumes(map));
76          /**
77           * Test 3:
78           *      subsume partially - map1 and map2.keySet()
79           */
80           assertTrue(map.subsumes(map2, map2.keySet()));
81          /**
82           * test 4:
83           *      map2 SUBSUMES and map using the map2.keySet()
84           */
85           assertTrue(map2.subsumes(map, map2.keySet()));
86  
87          /**
88           * test 5,6,7,8:
89           *      test1,2,3,4 with NULL's in the map and
90           *      not NULL's the map2 under the same key "3"
91           */
92           map.put("3", null);
93           map2.put("3", "not null");
94  
95           assertTrue(!map.subsumes(map2));
96           assertTrue(!map2.subsumes(map));
97           assertTrue(!map.subsumes(map2, map2.keySet()));
98           assertTrue(!map2.subsumes(map, map2.keySet()));
99  
100          /**
101           * Test 9,10,11,12 repeat the same test but with compatible (null) values
102           * under the same key "3"
103           */
104          map2.put("3", null);
105 
106          assertTrue(map.subsumes(map2));
107          assertTrue(!map2.subsumes(map));
108          assertTrue(map.subsumes(map2, map2.keySet()));
109          assertTrue(map2.subsumes(map, map2.keySet()));
110 
111          /**
112           * Test 13,14,15,16 repeat the same test but with null keys in the two of the maps
113           */
114          map.put(null, "5");
115          map2.put(null, "5");
116 
117          assertTrue(map.subsumes(map2));
118          assertTrue(!map2.subsumes(map));
119          assertTrue(map.subsumes(map2, map2.keySet()));
120          assertTrue(map2.subsumes(map, map2.keySet()));
121     } // testSubsume()
122 
123     /** Test suite routine for the test runner */
124     public static Test suite() {
125         return new TestSuite(TestFeatureMap.class);
126     } // suite
127 
128     public static void main(String args[]){
129         TestFeatureMap app = new TestFeatureMap("TestFeatureMap");
130         try {
131             app.testPutAndGet();
132             app.testSubsume();
133         } catch (Exception e) {
134             e.printStackTrace (Err.getPrintWriter());
135         }
136     } // main
137 } // TestFeatureMap