test/lib/jdk/test/lib/Utils.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58144 d003b3ef8b60
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
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
/*
53903
68bbd727dd5f 8219158: use 'test.root' property instead of traversing test-src path
iignatyev
parents: 51478
diff changeset
     2
 * Copyright (c) 2013, 2019, 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;
58144
d003b3ef8b60 8230854: Cleanup SuppressWarnings in test lib and remove noisy traces in StreamPumper
clanger
parents: 55199
diff changeset
    28
import java.lang.annotation.Annotation;
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    29
import java.net.InetAddress;
49255
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
    30
import java.net.InetSocketAddress;
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    31
import java.net.MalformedURLException;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    32
import java.net.ServerSocket;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    33
import java.net.URL;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    34
import java.net.URLClassLoader;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    35
import java.net.UnknownHostException;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    36
import java.nio.file.Files;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    37
import java.nio.file.Path;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    38
import java.nio.file.Paths;
50225
2942ae532175 8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents: 49255
diff changeset
    39
import java.nio.file.attribute.FileAttribute;
49255
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
    40
import java.nio.channels.SocketChannel;
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    41
import java.util.ArrayList;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    42
import java.util.Arrays;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    43
import java.util.Collection;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    44
import java.util.Collections;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    45
import java.util.Iterator;
40614
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
    46
import java.util.Map;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
    47
import java.util.HashMap;
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    48
import java.util.List;
40614
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
    49
import java.util.Objects;
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    50
import java.util.Random;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    51
import java.util.function.BooleanSupplier;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    52
import java.util.concurrent.TimeUnit;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    53
import java.util.function.Consumer;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    54
import java.util.function.Function;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    55
import java.util.regex.Matcher;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    56
import java.util.regex.Pattern;
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
import static jdk.test.lib.Asserts.assertTrue;
40614
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
    59
import jdk.test.lib.process.ProcessTools;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
    60
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
    61
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
 * 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
    64
 */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    65
