Path: blob/master/test/jdk/java/util/Collections/EmptyIterator.java
41149 views
/*1* Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 5017904 6356890 800492826* @summary Test empty iterators, enumerations, and collections27*/2829import java.util.Collection;30import java.util.Collections;31import java.util.Enumeration;32import java.util.Hashtable;33import java.util.Iterator;34import java.util.List;35import java.util.Map;36import java.util.NoSuchElementException;37import java.util.concurrent.SynchronousQueue;3839import static java.util.Collections.emptyEnumeration;40import static java.util.Collections.emptyIterator;41import static java.util.Collections.emptyList;42import static java.util.Collections.emptyListIterator;43import static java.util.Collections.emptyMap;44import static java.util.Collections.emptySet;45import static java.util.Collections.nCopies;46import static java.util.Collections.unmodifiableMap;4748public class EmptyIterator {4950void test(String[] args) throws Throwable {51testEmptyCollection(emptyList());52testEmptyCollection(emptySet());53testEmptyCollection(new SynchronousQueue<Object>());54testEmptyMap(emptyMap());5556Hashtable<?,?> emptyTable = new Hashtable<>();57testEmptyEnumeration(emptyTable.keys());58testEmptyEnumeration(emptyTable.elements());59testEmptyIterator(emptyTable.keySet().iterator());60testEmptyIterator(emptyTable.values().iterator());61testEmptyIterator(emptyTable.entrySet().iterator());6263final Enumeration<EmptyIterator> finalEmptyTyped = emptyEnumeration();64testEmptyEnumeration(finalEmptyTyped);6566final Enumeration<?> finalEmptyAbstract = emptyEnumeration();67testEmptyEnumeration(finalEmptyAbstract);6869testEmptyIterator(emptyIterator());70}7172void testEmptyEnumeration(final Enumeration<?> e) {73check(e == emptyEnumeration());74check(!e.hasMoreElements());75THROWS(NoSuchElementException.class,76new F(){void f(){ e.nextElement(); }});77}7879void testEmptyIterator(final Iterator<?> it) {80check(it == emptyIterator());81check(! it.hasNext());82THROWS(NoSuchElementException.class,83new F(){void f(){ it.next(); }});84THROWS(IllegalStateException.class,85new F(){void f(){ it.remove(); }});86}8788void testEmptyMap(Map<?,?> m) {89check(m == emptyMap());90check(m.entrySet().iterator() ==91Collections.<Map.Entry<?,?>>emptyIterator());92check(m.values().iterator() == emptyIterator());93check(m.keySet().iterator() == emptyIterator());94equal(m, unmodifiableMap(m));9596testEmptyCollection(m.keySet());97testEmptyCollection(m.entrySet());98testEmptyCollection(m.values());99}100101void testToArray(final Collection<?> c) {102Object[] a = c.toArray();103equal(a.length, 0);104equal(a.getClass().getComponentType(), Object.class);105THROWS(NullPointerException.class,106new F(){void f(){ c.toArray((Object[])null); }});107108{109String[] t = new String[0];110check(c.toArray(t) == t);111}112113{114String[] t = nCopies(10, "").toArray(new String[0]);115check(c.toArray(t) == t);116check(t[0] == null);117for (int i=1; i<t.length; i++)118check(t[i] == "");119}120}121122void testEmptyCollection(final Collection<?> c) {123testEmptyIterator(c.iterator());124125check(c.iterator() == emptyIterator());126if (c instanceof List)127check(((List<?>)c).listIterator() == emptyListIterator());128129testToArray(c);130}131132//--------------------- Infrastructure ---------------------------133volatile int passed = 0, failed = 0;134void pass() {passed++;}135void fail() {failed++; Thread.dumpStack();}136void fail(String msg) {System.err.println(msg); fail();}137void unexpected(Throwable t) {failed++; t.printStackTrace();}138void check(boolean cond) {if (cond) pass(); else fail();}139void equal(Object x, Object y) {140if (x == null ? y == null : x.equals(y)) pass();141else fail(x + " not equal to " + y);}142public static void main(String[] args) throws Throwable {143new EmptyIterator().instanceMain(args);}144void instanceMain(String[] args) throws Throwable {145try {test(args);} catch (Throwable t) {unexpected(t);}146System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);147if (failed > 0) throw new AssertionError("Some tests failed");}148abstract class F {abstract void f() throws Throwable;}149void THROWS(Class<? extends Throwable> k, F... fs) {150for (F f : fs)151try {f.f(); fail("Expected " + k.getName() + " not thrown");}152catch (Throwable t) {153if (k.isAssignableFrom(t.getClass())) pass();154else unexpected(t);}}155}156157158