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