1 /*
   2  * Copyright (c) 2007, 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 5017904 6356890 8004928
  27  * @summary Test empty iterators, enumerations, and collections
  28  */
  29 
  30 import static java.util.Collections.*;
  31 import java.util.*;
  32 
  33 public class EmptyIterator {
  34 
  35     void test(String[] args) throws Throwable {
  36         testEmptyCollection(Collections.<Object>emptyList());
  37         testEmptyCollection(Collections.<Object>emptySet());
  38         testEmptyCollection(new java.util.concurrent.
  39                             SynchronousQueue<Object>());
  40 
  41         testEmptyMap(Collections.<Object, Object>emptyMap());
  42 
  43         Hashtable<Object, Object> emptyTable = new Hashtable<Object, Object>();
  44         testEmptyEnumeration(emptyTable.keys());
  45         testEmptyEnumeration(emptyTable.elements());
  46         testEmptyIterator(emptyTable.keySet().iterator());
  47         testEmptyIterator(emptyTable.values().iterator());
  48         testEmptyIterator(emptyTable.entrySet().iterator());
  49 
  50         final Enumeration<EmptyIterator> finalEmptyTyped =
  51             Collections.emptyEnumeration();
  52         testEmptyEnumeration(finalEmptyTyped);
  53 
  54         final Enumeration finalEmptyAbstract =
  55             Collections.emptyEnumeration();
  56         testEmptyEnumeration(finalEmptyAbstract);
  57 
  58         @SuppressWarnings("unchecked") Iterator<?> x =
  59             new sun.tools.java.MethodSet()
  60             .lookupName(sun.tools.java.Identifier.lookup(""));
  61         testEmptyIterator(x);
  62     }
  63 
  64     <T> void testEmptyEnumeration(final Enumeration<T> e) {
  65         check(e == emptyEnumeration());
  66         check(! e.hasMoreElements());
  67         THROWS(NoSuchElementException.class,
  68                new F(){void f(){ e.nextElement(); }});
  69     }
  70 
  71     <T> void testEmptyIterator(final Iterator<T> it) {
  72         check(it == emptyIterator());
  73         check(! it.hasNext());
  74         THROWS(NoSuchElementException.class,
  75                new F(){void f(){ it.next(); }});
  76         THROWS(IllegalStateException.class,
  77                new F(){void f(){ it.remove(); }});
  78     }
  79 
  80     void testEmptyMap(Map<Object, Object> m) {
  81         check(m == emptyMap());
  82         check(m.entrySet().iterator() ==
  83               Collections.<Map.Entry<Object,Object>>emptyIterator());
  84         check(m.values().iterator() == emptyIterator());
  85         check(m.keySet().iterator() == emptyIterator());
  86         equal(m, unmodifiableMap(m));
  87 
  88         testEmptyCollection(m.keySet());
  89         testEmptyCollection(m.entrySet());
  90         testEmptyCollection(m.values());
  91     }
  92 
  93     <E> void testToArray(final Collection<E> c) {
  94         Object[] a = c.toArray();
  95         equal(a.length, 0);
  96         equal(a.getClass().getComponentType(), Object.class);
  97         THROWS(NullPointerException.class,
  98                new F(){void f(){ c.toArray((Object[])null); }});
  99 
 100         {
 101             String[] t = new String[0];
 102             check(c.toArray(t) == t);
 103         }
 104 
 105         {
 106             String[] t = nCopies(10, "").toArray(new String[0]);
 107             check(c.toArray(t) == t);
 108             check(t[0] == null);
 109             for (int i=1; i<t.length; i++)
 110                 check(t[i] == "");
 111         }
 112     }
 113 
 114     <E> void testEmptyCollection(final Collection<E> c) {
 115         testEmptyIterator(c.iterator());
 116 
 117         check(c.iterator() == emptyIterator());
 118         if (c instanceof List)
 119             check(((List<?>)c).listIterator() == emptyListIterator());
 120 
 121         testToArray(c);
 122     }
 123 
 124     //--------------------- Infrastructure ---------------------------
 125     volatile int passed = 0, failed = 0;
 126     void pass() {passed++;}
 127     void fail() {failed++; Thread.dumpStack();}
 128     void fail(String msg) {System.err.println(msg); fail();}
 129     void unexpected(Throwable t) {failed++; t.printStackTrace();}
 130     void check(boolean cond) {if (cond) pass(); else fail();}
 131     void equal(Object x, Object y) {
 132         if (x == null ? y == null : x.equals(y)) pass();
 133         else fail(x + " not equal to " + y);}
 134     public static void main(String[] args) throws Throwable {
 135         new EmptyIterator().instanceMain(args);}
 136     void instanceMain(String[] args) throws Throwable {
 137         try {test(args);} catch (Throwable t) {unexpected(t);}
 138         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
 139         if (failed > 0) throw new AssertionError("Some tests failed");}
 140     abstract class F {abstract void f() throws Throwable;}
 141     void THROWS(Class<? extends Throwable> k, F... fs) {
 142         for (F f : fs)
 143             try {f.f(); fail("Expected " + k.getName() + " not thrown");}
 144             catch (Throwable t) {
 145                 if (k.isAssignableFrom(t.getClass())) pass();
 146                 else unexpected(t);}}
 147 }