0N/A/*
2362N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6409434
0N/A * @summary Test behavior of nulls in checked collections
0N/A */
0N/A
0N/Aimport java.util.*;
0N/Aimport static java.util.Collections.*;
0N/A
0N/A@SuppressWarnings({"unchecked","serial"})
0N/Apublic class CheckedNull {
0N/A
0N/A void test(String[] args) throws Throwable {
0N/A testCollection(Collections.checkedCollection(
0N/A new ArrayList<String>(), String.class));
0N/A testCollection(Collections.checkedList(
0N/A new ArrayList<String>(), String.class));
0N/A testCollection(Collections.checkedSet(
0N/A new HashSet<String>(), String.class));
0N/A
0N/A final Comparator nullLow = new Comparator() {
0N/A public int compare(Object x, Object y) {
0N/A return x == y ? 0 :
0N/A x == null ? -1 :
0N/A y == null ? 1 :
0N/A ((Comparable)x).compareTo(y); }};
0N/A testCollection(Collections.checkedSortedSet(
0N/A new TreeSet<String>(nullLow), String.class));
0N/A
0N/A testMap(Collections.checkedMap(
0N/A new HashMap<String, String>(),
1771N/A String.class, String.class));
0N/A }
0N/A
0N/A ClassCastException cce(F f) {
0N/A try { f.f(); fail(); return null; }
0N/A catch (ClassCastException cce) { pass(); return cce; }
0N/A catch (Throwable t) { unexpected(t); return null; }
0N/A }
0N/A
0N/A void equalCCE(F ... fs) {
0N/A String detailMessage = null;
0N/A for (F f : fs)
0N/A if (detailMessage == null)
0N/A detailMessage = cce(f).getMessage();
0N/A else
0N/A equal(detailMessage, cce(f).getMessage());
0N/A }
0N/A
0N/A void add(Collection c, Object o) {
0N/A int s = c.size();
0N/A check(! c.contains(o));
0N/A check(c.add(o));
0N/A check(c.contains(o));
0N/A equal(c.size(), s+1);
0N/A check(c.remove(o));
0N/A check(! c.contains(o));
0N/A check(c.addAll(singleton(o)));
0N/A check(c.contains(o));
0N/A equal(c.size(), s+1);
0N/A check(c.remove(o));
0N/A equal(c.size(), s);
0N/A }
0N/A
0N/A void testCollection(final Collection c) {
0N/A try {
0N/A check(c.isEmpty());
0N/A add(c, null);
0N/A add(c, "foo");
0N/A
0N/A check(c.add("bar"));
0N/A add(c, null);
0N/A add(c, "foo");
0N/A
0N/A equalCCE(
0N/A new F(){void f(){ c.add(1); }},
0N/A new F(){void f(){ c.addAll(singleton(1)); }});
0N/A
0N/A } catch (Throwable t) { unexpected(t); }
0N/A }
0N/A
0N/A void put(Map m, Object k, Object v) {
0N/A int s = m.size();
0N/A check(! m.containsKey(k));
0N/A check(! m.containsValue(v));
0N/A equal(null, m.put(k, v));
0N/A check(m.containsKey(k));
0N/A check(m.containsValue(v));
0N/A equal(m.size(), s+1);
0N/A equal(v, m.remove(k));
0N/A check(! m.containsKey(k));
0N/A check(! m.containsValue(v));
0N/A m.putAll(singletonMap(k,v));
0N/A check(m.containsKey(k));
0N/A check(m.containsValue(v));
0N/A equal(m.size(), s+1);
0N/A equal(v,m.remove(k));
0N/A equal(m.size(), s);
0N/A }
0N/A
0N/A void testMap(final Map m) {
0N/A try {
0N/A check(m.isEmpty());
0N/A
0N/A put(m, "foo", null);
0N/A put(m, null, "foo");
0N/A put(m, null, null);
0N/A put(m, "foo", "bar");
0N/A
0N/A m.put("a", "b");
0N/A
0N/A put(m, "foo", null);
0N/A put(m, null, "foo");
0N/A put(m, null, null);
0N/A put(m, "foo", "bar");
0N/A
0N/A equalCCE(
0N/A new F(){void f(){ m.put(1, "foo"); }},
0N/A new F(){void f(){ m.putAll(singletonMap(1, "foo")); }});
0N/A
0N/A final Collection cheater = new ArrayList() {
0N/A public boolean contains(Object o) {
0N/A if (o instanceof Map.Entry)
0N/A ((Map.Entry)o).setValue(1);
0N/A return false; }};
0N/A
0N/A equalCCE(
0N/A new F(){void f(){ m.put("foo", 1); }},
0N/A new F(){void f(){ m.putAll(singletonMap("foo", 1)); }},
0N/A new F(){void f(){
0N/A ((Map.Entry)m.entrySet().iterator().next()).setValue(1); }},
0N/A new F(){void f(){
0N/A m.entrySet().removeAll(cheater);}},
0N/A new F(){void f(){
0N/A m.entrySet().retainAll(cheater);}});
0N/A
0N/A equalCCE(
0N/A new F(){void f(){ m.put(3, 1); }},
0N/A new F(){void f(){ m.putAll(singletonMap(3, 1)); }});
0N/A
0N/A equal(m.size(), 1);
0N/A equal(m.keySet(), singleton("a"));
0N/A equal(m.entrySet(),
0N/A singleton(new AbstractMap.SimpleImmutableEntry("a","b")));
0N/A
0N/A } catch (Throwable t) { unexpected(t); }
0N/A }
0N/A
0N/A //--------------------- Infrastructure ---------------------------
0N/A volatile int passed = 0, failed = 0;
0N/A void pass() {passed++;}
0N/A void fail() {failed++; Thread.dumpStack();}
0N/A void fail(String msg) {System.err.println(msg); fail();}
0N/A void unexpected(Throwable t) {failed++; t.printStackTrace();}
0N/A void check(boolean cond) {if (cond) pass(); else fail();}
0N/A void equal(Object x, Object y) {
0N/A if (x == null ? y == null : x.equals(y)) pass();
0N/A else fail(x + " not equal to " + y);}
0N/A public static void main(String[] args) throws Throwable {
0N/A new CheckedNull().instanceMain(args);}
0N/A void instanceMain(String[] args) throws Throwable {
0N/A try {test(args);} catch (Throwable t) {unexpected(t);}
0N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
0N/A if (failed > 0) throw new AssertionError("Some tests failed");}
0N/A abstract class F {abstract void f() throws Throwable;}
0N/A}