KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > NbCollectionsTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.util;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.ObjectInputStream JavaDoc;
26 import java.io.ObjectOutputStream JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.HashSet JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.LinkedList JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.NoSuchElementException JavaDoc;
39 import java.util.Scanner JavaDoc;
40 import java.util.Set JavaDoc;
41 import java.util.logging.Level JavaDoc;
42 import org.netbeans.junit.NbTestCase;
43
44 /**
45  * @author Jesse Glick
46  */

47 @SuppressWarnings JavaDoc("unchecked")
48 public class NbCollectionsTest extends NbTestCase {
49
50     public NbCollectionsTest(String JavaDoc name) {
51         super(name);
52     }
53     
54     protected Level JavaDoc logLevel() {
55         return Level.SEVERE;
56     }
57
58     public void testCheckedSetByCopy() throws Exception JavaDoc {
59         Set JavaDoc s = new HashSet JavaDoc();
60         s.add(1);
61         s.add(2);
62         Set JavaDoc<Integer JavaDoc> checked = NbCollections.checkedSetByCopy(s, Integer JavaDoc.class, true);
63         assertEquals(s, checked);
64         s.add("three");
65         try {
66             NbCollections.checkedSetByCopy(s, Integer JavaDoc.class, true);
67             fail();
68         } catch (ClassCastException JavaDoc e) {/*OK*/}
69         assertEquals(checked, NbCollections.checkedSetByCopy(s, Integer JavaDoc.class, false));
70         s.remove("three");
71         s.add(null);
72         checked = NbCollections.checkedSetByCopy(s, Integer JavaDoc.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 JavaDoc {
80         doTestCheckedListByCopy(new ArrayList JavaDoc());
81         doTestCheckedListByCopy(new LinkedList JavaDoc());
82     }
83     
84     private void doTestCheckedListByCopy(List JavaDoc l) {
85         l.add(1);
86         l.add(2);
87         List JavaDoc<Integer JavaDoc> checked = NbCollections.checkedListByCopy(l, Integer JavaDoc.class, true);
88         assertEquals(l, checked);
89         l.add("three");
90         try {
91             NbCollections.checkedListByCopy(l, Integer JavaDoc.class, true);
92             fail();
93         } catch (ClassCastException JavaDoc e) {/*OK*/}
94         assertEquals(checked, NbCollections.checkedListByCopy(l, Integer JavaDoc.class, false));
95         l.remove("three");
96         l.add(null);
97         checked = NbCollections.checkedListByCopy(l, Integer JavaDoc.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 JavaDoc {
105         Map JavaDoc m = new HashMap JavaDoc();
106         m.put(1, "hello");
107         m.put(2, "goodbye");
108         Map JavaDoc<Integer JavaDoc,String JavaDoc> checked = NbCollections.checkedMapByCopy(m, Integer JavaDoc.class, String JavaDoc.class, true);
109         assertEquals(m, checked);
110         m.put(2, new Object JavaDoc());
111         try {
112             NbCollections.checkedMapByCopy(m, Integer JavaDoc.class, String JavaDoc.class, true);
113             fail();
114         } catch (ClassCastException JavaDoc e) {/*OK*/}
115         assertEquals(Collections.singletonMap(1, "hello"), NbCollections.checkedMapByCopy(m, Integer JavaDoc.class, String JavaDoc.class, false));
116         m.remove(2);
117         Long JavaDoc three = 3L;
118         m.put(three, "oops!");
119         try {
120             NbCollections.checkedMapByCopy(m, Integer JavaDoc.class, String JavaDoc.class, true);
121             fail();
122         } catch (ClassCastException JavaDoc e) {/*OK*/}
123         assertEquals(Collections.singletonMap(1, "hello"), NbCollections.checkedMapByCopy(m, Integer JavaDoc.class, String JavaDoc.class, false));
124         m.remove(three);
125         m.put(null, null);
126         checked = NbCollections.checkedMapByCopy(m, Integer JavaDoc.class, String JavaDoc.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 JavaDoc {
134         Iterator JavaDoc raw = Arrays.asList("one", 2, "three").iterator();
135         Iterator JavaDoc<String JavaDoc> strings = NbCollections.checkedIteratorByFilter(raw, String JavaDoc.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 JavaDoc.class, true);
143         try {
144             while (strings.hasNext()) {
145                 strings.next();
146             }
147             fail();
148         } catch (ClassCastException JavaDoc e) {/*OK*/}
149         raw = Arrays.asList("one", "three").iterator();
150         strings = NbCollections.checkedIteratorByFilter(raw, String JavaDoc.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 JavaDoc l = new ArrayList JavaDoc(Arrays.asList(new Object JavaDoc[] {"one", 2, "three"}));
157         raw = l.iterator();
158         strings = NbCollections.checkedIteratorByFilter(raw, String JavaDoc.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 JavaDoc {
169         Set JavaDoc s = new HashSet JavaDoc();
170         s.add("hello");
171         s.add("there");
172         s.add(1);
173         s.add("goodbye");
174         s.add(2);
175         Set JavaDoc<String JavaDoc> s2 = NbCollections.checkedSetByFilter(s, String JavaDoc.class, false);
176         assertEquals(3, s2.size());
177         assertEquals(new HashSet JavaDoc(Arrays.asList(new String JavaDoc[] {"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 JavaDoc e) {/*OK*/}
184         Iterator JavaDoc<String JavaDoc> 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 JavaDoc(Arrays.asList(new String JavaDoc[] {"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 JavaDoc(Arrays.asList(new Integer JavaDoc[] {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 JavaDoc(Arrays.asList(new String JavaDoc[] {"new", "additional"})), s);
206         try {
207             ((Set JavaDoc) s2).add(13);
208             fail();
209         } catch (ClassCastException JavaDoc e) {/*OK*/}
210         // Other:
211
assertEquals("preserved by serialization", s2, cloneBySerialization(s2));
212         assertEquals("empty set filtered as empty", Collections.emptySet(), NbCollections.checkedSetByFilter(Collections.emptySet(), String JavaDoc.class, false));
213         assertEquals("empty set from wholly wrong set", Collections.emptySet(), NbCollections.checkedSetByFilter(Collections.singleton(5), String JavaDoc.class, false));
214         // Make sure iterator behaves fully acc. to contract:
215
Set JavaDoc<Integer JavaDoc> s3 = NbCollections.checkedSetByFilter(new HashSet JavaDoc(Collections.singleton(1)), Integer JavaDoc.class, false);
216         Iterator JavaDoc<Integer JavaDoc> it3 = s3.iterator();
217         assertTrue(it3.hasNext());
218         assertTrue(it3.hasNext());
219         assertEquals(new Integer JavaDoc(1), it3.next());
220         assertFalse(it3.hasNext());
221         assertFalse(it3.hasNext());
222         try {
223             it3.next();
224             fail();
225         } catch (NoSuchElementException JavaDoc e) {/*OK*/}
226         it3 = s3.iterator();
227         try {
228             it3.remove();
229             fail();
230         } catch (IllegalStateException JavaDoc e) {/*OK*/}
231         it3 = s3.iterator();
232         it3.next();
233         it3.remove();
234         try {
235             it3.remove();
236             fail();
237         } catch (IllegalStateException JavaDoc e) {/*OK*/}
238     }
239
240     public void testCheckedSetByFilterStrict() throws Exception JavaDoc {
241         Set JavaDoc s = new HashSet JavaDoc();
242         s.add("hello");
243         s.add("there");
244         s.add(1);
245         s.add("goodbye");
246         s.add(2);
247         Set JavaDoc<String JavaDoc> s2 = NbCollections.checkedSetByFilter(s, String JavaDoc.class, true);
248         try {
249             s2.size();
250             fail();
251         } catch (ClassCastException JavaDoc x) {/*OK*/}
252         try {
253             new HashSet JavaDoc<String JavaDoc>(s2);
254             fail();
255         } catch (ClassCastException JavaDoc x) {/*OK*/}
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 JavaDoc e) {/*OK*/}
264     }
265
266     public void testCheckedMapByFilter() throws Exception JavaDoc {
267         Map JavaDoc m = new HashMap JavaDoc();
268         m.put(1, "one");
269         m.put(2, "two");
270         m.put("three", "three");
271         m.put(4, 4);
272         Map JavaDoc<Integer JavaDoc,String JavaDoc> m2 = NbCollections.checkedMapByFilter(m, Integer JavaDoc.class, String JavaDoc.class, false);
273         assertEquals(2, m2.size());
274         assertEquals("one", m2.get(1));
275         try {
276             m2.get("three");
277             fail();
278         } catch (ClassCastException JavaDoc e) {/*OK*/}
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 JavaDoc e) {/*OK*/}
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 JavaDoc e) {/*OK*/}
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 JavaDoc e) {/*OK*/}
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 JavaDoc e) {/*OK*/}
310         assertFalse(m2.values().contains("three"));
311         // Destructive operations:
312
m2.put(1, "#one");
313         assertEquals("#one", m2.get(1));
314         assertEquals("#one", m.get(1));
315         try {
316             ((Map JavaDoc) m2).put("five", "five");
317             fail();
318         } catch (ClassCastException JavaDoc e) {/*OK*/}
319         try {
320             ((Map JavaDoc) m2).put(5, 5);
321             fail();
322         } catch (ClassCastException JavaDoc e) {/*OK*/}
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         // Other:
345
assertEquals(m2, cloneBySerialization(m2));
346         assertEquals(Collections.emptyMap(), NbCollections.checkedMapByFilter(Collections.emptyMap(), String JavaDoc.class, String JavaDoc.class, false));
347         assertEquals(Collections.emptyMap(), NbCollections.checkedMapByFilter(Collections.singletonMap(1, "two"), String JavaDoc.class, String JavaDoc.class, false));
348         assertEquals(Collections.emptyMap(), NbCollections.checkedMapByFilter(Collections.singletonMap("one", 2), String JavaDoc.class, String JavaDoc.class, false));
349         // Misc. return values have to reflect nature of view:
350
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 JavaDoc {
361         Map JavaDoc m = new HashMap JavaDoc();
362         m.put(1, "one");
363         m.put(2, "two");
364         m.put("three", "three");
365         m.put(4, 4);
366         Map JavaDoc<Integer JavaDoc,String JavaDoc> m2 = NbCollections.checkedMapByFilter(m, Integer JavaDoc.class, String JavaDoc.class, true);
367         try {
368             m2.size();
369             fail();
370         } catch (ClassCastException JavaDoc e) {/*OK*/}
371         try {
372             new HashMap JavaDoc<Integer JavaDoc,String JavaDoc>(m2);
373             fail();
374         } catch (ClassCastException JavaDoc e) {/*OK*/}
375         try {
376             m2.get("three");
377             fail();
378         } catch (ClassCastException JavaDoc e) {/*OK*/}
379         m.remove("three");
380         try {
381             m2.size();
382             fail();
383         } catch (ClassCastException JavaDoc e) {/*OK*/}
384         m.remove(4);
385         m.put("three", "three");
386         try {
387             m2.size();
388             fail();
389         } catch (ClassCastException JavaDoc e) {/*OK*/}
390         m.remove("three");
391         assertEquals(2, m2.size());
392         assertEquals("one", m2.get(1));
393     }
394
395     public void testCheckedEnumerationByFilter() throws Exception JavaDoc {
396         Enumeration JavaDoc raw = Collections.enumeration(Arrays.asList("one", 2, "three"));
397         Enumeration JavaDoc<String JavaDoc> strings = NbCollections.checkedEnumerationByFilter(raw, String JavaDoc.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 JavaDoc {
406         Enumeration JavaDoc raw = Collections.enumeration(Arrays.asList("one", 2, "three"));
407         Enumeration JavaDoc<String JavaDoc> strings = NbCollections.checkedEnumerationByFilter(raw, String JavaDoc.class, true);
408         try {
409             Collections.list(strings);
410             fail();
411         } catch (ClassCastException JavaDoc e) {/*OK*/}
412         raw = Collections.enumeration(Arrays.asList("one", "three"));
413         strings = NbCollections.checkedEnumerationByFilter(raw, String JavaDoc.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 JavaDoc cloneBySerialization(Object JavaDoc o) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
422         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
423         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(baos);
424         oos.writeObject(o);
425         oos.close();
426         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
427         return new ObjectInputStream JavaDoc(bais).readObject();
428     }
429
430     public void testIterable() throws Exception JavaDoc {
431         String JavaDoc text = "hello kitty!";
432         List JavaDoc<String JavaDoc> l1 = new ArrayList JavaDoc<String JavaDoc>();
433         for (String JavaDoc token : NbCollections.iterable(new Scanner JavaDoc(text))) {
434             l1.add(token);
435         }
436         assertEquals(Arrays.asList("hello", "kitty!"), l1);
437         for (String JavaDoc token : NbCollections.iterable(new Scanner JavaDoc(""))) {
438             fail();
439         }
440         try {
441             NbCollections.iterable((Iterator JavaDoc<?>) null);
442             fail();
443         } catch (NullPointerException JavaDoc x) {/* OK */}
444         List JavaDoc<URL JavaDoc> l2 = new ArrayList JavaDoc<URL JavaDoc>();
445         for (URL JavaDoc u : NbCollections.iterable(NbCollections.class.getClassLoader().getResources(NbCollections.class.getName().replace('.', '/') + ".class"))) {
446             assertNotNull(u);
447             l2.add(u);
448         }
449         assertFalse(l2.isEmpty()); // permissible to have >1 element in case JAR doubly added to CP
450
for (URL JavaDoc u : NbCollections.iterable(NbCollections.class.getClassLoader().getResources("nonexistent"))) {
451             fail();
452         }
453         try {
454             NbCollections.iterable((Enumeration JavaDoc<?>) null);
455             fail();
456         } catch (NullPointerException JavaDoc x) {/* OK */}
457     }
458
459 }
460
Popular Tags