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 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(); }});
|
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 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 final Enumeration<EmptyIterator> finalEmptyTyped =
51 Collections.emptyEnumeration();
52 testEmptyEnumeration(finalEmptyTyped);
53
54 final Enumeration finalEmptyAbstract =
55 Collections.emptyEnumeration();
56 testEmptyEnumeration(finalEmptyAbstract);
57
58 @SuppressWarnings("unchecked") Iterator<?> x =
59 new sun.tools.java.MethodSet()
60 .lookupName(sun.tools.java.Identifier.lookup(""));
61 testEmptyIterator(x);
62 }
63
64 <T> void testEmptyEnumeration(final Enumeration<T> e) {
65 check(e == emptyEnumeration());
66 check(! e.hasMoreElements());
67 THROWS(NoSuchElementException.class,
68 new F(){void f(){ e.nextElement(); }});
69 }
70
71 <T> void testEmptyIterator(final Iterator<T> it) {
72 check(it == emptyIterator());
73 check(! it.hasNext());
74 THROWS(NoSuchElementException.class,
75 new F(){void f(){ it.next(); }});
76 THROWS(IllegalStateException.class,
77 new F(){void f(){ it.remove(); }});
|