1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * @test 19 * @bug 6306829 20 * @summary Verify assertions in get() javadocs 21 * @author Martin Buchholz 22 */ 23 package test.java.util.Map; 24 25 import java.util.HashMap; 26 import java.util.Hashtable; 27 import java.util.IdentityHashMap; 28 import java.util.LinkedHashMap; 29 import java.util.Map; 30 import java.util.SortedMap; 31 import java.util.TreeMap; 32 import java.util.WeakHashMap; 33 import java.util.concurrent.ConcurrentHashMap; 34 import java.util.concurrent.ConcurrentMap; 35 import java.util.concurrent.ConcurrentSkipListMap; 36 37 import org.testng.annotations.Test; 38 import org.testng.Assert; 39 40 public class Get { 41 42 @Test testGet()43 public void testGet() { 44 testMap(new Hashtable<>()); 45 testMap(new HashMap<>()); 46 testMap(new IdentityHashMap<>()); 47 testMap(new LinkedHashMap<>()); 48 testMap(new ConcurrentHashMap<>()); 49 testMap(new WeakHashMap<>()); 50 testMap(new TreeMap<>()); 51 testMap(new ConcurrentSkipListMap<>()); 52 } 53 put(Map<Character,Boolean> m, Character key, Boolean value, Boolean oldValue)54 private static void put(Map<Character,Boolean> m, 55 Character key, Boolean value, 56 Boolean oldValue) { 57 if (oldValue != null) { 58 Assert.assertTrue(m.containsValue(oldValue)); 59 Assert.assertTrue(m.values().contains(oldValue)); 60 } 61 Assert.assertEquals(m.put(key, value), oldValue); 62 Assert.assertEquals(m.get(key), value); 63 Assert.assertTrue(m.containsKey(key)); 64 Assert.assertTrue(m.keySet().contains(key)); 65 Assert.assertTrue(m.containsValue(value)); 66 Assert.assertTrue(m.values().contains(value)); 67 Assert.assertTrue(! m.isEmpty()); 68 } 69 testMap(Map<Character,Boolean> m)70 private static void testMap(Map<Character,Boolean> m) { 71 // We verify following assertions in get(Object) method javadocs 72 boolean permitsNullKeys = (! (m instanceof ConcurrentMap || 73 m instanceof Hashtable || 74 m instanceof SortedMap)); 75 boolean permitsNullValues = (! (m instanceof ConcurrentMap || 76 m instanceof Hashtable)); 77 boolean usesIdentity = m instanceof IdentityHashMap; 78 79 System.err.println(m.getClass()); 80 put(m, 'A', true, null); 81 put(m, 'A', false, true); // Guaranteed identical by JLS 82 put(m, 'B', true, null); 83 put(m, new Character('A'), false, usesIdentity ? null : false); 84 if (permitsNullKeys) { 85 try { 86 put(m, null, true, null); 87 put(m, null, false, true); 88 } 89 catch (Throwable t) { Assert.fail(); } 90 } else { 91 try { m.get(null); Assert.fail(m.getClass().getName() + " did not reject null key"); } 92 catch (NullPointerException e) {} 93 catch (Throwable t) { Assert.fail(); } 94 95 try { m.put(null, true); Assert.fail(m.getClass().getName() + " did not reject null key"); } 96 catch (NullPointerException e) {} 97 catch (Throwable t) { Assert.fail(); } 98 } 99 if (permitsNullValues) { 100 try { 101 put(m, 'C', null, null); 102 put(m, 'C', true, null); 103 put(m, 'C', null, true); 104 } 105 catch (Throwable t) { Assert.fail(); } 106 } else { 107 try { m.put('A', null); Assert.fail(m.getClass().getName() + " did not reject null key"); } 108 catch (NullPointerException e) {} 109 catch (Throwable t) { Assert.fail(); } 110 111 try { m.put('C', null); Assert.fail(m.getClass().getName() + " did not reject null key"); } 112 catch (NullPointerException e) {} 113 catch (Throwable t) { Assert.fail(); } 114 } 115 } 116 }