jdk/test/java/util/Collections/EmptyIterator.java
changeset 28060 4019fb4fb1fb
parent 23010 6dadb192ad81
child 32991 b27c76b82713
equal deleted inserted replaced
28059:e576535359cc 28060:4019fb4fb1fb
     1 /*
     1 /*
     2  * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    31 import java.util.*;
    31 import java.util.*;
    32 
    32 
    33 public class EmptyIterator {
    33 public class EmptyIterator {
    34 
    34 
    35     void test(String[] args) throws Throwable {
    35     void test(String[] args) throws Throwable {
    36         testEmptyCollection(Collections.<Object>emptyList());
    36         testEmptyCollection(emptyList());
    37         testEmptyCollection(Collections.<Object>emptySet());
    37         testEmptyCollection(emptySet());
    38 
    38 
    39         testEmptyMap(Collections.<Object, Object>emptyMap());
    39         testEmptyMap(emptyMap());
    40 
    40 
    41         Hashtable<Object, Object> emptyTable = new Hashtable<Object, Object>();
    41         Hashtable<?,?> emptyTable = new Hashtable<>();
    42         testEmptyEnumeration(emptyTable.keys());
    42         testEmptyEnumeration(emptyTable.keys());
    43         testEmptyEnumeration(emptyTable.elements());
    43         testEmptyEnumeration(emptyTable.elements());
    44         testEmptyIterator(emptyTable.keySet().iterator());
    44         testEmptyIterator(emptyTable.keySet().iterator());
    45         testEmptyIterator(emptyTable.values().iterator());
    45         testEmptyIterator(emptyTable.values().iterator());
    46         testEmptyIterator(emptyTable.entrySet().iterator());
    46         testEmptyIterator(emptyTable.entrySet().iterator());
    47 
    47 
    48         final Enumeration<EmptyIterator> finalEmptyTyped =
    48         final Enumeration<EmptyIterator> finalEmptyTyped = emptyEnumeration();
    49             Collections.emptyEnumeration();
       
    50         testEmptyEnumeration(finalEmptyTyped);
    49         testEmptyEnumeration(finalEmptyTyped);
    51 
    50 
    52         final Enumeration finalEmptyAbstract =
    51         final Enumeration<?> finalEmptyAbstract = emptyEnumeration();
    53             Collections.emptyEnumeration();
       
    54         testEmptyEnumeration(finalEmptyAbstract);
    52         testEmptyEnumeration(finalEmptyAbstract);
    55 
    53 
    56         @SuppressWarnings("unchecked") Iterator<?> x =
    54         testEmptyIterator(emptyIterator());
    57             new sun.tools.java.MethodSet()
       
    58             .lookupName(sun.tools.java.Identifier.lookup(""));
       
    59         testEmptyIterator(x);
       
    60     }
    55     }
    61 
    56 
    62     <T> void testEmptyEnumeration(final Enumeration<T> e) {
    57     void testEmptyEnumeration(final Enumeration<?> e) {
    63         check(e == emptyEnumeration());
    58         check(e == emptyEnumeration());
    64         check(! e.hasMoreElements());
    59         check(!e.hasMoreElements());
    65         THROWS(NoSuchElementException.class,
    60         THROWS(NoSuchElementException.class,
    66                new F(){void f(){ e.nextElement(); }});
    61                new F(){void f(){ e.nextElement(); }});
    67     }
    62     }
    68 
    63 
    69     <T> void testEmptyIterator(final Iterator<T> it) {
    64     void testEmptyIterator(final Iterator<?> it) {
    70         check(it == emptyIterator());
    65         check(it == emptyIterator());
    71         check(! it.hasNext());
    66         check(! it.hasNext());
    72         THROWS(NoSuchElementException.class,
    67         THROWS(NoSuchElementException.class,
    73                new F(){void f(){ it.next(); }});
    68                new F(){void f(){ it.next(); }});
    74         THROWS(IllegalStateException.class,
    69         THROWS(IllegalStateException.class,
    75                new F(){void f(){ it.remove(); }});
    70                new F(){void f(){ it.remove(); }});
    76     }
    71     }
    77 
    72 
    78     void testEmptyMap(Map<Object, Object> m) {
    73     void testEmptyMap(Map<?,?> m) {
    79         check(m == emptyMap());
    74         check(m == emptyMap());
    80         check(m.entrySet().iterator() ==
    75         check(m.entrySet().iterator() ==
    81               Collections.<Map.Entry<Object,Object>>emptyIterator());
    76               Collections.<Map.Entry<?,?>>emptyIterator());
    82         check(m.values().iterator() == emptyIterator());
    77         check(m.values().iterator() == emptyIterator());
    83         check(m.keySet().iterator() == emptyIterator());
    78         check(m.keySet().iterator() == emptyIterator());
    84         equal(m, unmodifiableMap(m));
    79         equal(m, unmodifiableMap(m));
    85 
    80 
    86         testEmptyCollection(m.keySet());
    81         testEmptyCollection(m.keySet());
    87         testEmptyCollection(m.entrySet());
    82         testEmptyCollection(m.entrySet());
    88         testEmptyCollection(m.values());
    83         testEmptyCollection(m.values());
    89     }
    84     }
    90 
    85 
    91     <E> void testToArray(final Collection<E> c) {
    86     void testToArray(final Collection<?> c) {
    92         Object[] a = c.toArray();
    87         Object[] a = c.toArray();
    93         equal(a.length, 0);
    88         equal(a.length, 0);
    94         equal(a.getClass().getComponentType(), Object.class);
    89         equal(a.getClass().getComponentType(), Object.class);
    95         THROWS(NullPointerException.class,
    90         THROWS(NullPointerException.class,
    96                new F(){void f(){ c.toArray((Object[])null); }});
    91                new F(){void f(){ c.toArray((Object[])null); }});
   107             for (int i=1; i<t.length; i++)
   102             for (int i=1; i<t.length; i++)
   108                 check(t[i] == "");
   103                 check(t[i] == "");
   109         }
   104         }
   110     }
   105     }
   111 
   106 
   112     <E> void testEmptyCollection(final Collection<E> c) {
   107     void testEmptyCollection(final Collection<?> c) {
   113         testEmptyIterator(c.iterator());
   108         testEmptyIterator(c.iterator());
   114 
   109 
   115         check(c.iterator() == emptyIterator());
   110         check(c.iterator() == emptyIterator());
   116         if (c instanceof List)
   111         if (c instanceof List)
   117             check(((List<?>)c).listIterator() == emptyListIterator());
   112             check(((List<?>)c).listIterator() == emptyListIterator());