test/testlibrary_tests/RandomGeneratorTest.java

Print this page




  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  * @modules java.base/sun.misc
  29  *          java.management
  30  * @run driver RandomGeneratorTest SAME_SEED
  31  * @run driver RandomGeneratorTest NO_SEED
  32  * @run driver RandomGeneratorTest DIFFERENT_SEED
  33  */
  34 
  35 import jdk.test.lib.ProcessTools;
  36 import jdk.test.lib.Utils;

  37 import java.util.ArrayList;
  38 import java.util.List;
  39 import java.util.Random;



  40 
  41 /**
  42  * The test verifies correctness of work {@link jdk.test.lib.Utils#getRandomInstance()}.
  43  * Test works in three modes: same seed provided, no seed provided and
  44  * different seed provided. In the first case the test expects that all random numbers
  45  * will be repeated in all next iterations. For other two modes test expects that
  46  * randomly generated numbers differ from original.
  47  */
  48 public class RandomGeneratorTest {
  49     private static final String SEED_VM_OPTION = "-D" + Utils.SEED_PROPERTY_NAME + "=";
  50 
  51     public static void main( String[] args) throws Throwable {
  52         if (args.length == 0) {
  53             throw new Error("TESTBUG: No test mode provided.");
  54         }
  55         SeedOption seedOpt = SeedOption.valueOf(args[0]);
  56         List<String> jvmArgs = new ArrayList<String>();
  57         String optStr = seedOpt.getSeedOption();
  58         if (optStr != null) {
  59             jvmArgs.add(optStr);
  60         }
  61         jvmArgs.add(RandomRunner.class.getName());



  62         String[] cmdLineArgs = jvmArgs.toArray(new String[jvmArgs.size()]);
  63         String etalon = ProcessTools.executeTestJvm(cmdLineArgs).getStdout().trim();


  64         seedOpt.verify(etalon, cmdLineArgs);
  65     }
  66 
  67     /**
  68      * The utility enum helps to generate an appropriate string that should be passed
  69      * to the command line depends on the testing mode. It is also responsible for the result
  70      * validation.
  71      */
  72     private enum SeedOption {
  73         SAME_SEED {
  74             @Override
  75             public String getSeedOption() {
  76                 return SEED_VM_OPTION + Utils.SEED;
  77             }
  78 
  79             @Override
  80             protected boolean isOutputExpected(String orig, String output) {
  81                 return output.equals(orig);
  82             }
  83         },


 104          * Generates a string to be added as a command line argument.
 105          * It contains "-D" prefix, system property name, '=' sign
 106          * and seed value.
 107          * @return command line argument
 108          */
 109         public abstract String getSeedOption();
 110 
 111         protected boolean isOutputExpected(String orig, String output) {
 112             return !output.equals(orig);
 113         }
 114 
 115         /**
 116          * Verifies that the original output meets expectations
 117          * depending on the test mode. It compares the output of second execution
 118          * to original one.
 119          * @param orig original output
 120          * @param cmdLine command line arguments
 121          * @throws Throwable - Throws an exception in case test failure.
 122          */
 123         public void verify(String orig, String[] cmdLine) {
 124             String lastLineOrig = getLastLine(orig);
 125             String lastLine;
 126             try {
 127                 lastLine = getLastLine(ProcessTools.executeTestJvm(cmdLine).getStdout().trim());
 128             } catch (Throwable t) {
 129                 throw new Error("TESTBUG: Unexpedted exception during jvm execution.", t);
 130             }
 131             if (!isOutputExpected(lastLineOrig, lastLine)) {








 132                     throw new AssertionError("Unexpected random number sequence for mode: " + this.name());
 133             }
 134         }
 135 
 136         private static String getLastLine(String output) {
 137             return output.substring(output.lastIndexOf(Utils.NEW_LINE)).trim();
 138         }
 139     }
 140 
 141     /**
 142      * The helper class generates several random numbers
 143      * and prints them out.

 144      */
 145     public static class RandomRunner {
 146         private static final int COUNT = 10;
 147         public static void main(String[] args) {
 148             StringBuilder sb = new StringBuilder();
 149             Random rng = Utils.getRandomInstance();
 150             for (int i = 0; i < COUNT; i++) {
 151                 sb.append(rng.nextLong()).append(' ');
 152             }
 153             System.out.println(sb.toString());




 154         }
 155     }
 156 }


  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  * @modules java.base/sun.misc
  29  *          java.management
  30  * @run driver RandomGeneratorTest SAME_SEED
  31  * @run driver RandomGeneratorTest NO_SEED
  32  * @run driver RandomGeneratorTest DIFFERENT_SEED
  33  */
  34 
  35 import java.io.FileWriter;
  36 import java.io.IOException;
  37 import java.io.PrintWriter;
  38 import java.util.ArrayList;
  39 import java.util.List;
  40 import java.util.Random;
  41 import jdk.test.lib.OutputAnalyzer;
  42 import jdk.test.lib.ProcessTools;
  43 import jdk.test.lib.Utils;
  44 
  45 /**
  46  * The test verifies correctness of work {@link jdk.test.lib.Utils#getRandomInstance()}.
  47  * Test works in three modes: same seed provided, no seed provided and
  48  * different seed provided. In the first case the test expects that all random numbers
  49  * will be repeated in all next iterations. For other two modes test expects that
  50  * randomly generated numbers differ from original.
  51  */
  52 public class RandomGeneratorTest {
  53     private static final String SEED_VM_OPTION = "-D" + Utils.SEED_PROPERTY_NAME + "=";
  54 
  55     public static void main( String[] args) throws Throwable {
  56         if (args.length == 0) {
  57             throw new Error("TESTBUG: No test mode provided.");
  58         }
  59         SeedOption seedOpt = SeedOption.valueOf(args[0]);
  60         List<String> jvmArgs = new ArrayList<String>();
  61         String optStr = seedOpt.getSeedOption();
  62         if (optStr != null) {
  63             jvmArgs.add(optStr);
  64         }
  65         jvmArgs.add(RandomRunner.class.getName());
  66         String origFileName = seedOpt.name() + "_orig";
  67         jvmArgs.add(origFileName);
  68         int fileNameIndex = jvmArgs.size() - 1;
  69         String[] cmdLineArgs = jvmArgs.toArray(new String[jvmArgs.size()]);
  70         ProcessTools.executeTestJvm(cmdLineArgs).shouldHaveExitValue(0);
  71         String etalon = Utils.fileAsString(origFileName).trim();
  72         cmdLineArgs[fileNameIndex] = seedOpt.name();
  73         seedOpt.verify(etalon, cmdLineArgs);
  74     }
  75 
  76     /**
  77      * The utility enum helps to generate an appropriate string that should be passed
  78      * to the command line depends on the testing mode. It is also responsible for the result
  79      * validation.
  80      */
  81     private enum SeedOption {
  82         SAME_SEED {
  83             @Override
  84             public String getSeedOption() {
  85                 return SEED_VM_OPTION + Utils.SEED;
  86             }
  87 
  88             @Override
  89             protected boolean isOutputExpected(String orig, String output) {
  90                 return output.equals(orig);
  91             }
  92         },


 113          * Generates a string to be added as a command line argument.
 114          * It contains "-D" prefix, system property name, '=' sign
 115          * and seed value.
 116          * @return command line argument
 117          */
 118         public abstract String getSeedOption();
 119 
 120         protected boolean isOutputExpected(String orig, String output) {
 121             return !output.equals(orig);
 122         }
 123 
 124         /**
 125          * Verifies that the original output meets expectations
 126          * depending on the test mode. It compares the output of second execution
 127          * to original one.
 128          * @param orig original output
 129          * @param cmdLine command line arguments
 130          * @throws Throwable - Throws an exception in case test failure.
 131          */
 132         public void verify(String orig, String[] cmdLine) {
 133             String output;
 134             OutputAnalyzer oa;
 135             try {
 136                 oa = ProcessTools.executeTestJvm(cmdLine);
 137             } catch (Throwable t) {
 138                 throw new Error("TESTBUG: Unexpedted exception during jvm execution.", t);
 139             }
 140             oa.shouldHaveExitValue(0);
 141             try {
 142                 output = Utils.fileAsString(name()).trim();
 143             } catch (IOException ioe) {
 144                 throw new Error("TESTBUG: Problem during IO operation with file: " + name(), ioe);
 145             }
 146             if (!isOutputExpected(orig, output)) {
 147                 System.err.println("Initial output: " + orig);
 148                 System.err.println("Second run output: " + output);
 149                 throw new AssertionError("Unexpected random number sequence for mode: " + this.name());
 150             }
 151         }




 152     }
 153 
 154     /**
 155      * The helper class generates several random numbers
 156      * and put results to a file. The file name came as first
 157      * command line argument.
 158      */
 159     public static class RandomRunner {
 160         private static final int COUNT = 10;
 161         public static void main(String[] args) {
 162             StringBuilder sb = new StringBuilder();
 163             Random rng = Utils.getRandomInstance();
 164             for (int i = 0; i < COUNT; i++) {
 165                 sb.append(rng.nextLong()).append(' ');
 166             }
 167             try (PrintWriter pw = new PrintWriter(new FileWriter(args[0]))) {
 168                 pw.write(sb.toString());
 169             } catch (IOException ioe) {
 170                 throw new Error("TESTBUG: Problem during IO operation with file: " + args[0], ioe);
 171             }
 172         }
 173     }
 174 }