1 19 20 package org.openide.util; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.IOException ; 25 import java.io.ObjectInputStream ; 26 import java.io.ObjectOutputStream ; 27 import java.net.URL ; 28 import java.util.ArrayList ; 29 import java.util.Arrays ; 30 import java.util.Collections ; 31 import java.util.Enumeration ; 32 import java.util.HashMap ; 33 import java.util.HashSet ; 34 import java.util.Iterator ; 35 import java.util.LinkedList ; 36 import java.util.List ; 37 import java.util.Map ; 38 import java.util.NoSuchElementException ; 39 import java.util.Scanner ; 40 import java.util.Set ; 41 import java.util.logging.Level ; 42 import org.netbeans.junit.NbTestCase; 43 44 47 @SuppressWarnings ("unchecked") 48 public class NbCollectionsTest extends NbTestCase { 49 50 public NbCollectionsTest(String name) { 51 super(name); 52 } 53 54 protected Level logLevel() { 55 return Level.SEVERE; 56 } 57 58 public void testCheckedSetByCopy() throws Exception { 59 Set s = new HashSet (); 60 s.add(1); 61 s.add(2); 62 Set <Integer > checked = NbCollections.checkedSetByCopy(s, Integer .class, true); 63 assertEquals(s, checked); 64 s.add("three"); 65 try { 66 NbCollections.checkedSetByCopy(s, Integer .class, true); 67 fail(); 68 } catch (ClassCastException e) {} 69 assertEquals(checked, NbCollections.checkedSetByCopy(s, Integer .class, false)); 70 s.remove("three"); 71 s.add(null); 72 checked = NbCollections.checkedSetByCopy(s, Integer .class, true); 73 assertEquals("nulls preserved", s, checked); 74 s.clear(); 75 s.add(5); 76 assertEquals("modifications to original not reflected", 3, checked.size()); 77 } 78 79 public void testCheckedListByCopy() throws Exception { 80 doTestCheckedListByCopy(new ArrayList ()); 81 doTestCheckedListByCopy(new LinkedList ()); 82 } 83 84 private void doTestCheckedListByCopy(List l) { 85 l.add(1); 86 l.add(2); 87 List <Integer > checked = NbCollections.checkedListByCopy(l, Integer .class, true); 88 assertEquals(l, checked); 89 l.add("three"); 90 try { 91 NbCollections.checkedListByCopy(l, Integer .class, true); 92 fail(); 93 } catch (ClassCastException e) {} 94 assertEquals(checked, NbCollections.checkedListByCopy(l, Integer .class, false)); 95 l.remove("three"); 96 l.add(null); 97 checked = NbCollections.checkedListByCopy(l, Integer .class, true); 98 assertEquals("nulls preserved", l, checked); 99 l.clear(); 100 l.add(5); 101 assertEquals("modifications to original not reflected", 3, checked.size()); 102 } 103 104 public void testCheckedMapByCopy() throws Exception { 105 Map m = new HashMap (); 106 m.put(1, "hello"); 107 m.put(2, "goodbye"); 108 Map <Integer ,String > checked = NbCollections.checkedMapByCopy(m, Integer .class, String .class, true); 109 assertEquals(m, checked); 110 m.put(2, new Object ()); 111 try { 112 NbCollections.checkedMapByCopy(m, Integer .class, String .class, true); 113 fail(); 114 } catch (ClassCastException e) {} 115 assertEquals(Collections.singletonMap(1, "hello"), NbCollections.checkedMapByCopy(m, Integer .class, String .class, false)); 116 m.remove(2); 117 Long three = 3L; 118 m.put(three, "oops!"); 119 try { 120 NbCollections.checkedMapByCopy(m, Integer .class, String .class, true); 121 fail(); 122 } catch (ClassCastException e) {} 123 assertEquals(Collections.singletonMap(1, "hello"), NbCollections.checkedMapByCopy(m, Integer .class, String .class, false)); 124 m.remove(three); 125 m.put(null, null); 126 checked = NbCollections.checkedMapByCopy(m, Integer .class, String .class, true); 127 assertEquals("nulls preserved", m, checked); 128 m.clear(); 129 m.put(5, "new"); 130 assertEquals("modifications to original not reflected", 2, checked.size()); 131 } 132 133 public void testCheckedIteratorByFilter() throws Exception { 134 Iterator raw = Arrays.asList("one", 2, "three").iterator(); 135 Iterator <String > strings = NbCollections.checkedIteratorByFilter(raw, String .class, false); 136 assertTrue(strings.hasNext()); 137 assertEquals("one", strings.next()); 138 assertTrue(strings.hasNext()); 139 assertEquals("three", strings.next()); 140 assertFalse(strings.hasNext()); 141 raw = Arrays.asList("one", 2, "three").iterator(); 142 strings = NbCollections.checkedIteratorByFilter(raw, String .class, true); 143 try { 144 while (strings.hasNext()) { 145 strings.next(); 146 } 147 fail(); 148 } catch (ClassCastException e) {} 149 raw = Arrays.asList("one", "three").iterator(); 150 strings = NbCollections.checkedIteratorByFilter(raw, String .class, true); 151 assertTrue(strings.hasNext()); 152 assertEquals("one", strings.next()); 153 assertTrue(strings.hasNext()); 154 assertEquals("three", strings.next()); 155 assertFalse(strings.hasNext()); 156 List l = new ArrayList (Arrays.asList(new Object [] {"one", 2, "three"})); 157 raw = l.iterator(); 158 strings = NbCollections.checkedIteratorByFilter(raw, String .class, false); 159 assertTrue(strings.hasNext()); 160 assertEquals("one", strings.next()); 161 strings.remove(); 162 assertEquals(2, l.size()); 163 assertTrue(strings.hasNext()); 164 assertEquals("three", strings.next()); 165 assertFalse(strings.hasNext()); 166 } 167 168 public void testCheckedSetByFilter() throws Exception { 169 Set s = new HashSet (); 170 s.add("hello"); 171 s.add("there"); 172 s.add(1); 173 s.add("goodbye"); 174 s.add(2); 175 Set <String > s2 = NbCollections.checkedSetByFilter(s, String .class, false); 176 assertEquals(3, s2.size()); 177 assertEquals(new HashSet (Arrays.asList(new String [] {"hello", "there", "goodbye"})), s2); 178 assertTrue(s2.contains("hello")); 179 assertFalse(s2.contains("nowhere")); 180 try { 181 s2.contains(2); 182 fail(); 183 } catch (ClassCastException e) {} 184 Iterator <String > it = s2.iterator(); 185 while (it.hasNext()) { 186 if (it.next().equals("hello")) { 187 it.remove(); 188 } 189 } 190 assertEquals(2, s2.size()); 191 assertEquals(new HashSet (Arrays.asList(new String [] {"there", "goodbye"})), s2); 192 assertEquals(4, s.size()); 193 it = s2.iterator(); 194 while (it.hasNext()) { 195 it.next(); 196 it.remove(); 197 } 198 assertEquals(0, s2.size()); 199 assertEquals(Collections.emptySet(), s2); 200 assertEquals(new HashSet (Arrays.asList(new Integer [] {1, 2})), s); 201 s.clear(); 202 s.add("new"); 203 assertEquals("modifications to original found", Collections.singleton("new"), s2); 204 assertTrue(s2.add("additional")); 205 assertEquals("original set modified too", new HashSet (Arrays.asList(new String [] {"new", "additional"})), s); 206 try { 207 ((Set ) s2).add(13); 208 fail(); 209 } catch (ClassCastException e) {} 210 assertEquals("preserved by serialization", s2, cloneBySerialization(s2)); 212 assertEquals("empty set filtered as empty", Collections.emptySet(), NbCollections.checkedSetByFilter(Collections.emptySet(), String .class, false)); 213 assertEquals("empty set from wholly wrong set", Collections.emptySet(), NbCollections.checkedSetByFilter(Collections.singleton(5), String .class, false)); 214 Set <Integer > s3 = NbCollections.checkedSetByFilter(new HashSet (Collections.singleton(1)), Integer .class, false); 216 Iterator <Integer > it3 = s3.iterator(); 217 assertTrue(it3.hasNext()); 218 assertTrue(it3.hasNext()); 219 assertEquals(new Integer (1), it3.next()); 220 assertFalse(it3.hasNext()); 221 assertFalse(it3.hasNext()); 222 try { 223 it3.next(); 224 fail(); 225 } catch (NoSuchElementException e) {} 226 it3 = s3.iterator(); 227 try { 228 it3.remove(); 229 fail(); 230 } catch (IllegalStateException e) {} 231 it3 = s3.iterator(); 232 it3.next(); 233 it3.remove(); 234 try { 235 it3.remove(); 236 fail(); 237 } catch (IllegalStateException e) {} 238 } 239 240 public void testCheckedSetByFilterStrict() throws Exception { 241 Set s = new HashSet (); 242 s.add("hello"); 243 s.add("there"); 244 s.add(1); 245 s.add("goodbye"); 246 s.add(2); 247 Set <String > s2 = NbCollections.checkedSetByFilter(s, String .class, true); 248 try { 249 s2.size(); 250 fail(); 251 } catch (ClassCastException x) {} 252 try { 253 new HashSet <String >(s2); 254 fail(); 255 } catch (ClassCastException x) {} 256 s.remove(1); 257 s.remove(2); 258 assertEquals(3, s2.size()); 259 assertTrue(s2.contains("hello")); 260 try { 261 s2.contains(2); 262 fail(); 263 } catch (ClassCastException e) {} 264 } 265 266 public void testCheckedMapByFilter() throws Exception { 267 Map m = new HashMap (); 268 m.put(1, "one"); 269 m.put(2, "two"); 270 m.put("three", "three"); 271 m.put(4, 4); 272 Map <Integer ,String > m2 = NbCollections.checkedMapByFilter(m, Integer .class, String .class, false); 273 assertEquals(2, m2.size()); 274 assertEquals("one", m2.get(1)); 275 try { 276 m2.get("three"); 277 fail(); 278 } catch (ClassCastException e) {} 279 assertEquals(null, m2.get(4)); 280 assertTrue(m2.containsKey(1)); 281 assertFalse(m2.containsKey(5)); 282 try { 283 m2.containsKey("three"); 284 fail(); 285 } catch (ClassCastException e) {} 286 assertFalse(m2.containsKey(4)); 287 assertTrue(m2.containsValue("one")); 288 assertFalse(m2.containsValue("five")); 289 try { 290 m2.containsValue(3); 291 fail(); 292 } catch (ClassCastException e) {} 293 assertFalse(m2.containsValue("three")); 294 assertEquals(2, m2.entrySet().size()); 295 assertEquals(2, m2.keySet().size()); 296 assertEquals(2, m2.values().size()); 297 assertTrue(m2.keySet().contains(1)); 298 assertFalse(m2.keySet().contains(5)); 299 try { 300 m2.keySet().contains("three"); 301 fail(); 302 } catch (ClassCastException e) {} 303 assertFalse(m2.keySet().contains(4)); 304 assertTrue(m2.values().contains("one")); 305 assertFalse(m2.values().contains("five")); 306 try { 307 m2.values().contains(4); 308 fail(); 309 } catch (ClassCastException e) {} 310 assertFalse(m2.values().contains("three")); 311 m2.put(1, "#one"); 313 assertEquals("#one", m2.get(1)); 314 assertEquals("#one", m.get(1)); 315 try { 316 ((Map ) m2).put("five", "five"); 317 fail(); 318 } catch (ClassCastException e) {} 319 try { 320 ((Map ) m2).put(5, 5); 321 fail(); 322 } catch (ClassCastException e) {} 323 m2.remove(1); 324 assertEquals(Collections.singletonMap(2, "two"), m2); 325 assertEquals(3, m.size()); 326 m2.entrySet().clear(); 327 assertTrue(m2.isEmpty()); 328 assertEquals(2, m.size()); 329 m.clear(); 330 m.put(1, "one"); 331 m.put(2, "two"); 332 m.put(3, 3); 333 m.put("four", "four"); 334 assertTrue(m2.keySet().remove(1)); 335 assertFalse(m2.keySet().remove(3)); 336 assertFalse(m2.keySet().remove("four")); 337 assertEquals(Collections.singletonMap(2, "two"), m2); 338 m.put(1, "one"); 339 assertTrue(m2.values().remove("one")); 340 assertFalse(m2.values().remove(3)); 341 assertFalse(m2.values().remove("four")); 342 assertEquals(Collections.singletonMap(2, "two"), m2); 343 assertEquals(3, m.size()); 344 assertEquals(m2, cloneBySerialization(m2)); 346 assertEquals(Collections.emptyMap(), NbCollections.checkedMapByFilter(Collections.emptyMap(), String .class, String .class, false)); 347 assertEquals(Collections.emptyMap(), NbCollections.checkedMapByFilter(Collections.singletonMap(1, "two"), String .class, String .class, false)); 348 assertEquals(Collections.emptyMap(), NbCollections.checkedMapByFilter(Collections.singletonMap("one", 2), String .class, String .class, false)); 349 m.clear(); 351 m.put(1, 1); 352 assertEquals(null, m2.put(1, "one")); 353 m.put(1, 1); 354 assertEquals(null, m2.remove(1)); 355 m.put(1, "one"); 356 assertEquals("one", m2.put(1, "#one")); 357 assertEquals("#one", m2.remove(1)); 358 } 359 360 public void testCheckedMapByFilterStrict() throws Exception { 361 Map m = new HashMap (); 362 m.put(1, "one"); 363 m.put(2, "two"); 364 m.put("three", "three"); 365 m.put(4, 4); 366 Map <Integer ,String > m2 = NbCollections.checkedMapByFilter(m, Integer .class, String .class, true); 367 try { 368 m2.size(); 369 fail(); 370 } catch (ClassCastException e) {} 371 try { 372 new HashMap <Integer ,String >(m2); 373 fail(); 374 } catch (ClassCastException e) {} 375 try { 376 m2.get("three"); 377 fail(); 378 } catch (ClassCastException e) {} 379 m.remove("three"); 380 try { 381 m2.size(); 382 fail(); 383 } catch (ClassCastException e) {} 384 m.remove(4); 385 m.put("three", "three"); 386 try { 387 m2.size(); 388 fail(); 389 } catch (ClassCastException e) {} 390 m.remove("three"); 391 assertEquals(2, m2.size()); 392 assertEquals("one", m2.get(1)); 393 } 394 395 public void testCheckedEnumerationByFilter() throws Exception { 396 Enumeration raw = Collections.enumeration(Arrays.asList("one", 2, "three")); 397 Enumeration <String > strings = NbCollections.checkedEnumerationByFilter(raw, String .class, false); 398 assertTrue(strings.hasMoreElements()); 399 assertEquals("one", strings.nextElement()); 400 assertTrue(strings.hasMoreElements()); 401 assertEquals("three", strings.nextElement()); 402 assertFalse(strings.hasMoreElements()); 403 } 404 405 public void testCheckedEnumerationByFilterStrict() throws Exception { 406 Enumeration raw = Collections.enumeration(Arrays.asList("one", 2, "three")); 407 Enumeration <String > strings = NbCollections.checkedEnumerationByFilter(raw, String .class, true); 408 try { 409 Collections.list(strings); 410 fail(); 411 } catch (ClassCastException e) {} 412 raw = Collections.enumeration(Arrays.asList("one", "three")); 413 strings = NbCollections.checkedEnumerationByFilter(raw, String .class, true); 414 assertTrue(strings.hasMoreElements()); 415 assertEquals("one", strings.nextElement()); 416 assertTrue(strings.hasMoreElements()); 417 assertEquals("three", strings.nextElement()); 418 assertFalse(strings.hasMoreElements()); 419 } 420 421 static Object cloneBySerialization(Object o) throws IOException , ClassNotFoundException { 422 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 423 ObjectOutputStream oos = new ObjectOutputStream (baos); 424 oos.writeObject(o); 425 oos.close(); 426 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 427 return new ObjectInputStream (bais).readObject(); 428 } 429 430 public void testIterable() throws Exception { 431 String text = "hello kitty!"; 432 List <String > l1 = new ArrayList <String >(); 433 for (String token : NbCollections.iterable(new Scanner (text))) { 434 l1.add(token); 435 } 436 assertEquals(Arrays.asList("hello", "kitty!"), l1); 437 for (String token : NbCollections.iterable(new Scanner (""))) { 438 fail(); 439 } 440 try { 441 NbCollections.iterable((Iterator <?>) null); 442 fail(); 443 } catch (NullPointerException x) {} 444 List <URL > l2 = new ArrayList <URL >(); 445 for (URL u : NbCollections.iterable(NbCollections.class.getClassLoader().getResources(NbCollections.class.getName().replace('.', '/') + ".class"))) { 446 assertNotNull(u); 447 l2.add(u); 448 } 449 assertFalse(l2.isEmpty()); for (URL u : NbCollections.iterable(NbCollections.class.getClassLoader().getResources("nonexistent"))) { 451 fail(); 452 } 453 try { 454 NbCollections.iterable((Enumeration <?>) null); 455 fail(); 456 } catch (NullPointerException x) {} 457 } 458 459 } 460 | Popular Tags |