1 /* 2 * Copyright (c) 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 * Portions Copyright (c) 2012 IBM Corporation 26 */ 27 28 /* 29 * @test 30 * @bug 8000955 31 * @summary Map.Entry implementations need to comply with Map.Entry.hashCode() defined behaviour. 32 * @author ngmr 33 */ 34 import java.util.*; 35 import java.util.concurrent.ConcurrentHashMap; 36 import java.util.concurrent.ConcurrentSkipListMap; 37 38 public class EntryHashCode { 39 private static final int TEST_SIZE = 100; 40 41 static final Object[][] entryData = { 42 new Object[TEST_SIZE], 43 new Object[TEST_SIZE] 44 }; 45 46 @SuppressWarnings("unchecked") 47 static final Map<Object,Object>[] maps = (Map<Object,Object>[])new Map[] { 48 new HashMap<>(), 49 new Hashtable<>(), 50 new IdentityHashMap<>(), 51 new LinkedHashMap<>(), 52 new TreeMap<>(), 53 new WeakHashMap<>(), 54 new WeakHashMap<>(16, 0.75f, WeakHashMap.ValuePolicy.SOFT), 55 new WeakHashMap<>(16, 0.75f, WeakHashMap.ValuePolicy.STRONG), 56 new ConcurrentHashMap<>(), 57 new ConcurrentSkipListMap<>() 58 }; 59 60 static { 61 for (int i = 0; i < entryData[0].length; i++) { 62 // key objects need to be Comparable for use in TreeMap 63 entryData[0][i] = new Comparable<Object>() { 64 public int compareTo(Object o) { 65 return (hashCode() - o.hashCode()); 66 } 67 }; 68 entryData[1][i] = new Object(); 69 } 70 } 71 72 private static void addTestData(Map<Object,Object> map) { 73 for (int i = 0; i < entryData[0].length; i++) { 74 map.put(entryData[0][i], entryData[1][i]); 75 } 76 } 77 78 public static void main(String[] args) throws Exception { 79 Exception failure = null; 80 for (Map<Object,Object> map: maps) { 81 addTestData(map); 82 83 try { 84 for (Map.Entry<Object,Object> e: map.entrySet()) { 85 Object key = e.getKey(); 86 Object value = e.getValue(); 87 int expectedEntryHashCode = 88 (Objects.hashCode(key) ^ Objects.hashCode(value)); 89 90 if (e.hashCode() != expectedEntryHashCode) { 91 throw new Exception("FAILURE: " + 92 e.getClass().getName() + 93 ".hashCode() does not conform to defined" + 94 " behaviour of java.util.Map.Entry.hashCode()"); 95 } 96 } 97 } catch (Exception e) { 98 if (failure == null) { 99 failure = e; 100 } else { 101 failure.addSuppressed(e); 102 } 103 } finally { 104 map.clear(); 105 } 106 } 107 if (failure != null) { 108 throw failure; 109 } 110 } 111 }