hotspot/test/testlibrary_tests/RandomGeneratorTest.java
changeset 27453 9aeb9b97bef6
child 29350 8029ab8b2ec5
equal deleted inserted replaced
27452:b1fef8d1658d 27453:9aeb9b97bef6
       
     1 /*
       
     2  * Copyright (c) 2014, 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  * @summary Verify correctnes of the random generator from Utility.java
       
    27  * @library /testlibrary
       
    28  * @run driver RandomGeneratorTest SAME_SEED
       
    29  * @run driver RandomGeneratorTest NO_SEED
       
    30  * @run driver RandomGeneratorTest DIFFERENT_SEED
       
    31  */
       
    32 
       
    33 import com.oracle.java.testlibrary.ProcessTools;
       
    34 import com.oracle.java.testlibrary.Utils;
       
    35 import java.util.ArrayList;
       
    36 import java.util.List;
       
    37 import java.util.Random;
       
    38 
       
    39 /**
       
    40  * The test verifies correctness of work {@link com.oracle.java.testlibrary.Utils#getRandomInstance()}.
       
    41  * Test works in three modes: same seed provided, no seed provided and
       
    42  * different seed provided. In the first case the test expects that all random numbers
       
    43  * will be repeated in all next iterations. For other two modes test expects that
       
    44  * randomly generated numbers differ from original.
       
    45  */
       
    46 public class RandomGeneratorTest {
       
    47     private static final String SEED_VM_OPTION = "-D" + Utils.SEED_PROPERTY_NAME + "=";
       
    48 
       
    49     public static void main( String[] args) throws Throwable {
       
    50         if (args.length == 0) {
       
    51             throw new Error("TESTBUG: No test mode provided.");
       
    52         }
       
    53         SeedOption seedOpt = SeedOption.valueOf(args[0]);
       
    54         List<String> jvmArgs = new ArrayList<String>();
       
    55         String optStr = seedOpt.getSeedOption();
       
    56         if (optStr != null) {
       
    57             jvmArgs.add(optStr);
       
    58         }
       
    59         jvmArgs.add(RandomRunner.class.getName());
       
    60         String[] cmdLineArgs = jvmArgs.toArray(new String[jvmArgs.size()]);
       
    61         String etalon = ProcessTools.executeTestJvm(cmdLineArgs).getOutput().trim();
       
    62         seedOpt.verify(etalon, cmdLineArgs);
       
    63     }
       
    64 
       
    65     /**
       
    66      * The utility enum helps to generate an appropriate string that should be passed
       
    67      * to the command line depends on the testing mode. It is also responsible for the result
       
    68      * validation.
       
    69      */
       
    70     private enum SeedOption {
       
    71         SAME_SEED {
       
    72             @Override
       
    73             public String getSeedOption() {
       
    74                 return SEED_VM_OPTION + Utils.SEED;
       
    75             }
       
    76 
       
    77             @Override
       
    78             protected boolean isOutputExpected(String orig, String output) {
       
    79                 return output.equals(orig);
       
    80             }
       
    81         },
       
    82         DIFFERENT_SEED {
       
    83             @Override
       
    84             public String getSeedOption() {
       
    85                 return SEED_VM_OPTION + Utils.getRandomInstance().nextLong();
       
    86             }
       
    87 
       
    88             @Override
       
    89             public void verify(String orig, String[] cmdLine) {
       
    90                 cmdLine[0] = getSeedOption();
       
    91                 super.verify(orig, cmdLine);
       
    92             }
       
    93         },
       
    94         NO_SEED {
       
    95             @Override
       
    96             public String getSeedOption() {
       
    97                 return null;
       
    98             }
       
    99         };
       
   100 
       
   101         /**
       
   102          * Generates a string to be added as a command line argument.
       
   103          * It contains "-D" prefix, system property name, '=' sign
       
   104          * and seed value.
       
   105          * @return command line argument
       
   106          */
       
   107         public abstract String getSeedOption();
       
   108 
       
   109         protected boolean isOutputExpected(String orig, String output) {
       
   110             return !output.equals(orig);
       
   111         }
       
   112 
       
   113         /**
       
   114          * Verifies that the original output meets expectations
       
   115          * depending on the test mode. It compares the output of second execution
       
   116          * to original one.
       
   117          * @param orig original output
       
   118          * @param cmdLine command line arguments
       
   119          * @throws Throwable - Throws an exception in case test failure.
       
   120          */
       
   121         public void verify(String orig, String[] cmdLine) {
       
   122             String lastLineOrig = getLastLine(orig);
       
   123             String lastLine;
       
   124             try {
       
   125                 lastLine = getLastLine(ProcessTools.executeTestJvm(cmdLine).getOutput().trim());
       
   126             } catch (Throwable t) {
       
   127                 throw new Error("TESTBUG: Unexpedted exception during jvm execution.", t);
       
   128             }
       
   129             if (!isOutputExpected(lastLineOrig, lastLine)) {
       
   130                     throw new AssertionError("Unexpected random number sequence for mode: " + this.name());
       
   131             }
       
   132         }
       
   133 
       
   134         private static String getLastLine(String output) {
       
   135             return output.substring(output.lastIndexOf(Utils.NEW_LINE)).trim();
       
   136         }
       
   137     }
       
   138 
       
   139     /**
       
   140      * The helper class generates several random numbers
       
   141      * and prints them out.
       
   142      */
       
   143     public static class RandomRunner {
       
   144         private static final int COUNT = 10;
       
   145         public static void main(String[] args) {
       
   146             StringBuilder sb = new StringBuilder();
       
   147             Random rng = Utils.getRandomInstance();
       
   148             for (int i = 0; i < COUNT; i++) {
       
   149                 sb.append(rng.nextLong()).append(' ');
       
   150             }
       
   151             System.out.println(sb.toString());
       
   152         }
       
   153     }
       
   154 }