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