KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.ref.WeakReference JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.NoSuchElementException JavaDoc;
31 import java.util.Set JavaDoc;
32 import junit.textui.TestRunner;
33 import org.netbeans.junit.NbTestCase;
34 import org.netbeans.junit.NbTestSuite;
35
36 /** This is the base test for new and old enumerations. It contains
37  * factory methods for various kinds of enumerations and set of tests
38  * that use them. Factory methods are overriden in OldEnumerationsTest
39  *
40  * @author Jaroslav Tulach
41  */

42 public class EnumerationsTest extends NbTestCase {
43     
44     /** Creates a new instance of EnumerationsTest */
45     public EnumerationsTest(String JavaDoc testName) {
46         super(testName);
47     }
48     
49     //
50
// Factory methods
51
//
52

53     protected Enumeration JavaDoc singleton(Object JavaDoc obj) {
54         return Enumerations.singleton(obj);
55     }
56     protected Enumeration JavaDoc concat(Enumeration JavaDoc en1, Enumeration JavaDoc en2) {
57         return Enumerations.concat(en1, en2);
58     }
59     protected Enumeration JavaDoc concat(Enumeration JavaDoc enumOfEnums) {
60         return Enumerations.concat(enumOfEnums);
61     }
62     protected Enumeration JavaDoc removeDuplicates(Enumeration JavaDoc en) {
63         return Enumerations.removeDuplicates(en);
64     }
65     protected Enumeration JavaDoc empty() {
66         return Enumerations.empty();
67     }
68     protected Enumeration JavaDoc array(Object JavaDoc[] arr) {
69         return Enumerations.array(arr);
70     }
71     protected Enumeration JavaDoc convert(Enumeration JavaDoc en, final Map JavaDoc map) {
72         class P implements Enumerations.Processor {
73             public Object JavaDoc process(Object JavaDoc obj, Collection JavaDoc nothing) {
74                 return map.get(obj);
75             }
76         }
77         
78         
79         return Enumerations.convert(en, new P());
80     }
81     protected Enumeration JavaDoc removeNulls(Enumeration JavaDoc en) {
82         return Enumerations.removeNulls(en);
83     }
84     protected Enumeration JavaDoc filter(Enumeration JavaDoc en, final Set JavaDoc filter) {
85         class P implements Enumerations.Processor {
86             public Object JavaDoc process(Object JavaDoc obj, Collection JavaDoc nothing) {
87                 return filter.contains(obj) ? obj : null;
88             }
89         }
90         
91         return Enumerations.filter(en, new P());
92     }
93     
94     protected Enumeration JavaDoc filter(Enumeration JavaDoc en, final QueueProcess filter) {
95         class P implements Enumerations.Processor {
96             public Object JavaDoc process(Object JavaDoc obj, Collection JavaDoc nothing) {
97                 return filter.process(obj, nothing);
98             }
99         }
100         
101         return Enumerations.filter(en, new P());
102     }
103     
104     /**
105      * @param filter the set.contains (...) is called before each object is produced
106      * @return Enumeration
107      */

108     protected Enumeration JavaDoc queue(Collection JavaDoc initContent, final QueueProcess process) {
109         class C implements Enumerations.Processor {
110             public Object JavaDoc process(Object JavaDoc object, Collection JavaDoc toAdd) {
111                 return process.process(object, toAdd);
112             }
113         }
114         return Enumerations.queue(
115                 Collections.enumeration(initContent),
116                 new C()
117                 );
118     }
119     
120     /** Processor interface.
121      */

122     public static interface QueueProcess {
123         public Object JavaDoc process(Object JavaDoc object, Collection JavaDoc toAdd);
124     }
125     
126     //
127
// The tests
128
//
129

130     public void testEmptyIsEmpty() {
131         Enumeration JavaDoc e = empty();
132         assertFalse(e.hasMoreElements());
133         try {
134             e.nextElement();
135             fail("No elements");
136         } catch (NoSuchElementException JavaDoc ex) {
137             // ok
138
}
139     }
140     
141     public void testSingleIsSingle() {
142         Enumeration JavaDoc e = singleton(this);
143         assertTrue(e.hasMoreElements());
144         assertEquals("Returns me", this, e.nextElement());
145         assertFalse("Now it is empty", e.hasMoreElements());
146         try {
147             e.nextElement();
148             fail("No elements");
149         } catch (NoSuchElementException JavaDoc ex) {
150             // ok
151
}
152     }
153     
154     public void testConcatTwoAndArray() {
155         Object JavaDoc[] one = { new Integer JavaDoc(1), new Integer JavaDoc(2), new Integer JavaDoc(3) };
156         Object JavaDoc[] two = { "1", "2", "3" };
157         
158         ArrayList JavaDoc list = new ArrayList JavaDoc(Arrays.asList(one));
159         list.addAll(Arrays.asList(two));
160         
161         assertEnums(
162                 concat(array(one), array(two)),
163                 Collections.enumeration(list)
164                 );
165     }
166     
167     public void testConcatTwoAndArrayAndTakeOnlyStrings() {
168         Object JavaDoc[] one = { new Integer JavaDoc(1), new Integer JavaDoc(2), new Integer JavaDoc(3) };
169         Object JavaDoc[] two = { "1", "2", "3" };
170         Object JavaDoc[] three = { new Long JavaDoc(1) };
171         Object JavaDoc[] four = { "Kuk" };
172         
173         ArrayList JavaDoc list = new ArrayList JavaDoc(Arrays.asList(two));
174         list.addAll(Arrays.asList(four));
175         
176         Enumeration JavaDoc[] alls = {
177             array(one), array(two), array(three), array(four)
178         };
179         
180         assertEnums(
181                 filter(concat(array(alls)), new OnlyStrings()),
182                 Collections.enumeration(list)
183                 );
184     }
185     
186     public void testRemoveDuplicates() {
187         Object JavaDoc[] one = { new Integer JavaDoc(1), new Integer JavaDoc(2), new Integer JavaDoc(3) };
188         Object JavaDoc[] two = { "1", "2", "3" };
189         Object JavaDoc[] three = { new Integer JavaDoc(1) };
190         Object JavaDoc[] four = { "2", "3", "4" };
191         
192         Enumeration JavaDoc[] alls = {
193             array(one), array(two), array(three), array(four)
194         };
195         
196         assertEnums(
197                 removeDuplicates(concat(array(alls))),
198                 array(new Object JavaDoc[] { new Integer JavaDoc(1), new Integer JavaDoc(2), new Integer JavaDoc(3), "1", "2", "3", "4" })
199                 );
200         
201     }
202     
203     public void testRemoveDuplicatesAndGCWorks() {
204         
205         /*** Return { i1, "", "", "", i2 } */
206         class WeakEnum implements Enumeration JavaDoc {
207             public Object JavaDoc i1 = new Integer JavaDoc(1);
208             public Object JavaDoc i2 = new Integer JavaDoc(1);
209             
210             private int state;
211             
212             public boolean hasMoreElements() {
213                 return state < 5;
214             }
215             
216             public Object JavaDoc nextElement() {
217                 switch (state++) {
218                     case 0: return i1;
219                     case 1: case 2: case 3: return "";
220                     default: return i2;
221                 }
222             }
223         }
224         
225         WeakEnum weak = new WeakEnum();
226         Enumeration JavaDoc en = removeDuplicates(weak);
227         
228         assertTrue("Has some elements", en.hasMoreElements());
229         assertEquals("And the first one is get", weak.i1, en.nextElement());
230         
231         try {
232             WeakReference JavaDoc ref = new WeakReference JavaDoc(weak.i1);
233             weak.i1 = null;
234             assertGC("Try hard to GC the first integer", ref);
235             // does not matter whether it GCs or not
236
} catch (Throwable JavaDoc tw) {
237             // not GCed, but does not matter
238
}
239         assertTrue("Next object will be string", en.hasMoreElements());
240         assertEquals("is empty string", "", en.nextElement());
241         
242         assertFalse("The second integer is however equal to the original i1 and thus" +
243                 " the enum should not be there", en.hasMoreElements());
244     }
245     
246     public void testQueueEnum() {
247         class Pr implements QueueProcess {
248             public Object JavaDoc process(Object JavaDoc o, Collection JavaDoc c) {
249                 Integer JavaDoc i = (Integer JavaDoc)o;
250                 int plus = i.intValue() + 1;
251                 if (plus < 10) {
252                     c.add(new Integer JavaDoc(plus));
253                 }
254                 return i;
255             }
256         }
257         Pr p = new Pr();
258         
259         Enumeration JavaDoc en = queue(
260                 Collections.nCopies(1, new Integer JavaDoc(0)), p
261                 );
262         
263         for (int i = 0; i < 10; i++) {
264             assertTrue("has next", en.hasMoreElements());
265             en.nextElement();
266         }
267         
268         assertFalse("No next element", en.hasMoreElements());
269     }
270     
271     public void testFilteringAlsoDoesConvertions() throws Exception JavaDoc {
272         class Pr implements QueueProcess {
273             public Object JavaDoc process(Object JavaDoc o, Collection JavaDoc ignore) {
274                 Integer JavaDoc i = (Integer JavaDoc)o;
275                 int plus = i.intValue() + 1;
276                 return new Integer JavaDoc(plus);
277             }
278         }
279         Pr p = new Pr();
280         
281         Enumeration JavaDoc onetwo = array(new Object JavaDoc[] { new Integer JavaDoc(1), new Integer JavaDoc(2) });
282         Enumeration JavaDoc twothree = array(new Object JavaDoc[] { new Integer JavaDoc(2), new Integer JavaDoc(3) });
283         
284         assertEnums(
285                 filter(onetwo, p), twothree
286                 );
287     }
288     
289     
290     private static void assertEnums(Enumeration JavaDoc e1, Enumeration JavaDoc e2) {
291         int indx = 0;
292         while (e1.hasMoreElements() && e2.hasMoreElements()) {
293             Object JavaDoc i1 = e1.nextElement();
294             Object JavaDoc i2 = e2.nextElement();
295             assertEquals(indx++ + "th: ", i1, i2);
296         }
297         
298         if (e1.hasMoreElements()) {
299             fail("first one contains another element: " + e1.nextElement());
300         }
301         if (e2.hasMoreElements()) {
302             fail("second one contains another element: " + e2.nextElement());
303         }
304         
305         try {
306             e1.nextElement();
307             fail("First one should throw exception, but nothing happend");
308         } catch (NoSuchElementException JavaDoc ex) {
309             // ok
310
}
311         
312         try {
313             e2.nextElement();
314             fail("Second one should throw exception, but nothing happend");
315         } catch (NoSuchElementException JavaDoc ex) {
316             // ok
317
}
318     }
319     
320     public void testConvertIntegersToStringRemoveNulls() {
321         Object JavaDoc[] garbage = { new Integer JavaDoc(1), "kuk", "hle", new Integer JavaDoc(5) };
322         
323         assertEnums(
324                 removeNulls(convert(array(garbage), new MapIntegers())),
325                 array(new Object JavaDoc[] { "1", "5" })
326                 );
327     }
328     
329     public void testQueueEnumerationCanReturnNulls() {
330         Object JavaDoc[] nuls = { null, "NULL" };
331         
332         class P implements QueueProcess {
333             public Object JavaDoc process(Object JavaDoc toRet, Collection JavaDoc toAdd) {
334                 if (toRet == null) return null;
335                 
336                 if ("NULL".equals(toRet)) {
337                     toAdd.add(null);
338                     return null;
339                 }
340                 
341                 return null;
342             }
343         }
344         
345         assertEnums(
346                 array(new Object JavaDoc[] { null, null, null }),
347                 queue(Arrays.asList(nuls), new P())
348                 );
349     }
350     
351     /** Filters only strings.
352      */

353     private static final class OnlyStrings implements Set JavaDoc {
354         public boolean add(Object JavaDoc o) {
355             fail("Should not be every called");
356             return false;
357         }
358         
359         public boolean addAll(Collection JavaDoc c) {
360             fail("Should not be every called");
361             return false;
362         }
363         
364         public void clear() {
365             fail("Should not be every called");
366         }
367         
368         public boolean contains(Object JavaDoc o) {
369             return o instanceof String JavaDoc;
370         }
371         
372         public boolean containsAll(Collection JavaDoc c) {
373             fail("Should not be every called");
374             return false;
375         }
376         
377         public boolean isEmpty() {
378             fail("Should not be every called");
379             return false;
380         }
381         
382         public Iterator JavaDoc iterator() {
383             fail("Should not be every called");
384             return null;
385         }
386         
387         public boolean remove(Object JavaDoc o) {
388             fail("Should not be every called");
389             return false;
390         }
391         
392         public boolean removeAll(Collection JavaDoc c) {
393             fail("Should not be every called");
394             return false;
395         }
396         
397         public boolean retainAll(Collection JavaDoc c) {
398             fail("Should not be every called");
399             return false;
400         }
401         
402         public int size() {
403             fail("Should not be every called");
404             return 1;
405         }
406         
407         public Object JavaDoc[] toArray() {
408             fail("Should not be every called");
409             return null;
410         }
411         
412         public Object JavaDoc[] toArray(Object JavaDoc[] a) {
413             fail("Should not be every called");
414             return null;
415         }
416     }
417     
418     /** Filters only strings.
419      */

420     private static final class MapIntegers implements Map JavaDoc {
421         public boolean containsKey(Object JavaDoc key) {
422             fail("Should not be every called");
423             return false;
424         }
425         
426         public boolean containsValue(Object JavaDoc value) {
427             fail("Should not be every called");
428             return false;
429         }
430         
431         public Set JavaDoc entrySet() {
432             fail("Should not be every called");
433             return null;
434         }
435         
436         public Object JavaDoc get(Object JavaDoc key) {
437             if (key instanceof Integer JavaDoc) {
438                 return key.toString();
439             }
440             return null;
441         }
442         
443         public Set JavaDoc keySet() {
444             fail("Should not be every called");
445             return null;
446         }
447         
448         public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
449             fail("Should not be every called");
450             return null;
451         }
452         
453         public void putAll(Map JavaDoc t) {
454             fail("Should not be every called");
455         }
456         
457         public Collection JavaDoc values() {
458             fail("Should not be every called");
459             return null;
460         }
461         
462         public void clear() {
463             fail("Should not be every called");
464         }
465         
466         public boolean isEmpty() {
467             fail("Should not be every called");
468             return false;
469         }
470         
471         public Object JavaDoc remove(Object JavaDoc key) {
472             fail("Should not be every called");
473             return null;
474         }
475         
476         public int size() {
477             fail("Should not be every called");
478             return 1;
479         }
480         
481     }
482 }
483
Popular Tags