test/lib/jdk/test/lib/Utils.java
author mli
Mon, 17 Jul 2017 23:11:46 -0700
changeset 45876 027bfd63b502
parent 40849 e7794c8f668b
child 46237 3aeb6cbc0ccc
permissions -rw-r--r--
8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently Summary: Skips CFB128 related cases on Solaris pre-11.3 versions. Reviewed-by: valeriep Contributed-by: John Jiang <sha.jiang@oracle.com>
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
/*
45876
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
     2
 * Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.
34597
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.net.InetAddress;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    29
import java.net.MalformedURLException;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    30
import java.net.ServerSocket;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    31
import java.net.URL;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    32
import java.net.URLClassLoader;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    33
import java.net.UnknownHostException;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    34
import java.nio.file.Files;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    35
import java.nio.file.Path;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    36
import java.nio.file.Paths;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    37
import java.util.ArrayList;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    38
import java.util.Arrays;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    39
import java.util.Collection;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    40
import java.util.Collections;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    41
import java.util.Iterator;
40614
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
    42
import java.util.Map;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
    43
import java.util.HashMap;
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    44
import java.util.List;
40614
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
    45
import java.util.Objects;
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    46
import java.util.Random;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    47
import java.util.function.BooleanSupplier;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    48
import java.util.concurrent.TimeUnit;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    49
import java.util.function.Consumer;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    50
import java.util.function.Function;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    51
import java.util.regex.Matcher;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    52
import java.util.regex.Pattern;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    53
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;
40614
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
    55
import jdk.test.lib.process.ProcessTools;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
    56
import jdk.test.lib.process.OutputAnalyzer;
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    57
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
 * 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
    60
 */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    61
