1 |
|
/* |
2 |
< |
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved. |
2 |
> |
* Copyright (c) 2007, 2014, 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 |
23 |
|
|
24 |
|
/* |
25 |
|
* @test |
26 |
< |
* @bug 5017904 6356890 |
26 |
> |
* @bug 5017904 6356890 8004928 |
27 |
|
* @summary Test empty iterators, enumerations, and collections |
28 |
|
*/ |
29 |
|
|
30 |
– |
import java.util.*; |
30 |
|
import static java.util.Collections.*; |
31 |
+ |
import java.util.*; |
32 |
+ |
import java.util.concurrent.SynchronousQueue; |
33 |
|
|
34 |
|
public class EmptyIterator { |
35 |
|
|
36 |
|
void test(String[] args) throws Throwable { |
37 |
< |
testEmptyCollection(Collections.<Object>emptyList()); |
38 |
< |
testEmptyCollection(Collections.<Object>emptySet()); |
39 |
< |
// Removed jdk7 dependency from SynchronousQueue. |
40 |
< |
// testEmptyCollection(new java.util.concurrent. |
40 |
< |
// SynchronousQueue<Object>()); |
37 |
> |
testEmptyCollection(emptyList()); |
38 |
> |
testEmptyCollection(emptySet()); |
39 |
> |
testEmptyCollection(new SynchronousQueue<Object>()); |
40 |
> |
testEmptyMap(emptyMap()); |
41 |
|
|
42 |
< |
testEmptyMap(Collections.<Object, Object>emptyMap()); |
43 |
< |
|
44 |
< |
Hashtable<Object, Object> emptyTable = new Hashtable<Object, Object>(); |
42 |
> |
Hashtable<?,?> emptyTable = new Hashtable<>(); |
43 |
|
testEmptyEnumeration(emptyTable.keys()); |
44 |
|
testEmptyEnumeration(emptyTable.elements()); |
45 |
|
testEmptyIterator(emptyTable.keySet().iterator()); |
46 |
|
testEmptyIterator(emptyTable.values().iterator()); |
47 |
|
testEmptyIterator(emptyTable.entrySet().iterator()); |
48 |
|
|
49 |
< |
testEmptyEnumeration(javax.swing.tree.DefaultMutableTreeNode |
50 |
< |
.EMPTY_ENUMERATION); |
51 |
< |
testEmptyEnumeration(javax.swing.text.SimpleAttributeSet |
52 |
< |
.EMPTY.getAttributeNames()); |
53 |
< |
|
54 |
< |
@SuppressWarnings("unchecked") |
55 |
< |
Iterator<?> x = new sun.tools.java.MethodSet() |
58 |
< |
.lookupName(sun.tools.java.Identifier.lookup("")); |
59 |
< |
testEmptyIterator(x); |
49 |
> |
final Enumeration<EmptyIterator> finalEmptyTyped = emptyEnumeration(); |
50 |
> |
testEmptyEnumeration(finalEmptyTyped); |
51 |
> |
|
52 |
> |
final Enumeration<?> finalEmptyAbstract = emptyEnumeration(); |
53 |
> |
testEmptyEnumeration(finalEmptyAbstract); |
54 |
> |
|
55 |
> |
testEmptyIterator(emptyIterator()); |
56 |
|
} |
57 |
|
|
58 |
< |
<T> void testEmptyEnumeration(final Enumeration<T> e) { |
59 |
< |
check(! e.hasMoreElements()); |
58 |
> |
void testEmptyEnumeration(final Enumeration<?> e) { |
59 |
> |
check(e == emptyEnumeration()); |
60 |
> |
check(!e.hasMoreElements()); |
61 |
|
THROWS(NoSuchElementException.class, |
62 |
|
new F(){void f(){ e.nextElement(); }}); |
63 |
|
} |
64 |
|
|
65 |
< |
<T> void testEmptyIterator(final Iterator<T> it) { |
65 |
> |
void testEmptyIterator(final Iterator<?> it) { |
66 |
> |
check(it == emptyIterator()); |
67 |
|
check(! it.hasNext()); |
68 |
|
THROWS(NoSuchElementException.class, |
69 |
|
new F(){void f(){ it.next(); }}); |
71 |
|
new F(){void f(){ it.remove(); }}); |
72 |
|
} |
73 |
|
|
74 |
< |
void testEmptyMap(Map<Object, Object> m) { |
74 |
> |
void testEmptyMap(Map<?,?> m) { |
75 |
> |
check(m == emptyMap()); |
76 |
|
check(m.entrySet().iterator() == |
77 |
< |
Collections.<Map.Entry<Object,Object>>emptyIterator()); |
77 |
> |
Collections.<Map.Entry<?,?>>emptyIterator()); |
78 |
|
check(m.values().iterator() == emptyIterator()); |
79 |
|
check(m.keySet().iterator() == emptyIterator()); |
80 |
|
equal(m, unmodifiableMap(m)); |
84 |
|
testEmptyCollection(m.values()); |
85 |
|
} |
86 |
|
|
87 |
< |
<E> void testToArray(final Collection<E> c) { |
87 |
> |
void testToArray(final Collection<?> c) { |
88 |
|
Object[] a = c.toArray(); |
89 |
|
equal(a.length, 0); |
90 |
|
equal(a.getClass().getComponentType(), Object.class); |
105 |
|
} |
106 |
|
} |
107 |
|
|
108 |
< |
<E> void testEmptyCollection(final Collection<E> c) { |
108 |
> |
void testEmptyCollection(final Collection<?> c) { |
109 |
|
testEmptyIterator(c.iterator()); |
110 |
+ |
|
111 |
+ |
check(c.iterator() == emptyIterator()); |
112 |
+ |
if (c instanceof List) |
113 |
+ |
check(((List<?>)c).listIterator() == emptyListIterator()); |
114 |
+ |
|
115 |
|
testToArray(c); |
116 |
|
} |
117 |
|
|