Path: blob/master/test/jdk/java/util/Map/Get.java
41149 views
/*1* Copyright (c) 2005, 2012, 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 630682926* @summary Verify assertions in get() javadocs27* @author Martin Buchholz28*/2930import java.util.HashMap;31import java.util.Hashtable;32import java.util.IdentityHashMap;33import java.util.LinkedHashMap;34import java.util.Map;35import java.util.Objects;36import java.util.SortedMap;37import java.util.TreeMap;38import java.util.WeakHashMap;39import java.util.concurrent.ConcurrentHashMap;40import java.util.concurrent.ConcurrentMap;41import java.util.concurrent.ConcurrentSkipListMap;4243public class Get {4445private static void realMain(String[] args) throws Throwable {46testMap(new Hashtable<Character,Boolean>());47testMap(new HashMap<Character,Boolean>());48testMap(new IdentityHashMap<Character,Boolean>());49testMap(new LinkedHashMap<Character,Boolean>());50testMap(new ConcurrentHashMap<Character,Boolean>());51testMap(new WeakHashMap<Character,Boolean>());52testMap(new TreeMap<Character,Boolean>());53testMap(new ConcurrentSkipListMap<Character,Boolean>());54}5556private static void put(Map<Character,Boolean> m,57Character key, Boolean value,58Boolean oldValue) {59if (oldValue != null) {60check("containsValue(oldValue)", m.containsValue(oldValue));61check("values.contains(oldValue)", m.values().contains(oldValue));62}63equal(m.put(key, value), oldValue);64equal(m.get(key), value);65check("containsKey", m.containsKey(key));66check("keySet.contains", m.keySet().contains(key));67check("containsValue", m.containsValue(value));68check("values.contains", m.values().contains(value));69check("!isEmpty", ! m.isEmpty());70}7172private static void testMap(Map<Character,Boolean> m) {73// We verify following assertions in get(Object) method javadocs74boolean permitsNullKeys = (! (m instanceof ConcurrentMap ||75m instanceof Hashtable ||76m instanceof SortedMap));77boolean permitsNullValues = (! (m instanceof ConcurrentMap ||78m instanceof Hashtable));79boolean usesIdentity = m instanceof IdentityHashMap;8081System.err.println(m.getClass());82put(m, 'A', true, null);83put(m, 'A', false, true); // Guaranteed identical by JLS84put(m, 'B', true, null);85put(m, new Character('A'), false, usesIdentity ? null : false);86if (permitsNullKeys) {87try {88put(m, null, true, null);89put(m, null, false, true);90}91catch (Throwable t) { unexpected(m.getClass().getName(), t); }92} else {93try { m.get(null); fail(m.getClass().getName() + " did not reject null key"); }94catch (NullPointerException e) {}95catch (Throwable t) { unexpected(m.getClass().getName(), t); }9697try { m.put(null, true); fail(m.getClass().getName() + " did not reject null key"); }98catch (NullPointerException e) {}99catch (Throwable t) { unexpected(m.getClass().getName(), t); }100}101if (permitsNullValues) {102try {103put(m, 'C', null, null);104put(m, 'C', true, null);105put(m, 'C', null, true);106}107catch (Throwable t) { unexpected(m.getClass().getName(), t); }108} else {109try { m.put('A', null); fail(m.getClass().getName() + " did not reject null key"); }110catch (NullPointerException e) {}111catch (Throwable t) { unexpected(m.getClass().getName(), t); }112113try { m.put('C', null); fail(m.getClass().getName() + " did not reject null key"); }114catch (NullPointerException e) {}115catch (Throwable t) { unexpected(m.getClass().getName(), t); }116}117}118119//--------------------- Infrastructure ---------------------------120static volatile int passed = 0, failed = 0;121static void pass() { passed++; }122static void fail() { failed++; new Error("Failure").printStackTrace(System.err); }123static void fail(String msg) { failed++; new Error("Failure: " + msg).printStackTrace(System.err); }124static void unexpected(String msg, Throwable t) { System.err.println("Unexpected: " + msg); unexpected(t); }125static void unexpected(Throwable t) { failed++; t.printStackTrace(System.err); }126static void check(boolean cond) { if (cond) pass(); else fail(); }127static void check(String desc, boolean cond) { if (cond) pass(); else fail(desc); }128static void equal(Object x, Object y) {129if (Objects.equals(x,y)) pass(); else fail(x + " not equal to " + y);130}131132public static void main(String[] args) throws Throwable {133try { realMain(args); } catch (Throwable t) { unexpected(t); }134135System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);136if (failed > 0) throw new Error("Some tests failed");137}138}139140141