hotspot/test/testlibrary/com/oracle/java/testlibrary/Utils.java
author pchelko
Wed, 23 Apr 2014 17:56:05 +0400
changeset 24529 c580bcb3aabc
parent 23501 ba4a33b2a569
child 24003 372cef87666e
permissions -rw-r--r--
8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order Reviewed-by: anthony, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23175
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
     1
/*
23501
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
     2
 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
23175
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
     4
 *
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
     7
 * published by the Free Software Foundation.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
     8
 *
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    13
 * accompanied this code).
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    14
 *
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    18
 *
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    21
 * questions.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    22
 */
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    23
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    24
package com.oracle.java.testlibrary;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    25
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    26
import static com.oracle.java.testlibrary.Asserts.assertTrue;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    27
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    28
import java.io.BufferedReader;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    29
import java.io.File;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    30
import java.io.FileReader;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    31
import java.io.IOException;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    32
import java.net.InetAddress;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    33
import java.net.ServerSocket;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    34
import java.net.UnknownHostException;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    35
import java.util.ArrayList;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    36
import java.util.List;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    37
import java.util.Arrays;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    38
import java.util.Collections;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    39
import java.util.regex.Pattern;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    40
import java.util.regex.Matcher;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    41
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    42
/**
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    43
 * Common library for various test helper functions.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    44
 */
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    45
public final class Utils {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    46
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    47
    /**
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    48
     * Returns the sequence used by operating system to separate lines.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    49
     */
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    50
    public static final String NEW_LINE = System.getProperty("line.separator");
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    51
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    52
    /**
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    53
     * Returns the value of 'test.vm.opts'system property.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    54
     */
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    55
    public static final String VM_OPTIONS = System.getProperty("test.vm.opts", "").trim();
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    56
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    57
    /**
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    58
     * Returns the value of 'test.java.opts'system property.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    59
     */
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    60
    public static final String JAVA_OPTIONS = System.getProperty("test.java.opts", "").trim();
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    61
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    62
    /**
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    63
    * Returns the value of 'test.timeout.factor' system property
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    64
    * converted to {@code double}.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    65
    */
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    66
    public static final double TIMEOUT_FACTOR;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    67
    static {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    68
        String toFactor = System.getProperty("test.timeout.factor", "1.0");
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    69
        TIMEOUT_FACTOR = Double.parseDouble(toFactor);
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    70
    }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    71
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    72
    private Utils() {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    73
        // Private constructor to prevent class instantiation
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    74
    }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    75
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    76
    /**
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    77
     * Returns the list of VM options.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    78
     *
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    79
     * @return List of VM options
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    80
     */
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    81
    public static List<String> getVmOptions() {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    82
        return Arrays.asList(safeSplitString(VM_OPTIONS));
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    83
    }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    84
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    85
    /**
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    86
     * Returns the list of VM options with -J prefix.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    87
     *
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    88
     * @return The list of VM options with -J prefix
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    89
     */
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    90
    public static List<String> getForwardVmOptions() {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    91
        String[] opts = safeSplitString(VM_OPTIONS);
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    92
        for (int i = 0; i < opts.length; i++) {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    93
            opts[i] = "-J" + opts[i];
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    94
        }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    95
        return Arrays.asList(opts);
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    96
    }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    97
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    98
    /**
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
    99
     * Returns the default JTReg arguments for a jvm running a test.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   100
     * This is the combination of JTReg arguments test.vm.opts and test.java.opts.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   101
     * @return An array of options, or an empty array if no opptions.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   102
     */
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   103
    public static String[] getTestJavaOpts() {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   104
        List<String> opts = new ArrayList<String>();
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   105
        Collections.addAll(opts, safeSplitString(VM_OPTIONS));
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   106
        Collections.addAll(opts, safeSplitString(JAVA_OPTIONS));
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   107
        return opts.toArray(new String[0]);
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   108
    }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   109
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   110
    /**
23501
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   111
     * Returns the default JTReg arguments for a jvm running a test without
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   112
     * options that matches regular expresions in {@code filters}.
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   113
     * This is the combination of JTReg arguments test.vm.opts and test.java.opts.
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   114
     * @param filters Regular expressions used to filter out options.
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   115
     * @return An array of options, or an empty array if no opptions.
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   116
     */
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   117
    public static String[] getFilteredTestJavaOpts(String... filters) {
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   118
        String options[] = getTestJavaOpts();
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   119
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   120
        if (filters.length == 0) {
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   121
            return options;
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   122
        }
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   123
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   124
        List<String> filteredOptions = new ArrayList<String>(options.length);
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   125
        Pattern patterns[] = new Pattern[filters.length];
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   126
        for (int i = 0; i < filters.length; i++) {
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   127
            patterns[i] = Pattern.compile(filters[i]);
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   128
        }
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   129
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   130
        for (String option : options) {
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   131
            boolean matched = false;
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   132
            for (int i = 0; i < patterns.length && !matched; i++) {
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   133
                Matcher matcher = patterns[i].matcher(option);
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   134
                matched = matcher.find();
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   135
            }
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   136
            if (!matched) {
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   137
                filteredOptions.add(option);
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   138
            }
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   139
        }
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   140
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   141
        return filteredOptions.toArray(new String[filteredOptions.size()]);
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   142
    }
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   143
ba4a33b2a569 8038193: Add command line option tests for BMI options
iignatyev
parents: 23175
diff changeset
   144
    /**
23175
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   145
     * Combines given arguments with default JTReg arguments for a jvm running a test.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   146
     * This is the combination of JTReg arguments test.vm.opts and test.java.opts
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   147
     * @return The combination of JTReg test java options and user args.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   148
     */
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   149
    public static String[] addTestJavaOpts(String... userArgs) {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   150
        List<String> opts = new ArrayList<String>();
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   151
        Collections.addAll(opts, getTestJavaOpts());
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   152
        Collections.addAll(opts, userArgs);
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   153
        return opts.toArray(new String[0]);
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   154
    }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   155
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   156
    /**
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   157
     * Splits a string by white space.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   158
     * Works like String.split(), but returns an empty array
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   159
     * if the string is null or empty.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   160
     */
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   161
    private static String[] safeSplitString(String s) {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   162
        if (s == null || s.trim().isEmpty()) {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   163
            return new String[] {};
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   164
        }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   165
        return s.trim().split("\\s+");
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   166
    }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   167
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   168
    /**
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   169
     * @return The full command line for the ProcessBuilder.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   170
     */
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   171
    public static String getCommandLine(ProcessBuilder pb) {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   172
        StringBuilder cmd = new StringBuilder();
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   173
        for (String s : pb.command()) {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   174
            cmd.append(s).append(" ");
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   175
        }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   176
        return cmd.toString();
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   177
    }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   178
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   179
    /**
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   180
     * Returns the free port on the local host.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   181
     * The function will spin until a valid port number is found.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   182
     *
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   183
     * @return The port number
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   184
     * @throws InterruptedException if any thread has interrupted the current thread
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   185
     * @throws IOException if an I/O error occurs when opening the socket
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   186
     */
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   187
    public static int getFreePort() throws InterruptedException, IOException {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   188
        int port = -1;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   189
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   190
        while (port <= 0) {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   191
            Thread.sleep(100);
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   192
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   193
            ServerSocket serverSocket = null;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   194
            try {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   195
                serverSocket = new ServerSocket(0);
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   196
                port = serverSocket.getLocalPort();
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   197
            } finally {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   198
                serverSocket.close();
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   199
            }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   200
        }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   201
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   202
        return port;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   203
    }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   204
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   205
    /**
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   206
     * Returns the name of the local host.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   207
     *
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   208
     * @return The host name
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   209
     * @throws UnknownHostException if IP address of a host could not be determined
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   210
     */
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   211
    public static String getHostname() throws UnknownHostException {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   212
        InetAddress inetAddress = InetAddress.getLocalHost();
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   213
        String hostName = inetAddress.getHostName();
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   214
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   215
        assertTrue((hostName != null && !hostName.isEmpty()),
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   216
                "Cannot get hostname");
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   217
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   218
        return hostName;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   219
    }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   220
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   221
    /**
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   222
     * Uses "jcmd -l" to search for a jvm pid. This function will wait
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   223
     * forever (until jtreg timeout) for the pid to be found.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   224
     * @param key Regular expression to search for
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   225
     * @return The found pid.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   226
     */
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   227
    public static int waitForJvmPid(String key) throws Throwable {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   228
        final long iterationSleepMillis = 250;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   229
        System.out.println("waitForJvmPid: Waiting for key '" + key + "'");
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   230
        System.out.flush();
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   231
        while (true) {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   232
            int pid = tryFindJvmPid(key);
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   233
            if (pid >= 0) {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   234
                return pid;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   235
            }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   236
            Thread.sleep(iterationSleepMillis);
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   237
        }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   238
    }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   239
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   240
    /**
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   241
     * Searches for a jvm pid in the output from "jcmd -l".
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   242
     *
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   243
     * Example output from jcmd is:
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   244
     * 12498 sun.tools.jcmd.JCmd -l
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   245
     * 12254 /tmp/jdk8/tl/jdk/JTwork/classes/com/sun/tools/attach/Application.jar
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   246
     *
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   247
     * @param key A regular expression to search for.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   248
     * @return The found pid, or -1 if Enot found.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   249
     * @throws Exception If multiple matching jvms are found.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   250
     */
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   251
    public static int tryFindJvmPid(String key) throws Throwable {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   252
        OutputAnalyzer output = null;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   253
        try {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   254
            JDKToolLauncher jcmdLauncher = JDKToolLauncher.create("jcmd");
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   255
            jcmdLauncher.addToolArg("-l");
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   256
            output = ProcessTools.executeProcess(jcmdLauncher.getCommand());
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   257
            output.shouldHaveExitValue(0);
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   258
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   259
            // Search for a line starting with numbers (pid), follwed by the key.
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   260
            Pattern pattern = Pattern.compile("([0-9]+)\\s.*(" + key + ").*\\r?\\n");
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   261
            Matcher matcher = pattern.matcher(output.getStdout());
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   262
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   263
            int pid = -1;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   264
            if (matcher.find()) {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   265
                pid = Integer.parseInt(matcher.group(1));
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   266
                System.out.println("findJvmPid.pid: " + pid);
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   267
                if (matcher.find()) {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   268
                    throw new Exception("Found multiple JVM pids for key: " + key);
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   269
                }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   270
            }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   271
            return pid;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   272
        } catch (Throwable t) {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   273
            System.out.println(String.format("Utils.findJvmPid(%s) failed: %s", key, t));
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   274
            throw t;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   275
        }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   276
    }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   277
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   278
    /**
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   279
     * Returns file content as a list of strings
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   280
     *
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   281
     * @param file File to operate on
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   282
     * @return List of strings
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   283
     * @throws IOException
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   284
     */
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   285
    public static List<String> fileAsList(File file) throws IOException {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   286
        assertTrue(file.exists() && file.isFile(),
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   287
                file.getAbsolutePath() + " does not exist or not a file");
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   288
        List<String> output = new ArrayList<>();
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   289
        try (BufferedReader reader = new BufferedReader(new FileReader(file.getAbsolutePath()))) {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   290
            while (reader.ready()) {
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   291
                output.add(reader.readLine().replace(NEW_LINE, ""));
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   292
            }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   293
        }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   294
        return output;
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   295
    }
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   296
892220ab2ae1 8035150: ShouldNotReachHere() in ConstantPool::copy_entry_to
sla
parents:
diff changeset
   297
}