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