1 |
/* |
2 |
* Copyright (c) 2007, 2014, 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 8004928 |
27 |
* @summary Test empty iterators, enumerations, and collections |
28 |
*/ |
29 |
|
30 |
import java.util.Collection; |
31 |
import java.util.Collections; |
32 |
import java.util.Enumeration; |
33 |
import java.util.Hashtable; |
34 |
import java.util.Iterator; |
35 |
import java.util.List; |
36 |
import java.util.Map; |
37 |
import java.util.NoSuchElementException; |
38 |
import java.util.concurrent.SynchronousQueue; |
39 |
|
40 |
import static java.util.Collections.emptyEnumeration; |
41 |
import static java.util.Collections.emptyIterator; |
42 |
import static java.util.Collections.emptyList; |
43 |
import static java.util.Collections.emptyListIterator; |
44 |
import static java.util.Collections.emptyMap; |
45 |
import static java.util.Collections.emptySet; |
46 |
import static java.util.Collections.nCopies; |
47 |
import static java.util.Collections.unmodifiableMap; |
48 |
|
49 |
public class EmptyIterator { |
50 |
|
51 |
void test(String[] args) throws Throwable { |
52 |
testEmptyCollection(emptyList()); |
53 |
testEmptyCollection(emptySet()); |
54 |
testEmptyCollection(new SynchronousQueue<Object>()); |
55 |
testEmptyMap(emptyMap()); |
56 |
|
57 |
Hashtable<?,?> emptyTable = new Hashtable<>(); |
58 |
testEmptyEnumeration(emptyTable.keys()); |
59 |
testEmptyEnumeration(emptyTable.elements()); |
60 |
testEmptyIterator(emptyTable.keySet().iterator()); |
61 |
testEmptyIterator(emptyTable.values().iterator()); |
62 |
testEmptyIterator(emptyTable.entrySet().iterator()); |
63 |
|
64 |
final Enumeration<EmptyIterator> finalEmptyTyped = emptyEnumeration(); |
65 |
testEmptyEnumeration(finalEmptyTyped); |
66 |
|
67 |
final Enumeration<?> finalEmptyAbstract = emptyEnumeration(); |
68 |
testEmptyEnumeration(finalEmptyAbstract); |
69 |
|
70 |
testEmptyIterator(emptyIterator()); |
71 |
} |
72 |
|
73 |
void testEmptyEnumeration(final Enumeration<?> e) { |
74 |
check(e == emptyEnumeration()); |
75 |
check(!e.hasMoreElements()); |
76 |
THROWS(NoSuchElementException.class, |
77 |
new F(){void f(){ e.nextElement(); }}); |
78 |
} |
79 |
|
80 |
void testEmptyIterator(final Iterator<?> it) { |
81 |
check(it == emptyIterator()); |
82 |
check(! it.hasNext()); |
83 |
THROWS(NoSuchElementException.class, |
84 |
new F(){void f(){ it.next(); }}); |
85 |
THROWS(IllegalStateException.class, |
86 |
new F(){void f(){ it.remove(); }}); |
87 |
} |
88 |
|
89 |
void testEmptyMap(Map<?,?> m) { |
90 |
check(m == emptyMap()); |
91 |
check(m.entrySet().iterator() == |
92 |
Collections.<Map.Entry<?,?>>emptyIterator()); |
93 |
check(m.values().iterator() == emptyIterator()); |
94 |
check(m.keySet().iterator() == emptyIterator()); |
95 |
equal(m, unmodifiableMap(m)); |
96 |
|
97 |
testEmptyCollection(m.keySet()); |
98 |
testEmptyCollection(m.entrySet()); |
99 |
testEmptyCollection(m.values()); |
100 |
} |
101 |
|
102 |
void testToArray(final Collection<?> c) { |
103 |
Object[] a = c.toArray(); |
104 |
equal(a.length, 0); |
105 |
equal(a.getClass().getComponentType(), Object.class); |
106 |
THROWS(NullPointerException.class, |
107 |
new F(){void f(){ c.toArray((Object[])null); }}); |
108 |
|
109 |
{ |
110 |
String[] t = new String[0]; |
111 |
check(c.toArray(t) == t); |
112 |
} |
113 |
|
114 |
{ |
115 |
String[] t = nCopies(10, "").toArray(new String[0]); |
116 |
check(c.toArray(t) == t); |
117 |
check(t[0] == null); |
118 |
for (int i=1; i<t.length; i++) |
119 |
check(t[i] == ""); |
120 |
} |
121 |
} |
122 |
|
123 |
void testEmptyCollection(final Collection<?> c) { |
124 |
testEmptyIterator(c.iterator()); |
125 |
|
126 |
check(c.iterator() == emptyIterator()); |
127 |
if (c instanceof List) |
128 |
check(((List<?>)c).listIterator() == emptyListIterator()); |
129 |
|
130 |
testToArray(c); |
131 |
} |
132 |
|
133 |
//--------------------- Infrastructure --------------------------- |
134 |
volatile int passed = 0, failed = 0; |
135 |
void pass() {passed++;} |
136 |
void fail() {failed++; Thread.dumpStack();} |
137 |
void fail(String msg) {System.err.println(msg); fail();} |
138 |
void unexpected(Throwable t) {failed++; t.printStackTrace();} |
139 |
void check(boolean cond) {if (cond) pass(); else fail();} |
140 |
void equal(Object x, Object y) { |
141 |
if (x == null ? y == null : x.equals(y)) pass(); |
142 |
else fail(x + " not equal to " + y);} |
143 |
public static void main(String[] args) throws Throwable { |
144 |
new EmptyIterator().instanceMain(args);} |
145 |
void instanceMain(String[] args) throws Throwable { |
146 |
try {test(args);} catch (Throwable t) {unexpected(t);} |
147 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
148 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
149 |
abstract class F {abstract void f() throws Throwable;} |
150 |
void THROWS(Class<? extends Throwable> k, F... fs) { |
151 |
for (F f : fs) |
152 |
try {f.f(); fail("Expected " + k.getName() + " not thrown");} |
153 |
catch (Throwable t) { |
154 |
if (k.isAssignableFrom(t.getClass())) pass(); |
155 |
else unexpected(t);}} |
156 |
} |