2
|
1 |
/*
|
|
2 |
* Copyright 2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 6529795
|
|
27 |
* @summary next() does not change iterator state if throws NoSuchElementException
|
|
28 |
* @author Martin Buchholz
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.util.*;
|
|
32 |
import java.util.concurrent.*;
|
|
33 |
|
|
34 |
@SuppressWarnings("unchecked")
|
|
35 |
public class IteratorAtEnd {
|
|
36 |
private static final int SIZE = 6;
|
|
37 |
|
|
38 |
static void realMain(String[] args) throws Throwable {
|
|
39 |
testCollection(new ArrayList());
|
|
40 |
testCollection(new Vector());
|
|
41 |
testCollection(new LinkedList());
|
|
42 |
testCollection(new ArrayDeque());
|
|
43 |
testCollection(new TreeSet());
|
|
44 |
testCollection(new CopyOnWriteArrayList());
|
|
45 |
testCollection(new CopyOnWriteArraySet());
|
|
46 |
testCollection(new ConcurrentSkipListSet());
|
|
47 |
|
|
48 |
testCollection(new PriorityQueue());
|
|
49 |
testCollection(new LinkedBlockingQueue());
|
|
50 |
testCollection(new ArrayBlockingQueue(100));
|
|
51 |
testCollection(new ConcurrentLinkedQueue());
|
4110
|
52 |
testCollection(new LinkedTransferQueue());
|
2
|
53 |
|
|
54 |
testMap(new HashMap());
|
|
55 |
testMap(new Hashtable());
|
|
56 |
testMap(new LinkedHashMap());
|
|
57 |
testMap(new WeakHashMap());
|
|
58 |
testMap(new IdentityHashMap());
|
|
59 |
testMap(new ConcurrentHashMap());
|
|
60 |
testMap(new ConcurrentSkipListMap());
|
|
61 |
testMap(new TreeMap());
|
|
62 |
}
|
|
63 |
|
|
64 |
static void testCollection(Collection c) {
|
|
65 |
try {
|
|
66 |
for (int i = 0; i < SIZE; i++)
|
|
67 |
c.add(i);
|
|
68 |
test(c);
|
|
69 |
} catch (Throwable t) { unexpected(t); }
|
|
70 |
}
|
|
71 |
|
|
72 |
static void testMap(Map m) {
|
|
73 |
try {
|
|
74 |
for (int i = 0; i < 3*SIZE; i++)
|
|
75 |
m.put(i, i);
|
|
76 |
test(m.values());
|
|
77 |
test(m.keySet());
|
|
78 |
test(m.entrySet());
|
|
79 |
} catch (Throwable t) { unexpected(t); }
|
|
80 |
}
|
|
81 |
|
|
82 |
static void test(Collection c) {
|
|
83 |
try {
|
|
84 |
final Iterator it = c.iterator();
|
|
85 |
THROWS(NoSuchElementException.class,
|
|
86 |
new Fun() {void f() { while (true) it.next(); }});
|
|
87 |
try { it.remove(); }
|
|
88 |
catch (UnsupportedOperationException _) { return; }
|
|
89 |
pass();
|
|
90 |
} catch (Throwable t) { unexpected(t); }
|
|
91 |
|
|
92 |
if (c instanceof List) {
|
|
93 |
final List list = (List) c;
|
|
94 |
try {
|
|
95 |
final ListIterator it = list.listIterator(0);
|
|
96 |
it.next();
|
|
97 |
final Object x = it.previous();
|
|
98 |
THROWS(NoSuchElementException.class,
|
|
99 |
new Fun() {void f() { it.previous(); }});
|
|
100 |
try { it.remove(); }
|
|
101 |
catch (UnsupportedOperationException _) { return; }
|
|
102 |
pass();
|
|
103 |
check(! list.get(0).equals(x));
|
|
104 |
} catch (Throwable t) { unexpected(t); }
|
|
105 |
|
|
106 |
try {
|
|
107 |
final ListIterator it = list.listIterator(list.size());
|
|
108 |
it.previous();
|
|
109 |
final Object x = it.next();
|
|
110 |
THROWS(NoSuchElementException.class,
|
|
111 |
new Fun() {void f() { it.next(); }});
|
|
112 |
try { it.remove(); }
|
|
113 |
catch (UnsupportedOperationException _) { return; }
|
|
114 |
pass();
|
|
115 |
check(! list.get(list.size()-1).equals(x));
|
|
116 |
} catch (Throwable t) { unexpected(t); }
|
|
117 |
}
|
|
118 |
}
|
|
119 |
|
|
120 |
//--------------------- Infrastructure ---------------------------
|
|
121 |
static volatile int passed = 0, failed = 0;
|
|
122 |
static void pass() {passed++;}
|
|
123 |
static void fail() {failed++; Thread.dumpStack();}
|
|
124 |
static void fail(String msg) {System.out.println(msg); fail();}
|
|
125 |
static void unexpected(Throwable t) {failed++; t.printStackTrace();}
|
|
126 |
static void check(boolean cond) {if (cond) pass(); else fail();}
|
|
127 |
static void equal(Object x, Object y) {
|
|
128 |
if (x == null ? y == null : x.equals(y)) pass();
|
|
129 |
else fail(x + " not equal to " + y);}
|
|
130 |
public static void main(String[] args) throws Throwable {
|
|
131 |
try {realMain(args);} catch (Throwable t) {unexpected(t);}
|
|
132 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
|
|
133 |
if (failed > 0) throw new AssertionError("Some tests failed");}
|
|
134 |
private static abstract class Fun {abstract void f() throws Throwable;}
|
|
135 |
static void THROWS(Class<? extends Throwable> k, Fun... fs) {
|
|
136 |
for (Fun f : fs)
|
|
137 |
try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
|
|
138 |
catch (Throwable t) {
|
|
139 |
if (k.isAssignableFrom(t.getClass())) pass();
|
|
140 |
else unexpected(t);}}
|
|
141 |
}
|