1 /* 2 * Copyright (c) 2012, 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 import org.testng.annotations.Test; 25 26 import java.util.ArrayList; 27 import java.util.Arrays; 28 import java.util.Collection; 29 import java.util.Iterator; 30 import java.util.List; 31 32 import static org.testng.Assert.assertEquals; 33 import static org.testng.Assert.assertTrue; 34 import static org.testng.Assert.fail; 35 36 /** 37 * @test 38 * @run testng IteratorExtensionMethodsTest 39 * @summary test extension methods on Iterator 40 */ 41 @Test 42 public class IteratorExtensionMethodsTest { 43 44 public void testRemoveUnsupported() { 45 final Iterator iterator = new Iterator() { 46 @Override 47 public boolean hasNext() { 48 return false; 49 } 50 @Override 51 public Object next() { 52 return null; 53 } 54 }; 55 56 try { 57 iterator.remove(); 58 fail("expected UnsupportedOperationException from remove not thrown"); 59 } catch (UnsupportedOperationException ignore) { 60 } 61 } 62 63 public void testRemoveOverride() { 64 final IteratorWithRemove iterator = new IteratorWithRemove(); 65 iterator.remove(); 66 assertTrue(iterator.removed); 67 } 68 69 public void testForEach() throws Exception { 70 final Integer[] data = new Integer[1000]; 71 for (int i=0; i < data.length; i++) { 72 data[i] = i; 73 } 74 final List<Integer> source = Arrays.asList(data); 75 76 final String[] iterableCollectionClasses = { 77 "java.util.ArrayDeque", 78 "java.util.ArrayList", 79 "java.util.HashSet", 80 "java.util.LinkedHashSet", 81 "java.util.LinkedList", 82 "java.util.PriorityQueue", 83 "java.util.TreeSet", 84 "java.util.Vector", 85 "java.util.concurrent.ConcurrentLinkedDeque", 86 "java.util.concurrent.ConcurrentLinkedQueue", 87 "java.util.concurrent.ConcurrentSkipListSet", 88 "java.util.concurrent.CopyOnWriteArrayList", 89 "java.util.concurrent.CopyOnWriteArraySet", 90 "java.util.concurrent.LinkedBlockingDeque", 91 "java.util.concurrent.LinkedBlockingQueue", 92 "java.util.concurrent.LinkedTransferQueue", 93 "java.util.concurrent.PriorityBlockingQueue" 94 }; 95 96 for (final String iterableClass : iterableCollectionClasses) { 97 final Iterable<Integer> iterable = 98 (Iterable<Integer>) Class.forName(iterableClass).newInstance(); 99 ((Collection<Integer>) iterable).addAll(source); 100 final Iterator<Integer> iterator = iterable.iterator(); 101 final List<Integer> target = new ArrayList<>(source.size()); 102 iterator.forEach(target::add); 103 if ("java.util.HashSet".equals(iterableClass)) { 104 target.sort((x, y) -> x - y); 105 assertEquals(target, source); 106 } else { 107 assertEquals(target, source); 108 } 109 } 110 } 111 112 static class IteratorWithRemove implements Iterator { 113 114 public boolean removed; 115 116 IteratorWithRemove() { 117 removed = false; 118 } 119 120 @Override 121 public boolean hasNext() { 122 return false; 123 } 124 125 @Override 126 public Object next() { 127 return null; 128 } 129 130 @Override 131 public void remove() { 132 removed = true; 133 } 134 } 135 }