Warnings.
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common
8 * Development and Distribution License("CDDL") (collectively, the
9 * "License"). You may not use this file except in compliance with the
10 * License. You can obtain a copy of the License at
11 * http://www.netbeans.org/cddl-gplv2.html
12 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13 * specific language governing permissions and limitations under the
14 * License. When distributing the software, include this License Header
15 * Notice in each file and include the License file at
16 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17 * particular file as subject to the "Classpath" exception as provided
18 * by Sun in the GPL Version 2 section of the License file that
19 * accompanied this code. If applicable, add the following below the
20 * License Header, with the fields enclosed by brackets [] replaced by
21 * your own identifying information:
22 * "Portions Copyrighted [year] [name of copyright owner]"
26 * The Original Software is NetBeans. The Initial Developer of the Original
27 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28 * Microsystems, Inc. All Rights Reserved.
30 * If you wish your version of this file to be governed by only the CDDL
31 * or only the GPL Version 2, indicate your decision by adding
32 * "[Contributor] elects to include this software in this distribution
33 * under the [CDDL or GPL Version 2] license." If you do not indicate a
34 * single choice of license, a recipient has the option to distribute
35 * your version of this file under either the CDDL, the GPL Version 2 or
36 * to extend the choice of license to its licensees as provided above.
37 * However, if you add GPL Version 2 code and therefore, elected the GPL
38 * Version 2 license, then the option applies only if the new code is
39 * made subject to such option by the copyright holder.
42 package org.openide.util;
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.Collection;
47 import java.util.Collections;
48 import java.util.Enumeration;
49 import java.util.Iterator;
50 import java.util.List;
52 import java.util.NoSuchElementException;
54 import org.netbeans.junit.NbTestCase;
56 /** This is the base test for new and old enumerations. It contains
57 * factory methods for various kinds of enumerations and set of tests
58 * that use them. Factory methods are overriden in OldEnumerationsTest
60 * @author Jaroslav Tulach
62 public class EnumerationsTest extends NbTestCase {
64 /** Creates a new instance of EnumerationsTest */
65 public EnumerationsTest(String testName) {
73 protected <T> Enumeration<T> singleton(T obj) {
74 return Enumerations.singleton(obj);
76 protected <T> Enumeration<T> concat(Enumeration<T> en1, Enumeration<T> en2) {
77 return Enumerations.concat(en1, en2);
79 protected <T> Enumeration<T> concat(Enumeration<Enumeration<T>> enumOfEnums) {
80 return Enumerations.concat(enumOfEnums);
82 protected <T> Enumeration<T> removeDuplicates(Enumeration<T> en) {
83 return Enumerations.removeDuplicates(en);
85 protected <T> Enumeration<T> empty() {
86 return Enumerations.empty();
88 protected <T> Enumeration<T> array(T[] arr) {
89 return Enumerations.array(arr);
91 protected <T,R> Enumeration<R> convert(Enumeration<T> en, final Map<T,R> map) {
92 class P implements Enumerations.Processor<T,R> {
93 public R process(T obj, Collection<T> nothing) {
97 return Enumerations.convert(en, new P());
99 protected <T> Enumeration<T> removeNulls(Enumeration<T> en) {
100 return Enumerations.removeNulls(en);
102 protected <T> Enumeration<T> filter(Enumeration<T> en, final Set<T> filter) {
103 class P implements Enumerations.Processor<T,T> {
104 public T process(T obj, Collection<T> nothing) {
105 return filter.contains(obj) ? obj : null;
108 return Enumerations.filter(en, new P());
111 protected <T,R> Enumeration<R> filter(Enumeration<T> en, final QueueProcess<T,R> filter) {
112 class P implements Enumerations.Processor<T,R> {
113 public R process(T obj, Collection<T> nothing) {
114 return filter.process(obj, nothing);
117 return Enumerations.filter(en, new P());
121 * @param filter the set.contains (...) is called before each object is produced
122 * @return Enumeration
124 protected <T,R> Enumeration<R> queue(Collection<T> initContent, final QueueProcess<T,R> process) {
125 class C implements Enumerations.Processor<T,R> {
126 public R process(T object, Collection<T> toAdd) {
127 return process.process(object, toAdd);
130 return Enumerations.queue(
131 Collections.enumeration(initContent),
136 /** Processor interface.
138 public static interface QueueProcess<T,R> {
139 public R process(T object, Collection<T> toAdd);
146 public void testEmptyIsEmpty() {
147 Enumeration e = empty();
148 assertFalse(e.hasMoreElements());
152 } catch (NoSuchElementException ex) {
157 public void testSingleIsSingle() {
158 Enumeration e = singleton(this);
159 assertTrue(e.hasMoreElements());
160 assertEquals("Returns me", this, e.nextElement());
161 assertFalse("Now it is empty", e.hasMoreElements());
165 } catch (NoSuchElementException ex) {
170 public void testConcatTwoAndArray() {
171 Object[] one = { 1, 2, 3 };
172 Object[] two = { "1", "2", "3" };
174 List<Object> list = new ArrayList<Object>(Arrays.asList(one));
175 list.addAll(Arrays.asList(two));
178 concat(array(one), array(two)),
179 Collections.enumeration(list)
183 public void testConcatTwoAndArrayAndTakeOnlyStrings() {
184 Object[] one = { 1, 2, 3 };
185 Object[] two = { "1", "2", "3" };
186 Object[] three = { 1L };
187 Object[] four = { "Kuk" };
189 List<Object> list = new ArrayList<Object>(Arrays.asList(two));
190 list.addAll(Arrays.asList(four));
192 @SuppressWarnings("unchecked")
193 Enumeration<Object>[] alls = (Enumeration<Object>[]) new Enumeration<?>[] {
194 array(one), array(two), array(three), array(four)
198 filter(concat(array(alls)), new OnlyStrings()),
199 Collections.enumeration(list)
203 public void testRemoveDuplicates() {
204 Object[] one = { 1, 2, 3 };
205 Object[] two = { "1", "2", "3" };
206 Object[] three = { 1 };
207 Object[] four = { "2", "3", "4" };
209 @SuppressWarnings("unchecked")
210 Enumeration<Object>[] alls = (Enumeration<Object>[]) new Enumeration<?>[] {
211 array(one), array(two), array(three), array(four)
215 removeDuplicates(concat(array(alls))),
216 array(new Object[] { 1, 2, 3, "1", "2", "3", "4" })
221 public void testRemoveDuplicatesAndGCWorks() {
223 /*** Return { i1, "", "", "", i2 } */
224 class WeakEnum implements Enumeration<Object> {
225 public Object i1 = new Integer(1);
226 public Object i2 = new Integer(1);
230 public boolean hasMoreElements() {
234 public Object nextElement() {
237 case 1: case 2: case 3: return "";
243 WeakEnum weak = new WeakEnum();
244 Enumeration en = removeDuplicates(weak);
246 assertTrue("Has some elements", en.hasMoreElements());
247 assertEquals("And the first one is get", weak.i1, en.nextElement());
251 Reference<?> ref = new WeakReference<Object>(weak.i1);
255 assertGC("Try hard to GC the first integer", ref);
256 // does not matter whether it GCs or not
257 } catch (Throwable tw) {
258 // not GCed, but does not matter
261 assertTrue("Next object will be string", en.hasMoreElements());
262 assertEquals("is empty string", "", en.nextElement());
264 assertFalse("The second integer is however equal to the original i1 and thus" +
265 " the enum should not be there", en.hasMoreElements());
268 public void testQueueEnum() {
269 class Pr implements QueueProcess<Integer,Integer> {
270 public Integer process(Integer i, Collection<Integer> c) {
280 Enumeration en = queue(
281 Collections.nCopies(1, 0), p
284 for (int i = 0; i < 10; i++) {
285 assertTrue("has next", en.hasMoreElements());
289 assertFalse("No next element", en.hasMoreElements());
292 public void testFilteringAlsoDoesConvertions() throws Exception {
293 class Pr implements QueueProcess<Integer,Integer> {
294 public Integer process(Integer i, Collection<Integer> ignore) {
300 Enumeration<Integer> onetwo = array(new Integer[] { 1, 2 });
301 Enumeration<Integer> twothree = array(new Integer[] { 2, 3 });
304 filter(onetwo, p), twothree
309 private static <T> void assertEnums(Enumeration<T> e1, Enumeration<T> e2) {
311 while (e1.hasMoreElements() && e2.hasMoreElements()) {
312 T i1 = e1.nextElement();
313 T i2 = e2.nextElement();
314 assertEquals(indx++ + "th: ", i1, i2);
317 if (e1.hasMoreElements()) {
318 fail("first one contains another element: " + e1.nextElement());
320 if (e2.hasMoreElements()) {
321 fail("second one contains another element: " + e2.nextElement());
326 fail("First one should throw exception, but nothing happend");
327 } catch (NoSuchElementException ex) {
333 fail("Second one should throw exception, but nothing happend");
334 } catch (NoSuchElementException ex) {
339 public void testConvertIntegersToStringRemoveNulls() {
340 Object[] garbage = { 1, "kuk", "hle", 5 };
343 removeNulls(convert(array(garbage), new MapIntegers())),
344 array(new Object[] { "1", "5" })
348 public void testQueueEnumerationCanReturnNulls() {
349 Object[] nuls = { null, "NULL" };
351 class P implements QueueProcess<Object,Object> {
352 public Object process(Object toRet, Collection<Object> toAdd) {
353 if (toRet == null) return null;
355 if ("NULL".equals(toRet)) {
365 array(new Object[] { null, null, null }),
366 queue(Arrays.asList(nuls), new P())
370 /** Filters only strings.
372 private static final class OnlyStrings implements Set<Object> {
373 public boolean add(Object o) {
374 fail("Should not be every called");
378 public boolean addAll(Collection c) {
379 fail("Should not be every called");
383 public void clear() {
384 fail("Should not be every called");
387 public boolean contains(Object o) {
388 return o instanceof String;
391 public boolean containsAll(Collection c) {
392 fail("Should not be every called");
396 public boolean isEmpty() {
397 fail("Should not be every called");
401 public Iterator<Object> iterator() {
402 fail("Should not be every called");
406 public boolean remove(Object o) {
407 fail("Should not be every called");
411 public boolean removeAll(Collection c) {
412 fail("Should not be every called");
416 public boolean retainAll(Collection c) {
417 fail("Should not be every called");
422 fail("Should not be every called");
426 public Object[] toArray() {
427 fail("Should not be every called");
431 public <T> T[] toArray(T[] a) {
432 fail("Should not be every called");
437 /** Filters only strings.
439 private static final class MapIntegers implements Map<Object,Object> {
440 public boolean containsKey(Object key) {
441 fail("Should not be every called");
445 public boolean containsValue(Object value) {
446 fail("Should not be every called");
450 public Set<Map.Entry<Object,Object>> entrySet() {
451 fail("Should not be every called");
455 public Object get(Object key) {
456 if (key instanceof Integer) {
457 return key.toString();
462 public Set<Object> keySet() {
463 fail("Should not be every called");
467 public Object put(Object key, Object value) {
468 fail("Should not be every called");
472 public void putAll(Map t) {
473 fail("Should not be every called");
476 public Collection<Object> values() {
477 fail("Should not be every called");
481 public void clear() {
482 fail("Should not be every called");
485 public boolean isEmpty() {
486 fail("Should not be every called");
490 public Object remove(Object key) {
491 fail("Should not be every called");
496 fail("Should not be every called");