1 /* 2 * Copyright (c) 2015, 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 8072726 27 * @summary Tests for Enumeration-to-Iterator conversion. 28 * @run testng EnumerationAsIterator 29 */ 30 31 import java.util.*; 32 33 import org.testng.annotations.Test; 34 import static org.testng.Assert.*; 35 36 @Test 37 public class EnumerationAsIterator { 38 public void emptyHasNext() { 39 Iterator<Object> it = Collections.emptyEnumeration().asIterator(); 40 assertFalse(it.hasNext()); 41 } 42 43 @Test(expectedExceptions = {NoSuchElementException.class}) 44 public void emptyNextThrows() { 45 Iterator<Object> it = Collections.emptyEnumeration().asIterator(); 46 Object o = it.next(); 47 } 48 49 private List<String> copy(List<String> input) { 50 List<String> output = new ArrayList<>(); 51 Collections.enumeration(input) 52 .asIterator() 53 .forEachRemaining(output::add); 54 return output; 55 } 56 57 public void copyZero() { 58 assertEquals(copy(Collections.emptyList()), Collections.emptyList()); 59 } 60 61 public void copyOne() { 62 assertEquals(copy(Collections.singletonList("a")), 63 Collections.singletonList("a")); 64 } 65 66 public void copyMany() { 67 List<String> input = Arrays.asList("a", "b", "c"); 68 assertEquals(copy(input), input); 69 } 70 71 @Test(expectedExceptions = {UnsupportedOperationException.class}) 72 public void removeThrowsInitially() { 73 Collections.enumeration(Arrays.asList("a", "b", "c")) 74 .asIterator() 75 .remove(); 76 } 77 78 @Test(expectedExceptions = {UnsupportedOperationException.class}) 79 public void removeThrowsLater() { 80 Iterator<String> iter = 81 Collections.enumeration(Arrays.asList("a", "b", "c")).asIterator(); 82 iter.next(); 83 iter.remove(); 84 } 85 86 87 public void repeatedCalls() { 88 Iterator<String> iter = new Vector<>(Arrays.asList("a", "b", "c")) 89 .elements().asIterator(); 90 assertTrue(iter.hasNext()); 91 assertTrue(iter.hasNext()); 92 assertEquals(iter.next(), "a"); 93 assertEquals(iter.next(), "b"); 94 assertEquals(iter.next(), "c"); 95 assertFalse(iter.hasNext()); 96 assertFalse(iter.hasNext()); 97 } 98 }