< prev index next > test/jdk/java/util/Collection/IteratorAtEnd.java
Print this page
/*
! * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
/*
! * Copyright (c) 2007, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
* questions.
*/
/*
* @test
! * @bug 6529795
* @summary next() does not change iterator state if throws NoSuchElementException
* @author Martin Buchholz
*/
import java.util.ArrayDeque;
* questions.
*/
/*
* @test
! * @bug 6529795 8336669
* @summary next() does not change iterator state if throws NoSuchElementException
* @author Martin Buchholz
*/
import java.util.ArrayDeque;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.LinkedTransferQueue;
+ import java.util.stream.IntStream;
@SuppressWarnings("unchecked")
public class IteratorAtEnd {
private static final int SIZE = 6;
c.add(i);
test(c);
} catch (Throwable t) { unexpected(t); }
}
static void testMap(Map m) {
try {
for (int i = 0; i < 3*SIZE; i++)
! m.put(i, i);
test(m.values());
test(m.keySet());
test(m.entrySet());
} catch (Throwable t) { unexpected(t); }
}
static void test(Collection c) {
try {
final Iterator it = c.iterator();
THROWS(NoSuchElementException.class,
() -> { while (true) it.next(); });
try { it.remove(); }
catch (UnsupportedOperationException exc) { return; }
c.add(i);
test(c);
} catch (Throwable t) { unexpected(t); }
}
+ // A strong list of reference to map keys to avoid
+ // spurious purges in WeakHashMap
+ private static final List<String> MAP_KEYS = IntStream
+ .range(0, 3 * SIZE)
+ .mapToObj(i -> "BASE-" + i)
+ .toList();
+
static void testMap(Map m) {
try {
for (int i = 0; i < 3*SIZE; i++)
! m.put(MAP_KEYS.get(i), i);
test(m.values());
test(m.keySet());
test(m.entrySet());
} catch (Throwable t) { unexpected(t); }
}
static void test(Collection c) {
try {
+ assert !c.isEmpty() : "Calling remove on empty iterator will result in IllegalStateException";
final Iterator it = c.iterator();
THROWS(NoSuchElementException.class,
() -> { while (true) it.next(); });
try { it.remove(); }
catch (UnsupportedOperationException exc) { return; }
< prev index next >