test/jdk/sun/tools/jcmd/TestProcessHelper.java
author ysuenaga
Wed, 13 Nov 2019 10:27:06 +0900
changeset 59045 846fee5ea75e
parent 58265 577e17cab93f
child 58679 9c3209ff7550
permissions -rw-r--r--
8233785: Incorrect JDK version is reported in hs_err log Reviewed-by: dholmes, stuefe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
     1
/*
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
     4
 *
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
     8
 *
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    13
 * accompanied this code).
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    14
 *
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    18
 *
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    21
 * questions.
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    22
 */
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    23
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    24
import java.io.File;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    25
import java.io.IOException;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    26
import java.io.OutputStream;
58265
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
    27
import java.lang.invoke.MethodHandle;
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
    28
import java.lang.invoke.MethodHandles;
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    29
import java.lang.module.ModuleDescriptor;
58265
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
    30
import java.lang.reflect.Method;
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    31
import java.nio.file.FileSystems;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    32
import java.nio.file.Files;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    33
import java.nio.file.Path;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    34
import java.util.ArrayList;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    35
import java.util.LinkedList;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    36
import java.util.List;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    37
import java.util.jar.Attributes;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    38
import java.util.jar.JarEntry;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    39
import java.util.jar.JarOutputStream;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    40
import java.util.jar.Manifest;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    41
import java.util.stream.Collectors;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    42
import java.util.stream.Stream;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    43
58265
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
    44
import jdk.internal.module.ModuleInfoWriter;
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
    45
import jdk.test.lib.JDKToolFinder;
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
    46
import jdk.test.lib.process.ProcessTools;
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
    47
