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