public final class Utils {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    62
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
     * 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
    65
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    66
    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
    67
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
     * 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
    70
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    71
    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
    72
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
     * 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
    75
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    76
    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
    77
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
     * 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
    80
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    81
    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
    82
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
     * 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
    85
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    86
    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
    87
40846
5c77538cd897 8164608: [TESTBUG] compiler/profiling tests fail to compile
dpochepk
parents: 40614
diff changeset
    88
    /*
5c77538cd897 8164608: [TESTBUG] compiler/profiling tests fail to compile
dpochepk
parents: 40614
diff changeset
    89
     * Returns the value of 'test.jdk' system property
5c77538cd897 8164608: [TESTBUG] compiler/profiling tests fail to compile
dpochepk
parents: 40614
diff changeset
    90
     */
5c77538cd897 8164608: [TESTBUG] compiler/profiling tests fail to compile
dpochepk
parents: 40614
diff changeset
    91
    public static final String TEST_JDK = System.getProperty("test.jdk");
5c77538cd897 8164608: [TESTBUG] compiler/profiling tests fail to compile
dpochepk
parents: 40614
diff changeset
    92
5c77538cd897 8164608: [TESTBUG] compiler/profiling tests fail to compile
dpochepk
parents: 40614
diff changeset
    93
    /**
5c77538cd897 8164608: [TESTBUG] compiler/profiling tests fail to compile
dpochepk
parents: 40614
diff changeset
    94
     * Returns the value of 'test.classes' system property
5c77538cd897 8164608: [TESTBUG] compiler/profiling tests fail to compile
dpochepk
parents: 40614
diff changeset
    95
     */
5c77538cd897 8164608: [TESTBUG] compiler/profiling tests fail to compile
dpochepk
parents: 40614
diff changeset
    96
    public static final String TEST_CLASSES = System.getProperty("test.classes", ".");
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    97
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    98
     * 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
    99
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   100
    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
   101
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   102
    /* (non-javadoc)
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   103
     * 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
   104
     * "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
   105
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   106
    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
   107
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   108
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   109
     * 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
   110
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   111
    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
   112
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   113
    * 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
   114
    * converted to {@code double}.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   115
    */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   116
    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
   117
    static {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   118
        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
   119
        TIMEOUT_FACTOR = Double.parseDouble(toFactor);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   120
    }
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
    * 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
   124
    * converted to {@code long}.
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
    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
   127
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   128
    private Utils() {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   129
        // 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
   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.
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 List of VM options
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> getVmOptions() {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   138
        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
   139
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   140
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
     * 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
   143
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   144
     * @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
   145
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   146
    public static List<String> getForwardVmOptions() {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   147
        String[] opts = safeSplitString(VM_OPTIONS);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   148
        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
   149
            opts[i] = "-J" + opts[i];
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   150
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   151
        return Arrays.asList(opts);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   152
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   153
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   154
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   155
     * 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
   156
     * 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
   157
     * @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
   158
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   159
    public static String[] getTestJavaOpts() {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   160
        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
   161
        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
   162
        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
   163
        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
   164
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   165
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   166
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   167
     * 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
   168
     * 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
   169
     * @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
   170
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   171
    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
   172
        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
   173
        Collections.addAll(opts, getTestJavaOpts());
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   174
        Collections.addAll(opts, userArgs);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   175
        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
   176
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   177
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   178
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   179
     * 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
   180
     * 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
   181
     * 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
   182
     * 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
   183
     * @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
   184
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   185
    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
   186
            "(?:\\-XX\\:[\\+\\-]Use.+GC)"
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   187
            + "|(?:\\-Xconcgc)");
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   188
    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
   189
        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
   190
        for (String opt : opts) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   191
            if (useGcPattern.matcher(opt).matches()) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   192
                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
   193
            } else {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   194
                optsWithoutGC.add(opt);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   195
            }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   196
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   197
        return optsWithoutGC;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   198
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   199
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
     * 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
   202
     * 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
   203
     * 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
   204
     * @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
   205
     * @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
   206
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   207
    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
   208
        String options[] = getTestJavaOpts();
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
        if (filters.length == 0) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   211
            return options;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   212
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   213
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   214
        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
   215
        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
   216
        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
   217
            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
   218
        }
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
        for (String option : options) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   221
            boolean matched = false;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   222
            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
   223
                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
   224
                matched = matcher.find();
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
            if (!matched) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   227
                filteredOptions.add(option);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   228
            }
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
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   231
        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
   232
    }
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
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   235
     * 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
   236
     * 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
   237
     * 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
   238
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   239
    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
   240
        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
   241
            return new String[] {};
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   242
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   243
        return s.trim().split("\\s+");
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
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
     * @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
   248
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   249
    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
   250
        StringBuilder cmd = new StringBuilder();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   251
        for (String s : pb.command()) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   252
            cmd.append(s).append(" ");
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   253
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   254
        return cmd.toString();
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
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   257
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   258
     * 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
   259
     * 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
   260
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   261
     * @return The port number
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   262
     * @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
   263
     * @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
   264
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   265
    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
   266
        int port = -1;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   267
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   268
        while (port <= 0) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   269
            Thread.sleep(100);
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
            ServerSocket serverSocket = null;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   272
            try {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   273
                serverSocket = new ServerSocket(0);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   274
                port = serverSocket.getLocalPort();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   275
            } finally {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   276
                serverSocket.close();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   277
            }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   278
        }
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
        return port;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   281
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   282
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
     * 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
   285
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   286
     * @return The host name
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   287
     * @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
   288
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   289
    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
   290
        InetAddress inetAddress = InetAddress.getLocalHost();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   291
        String hostName = inetAddress.getHostName();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   292
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   293
        assertTrue((hostName != null && !hostName.isEmpty()),
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   294
                "Cannot get hostname");
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
        return hostName;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   297
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   298
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   299
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   300
     * 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
   301
     * 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
   302
     * @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
   303
     * @return The found 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
    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
   306
        final long iterationSleepMillis = 250;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   307
        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
   308
        System.out.flush();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   309
        while (true) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   310
            int pid = tryFindJvmPid(key);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   311
            if (pid >= 0) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   312
                return pid;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   313
            }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   314
            Thread.sleep(iterationSleepMillis);
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
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   317
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   318
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   319
     * 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
   320
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   321
     * Example output from jcmd is:
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   322
     * 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
   323
     * 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
   324
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   325
     * @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
   326
     * @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
   327
     * @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
   328
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   329
    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
   330
        OutputAnalyzer output = null;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   331
        try {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   332
            JDKToolLauncher jcmdLauncher = JDKToolLauncher.create("jcmd");
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   333
            jcmdLauncher.addToolArg("-l");
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   334
            output = ProcessTools.executeProcess(jcmdLauncher.getCommand());
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   335
            output.shouldHaveExitValue(0);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   336
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   337
            // 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
   338
            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
   339
            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
   340
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   341
            int pid = -1;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   342
            if (matcher.find()) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   343
                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
   344
                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
   345
                if (matcher.find()) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   346
                    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
   347
                }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   348
            }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   349
            return pid;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   350
        } catch (Throwable t) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   351
            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
   352
            throw t;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   353
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   354
    }
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
     * 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
   358
     * @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
   359
     * @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
   360
     *         system property
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   361
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   362
    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
   363
        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
   364
    }
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
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   367
     * 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
   368
     * or null if not found.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   369
     * @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
   370
     * @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
   371
     * @throws  IOException
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   372
     *          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
   373
     *          unmappable byte sequence is read
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 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
   376
        Path filePath = Paths.get(filename);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   377
        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
   378
        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
   379
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   380
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   381
    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
   382
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
     * 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
   385
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   386
     * @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
   387
     * @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
   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
    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
   391
        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
   392
        int i = 0;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   393
        for (byte b : bytes) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   394
            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
   395
            hexView[i++] = hexArray[b & 0x0F];
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   396
            hexView[i++] = ' ';
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   397
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   398
        return new String(hexView);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   399
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   400
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   401
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   402
     * 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
   403
     * 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
   404
     * 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
   405
     * 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
   406
     * @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
   407
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   408
    public static Random getRandomInstance() {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   409
        if (RANDOM_GENERATOR == null) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   410
            synchronized (Utils.class) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   411
                if (RANDOM_GENERATOR == null) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   412
                    RANDOM_GENERATOR = new Random(SEED);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   413
                    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
   414
                    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
   415
                }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   416
            }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   417
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   418
        return RANDOM_GENERATOR;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   419
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   420
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
     * 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
   423
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   424
     * @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
   425
     * @param collection collection of elements
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   426
     * @return random element of collection
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   427
     * @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
   428
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   429
    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
   430
            throws IllegalArgumentException {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   431
        if (collection.isEmpty()) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   432
            throw new IllegalArgumentException("Empty collection");
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   433
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   434
        Random random = getRandomInstance();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   435
        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
   436
        Iterator<T> iterator = collection.iterator();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   437
        while (--elementIndex != 0) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   438
            iterator.next();
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
        return iterator.next();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   441
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   442
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   443
    /**
40614
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   444
     * Returns random element of non empty array
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   445
     *
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   446
     * @param <T> a type of array element
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   447
     * @param array array of elements
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   448
     * @return random element of array
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   449
     * @throws IllegalArgumentException if array is empty
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   450
     */
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   451
    public static <T> T getRandomElement(T[] array)
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   452
            throws IllegalArgumentException {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   453
        if (array == null || array.length == 0) {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   454
            throw new IllegalArgumentException("Empty or null array");
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   455
        }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   456
        Random random = getRandomInstance();
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   457
        return array[random.nextInt(array.length)];
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   458
    }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   459
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   460
    /**
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   461
     * 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
   462
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   463
     * @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
   464
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   465
    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
   466
        waitForCondition(condition, -1L, 100L);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   467
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   468
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
     * 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
   471
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   472
     * @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
   473
     * @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
   474
     * specifying -1 will wait forever
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   475
     * @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
   476
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   477
    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
   478
            long timeout) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   479
        return waitForCondition(condition, timeout, 100L);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   480
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   481
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   482
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   483
     * 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
   484
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   485
     * @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
   486
     * @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
   487
     * specifying -1 will wait forever
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   488
     * @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
   489
     * @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
   490
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   491
    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
   492
            long timeout, long sleepTime) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   493
        long startTime = System.currentTimeMillis();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   494
        while (!(condition.getAsBoolean() || (timeout != -1L
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   495
                && ((System.currentTimeMillis() - startTime) > timeout)))) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   496
            try {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   497
                Thread.sleep(sleepTime);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   498
            } catch (InterruptedException e) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   499
                Thread.currentThread().interrupt();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   500
                throw new Error(e);
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
        return condition.getAsBoolean();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   504
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   505
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
     * 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
   508
     * 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
   509
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   510
    public static interface ThrowingRunnable {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   511
        void run() throws Throwable;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   512
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   513
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
     * 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
   516
     * 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
   517
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   518
     * @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
   519
     * @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
   520
     *                 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
   521
     * @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
   522
     *           {@code null} otherwise.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   523
     * @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
   524
     *                     match the filter.
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
    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
   527
            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
   528
        try {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   529
            test.run();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   530
        } catch (Throwable t) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   531
            if (filter.apply(t)) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   532
                return t;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   533
            } else {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   534
                throw t;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   535
            }
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
        return null;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   538
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   539
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   540
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   541
     * 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
   542
     * @param aClass class to load
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
    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
   545
        if (aClass == null) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   546
            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
   547
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   548
        try {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   549
            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
   550
                    ClassLoader.getSystemClassLoader());
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   551
        } catch (ClassNotFoundException e) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   552
            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
   553
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   554
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   555
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   556
     * @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
   557
     * @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
   558
     *         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
   559
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   560
    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
   561
        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
   562
                .map(Paths::get)
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   563
                .map(Path::toUri)
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   564
                .map(x -> {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   565
                    try {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   566
                        return x.toURL();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   567
                    } catch (MalformedURLException ex) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   568
                        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
   569
                                + " 'test.class.path'"
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   570
                                + " is not defined correctly", ex);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   571
                    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   572
                }).toArray(URL[]::new);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   573
        return new URLClassLoader(urls, parent);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   574
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   575
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
     * 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
   578
     * 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
   579
     * @param runnable what we run
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   580
     * @param expectedException expected exception
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   581
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   582
    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
   583
        runAndCheckException(runnable, t -> {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   584
            if (t == null) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   585
                if (expectedException != null) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   586
                    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
   587
                }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   588
            } else {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   589
                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
   590
                if (expectedException == null) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   591
                    throw new AssertionError(message, t);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   592
                } 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
   593
                    message += " instead of " + expectedException.getSimpleName();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   594
                    throw new AssertionError(message, t);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   595
                }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   596
            }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   597
        });
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   598
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   599
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
     * 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
   602
     * @param runnable what we run
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   603
     * @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
   604
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   605
    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
   606
        try {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   607
            runnable.run();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   608
            checkException.accept(null);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   609
        } catch (Throwable t) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   610
            checkException.accept(t);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   611
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   612
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   613
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   614
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   615
     * Converts to VM type signature
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   616
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   617
     * @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
   618
     * @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
   619
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   620
    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
   621
        if (type.isPrimitive()) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   622
            if (type == boolean.class) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   623
                return "Z";
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   624
            } else if (type == byte.class) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   625
                return "B";
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   626
            } else if (type == char.class) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   627
                return "C";
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   628
            } else if (type == double.class) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   629
                return "D";
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   630
            } else if (type == float.class) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   631
                return "F";
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   632
            } else if (type == int.class) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   633
                return "I";
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   634
            } else if (type == long.class) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   635
                return "J";
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   636
            } else if (type == short.class) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   637
                return "S";
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   638
            } else if (type == void.class) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   639
                return "V";
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   640
            } else {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   641
                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
   642
            }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   643
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   644
        String result = type.getName().replaceAll("\\.", "/");
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   645
        if (!type.isArray()) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   646
            return "L" + result + ";";
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   647
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   648
        return result;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   649
    }
40614
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   650
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   651
    public static Object[] getNullValues(Class<?>... types) {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   652
        Object[] result = new Object[types.length];
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   653
        int i = 0;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   654
        for (Class<?> type : types) {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   655
            result[i++] = NULL_VALUES.get(type);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   656
        }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   657
        return result;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   658
    }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   659
    private static Map<Class<?>, Object> NULL_VALUES = new HashMap<>();
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   660
    static {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   661
        NULL_VALUES.put(boolean.class, false);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   662
        NULL_VALUES.put(byte.class, (byte) 0);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   663
        NULL_VALUES.put(short.class, (short) 0);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   664
        NULL_VALUES.put(char.class, '\0');
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   665
        NULL_VALUES.put(int.class, 0);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   666
        NULL_VALUES.put(long.class, 0L);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   667
        NULL_VALUES.put(float.class, 0.0f);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   668
        NULL_VALUES.put(double.class, 0.0d);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   669
    }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   670
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   671
    /**
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   672
     * Returns mandatory property value
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   673
     * @param propName is a name of property to request
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   674
     * @return a String with requested property value
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   675
     */
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   676
    public static String getMandatoryProperty(String propName) {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   677
        Objects.requireNonNull(propName, "Requested null property");
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   678
        String prop = System.getProperty(propName);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   679
        Objects.requireNonNull(prop,
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   680
                String.format("A mandatory property '%s' isn't set", propName));
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   681
        return prop;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   682
    }
45876
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   683
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   684
    /*
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   685
     * Run uname with specified arguments.
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   686
     */
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   687
    public static OutputAnalyzer uname(String... args) throws Throwable {
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   688
        String[] cmds = new String[args.length + 1];
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   689
        cmds[0] = "uname";
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   690
        System.arraycopy(args, 0, cmds, 1, args.length);
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   691
        return ProcessTools.executeCommand(cmds);
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   692
    }
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   693
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   694
    /*
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   695
     * Returns the system distro.
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   696
     */
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   697
    public static String distro() {
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   698
        try {
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   699
            return uname("-v").asLines().get(0);
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   700
        } catch (Throwable t) {
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   701
            throw new RuntimeException("Failed to determine distro.", t);
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   702
        }
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   703
    }
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   704
}
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   705