test/hotspot/jtreg/serviceability/dcmd/framework/TestProcessJarLauncher.java
author dtitov
Mon, 08 Apr 2019 17:09:02 +0000
changeset 54460 6733a9176cce
permissions -rw-r--r--
8221730: jcmd process name matching broken Reviewed-by: jcbeyler, dholmes, cjplummer
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
54460
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
     1
/*
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
     4
 *
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
     8
 *
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    13
 * accompanied this code).
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    14
 *
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    18
 *
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    21
 * questions.
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    22
 */
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    23
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    24
import jdk.test.lib.util.JarUtils;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    25
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    26
import java.io.File;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    27
import java.io.IOException;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    28
import java.nio.file.Path;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    29
import java.nio.file.Paths;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    30
import java.util.jar.Attributes;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    31
import java.util.jar.Manifest;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    32
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    33
/**
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    34
 * Launches a new Java process using -jar Java option.
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    35
 */
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    36
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    37
public class TestProcessJarLauncher extends TestProcessLauncher {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    38
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    39
    private static final String JAR_FILE = "testprocess.jar";
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    40
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    41
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    42
    public TestProcessJarLauncher(String className) {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    43
        super(className);
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    44
    }
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    45
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    46
    protected String prepareLaunch(String javaExec, String pipePort) {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    47
        try {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    48
            File jarFile = prepareJar();
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    49
            return javaExec + " -jar " + jarFile.getAbsolutePath() + " -pipe.port=" + pipePort;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    50
        } catch (IOException e) {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    51
            throw new RuntimeException("Failed to prepare a jar file", e);
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    52
        }
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    53
    }
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    54
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    55
    private File prepareJar() throws IOException {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    56
        Path jarFile = USER_DIR.resolve(JAR_FILE);
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    57
        Manifest manifest = createManifest();
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    58
        Path testClass = TEST_CLASSES_DIR.resolve(className + ".class");
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    59
        JarUtils.createJarFile(jarFile, manifest, TEST_CLASSES_DIR, Paths.get("."));
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    60
        return jarFile.toFile();
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    61
    }
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    62
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    63
    private Manifest createManifest() {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    64
        Manifest manifest = new Manifest();
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    65
        manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    66
        manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, className);
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    67
        return manifest;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    68
    }
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    69
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    70
    public String getJarFile() {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    71
        return JAR_FILE;
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    72
    }
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents:
diff changeset
    73
}