public final class Utils {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    66
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    67
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    68
     * 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
    69
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    70
    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
    71
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
     * 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
    74
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    75
    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
    76
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
     * 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
    79
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    80
    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
    81
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
     * 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
    84
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    85
    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
    86
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    87
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    88
     * 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
    89
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
    90
    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
    91
53903
68bbd727dd5f 8219158: use 'test.root' property instead of traversing test-src path
iignatyev
parents: 51478
diff changeset
    92
    /**
68bbd727dd5f 8219158: use 'test.root' property instead of traversing test-src path
iignatyev
parents: 51478
diff changeset
    93
     * Returns the value of 'test.root' system property.
68bbd727dd5f 8219158: use 'test.root' property instead of traversing test-src path
iignatyev
parents: 51478
diff changeset
    94
     */
68bbd727dd5f 8219158: use 'test.root' property instead of traversing test-src path
iignatyev
parents: 51478
diff changeset
    95
    public static final String TEST_ROOT = System.getProperty("test.root", "").trim();
68bbd727dd5f 8219158: use 'test.root' property instead of traversing test-src path
iignatyev
parents: 51478
diff changeset
    96
40846
5c77538cd897 8164608: [TESTBUG] compiler/profiling tests fail to compile
dpochepk
parents: 40614
diff changeset
    97
    /*
5c77538cd897 8164608: [TESTBUG] compiler/profiling tests fail to compile
dpochepk
parents: 40614
diff changeset
    98
     * Returns the value of 'test.jdk' system property
5c77538cd897 8164608: [TESTBUG] compiler/profiling tests fail to compile
dpochepk
parents: 40614
diff changeset
    99
     */
5c77538cd897 8164608: [TESTBUG] compiler/profiling tests fail to compile
dpochepk
parents: 40614
diff changeset
   100
    public static final String TEST_JDK = System.getProperty("test.jdk");
5c77538cd897 8164608: [TESTBUG] compiler/profiling tests fail to compile
dpochepk
parents: 40614
diff changeset
   101
46213
02cb097768ec 8179516: add Utils.COMPILE_JDK constant
iignatyev
parents: 46207
diff changeset
   102
    /*
02cb097768ec 8179516: add Utils.COMPILE_JDK constant
iignatyev
parents: 46207
diff changeset
   103
     * Returns the value of 'compile.jdk' system property
02cb097768ec 8179516: add Utils.COMPILE_JDK constant
iignatyev
parents: 46207
diff changeset
   104
     */
53903
68bbd727dd5f 8219158: use 'test.root' property instead of traversing test-src path
iignatyev
parents: 51478
diff changeset
   105
    public static final String COMPILE_JDK = System.getProperty("compile.jdk", TEST_JDK);
46213
02cb097768ec 8179516: add Utils.COMPILE_JDK constant
iignatyev
parents: 46207
diff changeset
   106
40846
5c77538cd897 8164608: [TESTBUG] compiler/profiling tests fail to compile
dpochepk
parents: 40614
diff changeset
   107
    /**
5c77538cd897 8164608: [TESTBUG] compiler/profiling tests fail to compile
dpochepk
parents: 40614
diff changeset
   108
     * Returns the value of 'test.classes' system property
5c77538cd897 8164608: [TESTBUG] compiler/profiling tests fail to compile
dpochepk
parents: 40614
diff changeset
   109
     */
5c77538cd897 8164608: [TESTBUG] compiler/profiling tests fail to compile
dpochepk
parents: 40614
diff changeset
   110
    public static final String TEST_CLASSES = System.getProperty("test.classes", ".");
53903
68bbd727dd5f 8219158: use 'test.root' property instead of traversing test-src path
iignatyev
parents: 51478
diff changeset
   111
34597
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
     * 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
   114
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   115
    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
   116
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   117
    /* (non-javadoc)
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   118
     * 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
   119
     * "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
   120
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   121
    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
   122
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   123
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   124
     * 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
   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 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
   127
    /**
53903
68bbd727dd5f 8219158: use 'test.root' property instead of traversing test-src path
iignatyev
parents: 51478
diff changeset
   128
     * Returns the value of 'test.timeout.factor' system property
68bbd727dd5f 8219158: use 'test.root' property instead of traversing test-src path
iignatyev
parents: 51478
diff changeset
   129
     * converted to {@code double}.
68bbd727dd5f 8219158: use 'test.root' property instead of traversing test-src path
iignatyev
parents: 51478
diff changeset
   130
     */
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   131
    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
   132
    static {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   133
        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
   134
        TIMEOUT_FACTOR = Double.parseDouble(toFactor);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   135
    }
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
    /**
53903
68bbd727dd5f 8219158: use 'test.root' property instead of traversing test-src path
iignatyev
parents: 51478
diff changeset
   138
     * Returns the value of JTREG default test timeout in milliseconds
68bbd727dd5f 8219158: use 'test.root' property instead of traversing test-src path
iignatyev
parents: 51478
diff changeset
   139
     * converted to {@code long}.
68bbd727dd5f 8219158: use 'test.root' property instead of traversing test-src path
iignatyev
parents: 51478
diff changeset
   140
     */
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   141
    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
   142
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   143
    private Utils() {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   144
        // 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
   145
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   146
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   147
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   148
     * 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
   149
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   150
     * @return List of VM options
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   151
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   152
    public static List<String> getVmOptions() {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   153
        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
   154
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   155
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   156
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   157
     * 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
   158
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   159
     * @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
   160
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   161
    public static List<String> getForwardVmOptions() {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   162
        String[] opts = safeSplitString(VM_OPTIONS);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   163
        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
   164
            opts[i] = "-J" + opts[i];
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
        return Arrays.asList(opts);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   167
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   168
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   169
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   170
     * 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
   171
     * 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
   172
     * @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
   173
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   174
    public static String[] getTestJavaOpts() {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   175
        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
   176
        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
   177
        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
   178
        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
   179
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   180
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   181
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   182
     * 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
   183
     * 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
   184
     * @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
   185
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   186
    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
   187
        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
   188
        Collections.addAll(opts, getTestJavaOpts());
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   189
        Collections.addAll(opts, userArgs);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   190
        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
   191
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   192
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   193
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   194
     * 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
   195
     * 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
   196
     * 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
   197
     * 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
   198
     * @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
   199
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   200
    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
   201
            "(?:\\-XX\\:[\\+\\-]Use.+GC)"
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   202
            + "|(?:\\-Xconcgc)");
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   203
    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
   204
        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
   205
        for (String opt : opts) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   206
            if (useGcPattern.matcher(opt).matches()) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   207
                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
   208
            } else {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   209
                optsWithoutGC.add(opt);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   210
            }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   211
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   212
        return optsWithoutGC;
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
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   215
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   216
     * 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
   217
     * 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
   218
     * 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
   219
     * @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
   220
     * @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
   221
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   222
    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
   223
        String options[] = getTestJavaOpts();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   224
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   225
        if (filters.length == 0) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   226
            return options;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   227
        }
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
        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
   230
        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
   231
        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
   232
            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
   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
        for (String option : options) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   236
            boolean matched = false;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   237
            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
   238
                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
   239
                matched = matcher.find();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   240
            }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   241
            if (!matched) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   242
                filteredOptions.add(option);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   243
            }
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
        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
   247
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   248
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   249
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   250
     * 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
   251
     * 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
   252
     * 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
   253
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   254
    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
   255
        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
   256
            return new String[] {};
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
        return s.trim().split("\\s+");
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   259
    }
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
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   262
     * @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
   263
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   264
    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
   265
        StringBuilder cmd = new StringBuilder();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   266
        for (String s : pb.command()) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   267
            cmd.append(s).append(" ");
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   268
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   269
        return cmd.toString();
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
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   272
    /**
49255
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   273
     * Returns the socket address of an endpoint that refuses connections. The
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   274
     * endpoint is an InetSocketAddress where the address is the loopback address
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   275
     * and the port is a system port (1-1023 range).
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   276
     * This method is a better choice than getFreePort for tests that need
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   277
     * an endpoint that refuses connections.
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   278
     */
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   279
    public static InetSocketAddress refusingEndpoint() {
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   280
        InetAddress lb = InetAddress.getLoopbackAddress();
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   281
        int port = 1;
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   282
        while (port < 1024) {
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   283
            InetSocketAddress sa = new InetSocketAddress(lb, port);
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   284
            try {
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   285
                SocketChannel.open(sa).close();
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   286
            } catch (IOException ioe) {
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   287
                return sa;
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   288
            }
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   289
            port++;
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   290
        }
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   291
        throw new RuntimeException("Unable to find system port that is refusing connections");
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   292
    }
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   293
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   294
    /**
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   295
     * 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
   296
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   297
     * @return The port number
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   298
     * @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
   299
     */
