test/testlibrary/com/oracle/java/testlibrary/Utils.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File hotspot Sdiff test/testlibrary/com/oracle/java/testlibrary

test/testlibrary/com/oracle/java/testlibrary/Utils.java

Print this page
rev 7259 : 8044186: Introduce a reproducible random generator
Reviewed-by: iignatyev
Contributed-by: sergei.kovalev@oracle.com


   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 package com.oracle.java.testlibrary;
  25 
  26 import static com.oracle.java.testlibrary.Asserts.assertTrue;
  27 
  28 import java.io.BufferedReader;
  29 import java.io.File;
  30 import java.io.FileReader;
  31 import java.io.IOException;

  32 import java.net.InetAddress;
  33 import java.net.ServerSocket;
  34 import java.net.UnknownHostException;
  35 import java.util.ArrayList;
  36 import java.util.List;
  37 import java.util.Arrays;
  38 import java.util.Collections;
  39 import java.util.regex.Pattern;

  40 import java.util.regex.Matcher;
  41 import java.lang.reflect.Field;
  42 import sun.misc.Unsafe;
  43 
  44 /**
  45  * Common library for various test helper functions.
  46  */
  47 public final class Utils {
  48 
  49     /**
  50      * Returns the sequence used by operating system to separate lines.
  51      */
  52     public static final String NEW_LINE = System.getProperty("line.separator");
  53 
  54     /**
  55      * Returns the value of 'test.vm.opts'system property.
  56      */
  57     public static final String VM_OPTIONS = System.getProperty("test.vm.opts", "").trim();
  58 
  59     /**
  60      * Returns the value of 'test.java.opts'system property.
  61      */
  62     public static final String JAVA_OPTIONS = System.getProperty("test.java.opts", "").trim();
  63 
  64     private static Unsafe unsafe = null;
  65 
  66     /**















  67     * Returns the value of 'test.timeout.factor' system property
  68     * converted to {@code double}.
  69     */
  70     public static final double TIMEOUT_FACTOR;
  71     static {
  72         String toFactor = System.getProperty("test.timeout.factor", "1.0");
  73         TIMEOUT_FACTOR = Double.parseDouble(toFactor);
  74     }
  75 
  76     private Utils() {
  77         // Private constructor to prevent class instantiation
  78     }
  79 
  80     /**
  81      * Returns the list of VM options.
  82      *
  83      * @return List of VM options
  84      */
  85     public static List<String> getVmOptions() {
  86         return Arrays.asList(safeSplitString(VM_OPTIONS));


 314         return unsafe;
 315     }
 316     private static final char[] hexArray = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
 317 
 318     /**
 319      * Returns hex view of byte array
 320      *
 321      * @param bytes byte array to process
 322      * @return Space separated hexadecimal string representation of bytes
 323      */
 324 
 325     public static String toHexString(byte[] bytes) {
 326         char[] hexView = new char[bytes.length * 3];
 327         int i = 0;
 328         for (byte b : bytes) {
 329             hexView[i++] = hexArray[(b >> 4) & 0x0F];
 330             hexView[i++] = hexArray[b & 0x0F];
 331             hexView[i++] = ' ';
 332         }
 333         return new String(hexView);




















 334     }
 335 }


   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 package com.oracle.java.testlibrary;
  25 
  26 import static com.oracle.java.testlibrary.Asserts.assertTrue;

  27 import java.io.BufferedReader;
  28 import java.io.File;
  29 import java.io.FileReader;
  30 import java.io.IOException;
  31 import java.lang.reflect.Field;
  32 import java.net.InetAddress;
  33 import java.net.ServerSocket;
  34 import java.net.UnknownHostException;
  35 import java.util.ArrayList;

  36 import java.util.Arrays;
  37 import java.util.Collections;
  38 import java.util.List;
  39 import java.util.Random;
  40 import java.util.regex.Matcher;
  41 import java.util.regex.Pattern;
  42 import sun.misc.Unsafe;
  43 
  44 /**
  45  * Common library for various test helper functions.
  46  */
  47 public final class Utils {
  48 
  49     /**
  50      * Returns the sequence used by operating system to separate lines.
  51      */
  52     public static final String NEW_LINE = System.getProperty("line.separator");
  53 
  54     /**
  55      * Returns the value of 'test.vm.opts'system property.
  56      */
  57     public static final String VM_OPTIONS = System.getProperty("test.vm.opts", "").trim();
  58 
  59     /**
  60      * Returns the value of 'test.java.opts'system property.
  61      */
  62     public static final String JAVA_OPTIONS = System.getProperty("test.java.opts", "").trim();
  63 
  64     private static Unsafe unsafe = null;
  65 
  66     /**
  67      * Defines property name for seed value.
  68      */
  69     public static final String SEED_PROPERTY_NAME = "com.oracle.java.testlibrary.random.seed";
  70 
  71     /* (non-javadoc)
  72      * Random generator with (or without) predefined seed. Depends on
  73      * "com.oracle.java.testlibrary.random.seed" property value.
  74      */
  75     private static volatile Random RANDOM_GENERATOR;
  76 
  77     /**
  78      * Contains the seed value used for {@link java.util.Random} creation.
  79      */
  80     public static final long SEED = Long.getLong(SEED_PROPERTY_NAME, new Random().nextLong());
  81     /**
  82     * Returns the value of 'test.timeout.factor' system property
  83     * converted to {@code double}.
  84     */
  85     public static final double TIMEOUT_FACTOR;
  86     static {
  87         String toFactor = System.getProperty("test.timeout.factor", "1.0");
  88         TIMEOUT_FACTOR = Double.parseDouble(toFactor);
  89     }
  90 
  91     private Utils() {
  92         // Private constructor to prevent class instantiation
  93     }
  94 
  95     /**
  96      * Returns the list of VM options.
  97      *
  98      * @return List of VM options
  99      */
 100     public static List<String> getVmOptions() {
 101         return Arrays.asList(safeSplitString(VM_OPTIONS));


 329         return unsafe;
 330     }
 331     private static final char[] hexArray = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
 332 
 333     /**
 334      * Returns hex view of byte array
 335      *
 336      * @param bytes byte array to process
 337      * @return Space separated hexadecimal string representation of bytes
 338      */
 339 
 340     public static String toHexString(byte[] bytes) {
 341         char[] hexView = new char[bytes.length * 3];
 342         int i = 0;
 343         for (byte b : bytes) {
 344             hexView[i++] = hexArray[(b >> 4) & 0x0F];
 345             hexView[i++] = hexArray[b & 0x0F];
 346             hexView[i++] = ' ';
 347         }
 348         return new String(hexView);
 349     }
 350 
 351     /**
 352      * Returns {@link java.util.Random} generator initialized with particular seed.
 353      * The seed could be provided via system property {@link Utils#SEED_PROPERTY_NAME}
 354      * In case no seed is provided, the method uses a random number.
 355      * The used seed printed to stdout.
 356      * @return {@link java.util.Random} generator with particular seed.
 357      */
 358     public static Random getRandomInstance() {
 359         if (RANDOM_GENERATOR == null) {
 360             synchronized (Utils.class) {
 361                 if (RANDOM_GENERATOR == null) {
 362                     RANDOM_GENERATOR = new Random(SEED);
 363                     System.out.printf("For random generator using seed: %d%n", SEED);
 364                     System.out.printf("To re-run test with same seed value please add \"-D%s=%d\" to command line.%n", SEED_PROPERTY_NAME, SEED);
 365                 }
 366             }
 367         }
 368         return RANDOM_GENERATOR;
 369     }
 370 }
test/testlibrary/com/oracle/java/testlibrary/Utils.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File