test/jdk/java/net/httpclient/whitebox/java.net.http/java/net/http/AbstractRandomTest.java
branchhttp-client-branch
changeset 56092 fd85b2bf2b0d
parent 56091 aedd6133e7a0
child 56093 22d94c4a3641
equal deleted inserted replaced
56091:aedd6133e7a0 56092:fd85b2bf2b0d
     1 /*
       
     2  * Copyright (c) 2017, 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 package java.net.http;
       
    25 
       
    26 import java.util.Random;
       
    27 
       
    28 /** Abstract supertype for tests that need random numbers within a given range. */
       
    29 public class AbstractRandomTest {
       
    30 
       
    31     private static Long getSystemSeed() {
       
    32         Long seed = null;
       
    33         try {
       
    34             // note that Long.valueOf(null) also throws a NumberFormatException
       
    35             // so if the property is undefined this will still work correctly
       
    36             seed = Long.valueOf(System.getProperty("seed"));
       
    37         } catch (NumberFormatException e) {
       
    38             // do nothing: seed is still null
       
    39         }
       
    40         return seed;
       
    41     }
       
    42 
       
    43     private static long getSeed() {
       
    44         Long seed = getSystemSeed();
       
    45         if (seed == null) {
       
    46             seed = (new Random()).nextLong();
       
    47         }
       
    48         System.out.println("Seed from AbstractRandomTest.getSeed = "+seed+"L");
       
    49         return seed;
       
    50     }
       
    51 
       
    52     private static Random random = new Random(getSeed());
       
    53 
       
    54     protected static int randomRange(int lower, int upper) {
       
    55         if (lower > upper)
       
    56             throw new IllegalArgumentException("lower > upper");
       
    57         int diff = upper - lower;
       
    58         int r = lower + random.nextInt(diff);
       
    59         return r - (r % 8); // round down to multiple of 8 (align for longs)
       
    60     }
       
    61 }