49255
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   300
    public static int getFreePort() throws IOException {
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   301
        try (ServerSocket serverSocket =
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   302
                new ServerSocket(0, 5, InetAddress.getLoopbackAddress());) {
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 46237
diff changeset
   303
            return serverSocket.getLocalPort();
34597
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
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   306
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   307
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   308
     * 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
   309
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   310
     * @return The host name
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   311
     * @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
   312
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   313
    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
   314
        InetAddress inetAddress = InetAddress.getLocalHost();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   315
        String hostName = inetAddress.getHostName();
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
        assertTrue((hostName != null && !hostName.isEmpty()),
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   318
                "Cannot get hostname");
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   319
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   320
        return hostName;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   321
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   322
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   323
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   324
     * 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
   325
     * 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
   326
     * @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
   327
     * @return The found pid.
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 waitForJvmPid(String key) throws Throwable {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   330
        final long iterationSleepMillis = 250;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   331
        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
   332
        System.out.flush();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   333
        while (true) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   334
            int pid = tryFindJvmPid(key);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   335
            if (pid >= 0) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   336
                return pid;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   337
            }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   338
            Thread.sleep(iterationSleepMillis);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   339
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   340
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   341
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   342
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   343
     * 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
   344
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   345
     * Example output from jcmd is:
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   346
     * 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
   347
     * 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
   348
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   349
     * @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
   350
     * @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
   351
     * @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
   352
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   353
    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
   354
        OutputAnalyzer output = null;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   355
        try {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   356
            JDKToolLauncher jcmdLauncher = JDKToolLauncher.create("jcmd");
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   357
            jcmdLauncher.addToolArg("-l");
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   358
            output = ProcessTools.executeProcess(jcmdLauncher.getCommand());
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   359
            output.shouldHaveExitValue(0);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   360
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   361
            // 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
   362
            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
   363
            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
   364
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   365
            int pid = -1;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   366
            if (matcher.find()) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   367
                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
   368
                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
   369
                if (matcher.find()) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   370
                    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
   371
                }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   372
            }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   373
            return pid;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   374
        } catch (Throwable t) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   375
            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
   376
            throw t;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   377
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   378
    }
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
     * 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
   382
     * @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
   383
     * @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
   384
     *         system property
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
    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
   387
        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
   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
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   391
     * 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
   392
     * or null if not found.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   393
     * @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
   394
     * @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
   395
     * @throws  IOException
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   396
     *          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
   397
     *          unmappable byte sequence is read
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   398
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   399
    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
   400
        Path filePath = Paths.get(filename);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   401
        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
   402
        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
   403
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   404
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   405
    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
   406
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   407
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   408
     * Returns hex view of byte array
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   409
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   410
     * @param bytes byte array to process
