20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 package com.oracle.java.testlibrary;
25
26 import static com.oracle.java.testlibrary.Asserts.assertTrue;
27 import java.io.BufferedReader;
28 import java.io.File;
29 import java.io.FileReader;
30 import java.io.IOException;
31 import java.lang.reflect.Field;
32 import java.net.InetAddress;
33 import java.net.ServerSocket;
34 import java.net.UnknownHostException;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.Collections;
38 import java.util.List;
39 import java.util.Random;
40 import java.util.regex.Matcher;
41 import java.util.regex.Pattern;
42 import sun.misc.Unsafe;
43
44 /**
45 * Common library for various test helper functions.
46 */
47 public final class Utils {
48
49 /**
50 * Returns the sequence used by operating system to separate lines.
51 */
52 public static final String NEW_LINE = System.getProperty("line.separator");
53
54 /**
55 * Returns the value of 'test.vm.opts'system property.
56 */
57 public static final String VM_OPTIONS = System.getProperty("test.vm.opts", "").trim();
58
59 /**
71 /* (non-javadoc)
72 * Random generator with (or without) predefined seed. Depends on
73 * "com.oracle.java.testlibrary.random.seed" property value.
74 */
75 private static volatile Random RANDOM_GENERATOR;
76
77 /**
78 * Contains the seed value used for {@link java.util.Random} creation.
79 */
80 public static final long SEED = Long.getLong(SEED_PROPERTY_NAME, new Random().nextLong());
81 /**
82 * Returns the value of 'test.timeout.factor' system property
83 * converted to {@code double}.
84 */
85 public static final double TIMEOUT_FACTOR;
86 static {
87 String toFactor = System.getProperty("test.timeout.factor", "1.0");
88 TIMEOUT_FACTOR = Double.parseDouble(toFactor);
89 }
90
91 private Utils() {
92 // Private constructor to prevent class instantiation
93 }
94
95 /**
96 * Returns the list of VM options.
97 *
98 * @return List of VM options
99 */
100 public static List<String> getVmOptions() {
101 return Arrays.asList(safeSplitString(VM_OPTIONS));
102 }
103
104 /**
105 * Returns the list of VM options with -J prefix.
106 *
107 * @return The list of VM options with -J prefix
108 */
109 public static List<String> getForwardVmOptions() {
110 String[] opts = safeSplitString(VM_OPTIONS);
349 }
350
351 /**
352 * Returns {@link java.util.Random} generator initialized with particular seed.
353 * The seed could be provided via system property {@link Utils#SEED_PROPERTY_NAME}
354 * In case no seed is provided, the method uses a random number.
355 * The used seed printed to stdout.
356 * @return {@link java.util.Random} generator with particular seed.
357 */
358 public static Random getRandomInstance() {
359 if (RANDOM_GENERATOR == null) {
360 synchronized (Utils.class) {
361 if (RANDOM_GENERATOR == null) {
362 RANDOM_GENERATOR = new Random(SEED);
363 System.out.printf("For random generator using seed: %d%n", SEED);
364 System.out.printf("To re-run test with same seed value please add \"-D%s=%d\" to command line.%n", SEED_PROPERTY_NAME, SEED);
365 }
366 }
367 }
368 return RANDOM_GENERATOR;
369 }
370 }
|
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 package com.oracle.java.testlibrary;
25
26 import static com.oracle.java.testlibrary.Asserts.assertTrue;
27 import java.io.BufferedReader;
28 import java.io.File;
29 import java.io.FileReader;
30 import java.io.IOException;
31 import java.lang.reflect.Field;
32 import java.net.InetAddress;
33 import java.net.ServerSocket;
34 import java.net.UnknownHostException;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.Collections;
38 import java.util.List;
39 import java.util.Random;
40 import java.util.function.BooleanSupplier;
41 import java.util.regex.Matcher;
42 import java.util.regex.Pattern;
43 import sun.misc.Unsafe;
44
45 /**
46 * Common library for various test helper functions.
47 */
48 public final class Utils {
49
50 /**
51 * Returns the sequence used by operating system to separate lines.
52 */
53 public static final String NEW_LINE = System.getProperty("line.separator");
54
55 /**
56 * Returns the value of 'test.vm.opts'system property.
57 */
58 public static final String VM_OPTIONS = System.getProperty("test.vm.opts", "").trim();
59
60 /**
72 /* (non-javadoc)
73 * Random generator with (or without) predefined seed. Depends on
74 * "com.oracle.java.testlibrary.random.seed" property value.
75 */
76 private static volatile Random RANDOM_GENERATOR;
77
78 /**
79 * Contains the seed value used for {@link java.util.Random} creation.
80 */
81 public static final long SEED = Long.getLong(SEED_PROPERTY_NAME, new Random().nextLong());
82 /**
83 * Returns the value of 'test.timeout.factor' system property
84 * converted to {@code double}.
85 */
86 public static final double TIMEOUT_FACTOR;
87 static {
88 String toFactor = System.getProperty("test.timeout.factor", "1.0");
89 TIMEOUT_FACTOR = Double.parseDouble(toFactor);
90 }
91
92 /**
93 * Returns the value of 'test.iterations.count' system property
94 * converted to {@code int}.
95 */
96 public static final int ITERATIONS_COUNT =
97 Integer.getInteger("com.oracle.java.testlibrary.iterations", 1);
98
99 private Utils() {
100 // Private constructor to prevent class instantiation
101 }
102
103 /**
104 * Returns the list of VM options.
105 *
106 * @return List of VM options
107 */
108 public static List<String> getVmOptions() {
109 return Arrays.asList(safeSplitString(VM_OPTIONS));
110 }
111
112 /**
113 * Returns the list of VM options with -J prefix.
114 *
115 * @return The list of VM options with -J prefix
116 */
117 public static List<String> getForwardVmOptions() {
118 String[] opts = safeSplitString(VM_OPTIONS);
357 }
358
359 /**
360 * Returns {@link java.util.Random} generator initialized with particular seed.
361 * The seed could be provided via system property {@link Utils#SEED_PROPERTY_NAME}
362 * In case no seed is provided, the method uses a random number.
363 * The used seed printed to stdout.
364 * @return {@link java.util.Random} generator with particular seed.
365 */
366 public static Random getRandomInstance() {
367 if (RANDOM_GENERATOR == null) {
368 synchronized (Utils.class) {
369 if (RANDOM_GENERATOR == null) {
370 RANDOM_GENERATOR = new Random(SEED);
371 System.out.printf("For random generator using seed: %d%n", SEED);
372 System.out.printf("To re-run test with same seed value please add \"-D%s=%d\" to command line.%n", SEED_PROPERTY_NAME, SEED);
373 }
374 }
375 }
376 return RANDOM_GENERATOR;
377 }
378
379 /**
380 * Wait for condition to be true
381 *
382 * @param condition, a condition to wait for, using default sleep time 100
383 */
384 public static final void waitForCondition(BooleanSupplier condition) {
385 waitForCondition(condition, 100);
386 }
387
388 /**
389 * Wait until timeout for condition to be true
390 *
391 * @param condition, a condition to wait for
392 * @param sleepTime a time to sleep value in milliseconds
393 */
394 public static final void waitForCondition(BooleanSupplier condition,
395 long sleepTime) {
396 while (!condition.getAsBoolean()) {
397 try {
398 Thread.sleep(sleepTime);
399 } catch (InterruptedException e) {
400 Thread.currentThread().interrupt();
401 throw new Error(e);
402 }
403 }
404 }
405 }
|