jdk/test/java/util/Map/CheckRandomHashSeed.java
changeset 20145 08266da0533a
parent 20144 f998f8dc5b40
parent 19945 74049f7a28b4
child 20146 019d103c3e40
equal deleted inserted replaced
20144:f998f8dc5b40 20145:08266da0533a
     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 HashMap());
       
    57         testMap(new LinkedHashMap());
       
    58         testMap(new WeakHashMap());
       
    59         testMap(new Hashtable());
       
    60     }
       
    61 
       
    62     private static void testMap(Map map) {
       
    63         int hashSeed = getHashSeed(map);
       
    64         boolean hashSeedIsZero = (hashSeed == 0);
       
    65 
       
    66         if (expectRandom != hashSeedIsZero) {
       
    67             System.out.println("Test passed for " + map.getClass().getSimpleName() + " - expectRandom: " + expectRandom + ", hashSeed: " + hashSeed);
       
    68         } else {
       
    69             throw new Error ("Test FAILED for " + map.getClass().getSimpleName() + " -  expectRandom: " + expectRandom + ", hashSeed: " + hashSeed);
       
    70         }
       
    71     }
       
    72 
       
    73     private static int getHashSeed(Map map) {
       
    74         try {
       
    75             if (map instanceof HashMap || map instanceof LinkedHashMap) {
       
    76                 map.put("Key", "Value");
       
    77                 Field hashSeedField = HashMap.class.getDeclaredField("hashSeed");
       
    78                 hashSeedField.setAccessible(true);
       
    79                 int hashSeed = hashSeedField.getInt(map);
       
    80                 return hashSeed;
       
    81             } else {
       
    82                 map.put("Key", "Value");
       
    83                 Field hashSeedField = map.getClass().getDeclaredField("hashSeed");
       
    84                 hashSeedField.setAccessible(true);
       
    85                 int hashSeed = hashSeedField.getInt(map);
       
    86                 return hashSeed;
       
    87             }
       
    88         } catch(Exception e) {
       
    89             e.printStackTrace();
       
    90             throw new Error(e);
       
    91         }
       
    92     }
       
    93 }