jdk/test/com/sun/tools/attach/ProviderTest.java
author ykantser
Fri, 23 Jan 2015 14:20:52 +0100
changeset 28760 b83d51c77b31
parent 24366 57c0a8b7a936
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
8067945: SVC jdk/test/* should be cleaned from JRE layout dependency Reviewed-by: sla Contributed-by: alexander.kulyakhtin@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
     2
 * Copyright (c) 2005, 2013 Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    24
import java.io.File;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    25
import jdk.testlibrary.OutputAnalyzer;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    26
import jdk.testlibrary.JDKToolLauncher;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    27
import jdk.testlibrary.ProcessTools;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.tools.attach.VirtualMachine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.tools.attach.spi.AttachProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    31
/*
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    32
 * @test
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    33
 * @bug 6173612 6273707 6277253 6335921 6348630 6342019 6381757
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    34
 * @summary Basic unit tests for the VM attach mechanism.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    35
 * @library /lib/testlibrary
24366
57c0a8b7a936 8034960: Serviceability tests using @library failing with java.lang.NoClassDefFoundError
ykantser
parents: 21968
diff changeset
    36
 * @run build jdk.testlibrary.* SimpleProvider
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    37
 * @run main ProviderTest
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    38
 *
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    39
 * The test will attach and detach to/from the running Application.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    40
 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class ProviderTest {
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    42
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    43
    /*
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    44
     * The actual tests are in the nested class TestMain below.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    45
     * The responsibility of this class is to:
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    46
     * 1. Build the needed jar.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    47
     * 2. Run tests in ProviderTest.TestMain.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    48
     */
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    49
    public static void main(String args[]) throws Throwable {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    50
        try {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    51
            buildJar();
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    52
            runTests();
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    53
        } catch (Throwable t) {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    54
            System.out.println("TestProvider got unexpected exception: " + t);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    55
            t.printStackTrace();
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    56
            throw t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    58
    }
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    59
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    60
    /**
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    61
     * Runs the actual tests in the nested class TestMain.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    62
     * We need to run the tests in a separate process,
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    63
     * because we need to add to the classpath.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    64
     */
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    65
    private static void runTests() throws Throwable {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    66
        final String sep = File.separator;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    67
        String testClassPath = System.getProperty("test.class.path", "");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    68
        String testClasses = System.getProperty("test.classes", "") + sep;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    69
        String jdkLib = System.getProperty("test.jdk", ".") + sep + "lib" + sep;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    70
28760
b83d51c77b31 8067945: SVC jdk/test/* should be cleaned from JRE layout dependency
ykantser
parents: 24366
diff changeset
    71
        // Need to add SimpleProvider.jar to classpath.
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    72
        String classpath =
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    73
                testClassPath + File.pathSeparator +
28760
b83d51c77b31 8067945: SVC jdk/test/* should be cleaned from JRE layout dependency
ykantser
parents: 24366
diff changeset
    74
                testClasses + "SimpleProvider.jar";
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    75
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    76
        String[] args = {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    77
                "-classpath",
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    78
                classpath,
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    79
                "ProviderTest$TestMain" };
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    80
        OutputAnalyzer output = ProcessTools.executeTestJvm(args);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    81
        output.shouldHaveExitValue(0);
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    82
    }
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    83
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    84
    /**
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    85
     * Will build the SimpleProvider.jar.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    86
     */
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    87
    private static void buildJar() throws Throwable {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    88
        final String sep = File.separator;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    89
        String testClasses = System.getProperty("test.classes", "?") + sep;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    90
        String testSrc = System.getProperty("test.src", "?") + sep;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    91
        String serviceDir = "META-INF" + sep + "services" + sep;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    92
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    93
        RunnerUtil.createJar(
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    94
            "-cf", testClasses + "SimpleProvider.jar",
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    95
            "-C", testClasses, "SimpleProvider.class",
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    96
            "-C", testClasses, "SimpleVirtualMachine.class",
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    97
            "-C", testSrc,
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    98
            serviceDir + "com.sun.tools.attach.spi.AttachProvider");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    99
    }
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   100
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   101
    /**
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   102
     * This is the actual test code that attaches to the running Application.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   103
     * This class is run in a separate process.
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   104
     */
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   105
    public static class TestMain {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   106
        public static void main(String args[]) throws Exception {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   107
            // deal with internal builds where classes are loaded from the
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   108
            // 'classes' directory rather than rt.jar
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   109
            ClassLoader cl = AttachProvider.class.getClassLoader();
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   110
            if (cl != ClassLoader.getSystemClassLoader()) {
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   111
                System.out.println("Attach API not loaded by system class loader - test skipped");
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   112
                return;
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   113
            }
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   114
            VirtualMachine.attach("simple:1234").detach();
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
   115
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
}