50367
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   411
     * @return space separated hexadecimal string representation of bytes
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   412
     */
50367
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   413
     public static String toHexString(byte[] bytes) {
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   414
         char[] hexView = new char[bytes.length * 3 - 1];
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   415
         for (int i = 0; i < bytes.length - 1; i++) {
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   416
             hexView[i * 3] = hexArray[(bytes[i] >> 4) & 0x0F];
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   417
             hexView[i * 3 + 1] = hexArray[bytes[i] & 0x0F];
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   418
             hexView[i * 3 + 2] = ' ';
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   419
         }
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   420
         hexView[hexView.length - 2] = hexArray[(bytes[bytes.length - 1] >> 4) & 0x0F];
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   421
         hexView[hexView.length - 1] = hexArray[bytes[bytes.length - 1] & 0x0F];
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   422
         return new String(hexView);
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   423
     }
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   424
50367
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   425
     /**
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   426
      * Returns byte array of hex view
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   427
      *
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   428
      * @param hex hexadecimal string representation
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   429
      * @return byte array
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   430
      */
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   431
     public static byte[] toByteArray(String hex) {
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   432
         int length = hex.length();
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   433
         byte[] bytes = new byte[length / 2];
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   434
         for (int i = 0; i < length; i += 2) {
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   435
             bytes[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   436
                     + Character.digit(hex.charAt(i + 1), 16));
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   437
         }
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   438
         return bytes;
b73f9723aa54 8199388: Test development for ChaCha20 and Poly1305 algorithms
jjiang
parents: 50225
diff changeset
   439
     }
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   440
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
     * 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
   443
     * 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
   444
     * 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
   445
     * 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
   446
     * @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
   447
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   448
    public static Random getRandomInstance() {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   449
        if (RANDOM_GENERATOR == null) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   450
            synchronized (Utils.class) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   451
                if (RANDOM_GENERATOR == null) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   452
                    RANDOM_GENERATOR = new Random(SEED);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   453
                    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
   454
                    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
   455
                }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   456
            }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   457
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   458
        return RANDOM_GENERATOR;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   459
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   460
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   461
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   462
     * 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
   463
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   464
     * @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
   465
     * @param collection collection of elements
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   466
     * @return random element of collection
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   467
     * @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
   468
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   469
    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
   470
            throws IllegalArgumentException {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   471
        if (collection.isEmpty()) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   472
            throw new IllegalArgumentException("Empty collection");
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   473
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   474
        Random random = getRandomInstance();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   475
        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
   476
        Iterator<T> iterator = collection.iterator();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   477
        while (--elementIndex != 0) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   478
            iterator.next();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   479
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   480
        return iterator.next();
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
    /**
40614
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   484
     * Returns random element of non empty array
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   485
     *
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   486
     * @param <T> a type of array element
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   487
     * @param array array of elements
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   488
     * @return random element of array
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   489
     * @throws IllegalArgumentException if array is empty
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   490
     */
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   491
    public static <T> T getRandomElement(T[] array)
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   492
            throws IllegalArgumentException {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   493
        if (array == null || array.length == 0) {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   494
            throw new IllegalArgumentException("Empty or null array");
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   495
        }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   496
        Random random = getRandomInstance();
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   497
        return array[random.nextInt(array.length)];
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   498
    }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   499
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   500
    /**
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   501
     * 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
   502
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   503
     * @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
   504
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   505
    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
   506
        waitForCondition(condition, -1L, 100L);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   507
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   508
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
     * 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
   511
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   512
     * @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
   513
     * @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
   514
     * specifying -1 will wait forever
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   515
     * @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
   516
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   517
    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
   518
            long timeout) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   519
        return waitForCondition(condition, timeout, 100L);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   520
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   521
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   522
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   523
     * 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
   524
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   525
     * @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
   526
     * @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
   527
     * specifying -1 will wait forever
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   528
     * @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
   529
     * @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
   530
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   531
    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
   532
            long timeout, long sleepTime) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   533
        long startTime = System.currentTimeMillis();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   534
        while (!(condition.getAsBoolean() || (timeout != -1L
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   535
                && ((System.currentTimeMillis() - startTime) > timeout)))) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   536
            try {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   537
                Thread.sleep(sleepTime);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   538
            } catch (InterruptedException e) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   539
                Thread.currentThread().interrupt();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   540
                throw new Error(e);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   541
            }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   542
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   543
        return condition.getAsBoolean();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   544
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   545
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   546
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   547
     * 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
   548
     * 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
   549
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   550
    public static interface ThrowingRunnable {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   551
        void run() throws Throwable;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   552
    }
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
     * 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
   556
     * 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
   557
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   558
     * @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
   559
     * @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
   560
     *                 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
   561
     * @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
   562
     *           {@code null} otherwise.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   563
     * @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
   564
     *                     match the filter.
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   565
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   566
    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
   567
            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
   568
        try {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   569
            test.run();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   570
        } catch (Throwable t) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   571
            if (filter.apply(t)) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   572
                return t;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   573
            } else {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   574
                throw t;
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
        return null;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   578
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   579
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   580
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   581
     * 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
   582
     * @param aClass class to load
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   583
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   584
    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
   585
        if (aClass == null) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   586
            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
   587
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   588
        try {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   589
            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
   590
                    ClassLoader.getSystemClassLoader());
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   591
        } catch (ClassNotFoundException e) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   592
            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
   593
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   594
    }
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
     * @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
   597
     * @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
   598
     *         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
   599
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   600
    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
   601
        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
   602
                .map(Paths::get)
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   603
                .map(Path::toUri)
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   604
                .map(x -> {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   605
                    try {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   606
                        return x.toURL();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   607
                    } catch (MalformedURLException ex) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   608
                        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
   609
                                + " 'test.class.path'"
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   610
                                + " is not defined correctly", ex);
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
                }).toArray(URL[]::new);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   613
        return new URLClassLoader(urls, parent);
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
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
     * 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
   618
     * 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
   619
     * @param runnable what we run
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   620
     * @param expectedException expected exception
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   621
     */
51478
36c72eb60d5b 8209771: jdk.test.lib.Utils::runAndCheckException error
weijun
parents: 50367
diff changeset
   622
    public static void runAndCheckException(ThrowingRunnable runnable, Class<? extends Throwable> expectedException) {
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   623
        runAndCheckException(runnable, t -> {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   624
            if (t == null) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   625
                if (expectedException != null) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   626
                    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
   627
                }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   628
            } else {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   629
                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
   630
                if (expectedException == null) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   631
                    throw new AssertionError(message, t);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   632
                } 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
   633
                    message += " instead of " + expectedException.getSimpleName();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   634
                    throw new AssertionError(message, t);
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   635
                }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   636
            }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   637
        });
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   638
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   639
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   640
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   641
     * 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
   642
     * @param runnable what we run
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   643
     * @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
   644
     */
