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 fail("NoSuchElementException should have been thrown"); 48 } 49 50 private List<String> copy(List<String> input) { 51 List<String> output = new ArrayList<>(); 52 Collections.enumeration(input) 53 .asIterator() 54 .forEachRemaining(output::add); 55 return output; 56 } 57 58 public void copyZero() { 59 assertEquals(copy(Collections.emptyList()), Collections.emptyList()); 60 } 61 62 public void copyOne() { 63 assertEquals(copy(Collections.singletonList("a")), 64 Collections.singletonList("a")); 65 } 66 67 public void copyMany() { 68 List<String> input = Arrays.asList("a", "b", "c"); 69 assertEquals(copy(input), input); 70 } 71 72 public void repeatedCalls() { 73 Iterator<String> iter = new Vector<>(Arrays.asList("a", "b", "c")) 74 .elements().asIterator(); 75 assertTrue(iter.hasNext()); 76 assertTrue(iter.hasNext()); 77 assertEquals(iter.next(), "a"); 78 assertEquals(iter.next(), "b"); 79 assertEquals(iter.next(), "c"); 80 assertFalse(iter.hasNext()); 81 assertFalse(iter.hasNext()); 82 } 83 }