--- /dev/null 2012-12-13 07:37:25.995126596 -0800 +++ new/test/java/util/IteratorExtensionMethodsTest.java 2012-12-13 16:57:45.419811126 -0800 @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2012, 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. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import org.testng.annotations.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.fail; + +/** + * @test + * @run testng IteratorExtensionMethodsTest + * @summary test extension methods on Iterator + */ +@Test +public class IteratorExtensionMethodsTest { + + public void testRemoveUnsupported() { + final Iterator iterator = new Iterator() { + @Override + public boolean hasNext() { + return false; + } + @Override + public Object next() { + return null; + } + }; + + try { + iterator.remove(); + fail("expected UnsupportedOperationException from remove not thrown"); + } catch (UnsupportedOperationException ignore) { + } + } + + public void testRemoveOverride() { + final IteratorWithRemove iterator = new IteratorWithRemove(); + iterator.remove(); + assertTrue(iterator.removed); + } + + public void testForEach() throws Exception { + final Integer[] data = new Integer[1000]; + for (int i=0; i < data.length; i++) { + data[i] = i; + } + final List source = Arrays.asList(data); + + final String[] iterableCollectionClasses = { + "java.util.ArrayDeque", + "java.util.ArrayList", + "java.util.HashSet", + "java.util.LinkedHashSet", + "java.util.LinkedList", + "java.util.PriorityQueue", + "java.util.TreeSet", + "java.util.Vector", + "java.util.concurrent.ConcurrentLinkedDeque", + "java.util.concurrent.ConcurrentLinkedQueue", + "java.util.concurrent.ConcurrentSkipListSet", + "java.util.concurrent.CopyOnWriteArrayList", + "java.util.concurrent.CopyOnWriteArraySet", + "java.util.concurrent.LinkedBlockingDeque", + "java.util.concurrent.LinkedBlockingQueue", + "java.util.concurrent.LinkedTransferQueue", + "java.util.concurrent.PriorityBlockingQueue" + }; + + for (final String iterableClass : iterableCollectionClasses) { + final Iterable iterable = + (Iterable) Class.forName(iterableClass).newInstance(); + ((Collection) iterable).addAll(source); + final Iterator iterator = iterable.iterator(); + final List target = new ArrayList<>(source.size()); + iterator.forEach(target::add); + if ("java.util.HashSet".equals(iterableClass)) { + target.sort((x, y) -> x - y); + assertEquals(target, source); + } else { + assertEquals(target, source); + } + } + } + + static class IteratorWithRemove implements Iterator { + + public boolean removed; + + IteratorWithRemove() { + removed = false; + } + + @Override + public boolean hasNext() { + return false; + } + + @Override + public Object next() { + return null; + } + + @Override + public void remove() { + removed = true; + } + } +}