51478
36c72eb60d5b 8209771: jdk.test.lib.Utils::runAndCheckException error
weijun
parents: 50367
diff changeset
   645
    public static void runAndCheckException(ThrowingRunnable runnable, Consumer<Throwable> checkException) {
36c72eb60d5b 8209771: jdk.test.lib.Utils::runAndCheckException error
weijun
parents: 50367
diff changeset
   646
        Throwable throwable = null;
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   647
        try {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   648
            runnable.run();
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   649
        } catch (Throwable t) {
51478
36c72eb60d5b 8209771: jdk.test.lib.Utils::runAndCheckException error
weijun
parents: 50367
diff changeset
   650
            throwable = t;
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   651
        }
51478
36c72eb60d5b 8209771: jdk.test.lib.Utils::runAndCheckException error
weijun
parents: 50367
diff changeset
   652
        checkException.accept(throwable);
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   653
    }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   654
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   655
    /**
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   656
     * Converts to VM type signature
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   657
     *
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   658
     * @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
   659
     * @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
   660
     */
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   661
    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
   662
        if (type.isPrimitive()) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   663
            if (type == boolean.class) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   664
                return "Z";
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   665
            } else if (type == byte.class) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   666
                return "B";
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   667
            } else if (type == char.class) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   668
                return "C";
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   669
            } else if (type == double.class) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   670
                return "D";
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   671
            } else if (type == float.class) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   672
                return "F";
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   673
            } else if (type == int.class) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   674
                return "I";
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   675
            } else if (type == long.class) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   676
                return "J";
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   677
            } else if (type == short.class) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   678
                return "S";
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   679
            } else if (type == void.class) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   680
                return "V";
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   681
            } else {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   682
                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
   683
            }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   684
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   685
        String result = type.getName().replaceAll("\\.", "/");
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   686
        if (!type.isArray()) {
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   687
            return "L" + result + ";";
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   688
        }
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   689
        return result;
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   690
    }
