Path: blob/master/test/jdk/java/util/Collections/CheckedNull.java
41152 views
/*1* Copyright (c) 2007, 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 640943426* @summary Test behavior of nulls in checked collections27*/2829import java.util.AbstractMap;30import java.util.ArrayList;31import java.util.Collection;32import java.util.Collections;33import java.util.Comparator;34import java.util.HashMap;35import java.util.HashSet;36import java.util.Map;37import java.util.TreeSet;3839import static java.util.Collections.singleton;40import static java.util.Collections.singletonMap;4142@SuppressWarnings({"unchecked","serial"})43public class CheckedNull {4445void test(String[] args) throws Throwable {46testCollection(Collections.checkedCollection(47new ArrayList<String>(), String.class));48testCollection(Collections.checkedList(49new ArrayList<String>(), String.class));50testCollection(Collections.checkedSet(51new HashSet<String>(), String.class));5253final Comparator nullLow = new Comparator() {54public int compare(Object x, Object y) {55return x == y ? 0 :56x == null ? -1 :57y == null ? 1 :58((Comparable)x).compareTo(y); }};59testCollection(Collections.checkedSortedSet(60new TreeSet<String>(nullLow), String.class));6162testMap(Collections.checkedMap(63new HashMap<String, String>(),64String.class, String.class));65}6667ClassCastException cce(F f) {68try { f.f(); fail(); return null; }69catch (ClassCastException cce) { pass(); return cce; }70catch (Throwable t) { unexpected(t); return null; }71}7273void equalCCE(F ... fs) {74String detailMessage = null;75for (F f : fs)76if (detailMessage == null)77detailMessage = cce(f).getMessage();78else79equal(detailMessage, cce(f).getMessage());80}8182void add(Collection c, Object o) {83int s = c.size();84check(! c.contains(o));85check(c.add(o));86check(c.contains(o));87equal(c.size(), s+1);88check(c.remove(o));89check(! c.contains(o));90check(c.addAll(singleton(o)));91check(c.contains(o));92equal(c.size(), s+1);93check(c.remove(o));94equal(c.size(), s);95}9697void testCollection(final Collection c) {98try {99check(c.isEmpty());100add(c, null);101add(c, "foo");102103check(c.add("bar"));104add(c, null);105add(c, "foo");106107equalCCE(108new F(){void f(){ c.add(1); }},109new F(){void f(){ c.addAll(singleton(1)); }});110111} catch (Throwable t) { unexpected(t); }112}113114void put(Map m, Object k, Object v) {115int s = m.size();116check(! m.containsKey(k));117check(! m.containsValue(v));118equal(null, m.put(k, v));119check(m.containsKey(k));120check(m.containsValue(v));121equal(m.size(), s+1);122equal(v, m.remove(k));123check(! m.containsKey(k));124check(! m.containsValue(v));125m.putAll(singletonMap(k,v));126check(m.containsKey(k));127check(m.containsValue(v));128equal(m.size(), s+1);129equal(v,m.remove(k));130equal(m.size(), s);131}132133void testMap(final Map m) {134try {135check(m.isEmpty());136137put(m, "foo", null);138put(m, null, "foo");139put(m, null, null);140put(m, "foo", "bar");141142m.put("a", "b");143144put(m, "foo", null);145put(m, null, "foo");146put(m, null, null);147put(m, "foo", "bar");148149equalCCE(150new F(){void f(){ m.put(1, "foo"); }},151new F(){void f(){ m.putAll(singletonMap(1, "foo")); }});152153final Collection cheater = new ArrayList() {154public boolean contains(Object o) {155if (o instanceof Map.Entry)156((Map.Entry)o).setValue(1);157return false; }};158159equalCCE(160new F(){void f(){ m.put("foo", 1); }},161new F(){void f(){ m.putAll(singletonMap("foo", 1)); }},162new F(){void f(){163((Map.Entry)m.entrySet().iterator().next()).setValue(1); }},164new F(){void f(){165m.entrySet().removeAll(cheater);}},166new F(){void f(){167m.entrySet().retainAll(cheater);}});168169equalCCE(170new F(){void f(){ m.put(3, 1); }},171new F(){void f(){ m.putAll(singletonMap(3, 1)); }});172173equal(m.size(), 1);174equal(m.keySet(), singleton("a"));175equal(m.entrySet(),176singleton(new AbstractMap.SimpleImmutableEntry("a","b")));177178} catch (Throwable t) { unexpected(t); }179}180181//--------------------- Infrastructure ---------------------------182volatile int passed = 0, failed = 0;183void pass() {passed++;}184void fail() {failed++; Thread.dumpStack();}185void fail(String msg) {System.err.println(msg); fail();}186void unexpected(Throwable t) {failed++; t.printStackTrace();}187void check(boolean cond) {if (cond) pass(); else fail();}188void equal(Object x, Object y) {189if (x == null ? y == null : x.equals(y)) pass();190else fail(x + " not equal to " + y);}191public static void main(String[] args) throws Throwable {192new CheckedNull().instanceMain(args);}193void instanceMain(String[] args) throws Throwable {194try {test(args);} catch (Throwable t) {unexpected(t);}195System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);196if (failed > 0) throw new AssertionError("Some tests failed");}197abstract class F {abstract void f() throws Throwable;}198}199200201