author | kbarrett |
Tue, 22 Nov 2016 20:50:31 -0500 | |
changeset 42589 | 037b433e8069 |
parent 40631 | ed82623d7831 |
permissions | -rw-r--r-- |
27453 | 1 |
/* |
40631
ed82623d7831
8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
36851
diff
changeset
|
2 |
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. |
27453 | 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 |
|
40631
ed82623d7831
8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
36851
diff
changeset
|
27 |
* @library /test/lib |
36851 | 28 |
* @modules java.base/jdk.internal.misc |
29678
dd2f3932c21e
8075586: Add @modules as needed to the open hotspot tests
ykantser
parents:
29350
diff
changeset
|
29 |
* java.management |
27453 | 30 |
* @run driver RandomGeneratorTest SAME_SEED |
31 |
* @run driver RandomGeneratorTest NO_SEED |
|
32 |
* @run driver RandomGeneratorTest DIFFERENT_SEED |
|
33 |
*/ |
|
34 |
||
31232
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
35 |
import java.io.FileWriter; |
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
36 |
import java.io.IOException; |
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
37 |
import java.io.PrintWriter; |
27453 | 38 |
import java.util.ArrayList; |
39 |
import java.util.List; |
|
40 |
import java.util.Random; |
|
40631
ed82623d7831
8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
36851
diff
changeset
|
41 |
import jdk.test.lib.process.OutputAnalyzer; |
ed82623d7831
8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents:
36851
diff
changeset
|
42 |
import jdk.test.lib.process.ProcessTools; |
31232
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
43 |
import jdk.test.lib.Utils; |
27453 | 44 |
|
45 |
/** |
|
30604
b8d532cb6420
8067013: Rename the com.oracle.java.testlibary package
ykantser
parents:
29678
diff
changeset
|
46 |
* The test verifies correctness of work {@link jdk.test.lib.Utils#getRandomInstance()}. |
27453 | 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()); |
|
31232
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
66 |
String origFileName = seedOpt.name() + "_orig"; |
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
67 |
jvmArgs.add(origFileName); |
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
68 |
int fileNameIndex = jvmArgs.size() - 1; |
27453 | 69 |
String[] cmdLineArgs = jvmArgs.toArray(new String[jvmArgs.size()]); |
31232
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
70 |
ProcessTools.executeTestJvm(cmdLineArgs).shouldHaveExitValue(0); |
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
71 |
String etalon = Utils.fileAsString(origFileName).trim(); |
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
72 |
cmdLineArgs[fileNameIndex] = seedOpt.name(); |
27453 | 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 |
}, |
|
93 |
DIFFERENT_SEED { |
|
94 |
@Override |
|
95 |
public String getSeedOption() { |
|
96 |
return SEED_VM_OPTION + Utils.getRandomInstance().nextLong(); |
|
97 |
} |
|
98 |
||
99 |
@Override |
|
100 |
public void verify(String orig, String[] cmdLine) { |
|
101 |
cmdLine[0] = getSeedOption(); |
|
102 |
super.verify(orig, cmdLine); |
|
103 |
} |
|
104 |
}, |
|
105 |
NO_SEED { |
|
106 |
@Override |
|
107 |
public String getSeedOption() { |
|
108 |
return null; |
|
109 |
} |
|
110 |
}; |
|
111 |
||
112 |
/** |
|
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) { |
|
31232
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
133 |
String output; |
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
134 |
OutputAnalyzer oa; |
27453 | 135 |
try { |
31232
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
136 |
oa = ProcessTools.executeTestJvm(cmdLine); |
27453 | 137 |
} catch (Throwable t) { |
138 |
throw new Error("TESTBUG: Unexpedted exception during jvm execution.", t); |
|
139 |
} |
|
31232
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
140 |
oa.shouldHaveExitValue(0); |
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
141 |
try { |
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
142 |
output = Utils.fileAsString(name()).trim(); |
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
143 |
} catch (IOException ioe) { |
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
144 |
throw new Error("TESTBUG: Problem during IO operation with file: " + name(), ioe); |
27453 | 145 |
} |
31232
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
146 |
if (!isOutputExpected(orig, output)) { |
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
147 |
System.err.println("Initial output: " + orig); |
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
148 |
System.err.println("Second run output: " + output); |
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
149 |
throw new AssertionError("Unexpected random number sequence for mode: " + this.name()); |
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
150 |
} |
27453 | 151 |
} |
152 |
} |
|
153 |
||
154 |
/** |
|
155 |
* The helper class generates several random numbers |
|
31232
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
156 |
* and put results to a file. The file name came as first |
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
157 |
* command line argument. |
27453 | 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 |
} |
|
31232
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
167 |
try (PrintWriter pw = new PrintWriter(new FileWriter(args[0]))) { |
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
168 |
pw.write(sb.toString()); |
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
169 |
} catch (IOException ioe) { |
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
170 |
throw new Error("TESTBUG: Problem during IO operation with file: " + args[0], ioe); |
741963a4e59e
8078145: testlibrary_tests/RandomGeneratorTest.java failed with AssertionError : Unexpected random number sequence for mode: NO_SEED
skovalev
parents:
30604
diff
changeset
|
171 |
} |
27453 | 172 |
} |
173 |
} |
|
174 |
} |