Path: blob/master/test/jdk/java/util/Collections/EnumerationAsIterator.java
41152 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 807272626* @summary Tests for Enumeration-to-Iterator conversion.27* @run testng EnumerationAsIterator28*/2930import org.testng.annotations.DataProvider;31import org.testng.annotations.Test;3233import java.util.ArrayList;34import java.util.Arrays;35import java.util.Collection;36import java.util.Collections;37import java.util.Enumeration;38import java.util.Iterator;39import java.util.List;40import java.util.NoSuchElementException;41import java.util.concurrent.atomic.AtomicInteger;42import java.util.function.Supplier;4344import static org.testng.Assert.*;4546@Test47public class EnumerationAsIterator {48static Object[] of(String description, Supplier<Enumeration<?>> s, Collection<?> exp) {49return new Object[]{description, s, exp};50}5152static Object[] of(String description, Collection<?> c, Collection<?> exp) {53return of(description, () -> Collections.enumeration(c), exp);54}5556/**57* A wrapper Enumeration that doesn't override the58* default method on Enumeration.59*/60static <T> Enumeration<T> wrapInDefault(Enumeration<T> e) {61return new Enumeration<>() {62@Override63public boolean hasMoreElements() {64return e.hasMoreElements();65}6667@Override68public T nextElement() {69return e.nextElement();70}71};72}7374@DataProvider75public static Iterator<Object[]> unmodifiable() {76return Arrays.asList(77of("Default-wrapped ArrayList",78() -> wrapInDefault(79Collections.enumeration(new ArrayList<>(Arrays.asList("a")))),80Arrays.asList("a")),8182of("Unmodifiable ArrayList",83Collections.unmodifiableList(new ArrayList<>(Arrays.asList("a"))),84Arrays.asList("a")),8586of("Modifiable ArrayList",87new ArrayList<>(Arrays.asList("a")),88Arrays.asList("a"))89).iterator();90}9192@DataProvider93public static Iterator<Object[]> others() {94return Arrays.asList(95of("Default Collections.emptyEnumeration()",96() -> wrapInDefault(Collections.emptyEnumeration()),97Collections.emptyList()),9899of("Collections.emptyEnumeration()",100Collections::emptyEnumeration,101Collections.emptyList()),102103of("Collections.emptyList()",104Collections.emptyList(),105Collections.emptyList()),106107of("Collections.singletonList()",108Collections.singletonList("a"),109Collections.singletonList("a")),110111of("Arrays.asList(...)",112Arrays.asList("a", "b", "c"),113Arrays.asList("a", "b", "c"))114).iterator();115}116117@DataProvider118public static Iterator<Object[]> all() {119List<Object[]> all = new ArrayList<>();120unmodifiable().forEachRemaining(all::add);121others().forEachRemaining(all::add);122return all.iterator();123}124125@Test(dataProvider = "all")126public void consumeByNext(String description, Supplier<Enumeration<?>> s, Collection<?> exp) {127Iterator<?> i = s.get().asIterator();128int count = 0;129while (i.hasNext()) {130assertTrue(i.hasNext());131132i.next();133count++;134}135assertEquals(count, exp.size());136137assertFalse(i.hasNext());138139try {140i.next();141fail();142} catch (NoSuchElementException e) {143}144}145146@Test(dataProvider = "all")147public void consumeByForEachRemaining(String description,148Supplier<Enumeration<?>> s,149Collection<?> exp) {150Iterator<?> i = s.get().asIterator();151AtomicInteger ai = new AtomicInteger();152i.forEachRemaining(e -> ai.getAndIncrement());153assertEquals(ai.get(), exp.size());154i.forEachRemaining(e -> ai.getAndIncrement());155assertEquals(ai.get(), exp.size());156157assertFalse(i.hasNext());158159try {160i.next();161fail();162} catch (NoSuchElementException e) {163}164}165166@Test(dataProvider = "all")167public void consumeByNextThenForEachRemaining(String description,168Supplier<Enumeration<?>> s,169Collection<?> exp) {170Iterator<?> i = s.get().asIterator();171AtomicInteger ai = new AtomicInteger();172if (i.hasNext()) {173i.next();174ai.getAndIncrement();175}176i.forEachRemaining(e -> ai.getAndIncrement());177assertEquals(ai.get(), exp.size());178i.forEachRemaining(e -> ai.getAndIncrement());179assertEquals(ai.get(), exp.size());180181assertFalse(i.hasNext());182183try {184i.next();185fail();186} catch (NoSuchElementException e) {187}188}189190@Test(dataProvider = "all")191public void contents(String description, Supplier<Enumeration<?>> s, Collection<?> exp) {192assertEquals(copy(s.get()), exp);193}194195private List<?> copy(Enumeration<?> input) {196List<Object> output = new ArrayList<>();197input.asIterator().forEachRemaining(output::add);198return output;199}200201@Test(dataProvider = "unmodifiable",202expectedExceptions=UnsupportedOperationException.class)203public void removeThrowsAfterAdvancingE(String description,204Supplier<Enumeration<?>> s,205Collection<?> exp) {206Enumeration<?> e = s.get();207e.nextElement();208e.asIterator().remove();209}210211@Test(dataProvider = "unmodifiable",212expectedExceptions=UnsupportedOperationException.class)213public void removeThrowsAfterAdvancingI(String description,214Supplier<Enumeration<?>> s,215Collection<?> exp) {216Iterator<?> i = s.get().asIterator();217i.next();218i.remove();219}220}221222223