Utils.java revision 2241:2d994eeff61a
1/*
2 * Copyright (c) 2013, 2016, 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
24package jdk.test.lib;
25
26import java.io.File;
27import java.io.IOException;
28import java.lang.reflect.Field;
29import java.net.InetAddress;
30import java.net.MalformedURLException;
31import java.net.ServerSocket;
32import java.net.URL;
33import java.net.URLClassLoader;
34import java.net.UnknownHostException;
35import java.nio.file.Files;
36import java.nio.file.Path;
37import java.nio.file.Paths;
38import java.util.ArrayList;
39import java.util.Arrays;
40import java.util.Collection;
41import java.util.Collections;
42import java.util.Iterator;
43import java.util.Map;
44import java.util.HashMap;
45import java.util.List;
46import java.util.Objects;
47import java.util.Random;
48import java.util.function.BooleanSupplier;
49import java.util.concurrent.TimeUnit;
50import java.util.function.Consumer;
51import java.util.function.Function;
52import java.util.regex.Matcher;
53import java.util.regex.Pattern;
54import jdk.internal.misc.Unsafe;
55
56import static jdk.test.lib.Asserts.assertTrue;
57import jdk.test.lib.process.ProcessTools;
58import jdk.test.lib.process.OutputAnalyzer;
59
60/**
61 * Common library for various test helper functions.
62 */
63public final class Utils {
64
65    /**
66     * Returns the value of 'test.class.path' system property.
67     */
68    public static final String TEST_CLASS_PATH = System.getProperty("test.class.path", ".");
69
70    /**
71     * Returns the sequence used by operating system to separate lines.
72     */
73    public static final String NEW_LINE = System.getProperty("line.separator");
74
75    /**
76     * Returns the value of 'test.vm.opts' system property.
77     */
78    public static final String VM_OPTIONS = System.getProperty("test.vm.opts", "").trim();
79
80    /**
81     * Returns the value of 'test.java.opts' system property.
82     */
83    public static final String JAVA_OPTIONS = System.getProperty("test.java.opts", "").trim();
84
85    /**
86     * Returns the value of 'test.src' system property.
87     */
88    public static final String TEST_SRC = System.getProperty("test.src", "").trim();
89
90    /*
91     * Returns the value of 'test.jdk' system property
92     */
93    public static final String TEST_JDK = System.getProperty("test.jdk");
94
95    /**
96     * Returns the value of 'test.classes' system property
97     */
98    public static final String TEST_CLASSES = System.getProperty("test.classes", ".");
99
100    private static Unsafe unsafe = null;
101
102    /**
103     * Defines property name for seed value.
104     */
105    public static final String SEED_PROPERTY_NAME = "jdk.test.lib.random.seed";
106
107    /* (non-javadoc)
108     * Random generator with (or without) predefined seed. Depends on
109     * "jdk.test.lib.random.seed" property value.
110     */
111    private static volatile Random RANDOM_GENERATOR;
112
113    /**
114     * Contains the seed value used for {@link java.util.Random} creation.
115     */
116    public static final long SEED = Long.getLong(SEED_PROPERTY_NAME, new Random().nextLong());
117    /**
118    * Returns the value of 'test.timeout.factor' system property
119    * converted to {@code double}.
120    */
121    public static final double TIMEOUT_FACTOR;
122    static {
123        String toFactor = System.getProperty("test.timeout.factor", "1.0");
124        TIMEOUT_FACTOR = Double.parseDouble(toFactor);
125    }
126
127    /**
128    * Returns the value of JTREG default test timeout in milliseconds
129    * converted to {@code long}.
130    */
131    public static final long DEFAULT_TEST_TIMEOUT = TimeUnit.SECONDS.toMillis(120);
132
133    private Utils() {
134        // Private constructor to prevent class instantiation
135    }
136
137    /**
138     * Returns the list of VM options.
139     *
140     * @return List of VM options
141     */
142    public static List<String> getVmOptions() {
143        return Arrays.asList(safeSplitString(VM_OPTIONS));
144    }
145
146    /**
147     * Returns the list of VM options with -J prefix.
148     *
149     * @return The list of VM options with -J prefix
150     */
151    public static List<String> getForwardVmOptions() {
152        String[] opts = safeSplitString(VM_OPTIONS);
153        for (int i = 0; i < opts.length; i++) {
154            opts[i] = "-J" + opts[i];
155        }
156        return Arrays.asList(opts);
157    }
158
159    /**
160     * Returns the default JTReg arguments for a jvm running a test.
161     * This is the combination of JTReg arguments test.vm.opts and test.java.opts.
162     * @return An array of options, or an empty array if no options.
163     */
164    public static String[] getTestJavaOpts() {
165        List<String> opts = new ArrayList<String>();
166        Collections.addAll(opts, safeSplitString(VM_OPTIONS));
167        Collections.addAll(opts, safeSplitString(JAVA_OPTIONS));
168        return opts.toArray(new String[0]);
169    }
170
171    /**
172     * Combines given arguments with default JTReg arguments for a jvm running a test.
173     * This is the combination of JTReg arguments test.vm.opts and test.java.opts
174     * @return The combination of JTReg test java options and user args.
175     */
176    public static String[] addTestJavaOpts(String... userArgs) {
177        List<String> opts = new ArrayList<String>();
178        Collections.addAll(opts, getTestJavaOpts());
179        Collections.addAll(opts, userArgs);
180        return opts.toArray(new String[0]);
181    }
182
183    /**
184     * Removes any options specifying which GC to use, for example "-XX:+UseG1GC".
185     * Removes any options matching: -XX:(+/-)Use*GC
186     * Used when a test need to set its own GC version. Then any
187     * GC specified by the framework must first be removed.
188     * @return A copy of given opts with all GC options removed.
189     */
190    private static final Pattern useGcPattern = Pattern.compile(
191            "(?:\\-XX\\:[\\+\\-]Use.+GC)"
192            + "|(?:\\-Xconcgc)");
193    public static List<String> removeGcOpts(List<String> opts) {
194        List<String> optsWithoutGC = new ArrayList<String>();
195        for (String opt : opts) {
196            if (useGcPattern.matcher(opt).matches()) {
197                System.out.println("removeGcOpts: removed " + opt);
198            } else {
199                optsWithoutGC.add(opt);
200            }
201        }
202        return optsWithoutGC;
203    }
204
205    /**
206     * Returns the default JTReg arguments for a jvm running a test without
207     * options that matches regular expressions in {@code filters}.
208     * This is the combination of JTReg arguments test.vm.opts and test.java.opts.
209     * @param filters Regular expressions used to filter out options.
210     * @return An array of options, or an empty array if no options.
211     */
212    public static String[] getFilteredTestJavaOpts(String... filters) {
213        String options[] = getTestJavaOpts();
214
215        if (filters.length == 0) {
216            return options;
217        }
218
219        List<String> filteredOptions = new ArrayList<String>(options.length);
220        Pattern patterns[] = new Pattern[filters.length];
221        for (int i = 0; i < filters.length; i++) {
222            patterns[i] = Pattern.compile(filters[i]);
223        }
224
225        for (String option : options) {
226            boolean matched = false;
227            for (int i = 0; i < patterns.length && !matched; i++) {
228                Matcher matcher = patterns[i].matcher(option);
229                matched = matcher.find();
230            }
231            if (!matched) {
232                filteredOptions.add(option);
233            }
234        }
235
236        return filteredOptions.toArray(new String[filteredOptions.size()]);
237    }
238
239    /**
240     * Splits a string by white space.
241     * Works like String.split(), but returns an empty array
242     * if the string is null or empty.
243     */
244    private static String[] safeSplitString(String s) {
245        if (s == null || s.trim().isEmpty()) {
246            return new String[] {};
247        }
248        return s.trim().split("\\s+");
249    }
250
251    /**
252     * @return The full command line for the ProcessBuilder.
253     */
254    public static String getCommandLine(ProcessBuilder pb) {
255        StringBuilder cmd = new StringBuilder();
256        for (String s : pb.command()) {
257            cmd.append(s).append(" ");
258        }
259        return cmd.toString();
260    }
261
262    /**
263     * Returns the free port on the local host.
264     * The function will spin until a valid port number is found.
265     *
266     * @return The port number
267     * @throws InterruptedException if any thread has interrupted the current thread
268     * @throws IOException if an I/O error occurs when opening the socket
269     */
270    public static int getFreePort() throws InterruptedException, IOException {
271        int port = -1;
272
273        while (port <= 0) {
274            Thread.sleep(100);
275
276            ServerSocket serverSocket = null;
277            try {
278                serverSocket = new ServerSocket(0);
279                port = serverSocket.getLocalPort();
280            } finally {
281                serverSocket.close();
282            }
283        }
284
285        return port;
286    }
287
288    /**
289     * Returns the name of the local host.
290     *
291     * @return The host name
292     * @throws UnknownHostException if IP address of a host could not be determined
293     */
294    public static String getHostname() throws UnknownHostException {
295        InetAddress inetAddress = InetAddress.getLocalHost();
296        String hostName = inetAddress.getHostName();
297
298        assertTrue((hostName != null && !hostName.isEmpty()),
299                "Cannot get hostname");
300
301        return hostName;
302    }
303
304    /**
305     * Uses "jcmd -l" to search for a jvm pid. This function will wait
306     * forever (until jtreg timeout) for the pid to be found.
307     * @param key Regular expression to search for
308     * @return The found pid.
309     */
310    public static int waitForJvmPid(String key) throws Throwable {
311        final long iterationSleepMillis = 250;
312        System.out.println("waitForJvmPid: Waiting for key '" + key + "'");
313        System.out.flush();
314        while (true) {
315            int pid = tryFindJvmPid(key);
316            if (pid >= 0) {
317                return pid;
318            }
319            Thread.sleep(iterationSleepMillis);
320        }
321    }
322
323    /**
324     * Searches for a jvm pid in the output from "jcmd -l".
325     *
326     * Example output from jcmd is:
327     * 12498 sun.tools.jcmd.JCmd -l
328     * 12254 /tmp/jdk8/tl/jdk/JTwork/classes/com/sun/tools/attach/Application.jar
329     *
330     * @param key A regular expression to search for.
331     * @return The found pid, or -1 if not found.
332     * @throws Exception If multiple matching jvms are found.
333     */
334    public static int tryFindJvmPid(String key) throws Throwable {
335        OutputAnalyzer output = null;
336        try {
337            JDKToolLauncher jcmdLauncher = JDKToolLauncher.create("jcmd");
338            jcmdLauncher.addToolArg("-l");
339            output = ProcessTools.executeProcess(jcmdLauncher.getCommand());
340            output.shouldHaveExitValue(0);
341
342            // Search for a line starting with numbers (pid), follwed by the key.
343            Pattern pattern = Pattern.compile("([0-9]+)\\s.*(" + key + ").*\\r?\\n");
344            Matcher matcher = pattern.matcher(output.getStdout());
345
346            int pid = -1;
347            if (matcher.find()) {
348                pid = Integer.parseInt(matcher.group(1));
349                System.out.println("findJvmPid.pid: " + pid);
350                if (matcher.find()) {
351                    throw new Exception("Found multiple JVM pids for key: " + key);
352                }
353            }
354            return pid;
355        } catch (Throwable t) {
356            System.out.println(String.format("Utils.findJvmPid(%s) failed: %s", key, t));
357            throw t;
358        }
359    }
360
361    /**
362     * Adjusts the provided timeout value for the TIMEOUT_FACTOR
363     * @param tOut the timeout value to be adjusted
364     * @return The timeout value adjusted for the value of "test.timeout.factor"
365     *         system property
366     */
367    public static long adjustTimeout(long tOut) {
368        return Math.round(tOut * Utils.TIMEOUT_FACTOR);
369    }
370
371    /**
372     * Return the contents of the named file as a single String,
373     * or null if not found.
374     * @param filename name of the file to read
375     * @return String contents of file, or null if file not found.
376     * @throws  IOException
377     *          if an I/O error occurs reading from the file or a malformed or
378     *          unmappable byte sequence is read
379     */
380    public static String fileAsString(String filename) throws IOException {
381        Path filePath = Paths.get(filename);
382        if (!Files.exists(filePath)) return null;
383        return new String(Files.readAllBytes(filePath));
384    }
385
386    /**
387     * @return Unsafe instance.
388     */
389    public static synchronized Unsafe getUnsafe() {
390        if (unsafe == null) {
391            try {
392                Field f = Unsafe.class.getDeclaredField("theUnsafe");
393                f.setAccessible(true);
394                unsafe = (Unsafe) f.get(null);
395            } catch (NoSuchFieldException | IllegalAccessException e) {
396                throw new RuntimeException("Unable to get Unsafe instance.", e);
397            }
398        }
399        return unsafe;
400    }
401    private static final char[] hexArray = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
402
403    /**
404     * Returns hex view of byte array
405     *
406     * @param bytes byte array to process
407     * @return Space separated hexadecimal string representation of bytes
408     */
409
410    public static String toHexString(byte[] bytes) {
411        char[] hexView = new char[bytes.length * 3];
412        int i = 0;
413        for (byte b : bytes) {
414            hexView[i++] = hexArray[(b >> 4) & 0x0F];
415            hexView[i++] = hexArray[b & 0x0F];
416            hexView[i++] = ' ';
417        }
418        return new String(hexView);
419    }
420
421    /**
422     * Returns {@link java.util.Random} generator initialized with particular seed.
423     * The seed could be provided via system property {@link Utils#SEED_PROPERTY_NAME}
424     * In case no seed is provided, the method uses a random number.
425     * The used seed printed to stdout.
426     * @return {@link java.util.Random} generator with particular seed.
427     */
428    public static Random getRandomInstance() {
429        if (RANDOM_GENERATOR == null) {
430            synchronized (Utils.class) {
431                if (RANDOM_GENERATOR == null) {
432                    RANDOM_GENERATOR = new Random(SEED);
433                    System.out.printf("For random generator using seed: %d%n", SEED);
434                    System.out.printf("To re-run test with same seed value please add \"-D%s=%d\" to command line.%n", SEED_PROPERTY_NAME, SEED);
435                }
436            }
437        }
438        return RANDOM_GENERATOR;
439    }
440
441    /**
442     * Returns random element of non empty collection
443     *
444     * @param <T> a type of collection element
445     * @param collection collection of elements
446     * @return random element of collection
447     * @throws IllegalArgumentException if collection is empty
448     */
449    public static <T> T getRandomElement(Collection<T> collection)
450            throws IllegalArgumentException {
451        if (collection.isEmpty()) {
452            throw new IllegalArgumentException("Empty collection");
453        }
454        Random random = getRandomInstance();
455        int elementIndex = 1 + random.nextInt(collection.size() - 1);
456        Iterator<T> iterator = collection.iterator();
457        while (--elementIndex != 0) {
458            iterator.next();
459        }
460        return iterator.next();
461    }
462
463    /**
464     * Returns random element of non empty array
465     *
466     * @param <T> a type of array element
467     * @param array array of elements
468     * @return random element of array
469     * @throws IllegalArgumentException if array is empty
470     */
471    public static <T> T getRandomElement(T[] array)
472            throws IllegalArgumentException {
473        if (array == null || array.length == 0) {
474            throw new IllegalArgumentException("Empty or null array");
475        }
476        Random random = getRandomInstance();
477        return array[random.nextInt(array.length)];
478    }
479
480    /**
481     * Wait for condition to be true
482     *
483     * @param condition, a condition to wait for
484     */
485    public static final void waitForCondition(BooleanSupplier condition) {
486        waitForCondition(condition, -1L, 100L);
487    }
488
489    /**
490     * Wait until timeout for condition to be true
491     *
492     * @param condition, a condition to wait for
493     * @param timeout a time in milliseconds to wait for condition to be true
494     * specifying -1 will wait forever
495     * @return condition value, to determine if wait was successful
496     */
497    public static final boolean waitForCondition(BooleanSupplier condition,
498            long timeout) {
499        return waitForCondition(condition, timeout, 100L);
500    }
501
502    /**
503     * Wait until timeout for condition to be true for specified time
504     *
505     * @param condition, a condition to wait for
506     * @param timeout a time in milliseconds to wait for condition to be true,
507     * specifying -1 will wait forever
508     * @param sleepTime a time to sleep value in milliseconds
509     * @return condition value, to determine if wait was successful
510     */
511    public static final boolean waitForCondition(BooleanSupplier condition,
512            long timeout, long sleepTime) {
513        long startTime = System.currentTimeMillis();
514        while (!(condition.getAsBoolean() || (timeout != -1L
515                && ((System.currentTimeMillis() - startTime) > timeout)))) {
516            try {
517                Thread.sleep(sleepTime);
518            } catch (InterruptedException e) {
519                Thread.currentThread().interrupt();
520                throw new Error(e);
521            }
522        }
523        return condition.getAsBoolean();
524    }
525
526    /**
527     * Interface same as java.lang.Runnable but with
528     * method {@code run()} able to throw any Throwable.
529     */
530    public static interface ThrowingRunnable {
531        void run() throws Throwable;
532    }
533
534    /**
535     * Filters out an exception that may be thrown by the given
536     * test according to the given filter.
537     *
538     * @param test - method that is invoked and checked for exception.
539     * @param filter - function that checks if the thrown exception matches
540     *                 criteria given in the filter's implementation.
541     * @return - exception that matches the filter if it has been thrown or
542     *           {@code null} otherwise.
543     * @throws Throwable - if test has thrown an exception that does not
544     *                     match the filter.
545     */
546    public static Throwable filterException(ThrowingRunnable test,
547            Function<Throwable, Boolean> filter) throws Throwable {
548        try {
549            test.run();
550        } catch (Throwable t) {
551            if (filter.apply(t)) {
552                return t;
553            } else {
554                throw t;
555            }
556        }
557        return null;
558    }
559
560    /**
561     * Ensures a requested class is loaded
562     * @param aClass class to load
563     */
564    public static void ensureClassIsLoaded(Class<?> aClass) {
565        if (aClass == null) {
566            throw new Error("Requested null class");
567        }
568        try {
569            Class.forName(aClass.getName(), /* initialize = */ true,
570                    ClassLoader.getSystemClassLoader());
571        } catch (ClassNotFoundException e) {
572            throw new Error("Class not found", e);
573        }
574    }
575    /**
576     * @param parent a class loader to be the parent for the returned one
577     * @return an UrlClassLoader with urls made of the 'test.class.path' jtreg
578     *         property and with the given parent
579     */
580    public static URLClassLoader getTestClassPathURLClassLoader(ClassLoader parent) {
581        URL[] urls = Arrays.stream(TEST_CLASS_PATH.split(File.pathSeparator))
582                .map(Paths::get)
583                .map(Path::toUri)
584                .map(x -> {
585                    try {
586                        return x.toURL();
587                    } catch (MalformedURLException ex) {
588                        throw new Error("Test issue. JTREG property"
589                                + " 'test.class.path'"
590                                + " is not defined correctly", ex);
591                    }
592                }).toArray(URL[]::new);
593        return new URLClassLoader(urls, parent);
594    }
595
596    /**
597     * Runs runnable and checks that it throws expected exception. If exceptionException is null it means
598     * that we expect no exception to be thrown.
599     * @param runnable what we run
600     * @param expectedException expected exception
601     */
602    public static void runAndCheckException(Runnable runnable, Class<? extends Throwable> expectedException) {
603        runAndCheckException(runnable, t -> {
604            if (t == null) {
605                if (expectedException != null) {
606                    throw new AssertionError("Didn't get expected exception " + expectedException.getSimpleName());
607                }
608            } else {
609                String message = "Got unexpected exception " + t.getClass().getSimpleName();
610                if (expectedException == null) {
611                    throw new AssertionError(message, t);
612                } else if (!expectedException.isAssignableFrom(t.getClass())) {
613                    message += " instead of " + expectedException.getSimpleName();
614                    throw new AssertionError(message, t);
615                }
616            }
617        });
618    }
619
620    /**
621     * Runs runnable and makes some checks to ensure that it throws expected exception.
622     * @param runnable what we run
623     * @param checkException a consumer which checks that we got expected exception and raises a new exception otherwise
624     */
625    public static void runAndCheckException(Runnable runnable, Consumer<Throwable> checkException) {
626        try {
627            runnable.run();
628            checkException.accept(null);
629        } catch (Throwable t) {
630            checkException.accept(t);
631        }
632    }
633
634    /**
635     * Converts to VM type signature
636     *
637     * @param type Java type to convert
638     * @return string representation of VM type
639     */
640    public static String toJVMTypeSignature(Class<?> type) {
641        if (type.isPrimitive()) {
642            if (type == boolean.class) {
643                return "Z";
644            } else if (type == byte.class) {
645                return "B";
646            } else if (type == char.class) {
647                return "C";
648            } else if (type == double.class) {
649                return "D";
650            } else if (type == float.class) {
651                return "F";
652            } else if (type == int.class) {
653                return "I";
654            } else if (type == long.class) {
655                return "J";
656            } else if (type == short.class) {
657                return "S";
658            } else if (type == void.class) {
659                return "V";
660            } else {
661                throw new Error("Unsupported type: " + type);
662            }
663        }
664        String result = type.getName().replaceAll("\\.", "/");
665        if (!type.isArray()) {
666            return "L" + result + ";";
667        }
668        return result;
669    }
670
671    public static Object[] getNullValues(Class<?>... types) {
672        Object[] result = new Object[types.length];
673        int i = 0;
674        for (Class<?> type : types) {
675            result[i++] = NULL_VALUES.get(type);
676        }
677        return result;
678    }
679    private static Map<Class<?>, Object> NULL_VALUES = new HashMap<>();
680    static {
681        NULL_VALUES.put(boolean.class, false);
682        NULL_VALUES.put(byte.class, (byte) 0);
683        NULL_VALUES.put(short.class, (short) 0);
684        NULL_VALUES.put(char.class, '\0');
685        NULL_VALUES.put(int.class, 0);
686        NULL_VALUES.put(long.class, 0L);
687        NULL_VALUES.put(float.class, 0.0f);
688        NULL_VALUES.put(double.class, 0.0d);
689    }
690
691    /**
692     * Returns mandatory property value
693     * @param propName is a name of property to request
694     * @return a String with requested property value
695     */
696    public static String getMandatoryProperty(String propName) {
697        Objects.requireNonNull(propName, "Requested null property");
698        String prop = System.getProperty(propName);
699        Objects.requireNonNull(prop,
700                String.format("A mandatory property '%s' isn't set", propName));
701        return prop;
702    }
703}
704
705