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