ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collections/EmptyIterator.java
Revision: 1.5
Committed: Wed Feb 8 00:01:38 2012 UTC (12 years, 3 months ago) by dl
Branch: MAIN
Changes since 1.4: +0 -8 lines
Log Message:
Sync with openjdk

File Contents

# Content
1 /*
2 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @bug 5017904 6356890
27 * @summary Test empty iterators, enumerations, and collections
28 */
29
30 import java.util.*;
31 import static java.util.Collections.*;
32
33 public class EmptyIterator {
34
35 void test(String[] args) throws Throwable {
36 testEmptyCollection(Collections.<Object>emptyList());
37 testEmptyCollection(Collections.<Object>emptySet());
38 // Removed jdk7 dependency from SynchronousQueue.
39 // testEmptyCollection(new java.util.concurrent.
40 // SynchronousQueue<Object>());
41
42 testEmptyMap(Collections.<Object, Object>emptyMap());
43
44 Hashtable<Object, Object> emptyTable = new Hashtable<Object, Object>();
45 testEmptyEnumeration(emptyTable.keys());
46 testEmptyEnumeration(emptyTable.elements());
47 testEmptyIterator(emptyTable.keySet().iterator());
48 testEmptyIterator(emptyTable.values().iterator());
49 testEmptyIterator(emptyTable.entrySet().iterator());
50
51 testEmptyEnumeration(javax.swing.tree.DefaultMutableTreeNode
52 .EMPTY_ENUMERATION);
53 testEmptyEnumeration(javax.swing.text.SimpleAttributeSet
54 .EMPTY.getAttributeNames());
55
56 @SuppressWarnings("unchecked")
57 Iterator<?> x = new sun.tools.java.MethodSet()
58 .lookupName(sun.tools.java.Identifier.lookup(""));
59 testEmptyIterator(x);
60 }
61
62 <T> void testEmptyEnumeration(final Enumeration<T> e) {
63 check(! e.hasMoreElements());
64 THROWS(NoSuchElementException.class,
65 new F(){void f(){ e.nextElement(); }});
66 }
67
68 <T> void testEmptyIterator(final Iterator<T> it) {
69 check(! it.hasNext());
70 THROWS(NoSuchElementException.class,
71 new F(){void f(){ it.next(); }});
72 THROWS(IllegalStateException.class,
73 new F(){void f(){ it.remove(); }});
74 }
75
76 void testEmptyMap(Map<Object, Object> m) {
77 check(m.entrySet().iterator() ==
78 Collections.<Map.Entry<Object,Object>>emptyIterator());
79 check(m.values().iterator() == emptyIterator());
80 check(m.keySet().iterator() == emptyIterator());
81 equal(m, unmodifiableMap(m));
82
83 testEmptyCollection(m.keySet());
84 testEmptyCollection(m.entrySet());
85 testEmptyCollection(m.values());
86 }
87
88 <E> void testToArray(final Collection<E> c) {
89 Object[] a = c.toArray();
90 equal(a.length, 0);
91 equal(a.getClass().getComponentType(), Object.class);
92 THROWS(NullPointerException.class,
93 new F(){void f(){ c.toArray((Object[])null); }});
94
95 {
96 String[] t = new String[0];
97 check(c.toArray(t) == t);
98 }
99
100 {
101 String[] t = nCopies(10, "").toArray(new String[0]);
102 check(c.toArray(t) == t);
103 check(t[0] == null);
104 for (int i=1; i<t.length; i++)
105 check(t[i] == "");
106 }
107 }
108
109 <E> void testEmptyCollection(final Collection<E> c) {
110 testEmptyIterator(c.iterator());
111 testToArray(c);
112 }
113
114 //--------------------- Infrastructure ---------------------------
115 volatile int passed = 0, failed = 0;
116 void pass() {passed++;}
117 void fail() {failed++; Thread.dumpStack();}
118 void fail(String msg) {System.err.println(msg); fail();}
119 void unexpected(Throwable t) {failed++; t.printStackTrace();}
120 void check(boolean cond) {if (cond) pass(); else fail();}
121 void equal(Object x, Object y) {
122 if (x == null ? y == null : x.equals(y)) pass();
123 else fail(x + " not equal to " + y);}
124 public static void main(String[] args) throws Throwable {
125 new EmptyIterator().instanceMain(args);}
126 void instanceMain(String[] args) throws Throwable {
127 try {test(args);} catch (Throwable t) {unexpected(t);}
128 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
129 if (failed > 0) throw new AssertionError("Some tests failed");}
130 abstract class F {abstract void f() throws Throwable;}
131 void THROWS(Class<? extends Throwable> k, F... fs) {
132 for (F f : fs)
133 try {f.f(); fail("Expected " + k.getName() + " not thrown");}
134 catch (Throwable t) {
135 if (k.isAssignableFrom(t.getClass())) pass();
136 else unexpected(t);}}
137 }