import jdk.test.lib.util.JarUtils;
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
    48
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    49
/*
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    50
 * @test
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    51
 * @bug 8205654
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    52
 * @summary Unit test for sun.tools.ProcessHelper class. The test launches Java processes with different Java options
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    53
 * and checks that sun.tools.ProcessHelper.getMainClass(pid) method returns a correct main class.                                                                                                                               return a .
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    54
 *
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    55
 * @requires os.family == "linux"
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    56
 * @library /test/lib
58265
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
    57
 * @modules jdk.jcmd/sun.tools.common:+open
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    58
 *          java.base/jdk.internal.module
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    59
 * @build test.TestProcess
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    60
 * @run main/othervm TestProcessHelper
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    61
 */
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    62
public class TestProcessHelper {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    63
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    64
    private static final String TEST_PROCESS_MAIN_CLASS_NAME = "TestProcess";
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    65
    private static final String TEST_PROCESS_MAIN_CLASS_PACKAGE = "test";
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    66
    private static final String TEST_PROCESS_MAIN_CLASS = TEST_PROCESS_MAIN_CLASS_PACKAGE + "."
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    67
            + TEST_PROCESS_MAIN_CLASS_NAME;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    68
    private static final Path TEST_CLASSES = FileSystems.getDefault().getPath(System.getProperty("test.classes"));
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    69
    private static final Path USER_DIR = FileSystems.getDefault().getPath(System.getProperty("user.dir", "."));
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    70
    private static final Path TEST_MODULES = USER_DIR.resolve("testmodules");
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    71
    private static final String JAVA_PATH = JDKToolFinder.getJDKTool("java");
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    72
    private static final Path TEST_CLASS = TEST_CLASSES.resolve(TEST_PROCESS_MAIN_CLASS_PACKAGE)
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    73
            .resolve(TEST_PROCESS_MAIN_CLASS_NAME + ".class");
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    74
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    75
    private static final String[] CP_OPTIONS = {"-cp", "-classpath", "--class-path"};
55386
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
    76
    private static final String[][] VM_ARGS = {{}, {"-Dtest1=aaa"}, {"-Dtest1=aaa", "-Dtest2=bbb ccc"}};
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    77
    private static final String[][] ARGS = {{}, {"param1"}, {"param1", "param2"}};
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    78
    private static final String[] MP_OPTIONS = {"-p", "--module-path"};
55386
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
    79
    private static final String[] MODULE_OPTIONS = {"-m", "--module", "--module="};
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    80
    private static final String JAR_OPTION = "-jar";
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    81
    private static final String MODULE_NAME = "module1";
55386
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
    82
    private static final String[][] EXTRA_MODULAR_OPTIONS = {null,
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
    83
            {"--add-opens", "java.base/java.net=ALL-UNNAMED"},
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
    84
            {"--add-exports", "java.base/java.net=ALL-UNNAMED"},
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
    85
            {"--add-reads", "java.base/java.net=ALL-UNNAMED"},
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
    86
            {"--add-modules", "java.management"},
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
    87
            {"--limit-modules", "java.management"},
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
    88
            {"--upgrade-module-path", "test"}};
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    89
55386
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
    90
    private static final String[] PATCH_MODULE_OPTIONS = {"--patch-module", null};
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    91
58265
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
    92
    private static final MethodHandle MH_GET_MAIN_CLASS = resolveMainClassMH();
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
    93
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
    94
    private static MethodHandle resolveMainClassMH() {
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
    95
        try {
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
    96
            Method getMainClassMethod = Class
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
    97
                .forName("sun.tools.common.ProcessHelper")
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
    98
                .getDeclaredMethod("getMainClass", String.class);
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
    99
            getMainClassMethod.setAccessible(true);
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
   100
            return MethodHandles.lookup().unreflect(getMainClassMethod);
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
   101
        } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException e) {
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
   102
            throw new RuntimeException(e);
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
   103
        }
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
   104
    }
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
   105
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
   106
    private static String callGetMainClass(Process p) {
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
   107
        try {
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
   108
            return (String)MH_GET_MAIN_CLASS.invoke(Long.toString(p.pid()));
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
   109
        } catch (Throwable e) {
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
   110
            throw new RuntimeException(e);
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
   111
        }
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
   112
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
   113
    }
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
   114
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   115
    public static void main(String[] args) throws Exception {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   116
        new TestProcessHelper().runTests();
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   117
    }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   118
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   119
    public void runTests() throws Exception {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   120
        testClassPath();
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   121
        testJar();
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   122
        testModule();
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   123
    }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   124
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   125
    // Test Java processes that are started with -classpath, -cp, or --class-path options
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   126
    // and with different combinations of VM and program args.
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   127
    private void testClassPath() throws Exception {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   128
        for (String cp : CP_OPTIONS) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   129
            for (String[] vma : VM_ARGS) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   130
                for (String[] arg : ARGS) {
55386
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   131
                    for (String[] modularOptions : EXTRA_MODULAR_OPTIONS) {
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   132
                        List<String> cmd = new LinkedList<>();
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   133
                        cmd.add(JAVA_PATH);
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   134
                        cmd.add(cp);
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   135
                        cmd.add(TEST_CLASSES.toAbsolutePath().toString());
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   136
                        for (String v : vma) {
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   137
                            cmd.add(v);
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   138
                        }
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   139
                        if (modularOptions != null) {
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   140
                            cmd.add(modularOptions[0]);
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   141
                            cmd.add(modularOptions[1]);
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   142
                        }
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   143
                        cmd.add(TEST_PROCESS_MAIN_CLASS);
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   144
                        for (String a : arg) {
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   145
                            cmd.add(a);
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   146
                        }
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   147
                        testProcessHelper(cmd, TEST_PROCESS_MAIN_CLASS);
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   148
                    }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   149
                }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   150
            }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   151
        }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   152
    }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   153
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   154
    // Test Java processes that are started with -jar option
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   155
    // and with different combinations of VM and program args.
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   156
    private void testJar() throws Exception {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   157
        File jarFile = prepareJar();
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   158
        for (String[] vma : VM_ARGS) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   159
            for (String[] arg : ARGS) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   160
                List<String> cmd = new LinkedList<>();
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   161
                cmd.add(JAVA_PATH);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   162
                for (String v : vma) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   163
                    cmd.add(v);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   164
                }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   165
                cmd.add(JAR_OPTION);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   166
                cmd.add(jarFile.getAbsolutePath());
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   167
                for (String a : arg) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   168
                    cmd.add(a);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   169
                }