40614
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   691
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   692
    public static Object[] getNullValues(Class<?>... types) {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   693
        Object[] result = new Object[types.length];
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   694
        int i = 0;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   695
        for (Class<?> type : types) {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   696
            result[i++] = NULL_VALUES.get(type);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   697
        }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   698
        return result;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   699
    }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   700
    private static Map<Class<?>, Object> NULL_VALUES = new HashMap<>();
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   701
    static {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   702
        NULL_VALUES.put(boolean.class, false);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   703
        NULL_VALUES.put(byte.class, (byte) 0);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   704
        NULL_VALUES.put(short.class, (short) 0);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   705
        NULL_VALUES.put(char.class, '\0');
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   706
        NULL_VALUES.put(int.class, 0);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   707
        NULL_VALUES.put(long.class, 0L);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   708
        NULL_VALUES.put(float.class, 0.0f);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   709
        NULL_VALUES.put(double.class, 0.0d);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   710
    }
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   711
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   712
    /**
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   713
     * Returns mandatory property value
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   714
     * @param propName is a name of property to request
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   715
     * @return a String with requested property value
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   716
     */
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   717
    public static String getMandatoryProperty(String propName) {
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   718
        Objects.requireNonNull(propName, "Requested null property");
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   719
        String prop = System.getProperty(propName);
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   720
        Objects.requireNonNull(prop,
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   721
                String.format("A mandatory property '%s' isn't set", propName));
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   722
        return prop;
b5d80754b40e 8157957: ClassNotFoundException: jdk.test.lib.JDKToolFinder
ctornqvi
parents: 36793
diff changeset
   723
    }
45876
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   724
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   725
    /*
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   726
     * Run uname with specified arguments.
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   727
     */
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   728
    public static OutputAnalyzer uname(String... args) throws Throwable {
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   729
        String[] cmds = new String[args.length + 1];
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   730
        cmds[0] = "uname";
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   731
        System.arraycopy(args, 0, cmds, 1, args.length);
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   732
        return ProcessTools.executeCommand(cmds);
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   733
    }
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   734
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   735
    /*
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   736
     * Returns the system distro.
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   737
     */
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   738
    public static String distro() {
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   739
        try {
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   740
            return uname("-v").asLines().get(0);
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   741
        } catch (Throwable t) {
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   742
            throw new RuntimeException("Failed to determine distro.", t);
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   743
        }
027bfd63b502 8177017: com/oracle/security/ucrypto/TestAES.java fails intermittently
mli
parents: 40849
diff changeset
   744
    }
