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 package test.java.util.Map; 35 36 import java.util.*; 37 import java.util.concurrent.ConcurrentHashMap; 38 import java.util.concurrent.ConcurrentSkipListMap; 39 40 import org.junit.Assert; 41 import org.testng.annotations.Test; 42 43 public class EntryHashCode { 44 private static final int TEST_SIZE = 100; 45 46 static final Object[][] entryData = { 47 new Object[TEST_SIZE], 48 new Object[TEST_SIZE] 49 }; 50 51 @SuppressWarnings("unchecked") 52 static final Map<Object,Object>[] maps = (Map<Object,Object>[])new Map[] { 53 new HashMap<>(), 54 new Hashtable<>(), 55 new IdentityHashMap<>(), 56 new LinkedHashMap<>(), 57 new TreeMap<>(), 58 new WeakHashMap<>(), 59 new ConcurrentHashMap<>(), 60 new ConcurrentSkipListMap<>() 61 }; 62 63 static { 64 for (int i = 0; i < entryData[0].length; i++) { 65 // key objects need to be Comparable for use in TreeMap 66 entryData[0][i] = new Comparable<Object>() { 67 public int compareTo(Object o) { 68 return (hashCode() - o.hashCode()); 69 } 70 }; 71 entryData[1][i] = new Object(); 72 } 73 } 74 addTestData(Map<Object,Object> map)75 private static void addTestData(Map<Object,Object> map) { 76 for (int i = 0; i < entryData[0].length; i++) { 77 map.put(entryData[0][i], entryData[1][i]); 78 } 79 } 80 81 @Test testEntryHashCode()82 public void testEntryHashCode() throws Exception { 83 Exception failure = null; 84 for (Map<Object,Object> map: maps) { 85 addTestData(map); 86 87 try { 88 for (Map.Entry<Object,Object> e: map.entrySet()) { 89 Object key = e.getKey(); 90 Object value = e.getValue(); 91 int expectedEntryHashCode = 92 (Objects.hashCode(key) ^ Objects.hashCode(value)); 93 94 Assert.assertEquals(e.hashCode(), expectedEntryHashCode); 95 } 96 } catch (Exception e) { 97 if (failure == null) { 98 failure = e; 99 } else { 100 failure.addSuppressed(e); 101 } 102 } finally { 103 map.clear(); 104 } 105 } 106 if (failure != null) { 107 Assert.fail();; 108 } 109 } 110 }