Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Collections/CheckedNull.java
38812 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.*;30import static java.util.Collections.*;3132@SuppressWarnings({"unchecked","serial"})33public class CheckedNull {3435void test(String[] args) throws Throwable {36testCollection(Collections.checkedCollection(37new ArrayList<String>(), String.class));38testCollection(Collections.checkedList(39new ArrayList<String>(), String.class));40testCollection(Collections.checkedSet(41new HashSet<String>(), String.class));4243final Comparator nullLow = new Comparator() {44public int compare(Object x, Object y) {45return x == y ? 0 :46x == null ? -1 :47y == null ? 1 :48((Comparable)x).compareTo(y); }};49testCollection(Collections.checkedSortedSet(50new TreeSet<String>(nullLow), String.class));5152testMap(Collections.checkedMap(53new HashMap<String, String>(),54String.class, String.class));55}5657ClassCastException cce(F f) {58try { f.f(); fail(); return null; }59catch (ClassCastException cce) { pass(); return cce; }60catch (Throwable t) { unexpected(t); return null; }61}6263void equalCCE(F ... fs) {64String detailMessage = null;65for (F f : fs)66if (detailMessage == null)67detailMessage = cce(f).getMessage();68else69equal(detailMessage, cce(f).getMessage());70}7172void add(Collection c, Object o) {73int s = c.size();74check(! c.contains(o));75check(c.add(o));76check(c.contains(o));77equal(c.size(), s+1);78check(c.remove(o));79check(! c.contains(o));80check(c.addAll(singleton(o)));81check(c.contains(o));82equal(c.size(), s+1);83check(c.remove(o));84equal(c.size(), s);85}8687void testCollection(final Collection c) {88try {89check(c.isEmpty());90add(c, null);91add(c, "foo");9293check(c.add("bar"));94add(c, null);95add(c, "foo");9697equalCCE(98new F(){void f(){ c.add(1); }},99new F(){void f(){ c.addAll(singleton(1)); }});100101} catch (Throwable t) { unexpected(t); }102}103104void put(Map m, Object k, Object v) {105int s = m.size();106check(! m.containsKey(k));107check(! m.containsValue(v));108equal(null, m.put(k, v));109check(m.containsKey(k));110check(m.containsValue(v));111equal(m.size(), s+1);112equal(v, m.remove(k));113check(! m.containsKey(k));114check(! m.containsValue(v));115m.putAll(singletonMap(k,v));116check(m.containsKey(k));117check(m.containsValue(v));118equal(m.size(), s+1);119equal(v,m.remove(k));120equal(m.size(), s);121}122123void testMap(final Map m) {124try {125check(m.isEmpty());126127put(m, "foo", null);128put(m, null, "foo");129put(m, null, null);130put(m, "foo", "bar");131132m.put("a", "b");133134put(m, "foo", null);135put(m, null, "foo");136put(m, null, null);137put(m, "foo", "bar");138139equalCCE(140new F(){void f(){ m.put(1, "foo"); }},141new F(){void f(){ m.putAll(singletonMap(1, "foo")); }});142143final Collection cheater = new ArrayList() {144public boolean contains(Object o) {145if (o instanceof Map.Entry)146((Map.Entry)o).setValue(1);147return false; }};148149equalCCE(150new F(){void f(){ m.put("foo", 1); }},151new F(){void f(){ m.putAll(singletonMap("foo", 1)); }},152new F(){void f(){153((Map.Entry)m.entrySet().iterator().next()).setValue(1); }},154new F(){void f(){155m.entrySet().removeAll(cheater);}},156new F(){void f(){157m.entrySet().retainAll(cheater);}});158159equalCCE(160new F(){void f(){ m.put(3, 1); }},161new F(){void f(){ m.putAll(singletonMap(3, 1)); }});162163equal(m.size(), 1);164equal(m.keySet(), singleton("a"));165equal(m.entrySet(),166singleton(new AbstractMap.SimpleImmutableEntry("a","b")));167168} catch (Throwable t) { unexpected(t); }169}170171//--------------------- Infrastructure ---------------------------172volatile int passed = 0, failed = 0;173void pass() {passed++;}174void fail() {failed++; Thread.dumpStack();}175void fail(String msg) {System.err.println(msg); fail();}176void unexpected(Throwable t) {failed++; t.printStackTrace();}177void check(boolean cond) {if (cond) pass(); else fail();}178void equal(Object x, Object y) {179if (x == null ? y == null : x.equals(y)) pass();180else fail(x + " not equal to " + y);}181public static void main(String[] args) throws Throwable {182new CheckedNull().instanceMain(args);}183void instanceMain(String[] args) throws Throwable {184try {test(args);} catch (Throwable t) {unexpected(t);}185System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);186if (failed > 0) throw new AssertionError("Some tests failed");}187abstract class F {abstract void f() throws Throwable;}188}189190191