46237
kvn
parents: 46213 45876
diff changeset
   745
46207
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   746
    // This method is intended to be called from a jtreg test.
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   747
    // It will identify the name of the test by means of stack walking.
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   748
    // It can handle both jtreg tests and a testng tests wrapped inside jtreg tests.
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   749
    // For jtreg tests the name of the test will be searched by stack-walking
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   750
    // until the method main() is found; the class containing that method is the
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   751
    // main test class and will be returned as the name of the test.
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   752
    // Special handling is used for testng tests.
58144
d003b3ef8b60 8230854: Cleanup SuppressWarnings in test lib and remove noisy traces in StreamPumper
clanger
parents: 55199
diff changeset
   753
    @SuppressWarnings("unchecked")
46207
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   754
    public static String getTestName() {
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   755
        String result = null;
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   756
        // If we are using testng, then we should be able to load the "Test" annotation.
58144
d003b3ef8b60 8230854: Cleanup SuppressWarnings in test lib and remove noisy traces in StreamPumper
clanger
parents: 55199
diff changeset
   757
        Class<? extends Annotation> testClassAnnotation;
46207
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   758
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   759
        try {
58144
d003b3ef8b60 8230854: Cleanup SuppressWarnings in test lib and remove noisy traces in StreamPumper
clanger
parents: 55199
diff changeset
   760
            testClassAnnotation = (Class<? extends Annotation>)Class.forName("org.testng.annotations.Test");
46207
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   761
        } catch (ClassNotFoundException e) {
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   762
            testClassAnnotation = null;
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   763
        }
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   764
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   765
        StackTraceElement[] elms = (new Throwable()).getStackTrace();
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   766
        for (StackTraceElement n: elms) {
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   767
            String className = n.getClassName();
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   768
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   769
            // If this is a "main" method, then use its class name, but only
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   770
            // if we are not using testng.
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   771
            if (testClassAnnotation == null && "main".equals(n.getMethodName())) {
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   772
                result = className;
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   773
                break;
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   774
            }
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   775
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   776
            // If this is a testng test, the test will have no "main" method. We can
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   777
            // detect a testng test class by looking for the org.testng.annotations.Test
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   778
            // annotation. If present, then use the name of this class.
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   779
            if (testClassAnnotation != null) {
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   780
                try {
58144
d003b3ef8b60 8230854: Cleanup SuppressWarnings in test lib and remove noisy traces in StreamPumper
clanger
parents: 55199
diff changeset
   781
                    Class<?> c = Class.forName(className);
46207
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   782
                    if (c.isAnnotationPresent(testClassAnnotation)) {
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   783
                        result = className;
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   784
                        break;
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   785
                    }
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   786
                } catch (ClassNotFoundException e) {
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   787
                    throw new RuntimeException("Unexpected exception: " + e, e);
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   788
                }
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   789
            }
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   790
        }
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   791
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   792
        if (result == null) {
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   793
            throw new RuntimeException("Couldn't find main test class in stack trace");
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   794
        }
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   795
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   796
        return result;
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   797
    }
7e77d58073d6 8177728: [TESTBUG] Improve CDS test utils
mseledtsov
parents: 40849
diff changeset
   798
50225
2942ae532175 8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents: 49255
diff changeset
   799
    /**
2942ae532175 8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents: 49255
diff changeset
   800
     * Creates an empty file in "user.dir" if the property set.
2942ae532175 8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents: 49255
diff changeset
   801
     * <p>
2942ae532175 8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents: 49255
diff changeset
   802
     * This method is meant as a replacement for {@code Files#createTempFile(String, String, FileAttribute...)}
2942ae532175 8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents: 49255
diff changeset
   803
     * that doesn't leave files behind in /tmp directory of the test machine
2942ae532175 8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents: 49255
diff changeset
   804
     * <p>
2942ae532175 8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents: 49255
diff changeset
   805
     * If the property "user.dir" is not set, "." will be used.
2942ae532175 8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents: 49255
diff changeset
   806
     *
2942ae532175 8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents: 49255
diff changeset
   807
     * @param prefix
2942ae532175 8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents: 49255
diff changeset
   808
     * @param suffix
2942ae532175 8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents: 49255
diff changeset
   809
     * @param attrs
2942ae532175 8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents: 49255
diff changeset
   810
     * @return the path to the newly created file that did not exist before this
2942ae532175 8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents: 49255
diff changeset
   811
     *         method was invoked
2942ae532175 8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents: 49255
diff changeset
   812
     * @throws IOException
2942ae532175 8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents: 49255
diff changeset
   813
     *
2942ae532175 8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents: 49255
diff changeset
   814
     * @see {@link Files#createTempFile(String, String, FileAttribute...)}
2942ae532175 8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents: 49255
diff changeset
   815
     */
