1 /* |
|
2 * Copyright (c) 2013, 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 8005698 |
|
27 * @summary Check operation of jdk.map.useRandomSeed property |
|
28 * @run main CheckRandomHashSeed |
|
29 * @run main/othervm -Djdk.map.useRandomSeed=false CheckRandomHashSeed |
|
30 * @run main/othervm -Djdk.map.useRandomSeed=bogus CheckRandomHashSeed |
|
31 * @run main/othervm -Djdk.map.useRandomSeed=true CheckRandomHashSeed true |
|
32 * @author Brent Christian |
|
33 */ |
|
34 import java.lang.reflect.Field; |
|
35 import java.util.Map; |
|
36 import java.util.HashMap; |
|
37 import java.util.LinkedHashMap; |
|
38 import java.util.Hashtable; |
|
39 import java.util.WeakHashMap; |
|
40 |
|
41 public class CheckRandomHashSeed { |
|
42 private final static String PROP_NAME = "jdk.map.useRandomSeed"; |
|
43 static boolean expectRandom = false; |
|
44 |
|
45 public static void main(String[] args) { |
|
46 if (args.length > 0 && args[0].equals("true")) { |
|
47 expectRandom = true; |
|
48 } |
|
49 String hashSeedProp = System.getProperty(PROP_NAME); |
|
50 boolean propSet = (null != hashSeedProp) |
|
51 ? Boolean.parseBoolean(hashSeedProp) : false; |
|
52 if (expectRandom != propSet) { |
|
53 throw new Error("Error in test setup: " + (expectRandom ? "" : "not " ) + "expecting random hashSeed, but " + PROP_NAME + " is " + (propSet ? "" : "not ") + "enabled"); |
|
54 } |
|
55 |
|
56 testMap(new WeakHashMap()); |
|
57 testMap(new Hashtable()); |
|
58 } |
|
59 |
|
60 private static void testMap(Map map) { |
|
61 int hashSeed = getHashSeed(map); |
|
62 boolean hashSeedIsZero = (hashSeed == 0); |
|
63 |
|
64 if (expectRandom != hashSeedIsZero) { |
|
65 System.out.println("Test passed for " + map.getClass().getSimpleName() + " - expectRandom: " + expectRandom + ", hashSeed: " + hashSeed); |
|
66 } else { |
|
67 throw new Error ("Test FAILED for " + map.getClass().getSimpleName() + " - expectRandom: " + expectRandom + ", hashSeed: " + hashSeed); |
|
68 } |
|
69 } |
|
70 |
|
71 private static int getHashSeed(Map map) { |
|
72 try { |
|
73 if (map instanceof HashMap || map instanceof LinkedHashMap) { |
|
74 map.put("Key", "Value"); |
|
75 Field hashSeedField = HashMap.class.getDeclaredField("hashSeed"); |
|
76 hashSeedField.setAccessible(true); |
|
77 int hashSeed = hashSeedField.getInt(map); |
|
78 return hashSeed; |
|
79 } else { |
|
80 map.put("Key", "Value"); |
|
81 Field hashSeedField = map.getClass().getDeclaredField("hashSeed"); |
|
82 hashSeedField.setAccessible(true); |
|
83 int hashSeed = hashSeedField.getInt(map); |
|
84 return hashSeed; |
|
85 } |
|
86 } catch(Exception e) { |
|
87 e.printStackTrace(); |
|
88 throw new Error(e); |
|
89 } |
|
90 } |
|
91 } |
|