< prev index next >

test/jdk/java/util/Collection/IteratorAtEnd.java

Print this page
@@ -1,7 +1,7 @@
  /*
-  * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
+  * 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.

@@ -21,11 +21,11 @@
   * questions.
   */
  
  /*
   * @test
-  * @bug 6529795
+  * @bug 6529795 8336669
   * @summary next() does not change iterator state if throws NoSuchElementException
   * @author Martin Buchholz
   */
  
  import java.util.ArrayDeque;

@@ -54,10 +54,11 @@
  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;
  

@@ -94,22 +95,30 @@
                  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(i, 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 >