0N/A/*
2362N/A * Copyright (c) 2005, 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 6306829
0N/A * @summary Verify assertions in get() javadocs
0N/A * @author Martin Buchholz
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/Aimport java.util.concurrent.*;
0N/Aimport java.util.concurrent.atomic.*;
0N/A
0N/Apublic class Get {
0N/A
0N/A private static void realMain(String[] args) throws Throwable {
0N/A testMap(new Hashtable<Character,Boolean>());
0N/A testMap(new HashMap<Character,Boolean>());
0N/A testMap(new IdentityHashMap<Character,Boolean>());
0N/A testMap(new LinkedHashMap<Character,Boolean>());
0N/A testMap(new ConcurrentHashMap<Character,Boolean>());
0N/A testMap(new WeakHashMap<Character,Boolean>());
0N/A testMap(new TreeMap<Character,Boolean>());
0N/A testMap(new ConcurrentSkipListMap<Character,Boolean>());
0N/A }
0N/A
0N/A private static void put(Map<Character,Boolean> m,
0N/A Character key, Boolean value,
0N/A Boolean oldValue) {
0N/A if (oldValue != null) {
5047N/A check("containsValue(oldValue)", m.containsValue(oldValue));
5047N/A check("values.contains(oldValue)", m.values().contains(oldValue));
0N/A }
0N/A equal(m.put(key, value), oldValue);
0N/A equal(m.get(key), value);
5047N/A check("containsKey", m.containsKey(key));
5047N/A check("keySet.contains", m.keySet().contains(key));
5047N/A check("containsValue", m.containsValue(value));
5047N/A check("values.contains", m.values().contains(value));
5047N/A check("!isEmpty", ! m.isEmpty());
0N/A }
0N/A
0N/A private static void testMap(Map<Character,Boolean> m) {
0N/A // We verify following assertions in get(Object) method javadocs
0N/A boolean permitsNullKeys = (! (m instanceof ConcurrentMap ||
0N/A m instanceof Hashtable ||
0N/A m instanceof SortedMap));
0N/A boolean permitsNullValues = (! (m instanceof ConcurrentMap ||
0N/A m instanceof Hashtable));
0N/A boolean usesIdentity = m instanceof IdentityHashMap;
0N/A
5047N/A System.err.println(m.getClass());
0N/A put(m, 'A', true, null);
0N/A put(m, 'A', false, true); // Guaranteed identical by JLS
0N/A put(m, 'B', true, null);
0N/A put(m, new Character('A'), false, usesIdentity ? null : false);
0N/A if (permitsNullKeys) {
0N/A try {
0N/A put(m, null, true, null);
0N/A put(m, null, false, true);
0N/A }
5047N/A catch (Throwable t) { unexpected(m.getClass().getName(), t); }
0N/A } else {
5047N/A try { m.get(null); fail(m.getClass().getName() + " did not reject null key"); }
0N/A catch (NullPointerException e) {}
5047N/A catch (Throwable t) { unexpected(m.getClass().getName(), t); }
0N/A
5047N/A try { m.put(null, true); fail(m.getClass().getName() + " did not reject null key"); }
0N/A catch (NullPointerException e) {}
5047N/A catch (Throwable t) { unexpected(m.getClass().getName(), t); }
0N/A }
0N/A if (permitsNullValues) {
0N/A try {
0N/A put(m, 'C', null, null);
0N/A put(m, 'C', true, null);
0N/A put(m, 'C', null, true);
0N/A }
5047N/A catch (Throwable t) { unexpected(m.getClass().getName(), t); }
0N/A } else {
5047N/A try { m.put('A', null); fail(m.getClass().getName() + " did not reject null key"); }
0N/A catch (NullPointerException e) {}
5047N/A catch (Throwable t) { unexpected(m.getClass().getName(), t); }
0N/A
5047N/A try { m.put('C', null); fail(m.getClass().getName() + " did not reject null key"); }
0N/A catch (NullPointerException e) {}
5047N/A catch (Throwable t) { unexpected(m.getClass().getName(), t); }
0N/A }
0N/A }
0N/A
0N/A //--------------------- Infrastructure ---------------------------
0N/A static volatile int passed = 0, failed = 0;
0N/A static void pass() { passed++; }
5047N/A static void fail() { failed++; (new Error("Failure")).printStackTrace(System.err); }
5047N/A static void fail(String msg) { failed++; (new Error("Failure: " + msg)).printStackTrace(System.err); }
5047N/A static void unexpected(String msg, Throwable t) { System.err.println("Unexpected: " + msg); unexpected(t); }
5047N/A static void unexpected(Throwable t) { failed++; t.printStackTrace(System.err); }
0N/A static void check(boolean cond) { if (cond) pass(); else fail(); }
5047N/A static void check(String desc, boolean cond) { if (cond) pass(); else fail(desc); }
0N/A static void equal(Object x, Object y) {
5047N/A if(Objects.equals(x,y)) pass(); else fail(x + " not equal to " + y);
5047N/A }
0N/A
0N/A public static void main(String[] args) throws Throwable {
0N/A try { realMain(args); } catch (Throwable t) { unexpected(t); }
0N/A
0N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
5047N/A if (failed > 0) throw new Error("Some tests failed");
0N/A }
0N/A}