54460
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents: 53707
diff changeset
   170
                testProcessHelper(cmd, jarFile.getAbsolutePath());
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   171
            }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   172
        }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   173
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   174
    }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   175
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   176
    // Test Java processes that are started with -m or --module options
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   177
    // and with different combination of VM and program args.
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   178
    private void testModule() throws Exception {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   179
        prepareModule();
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   180
        for (String mp : MP_OPTIONS) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   181
            for (String m : MODULE_OPTIONS) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   182
                for (String[] vma : VM_ARGS) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   183
                    for (String[] arg : ARGS) {
55386
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   184
                        for(String patchModuleOption : PATCH_MODULE_OPTIONS) {
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   185
                            List<String> cmd = new LinkedList<>();
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   186
                            cmd.add(JAVA_PATH);
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   187
                            cmd.add(mp);
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   188
                            cmd.add(TEST_MODULES.toAbsolutePath().toString());
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   189
                            if (patchModuleOption != null) {
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   190
                                cmd.add(patchModuleOption);
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   191
                                cmd.add(MODULE_NAME + "=" + TEST_MODULES.toAbsolutePath().toString());
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   192
                            }
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   193
                            for (String v : vma) {
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   194
                                cmd.add(v);
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   195
                            }
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   196
                            if (m.endsWith("=")) {
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   197
                                cmd.add(m + MODULE_NAME + "/" + TEST_PROCESS_MAIN_CLASS);
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   198
                            } else {
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   199
                                cmd.add(m);
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   200
                                cmd.add(MODULE_NAME + "/" + TEST_PROCESS_MAIN_CLASS);
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   201
                            }
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   202
                            for (String a : arg) {
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   203
                                cmd.add(a);
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   204
                            }
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   205
                            testProcessHelper(cmd, MODULE_NAME + "/" + TEST_PROCESS_MAIN_CLASS);
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   206
                        }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   207
                    }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   208
                }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   209
            }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   210
        }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   211
    }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   212
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   213
    private void checkMainClass(Process p, String expectedMainClass) {
58265
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
   214
        String mainClass = callGetMainClass(p);
58143
b35771556cd0 8230850: Test sun/tools/jcmd/TestProcessHelper.java fails intermittently
clanger
parents: 55386
diff changeset
   215
        // getMainClass() may return null, e.g. due to timing issues.
b35771556cd0 8230850: Test sun/tools/jcmd/TestProcessHelper.java fails intermittently
clanger
parents: 55386
diff changeset
   216
        // Attempt some limited retries.
b35771556cd0 8230850: Test sun/tools/jcmd/TestProcessHelper.java fails intermittently
clanger
parents: 55386
diff changeset
   217
        if (mainClass == null) {
b35771556cd0 8230850: Test sun/tools/jcmd/TestProcessHelper.java fails intermittently
clanger
parents: 55386
diff changeset
   218
            System.err.println("Main class returned by ProcessHelper was null.");
b35771556cd0 8230850: Test sun/tools/jcmd/TestProcessHelper.java fails intermittently
clanger
parents: 55386
diff changeset
   219
            // sleep time doubles each round, altogether, wait no longer than 1 sec
b35771556cd0 8230850: Test sun/tools/jcmd/TestProcessHelper.java fails intermittently
clanger
parents: 55386
diff changeset
   220
            final int MAX_RETRIES = 10;
b35771556cd0 8230850: Test sun/tools/jcmd/TestProcessHelper.java fails intermittently
clanger
parents: 55386
diff changeset
   221
            int retrycount = 0;
b35771556cd0 8230850: Test sun/tools/jcmd/TestProcessHelper.java fails intermittently
clanger
parents: 55386
diff changeset
   222
            long sleepms = 1;
b35771556cd0 8230850: Test sun/tools/jcmd/TestProcessHelper.java fails intermittently
clanger
parents: 55386
diff changeset
   223
            while (retrycount < MAX_RETRIES && mainClass == null) {
b35771556cd0 8230850: Test sun/tools/jcmd/TestProcessHelper.java fails intermittently
clanger
parents: 55386
diff changeset
   224
                System.err.println("Retry " + retrycount + ", sleeping for " + sleepms + "ms.");
b35771556cd0 8230850: Test sun/tools/jcmd/TestProcessHelper.java fails intermittently
clanger
parents: 55386
diff changeset
   225
                try {
b35771556cd0 8230850: Test sun/tools/jcmd/TestProcessHelper.java fails intermittently
clanger
parents: 55386
diff changeset
   226
                    Thread.sleep(sleepms);
b35771556cd0 8230850: Test sun/tools/jcmd/TestProcessHelper.java fails intermittently
clanger
parents: 55386
diff changeset
   227
                } catch (InterruptedException e) {
b35771556cd0 8230850: Test sun/tools/jcmd/TestProcessHelper.java fails intermittently
clanger
parents: 55386
diff changeset
   228
                    // ignore
b35771556cd0 8230850: Test sun/tools/jcmd/TestProcessHelper.java fails intermittently
clanger
parents: 55386
diff changeset
   229
                }
58265
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 58143
diff changeset
   230
                mainClass = callGetMainClass(p);
58143
b35771556cd0 8230850: Test sun/tools/jcmd/TestProcessHelper.java fails intermittently
clanger
parents: 55386
diff changeset
   231
                retrycount++;
b35771556cd0 8230850: Test sun/tools/jcmd/TestProcessHelper.java fails intermittently
clanger
parents: 55386
diff changeset
   232
                sleepms *= 2;
b35771556cd0 8230850: Test sun/tools/jcmd/TestProcessHelper.java fails intermittently
clanger
parents: 55386
diff changeset
   233
            }
b35771556cd0 8230850: Test sun/tools/jcmd/TestProcessHelper.java fails intermittently
clanger
parents: 55386
diff changeset
   234
        }
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   235
        p.destroyForcibly();
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   236
        if (!expectedMainClass.equals(mainClass)) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   237
            throw new RuntimeException("Main class is wrong: " + mainClass);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   238
        }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   239
    }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   240
