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