1 /* 2 * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 6306829 27 * @summary Verify assertions in get() javadocs 28 * @author Martin Buchholz 29 */ 30 package test.java.util.Map; 31 32 import java.util.HashMap; 33 import java.util.Hashtable; 34 import java.util.IdentityHashMap; 35 import java.util.LinkedHashMap; 36 import java.util.Map; 37 import java.util.SortedMap; 38 import java.util.TreeMap; 39 import java.util.WeakHashMap; 40 import java.util.concurrent.ConcurrentHashMap; 41 import java.util.concurrent.ConcurrentMap; 42 import java.util.concurrent.ConcurrentSkipListMap; 43 44 import org.testng.annotations.Test; 45 import org.testng.Assert; 46 47 public class Get { 48 49 @Test testGet()50 public void testGet() { 51 testMap(new Hashtable<>()); 52 testMap(new HashMap<>()); 53 testMap(new IdentityHashMap<>()); 54 testMap(new LinkedHashMap<>()); 55 testMap(new ConcurrentHashMap<>()); 56 testMap(new WeakHashMap<>()); 57 testMap(new TreeMap<>()); 58 testMap(new ConcurrentSkipListMap<>()); 59 } 60 put(Map<Character,Boolean> m, Character key, Boolean value, Boolean oldValue)61 private static void put(Map<Character,Boolean> m, 62 Character key, Boolean value, 63 Boolean oldValue) { 64 if (oldValue != null) { 65 Assert.assertTrue(m.containsValue(oldValue)); 66 Assert.assertTrue(m.values().contains(oldValue)); 67 } 68 Assert.assertEquals(m.put(key, value), oldValue); 69 Assert.assertEquals(m.get(key), value); 70 Assert.assertTrue(m.containsKey(key)); 71 Assert.assertTrue(m.keySet().contains(key)); 72 Assert.assertTrue(m.containsValue(value)); 73 Assert.assertTrue(m.values().contains(value)); 74 Assert.assertTrue(! m.isEmpty()); 75 } 76 testMap(Map<Character,Boolean> m)77 private static void testMap(Map<Character,Boolean> m) { 78 // We verify following assertions in get(Object) method javadocs 79 boolean permitsNullKeys = (! (m instanceof ConcurrentMap || 80 m instanceof Hashtable || 81 m instanceof SortedMap)); 82 boolean permitsNullValues = (! (m instanceof ConcurrentMap || 83 m instanceof Hashtable)); 84 boolean usesIdentity = m instanceof IdentityHashMap; 85 86 System.err.println(m.getClass()); 87 put(m, 'A', true, null); 88 put(m, 'A', false, true); // Guaranteed identical by JLS 89 put(m, 'B', true, null); 90 put(m, new Character('A'), false, usesIdentity ? null : false); 91 if (permitsNullKeys) { 92 try { 93 put(m, null, true, null); 94 put(m, null, false, true); 95 } 96 catch (Throwable t) { Assert.fail(); } 97 } else { 98 try { m.get(null); Assert.fail(m.getClass().getName() + " did not reject null key"); } 99 catch (NullPointerException e) {} 100 catch (Throwable t) { Assert.fail(); } 101 102 try { m.put(null, true); Assert.fail(m.getClass().getName() + " did not reject null key"); } 103 catch (NullPointerException e) {} 104 catch (Throwable t) { Assert.fail(); } 105 } 106 if (permitsNullValues) { 107 try { 108 put(m, 'C', null, null); 109 put(m, 'C', true, null); 110 put(m, 'C', null, true); 111 } 112 catch (Throwable t) { Assert.fail(); } 113 } else { 114 try { m.put('A', null); Assert.fail(m.getClass().getName() + " did not reject null key"); } 115 catch (NullPointerException e) {} 116 catch (Throwable t) { Assert.fail(); } 117 118 try { m.put('C', null); Assert.fail(m.getClass().getName() + " did not reject null key"); } 119 catch (NullPointerException e) {} 120 catch (Throwable t) { Assert.fail(); } 121 } 122 } 123 }