54460
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents: 53707
diff changeset
   241
    private void testProcessHelper(List<String> args, String expectedValue) throws Exception {
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   242
        ProcessBuilder pb = new ProcessBuilder(args);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   243
        String cmd = pb.command().stream().collect(Collectors.joining(" "));
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   244
        System.out.println("Starting the process:" + cmd);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   245
        Process p = ProcessTools.startProcess("test", pb);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   246
        if (!p.isAlive()) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   247
            throw new RuntimeException("Cannot start the process: " + cmd);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   248
        }
54460
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents: 53707
diff changeset
   249
        checkMainClass(p, expectedValue);
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   250
    }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   251
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   252
    private File prepareJar() throws Exception {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   253
        Path jarFile = USER_DIR.resolve("testprocess.jar");
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   254
        Manifest manifest = createManifest();
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   255
        JarUtils.createJarFile(jarFile, manifest, TEST_CLASSES, TEST_CLASS);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   256
        return jarFile.toFile();
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   257
    }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   258
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   259
    private void prepareModule() throws Exception {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   260
        TEST_MODULES.toFile().mkdirs();
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   261
        Path moduleJar = TEST_MODULES.resolve("mod1.jar");
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   262
        ModuleDescriptor md = createModuleDescriptor();
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   263
        createModuleJarFile(moduleJar, md, TEST_CLASSES, TEST_CLASS);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   264
    }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   265
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   266
    private Manifest createManifest() {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   267
        Manifest manifest = new Manifest();
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   268
        manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   269
        manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, TEST_PROCESS_MAIN_CLASS);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   270
        return manifest;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   271
    }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   272
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   273
    private ModuleDescriptor createModuleDescriptor() {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   274
        ModuleDescriptor.Builder builder
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   275
                = ModuleDescriptor.newModule(MODULE_NAME).requires("java.base");
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   276
        return builder.build();
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   277
    }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   278
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   279
    private static void createModuleJarFile(Path jarfile, ModuleDescriptor md, Path dir, Path... files)
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   280
            throws IOException {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   281
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   282
        Path parent = jarfile.getParent();
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   283
        if (parent != null) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   284
            Files.createDirectories(parent);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   285
        }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   286
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   287
        List<Path> entries = findAllRegularFiles(dir, files);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   288
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   289
        try (OutputStream out = Files.newOutputStream(jarfile);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   290
             JarOutputStream jos = new JarOutputStream(out)) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   291
            if (md != null) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   292
                JarEntry je = new JarEntry("module-info.class");
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   293
                jos.putNextEntry(je);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   294
                ModuleInfoWriter.write(md, jos);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   295
                jos.closeEntry();
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   296
            }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   297
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   298
            for (Path entry : entries) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   299
                String name = toJarEntryName(entry);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   300
                jos.putNextEntry(new JarEntry(name));
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   301
                Files.copy(dir.resolve(entry), jos);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   302
                jos.closeEntry();
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   303
            }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   304
        }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   305
    }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   306
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   307
    private static String toJarEntryName(Path file) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   308
        Path normalized = file.normalize();
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   309
        return normalized.subpath(0, normalized.getNameCount())
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   310
                .toString()
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   311
                .replace(File.separatorChar, '/');
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   312
    }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   313
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   314
    private static List<Path> findAllRegularFiles(Path dir, Path[] files) throws IOException {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   315
        List<Path> entries = new ArrayList<>();
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   316
        for (Path file : files) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   317
            try (Stream<Path> stream = Files.find(dir.resolve(file), Integer.MAX_VALUE,
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   318
                    (p, attrs) -> attrs.isRegularFile())) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   319
                stream.map(dir::relativize)
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   320
                        .forEach(entries::add);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   321
            }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   322
        }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   323
        return entries;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   324
    }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   325
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   326
}