1 19 20 package com.hp.hpl.jena.util.test; 23 24 25 import java.util.*; 28 import java.util.HashSet ; 29 import java.util.Map ; 30 31 import com.hp.hpl.jena.util.OneToManyMap; 32 33 import junit.framework.TestCase; 34 35 36 37 45 public class TestOneToManyMap 46 extends TestCase 47 { 48 51 54 57 private String s0 = "s0"; 58 private String s1 = "s1"; 59 private String s2 = "s2"; 60 private String s3 = "s3"; 61 private String s4 = "s4"; 62 63 64 67 70 public void setUp() { 71 } 72 73 public void tearDown() { 74 } 75 76 public void testConstruct0() { 77 OneToManyMap map0 = new OneToManyMap(); 78 assertNotNull( map0 ); 79 80 assertTrue( map0.isEmpty() ); 81 82 OneToManyMap map1 = new OneToManyMap( map0 ); 83 assertNotNull( map1 ); 84 assertTrue( map1.isEmpty() ); 85 } 86 87 public void testConstruct1() { 88 OneToManyMap map0 = new OneToManyMap(); 89 90 map0.put( s0, s1 ); 91 assertTrue( map0.contains( s0, s1 ) ); 92 93 OneToManyMap map1 = new OneToManyMap( map0 ); 94 assertTrue( map0.contains( s0, s1 ) ); 95 96 map0.put( s0, s2 ); 97 assertTrue( map0.contains( s0, s2 ) ); 98 assertFalse( map0.contains( s1, s2 ) ); 99 100 } 101 102 public void testClear() { 103 OneToManyMap map0 = new OneToManyMap(); 104 105 map0.put( s0, s1 ); 106 assertTrue( map0.contains( s0, s1 ) ); 107 assertFalse( map0.isEmpty() ); 108 109 map0.clear(); 110 assertFalse( map0.contains( s0, s1 ) ); 111 assertTrue( map0.isEmpty() ); 112 } 113 114 public void testContainsKey() { 115 OneToManyMap map0 = new OneToManyMap(); 116 assertFalse( map0.containsKey( s0 ) ); 117 assertFalse( map0.containsKey( s1 ) ); 118 map0.put( s0, s1 ); 119 assertTrue( map0.containsKey( s0 ) ); 120 assertFalse( map0.containsKey( s1 ) ); 121 } 122 123 public void testContainsValue() { 124 OneToManyMap map0 = new OneToManyMap(); 125 assertFalse( map0.containsValue( s0 ) ); 126 assertFalse( map0.containsValue( s1 ) ); 127 assertFalse( map0.containsValue( s2 ) ); 128 map0.put( s0, s1 ); 129 assertFalse( map0.containsValue( s0 ) ); 130 assertTrue( map0.containsValue( s1 ) ); 131 assertFalse( map0.containsValue( s2 ) ); 132 map0.put( s0, s2 ); 133 assertFalse( map0.containsValue( s0 ) ); 134 assertTrue( map0.containsValue( s1 ) ); 135 assertTrue( map0.containsValue( s2 ) ); 136 } 137 138 public void testContains() { 139 OneToManyMap map0 = new OneToManyMap(); 140 assertFalse( map0.contains( s0, s1 ) ); 141 assertFalse( map0.contains( s0, s2 ) ); 142 assertFalse( map0.contains( s1, s2 ) ); 143 map0.put( s0, s1 ); 144 assertTrue( map0.contains( s0, s1 ) ); 145 assertFalse( map0.contains( s0, s2 ) ); 146 assertFalse( map0.contains( s1, s2 ) ); 147 map0.put( s0, s2 ); 148 assertTrue( map0.contains( s0, s1 ) ); 149 assertTrue( map0.contains( s0, s2 ) ); 150 assertFalse( map0.contains( s1, s2 ) ); 151 } 152 153 public void testEntrySet() { 154 OneToManyMap map0 = new OneToManyMap(); 155 map0.put( s0, s1 ); 156 map0.put( s0, s2 ); 157 map0.put( s3, s4 ); 158 159 boolean s0s1 = false; 160 boolean s0s2 = false; 161 boolean s3s4 = false; 162 163 for (Iterator i = map0.entrySet().iterator(); i.hasNext(); ) { 164 Map.Entry e = (Map.Entry ) i.next(); 165 if (e.getKey().equals( s0 ) && e.getValue().equals( s1 )) { 166 s0s1 = true; 167 } 168 else if (e.getKey().equals( s0 ) && e.getValue().equals( s2 )) { 169 s0s2 = true; 170 } 171 else if (e.getKey().equals( s3 ) && e.getValue().equals( s4 )) { 172 s3s4 = true; 173 } 174 else { 175 throw new IllegalArgumentException ( "unexpected: " + e ); 176 } 177 } 178 179 assertTrue( s0s1 ); 180 assertTrue( s0s2 ); 181 assertTrue( s3s4 ); 182 } 183 184 public void testEquals() { 185 OneToManyMap map0 = new OneToManyMap(); 186 map0.put( s0, s1 ); 187 map0.put( s0, s2 ); 188 map0.put( s3, s4 ); 189 OneToManyMap map1 = new OneToManyMap(); 190 map1.put( s3, s4 ); 191 map1.put( s0, s1 ); 192 map1.put( s0, s2 ); 193 OneToManyMap map2 = new OneToManyMap(); 194 map2.put( s0, s2 ); 195 map2.put( s3, s4 ); 196 197 assertTrue( map0.equals( map1 ) ); 198 assertTrue( map1.equals( map0 ) ); 199 assertTrue( map0.hashCode() == map1.hashCode() ); 200 201 assertFalse( map0.equals( map2 )); 202 assertFalse( map2.equals( map0 )); 203 } 204 205 public void testGet() { 206 OneToManyMap map0 = new OneToManyMap(); 207 assertNull( map0.get( s0 )); 208 map0.put( s0, s1 ); 209 assertEquals( s1, map0.get( s0 )); 210 map0.put( s0, s2 ); 211 assertTrue( map0.get( s0 ).equals( s1 ) || 212 map0.get( s0 ).equals( s2 )); 213 } 214 215 public void testGetAll() { 216 OneToManyMap map0 = new OneToManyMap(); 217 Iterator i = map0.getAll(s0); 218 assertNotNull( i ); 219 assertFalse( i.hasNext() ); 220 221 map0.put( s0, s1 ); 222 i = map0.getAll(s0); 223 assertNotNull( i ); 224 assertTrue( i.hasNext() ); 225 assertEquals( s1, i.next() ); 226 assertFalse( i.hasNext() ); 227 228 map0.put( s0, s2 ); 229 i = map0.getAll(s0); 230 assertNotNull( i ); 231 boolean founds1 = false, founds2 = false; 232 while (i.hasNext()) { 233 Object x = i.next(); 234 if (x.equals(s1)) { 235 founds1 = true; 236 } 237 else if (x.equals(s2)) { 238 founds2 = true; 239 } 240 else { 241 throw new IllegalArgumentException ( x.toString() ); 242 } 243 } 244 assertTrue( founds1 ); 245 assertTrue( founds2 ); 246 } 247 248 public void testKeySet() { 249 OneToManyMap map0 = new OneToManyMap(); 250 Set keys = new HashSet (); 251 assertEquals( keys, map0.keySet() ); 252 253 map0.put( s0, s1 ); 254 keys.add( s0 ); 255 assertEquals( keys, map0.keySet() ); 256 257 map0.put( s2, s1 ); 258 keys.add( s2 ); 259 assertEquals( keys, map0.keySet() ); 260 } 261 262 public void testPutAll0() { 263 OneToManyMap map0 = new OneToManyMap(); 264 map0.put( s0, s1 ); 265 map0.put( s0, s2 ); 266 map0.put( s3, s4 ); 267 268 OneToManyMap map1 = new OneToManyMap(); 269 map1.put( s0, s2 ); 270 map1.put( s3, s4 ); 271 map1.put( s0, s1 ); 272 273 OneToManyMap map2 = new OneToManyMap(); 274 map2.putAll( map1 ); 275 assertEquals( map0, map2 ); 276 } 277 278 public void testPutAll1() { 279 OneToManyMap map0 = new OneToManyMap(); 280 map0.put( s0, s1 ); 281 map0.put( s3, s4 ); 282 283 Map map1 = new HashMap(); 284 map1.put( s3, s4 ); 285 map1.put( s0, s1 ); 286 287 OneToManyMap map2 = new OneToManyMap(); 288 map2.putAll( map1 ); 289 assertEquals( map0, map2 ); 290 } 291 292 public void testRemove0() { 293 OneToManyMap map0 = new OneToManyMap(); 294 map0.put( s0, s1 ); 295 map0.put( s3, s4 ); 296 297 map0.remove( s0 ); 298 map0.remove( s3 ); 299 300 assertTrue( map0.isEmpty() ); 301 } 302 303 public void testRemove1() { 304 OneToManyMap map0 = new OneToManyMap(); 305 map0.put( s0, s1 ); 306 map0.put( s0, s2 ); 307 map0.put( s3, s4 ); 308 309 map0.remove( s0, s2 ); 310 map0.remove( s3, s4 ); 311 312 assertFalse( map0.isEmpty() ); 313 314 map0.remove( s0, s1 ); 315 assertTrue( map0.isEmpty() ); 316 } 317 318 public void testSize() { 319 OneToManyMap map0 = new OneToManyMap(); 320 assertEquals( 0, map0.size() ); 321 map0.put( s0, s1 ); 322 assertEquals( 1, map0.size() ); 323 map0.put( s0, s2 ); 324 assertEquals( 2, map0.size() ); 325 map0.put( s3, s4 ); 326 assertEquals( 3, map0.size() ); 327 map0.remove( s0, s2 ); 328 assertEquals( 2, map0.size() ); 329 map0.remove( s3, s4 ); 330 assertEquals( 1, map0.size() ); 331 map0.remove( s0, s1 ); 332 assertEquals( 0, map0.size() ); 333 } 334 335 public void testValues() { 336 OneToManyMap map0 = new OneToManyMap(); 337 Set vals = new HashSet (); 338 assertEquals( vals, map0.values() ); 339 340 map0.put( s0, s1 ); 341 vals.add( s1 ); 342 assertEquals( vals, map0.values() ); 343 344 map0.put( s2, s1 ); 345 assertEquals( vals, map0.values() ); 346 347 map0.put( s2, s3 ); 348 vals.add( s3 ); 349 assertEquals( vals, map0.values() ); 350 } 351 352 355 359 } 360 361 362 388 | Popular Tags |