1 /*
2 * Copyright (c) 2007, 2026, 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 6529795 8336669
27 * @summary next() does not change iterator state if throws NoSuchElementException
28 * @author Martin Buchholz
29 */
30
31 import java.util.ArrayDeque;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.HashMap;
35 import java.util.Hashtable;
36 import java.util.IdentityHashMap;
37 import java.util.Iterator;
38 import java.util.LinkedHashMap;
39 import java.util.LinkedList;
40 import java.util.List;
41 import java.util.ListIterator;
42 import java.util.Map;
43 import java.util.NoSuchElementException;
44 import java.util.PriorityQueue;
45 import java.util.TreeMap;
46 import java.util.TreeSet;
47 import java.util.Vector;
48 import java.util.WeakHashMap;
49 import java.util.concurrent.ArrayBlockingQueue;
50 import java.util.concurrent.ConcurrentHashMap;
51 import java.util.concurrent.ConcurrentLinkedDeque;
52 import java.util.concurrent.ConcurrentLinkedQueue;
53 import java.util.concurrent.ConcurrentSkipListMap;
54 import java.util.concurrent.ConcurrentSkipListSet;
55 import java.util.concurrent.CopyOnWriteArrayList;
56 import java.util.concurrent.CopyOnWriteArraySet;
57 import java.util.concurrent.LinkedBlockingQueue;
58 import java.util.concurrent.LinkedTransferQueue;
59 import java.util.stream.IntStream;
60
61 @SuppressWarnings("unchecked")
62 public class IteratorAtEnd {
63 private static final int SIZE = 6;
64
65 static void realMain(String[] args) throws Throwable {
66 testCollection(new ArrayList());
67 testCollection(new Vector());
68 testCollection(new LinkedList());
69 testCollection(new ArrayDeque());
70 testCollection(new TreeSet());
71 testCollection(new CopyOnWriteArrayList());
72 testCollection(new CopyOnWriteArraySet());
73 testCollection(new ConcurrentSkipListSet());
74
75 testCollection(new PriorityQueue());
76 testCollection(new LinkedBlockingQueue());
77 testCollection(new ArrayBlockingQueue(100));
78 testCollection(new ConcurrentLinkedDeque());
79 testCollection(new ConcurrentLinkedQueue());
80 testCollection(new LinkedTransferQueue());
81
82 testMap(new HashMap());
83 testMap(new Hashtable());
84 testMap(new LinkedHashMap());
85 testMap(new WeakHashMap());
86 testMap(new IdentityHashMap());
87 testMap(new ConcurrentHashMap());
88 testMap(new ConcurrentSkipListMap());
89 testMap(new TreeMap());
90 }
91
92 static void testCollection(Collection c) {
93 try {
94 for (int i = 0; i < SIZE; i++)
95 c.add(i);
96 test(c);
97 } catch (Throwable t) { unexpected(t); }
98 }
99
100 // A strong list of reference to map keys to avoid
101 // spurious purges in WeakHashMap
102 private static final List<String> MAP_KEYS = IntStream
103 .range(0, 3 * SIZE)
104 .mapToObj(i -> "BASE-" + i)
105 .toList();
106
107 static void testMap(Map m) {
108 try {
109 for (int i = 0; i < 3*SIZE; i++)
110 m.put(MAP_KEYS.get(i), i);
111 test(m.values());
112 test(m.keySet());
113 test(m.entrySet());
114 } catch (Throwable t) { unexpected(t); }
115 }
116
117 static void test(Collection c) {
118 try {
119 assert !c.isEmpty() : "Calling remove on empty iterator will result in IllegalStateException";
120 final Iterator it = c.iterator();
121 THROWS(NoSuchElementException.class,
122 () -> { while (true) it.next(); });
123 try { it.remove(); }
124 catch (UnsupportedOperationException exc) { return; }
125 pass();
126 } catch (Throwable t) { unexpected(t); }
127
128 if (c instanceof List) {
129 final List list = (List) c;
130 try {
131 final ListIterator it = list.listIterator(0);
132 it.next();
133 final Object x = it.previous();
134 THROWS(NoSuchElementException.class, () -> it.previous());
135 try { it.remove(); }
136 catch (UnsupportedOperationException exc) { return; }
137 pass();
138 check(! list.get(0).equals(x));
139 } catch (Throwable t) { unexpected(t); }
140
141 try {
142 final ListIterator it = list.listIterator(list.size());
143 it.previous();
144 final Object x = it.next();
145 THROWS(NoSuchElementException.class, () -> it.next());
146 try { it.remove(); }
147 catch (UnsupportedOperationException exc) { return; }
148 pass();
149 check(! list.get(list.size()-1).equals(x));
150 } catch (Throwable t) { unexpected(t); }
151 }
152 }
153
154 //--------------------- Infrastructure ---------------------------
155 static volatile int passed = 0, failed = 0;
156 static void pass() {passed++;}
157 static void fail() {failed++; Thread.dumpStack();}
158 static void fail(String msg) {System.out.println(msg); fail();}
159 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
160 static void check(boolean cond) {if (cond) pass(); else fail();}
161 static void equal(Object x, Object y) {
162 if (x == null ? y == null : x.equals(y)) pass();
163 else fail(x + " not equal to " + y);}
164 public static void main(String[] args) throws Throwable {
165 try {realMain(args);} catch (Throwable t) {unexpected(t);}
166 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
167 if (failed > 0) throw new AssertionError("Some tests failed");}
168 interface Fun {void f() throws Throwable;}
169 static void THROWS(Class<? extends Throwable> k, Fun... fs) {
170 for (Fun f : fs)
171 try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
172 catch (Throwable t) {
173 if (k.isAssignableFrom(t.getClass())) pass();
174 else unexpected(t);}}
175 }