< prev index next >

test/jdk/java/util/Collections/EnumerationAsIterator.java

Print this page

  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 org.testng.annotations.DataProvider;
 32 import org.testng.annotations.Test;
 33 
 34 import java.util.ArrayList;
 35 import java.util.Arrays;
 36 import java.util.Collection;
 37 import java.util.Collections;
 38 import java.util.Enumeration;
 39 import java.util.Iterator;
 40 import java.util.List;
 41 import java.util.NoSuchElementException;
 42 import java.util.concurrent.atomic.AtomicInteger;
 43 import java.util.function.Supplier;
 44 
 45 import static org.testng.Assert.*;
 46 
 47 @Test
 48 public class EnumerationAsIterator {

 49     static Object[] of(String description, Supplier<Enumeration<?>> s, Collection<?> exp) {
 50         return new Object[]{description, s, exp};
 51     }
 52 
 53     static Object[] of(String description, Collection<?> c, Collection<?> exp) {
 54         return of(description, () -> Collections.enumeration(c), exp);
 55     }
 56 
 57     /**
 58      * A wrapper Enumeration that doesn't override the
 59      * default method on Enumeration.
 60      */
 61     static <T> Enumeration<T> wrapInDefault(Enumeration<T> e) {
 62         return new Enumeration<>() {
 63             @Override
 64             public boolean hasMoreElements() {
 65                 return e.hasMoreElements();
 66             }
 67 
 68             @Override

 94     public static Iterator<Object[]> others() {
 95         return Arrays.asList(
 96             of("Default Collections.emptyEnumeration()",
 97                () -> wrapInDefault(Collections.emptyEnumeration()),
 98                Collections.emptyList()),
 99 
100             of("Collections.emptyEnumeration()",
101                Collections::emptyEnumeration,
102                Collections.emptyList()),
103 
104             of("Collections.emptyList()",
105                Collections.emptyList(),
106                Collections.emptyList()),
107 
108             of("Collections.singletonList()",
109                Collections.singletonList("a"),
110                Collections.singletonList("a")),
111 
112             of("Arrays.asList(...)",
113                Arrays.asList("a", "b", "c"),
114                Arrays.asList("a", "b", "c"))




115         ).iterator();
116     }
117 
118     @DataProvider
119     public static Iterator<Object[]> all() {
120         List<Object[]> all = new ArrayList<>();
121         unmodifiable().forEachRemaining(all::add);
122         others().forEachRemaining(all::add);
123         return all.iterator();
124     }
125 
126     @Test(dataProvider = "all")
127     public void consumeByNext(String description, Supplier<Enumeration<?>> s, Collection<?> exp) {
128         Iterator<?> i = s.get().asIterator();
129         int count = 0;
130         while (i.hasNext()) {
131             assertTrue(i.hasNext());
132 
133             i.next();
134             count++;

  1 /*
  2  * Copyright (c) 2015, 2026, 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  * @library /test/lib
 29  * @run testng EnumerationAsIterator
 30  */
 31 
 32 import jdk.test.lib.valueclass.VClass;
 33 import org.testng.annotations.DataProvider;
 34 import org.testng.annotations.Test;
 35 
 36 import java.util.ArrayList;
 37 import java.util.Arrays;
 38 import java.util.Collection;
 39 import java.util.Collections;
 40 import java.util.Enumeration;
 41 import java.util.Iterator;
 42 import java.util.List;
 43 import java.util.NoSuchElementException;
 44 import java.util.concurrent.atomic.AtomicInteger;
 45 import java.util.function.Supplier;
 46 
 47 import static org.testng.Assert.*;
 48 
 49 @Test
 50 public class EnumerationAsIterator {
 51 
 52     static Object[] of(String description, Supplier<Enumeration<?>> s, Collection<?> exp) {
 53         return new Object[]{description, s, exp};
 54     }
 55 
 56     static Object[] of(String description, Collection<?> c, Collection<?> exp) {
 57         return of(description, () -> Collections.enumeration(c), exp);
 58     }
 59 
 60     /**
 61      * A wrapper Enumeration that doesn't override the
 62      * default method on Enumeration.
 63      */
 64     static <T> Enumeration<T> wrapInDefault(Enumeration<T> e) {
 65         return new Enumeration<>() {
 66             @Override
 67             public boolean hasMoreElements() {
 68                 return e.hasMoreElements();
 69             }
 70 
 71             @Override

 97     public static Iterator<Object[]> others() {
 98         return Arrays.asList(
 99             of("Default Collections.emptyEnumeration()",
100                () -> wrapInDefault(Collections.emptyEnumeration()),
101                Collections.emptyList()),
102 
103             of("Collections.emptyEnumeration()",
104                Collections::emptyEnumeration,
105                Collections.emptyList()),
106 
107             of("Collections.emptyList()",
108                Collections.emptyList(),
109                Collections.emptyList()),
110 
111             of("Collections.singletonList()",
112                Collections.singletonList("a"),
113                Collections.singletonList("a")),
114 
115             of("Arrays.asList(...)",
116                Arrays.asList("a", "b", "c"),
117                Arrays.asList("a", "b", "c")),
118 
119             of("Value Collections.enumeration()",
120                () -> Collections.enumeration(Arrays.asList(new VClass(1, new int[] { 1 }), new VClass(2, new int[] { 2 }))),
121                Arrays.asList(new VClass(1, new int[] { 1 }), new VClass(2, new int[] { 2 })))
122         ).iterator();
123     }
124 
125     @DataProvider
126     public static Iterator<Object[]> all() {
127         List<Object[]> all = new ArrayList<>();
128         unmodifiable().forEachRemaining(all::add);
129         others().forEachRemaining(all::add);
130         return all.iterator();
131     }
132 
133     @Test(dataProvider = "all")
134     public void consumeByNext(String description, Supplier<Enumeration<?>> s, Collection<?> exp) {
135         Iterator<?> i = s.get().asIterator();
136         int count = 0;
137         while (i.hasNext()) {
138             assertTrue(i.hasNext());
139 
140             i.next();
141             count++;
< prev index next >