2942ae532175 8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents: 49255
diff changeset
   816
    public static Path createTempFile(String prefix, String suffix, FileAttribute<?>... attrs) throws IOException {
2942ae532175 8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents: 49255
diff changeset
   817
        Path dir = Paths.get(System.getProperty("user.dir", "."));
2942ae532175 8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents: 49255
diff changeset
   818
        return Files.createTempFile(dir, prefix, suffix);
2942ae532175 8201331: Better test cleanup for jdk/jfr/api/consumer/TestRecordingFile.java
egahlin
parents: 49255
diff changeset
   819
    }
55199
3a09397c147e 8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp
mseledtsov
parents: 53903
diff changeset
   820
3a09397c147e 8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp
mseledtsov
parents: 53903
diff changeset
   821
    /**
3a09397c147e 8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp
mseledtsov
parents: 53903
diff changeset
   822
     * Creates an empty directory in "user.dir" or "."
3a09397c147e 8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp
mseledtsov
parents: 53903
diff changeset
   823
     * <p>
3a09397c147e 8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp
mseledtsov
parents: 53903
diff changeset
   824
     * This method is meant as a replacement for {@code Files#createTempDirectory(String, String, FileAttribute...)}
3a09397c147e 8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp
mseledtsov
parents: 53903
diff changeset
   825
     * that doesn't leave files behind in /tmp directory of the test machine
3a09397c147e 8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp
mseledtsov
parents: 53903
diff changeset
   826
     * <p>
3a09397c147e 8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp
mseledtsov
parents: 53903
diff changeset
   827
     * If the property "user.dir" is not set, "." will be used.
3a09397c147e 8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp
mseledtsov
parents: 53903
diff changeset
   828
     *
3a09397c147e 8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp
mseledtsov
parents: 53903
diff changeset
   829
     * @param prefix
3a09397c147e 8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp
mseledtsov
parents: 53903
diff changeset
   830
     * @param attrs
3a09397c147e 8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp
mseledtsov
parents: 53903
diff changeset
   831
     * @return the path to the newly created directory
3a09397c147e 8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp
mseledtsov
parents: 53903
diff changeset
   832
     * @throws IOException
3a09397c147e 8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp
mseledtsov
parents: 53903
diff changeset
   833
     *
3a09397c147e 8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp
mseledtsov
parents: 53903
diff changeset
   834
     * @see {@link Files#createTempDirectory(String, String, FileAttribute...)}
3a09397c147e 8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp
mseledtsov
parents: 53903
diff changeset
   835
     */
3a09397c147e 8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp
mseledtsov
parents: 53903
diff changeset
   836
    public static Path createTempDirectory(String prefix, FileAttribute<?>... attrs) throws IOException {
3a09397c147e 8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp
mseledtsov
parents: 53903
diff changeset
   837
        Path dir = Paths.get(System.getProperty("user.dir", "."));
3a09397c147e 8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp
mseledtsov
parents: 53903
diff changeset
   838
        return Files.createTempDirectory(dir, prefix);
3a09397c147e 8223396: [TESTBUG] several jfr tests do not clean up files created in /tmp
mseledtsov
parents: 53903
diff changeset
   839
    }
34597
e5f2344c6fab 8141526: Allow to collect stdout/stderr from the FinalizationRunner even before the process returns
jbachorik
parents:
diff changeset
   840
}