25
25
26
26
import java .io .File ;
27
27
import java .io .IOException ;
28
- import java .lang .annotation .Annotation ;
29
- import java .lang .reflect .Method ;
30
28
import java .net .Inet6Address ;
31
29
import java .net .InetAddress ;
32
30
import java .net .InetSocketAddress ;
35
33
import java .net .URL ;
36
34
import java .net .URLClassLoader ;
37
35
import java .net .UnknownHostException ;
36
+ import java .nio .ByteBuffer ;
37
+ import java .nio .charset .StandardCharsets ;
38
38
import java .nio .file .Files ;
39
39
import java .nio .file .Path ;
40
40
import java .nio .file .Paths ;
41
41
import java .nio .file .attribute .FileAttribute ;
42
42
import java .nio .channels .SocketChannel ;
43
+ import java .security .MessageDigest ;
44
+ import java .security .NoSuchAlgorithmException ;
43
45
import java .util .ArrayList ;
44
46
import java .util .Arrays ;
45
47
import java .util .Collection ;
@@ -127,9 +129,8 @@ public final class Utils {
127
129
*/
128
130
public static final String SEED_PROPERTY_NAME = "jdk.test.lib.random.seed" ;
129
131
130
- /* (non-javadoc)
131
- * Random generator with (or without) predefined seed. Depends on
132
- * "jdk.test.lib.random.seed" property value.
132
+ /**
133
+ * Random generator with predefined seed.
133
134
*/
134
135
private static volatile Random RANDOM_GENERATOR ;
135
136
@@ -141,7 +142,32 @@ public final class Utils {
141
142
/**
142
143
* Contains the seed value used for {@link java.util.Random} creation.
143
144
*/
144
- public static final long SEED = Long .getLong (SEED_PROPERTY_NAME , new Random ().nextLong ());
145
+ public static final long SEED ;
146
+ static {
147
+ var seed = Long .getLong (SEED_PROPERTY_NAME );
148
+ if (seed != null ) {
149
+ // use explicitly set seed
150
+ SEED = seed ;
151
+ } else {
152
+ var v = Runtime .version ();
153
+ // promotable builds have build number, and it's greater than 0
154
+ if (v .build ().orElse (0 ) > 0 ) {
155
+ // promotable build -> use 1st 8 bytes of md5($version)
156
+ try {
157
+ var md = MessageDigest .getInstance ("MD5" );
158
+ var bytes = v .toString ()
159
+ .getBytes (StandardCharsets .UTF_8 );
160
+ bytes = md .digest (bytes );
161
+ SEED = ByteBuffer .wrap (bytes ).getLong ();
162
+ } catch (NoSuchAlgorithmException e ) {
163
+ throw new Error (e );
164
+ }
165
+ } else {
166
+ // "personal" build -> use random seed
167
+ SEED = new Random ().nextLong ();
168
+ }
169
+ }
170
+ }
145
171
/**
146
172
* Returns the value of 'test.timeout.factor' system property
147
173
* converted to {@code double}.
@@ -531,9 +557,13 @@ public static byte[] toByteArray(String hex) {
531
557
532
558
/**
533
559
* Returns {@link java.util.Random} generator initialized with particular seed.
534
- * The seed could be provided via system property {@link Utils#SEED_PROPERTY_NAME}
535
- * In case no seed is provided, the method uses a random number.
560
+ * The seed could be provided via system property {@link Utils#SEED_PROPERTY_NAME}.
561
+ * In case no seed is provided and the build under test is "promotable"
562
+ * (its build number ({@code $BUILD} in {@link Runtime.Version}) is greater than 0,
563
+ * the seed based on string representation of {@link Runtime#version()} is used.
564
+ * Otherwise, the seed is randomly generated.
536
565
* The used seed printed to stdout.
566
+ *
537
567
* @return {@link java.util.Random} generator with particular seed.
538
568
*/
539
569
public static Random getRandomInstance () {
0 commit comments