1 /*
   2  * Copyright (c) 2005, 2013 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 
  24 import java.io.File;
  25 import jdk.testlibrary.OutputAnalyzer;
  26 import jdk.testlibrary.JDKToolLauncher;
  27 import jdk.testlibrary.ProcessTools;
  28 import com.sun.tools.attach.VirtualMachine;
  29 import 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.
  35  * @library /lib/testlibrary
  36  * @run build SimpleProvider
  37  * @run main ProviderTest
  38  *
  39  * The test will attach and detach to/from the running Application.
  40  */
  41 public class ProviderTest {
  42 
  43     /*
  44      * The actual tests are in the nested class TestMain below.
  45      * The responsibility of this class is to:
  46      * 1. Build the needed jar.
  47      * 2. Run tests in ProviderTest.TestMain.
  48      */
  49     public static void main(String args[]) throws Throwable {
  50         try {
  51             buildJar();
  52             runTests();
  53         } catch (Throwable t) {
  54             System.out.println("TestProvider got unexpected exception: " + t);
  55             t.printStackTrace();
  56             throw t;
  57         }
  58     }
  59 
  60     /**
  61      * Runs the actual tests in the nested class TestMain.
  62      * We need to run the tests in a separate process,
  63      * because we need to add to the classpath.
  64      */
  65     private static void runTests() throws Throwable {
  66         final String sep = File.separator;
  67         String testClassPath = System.getProperty("test.class.path", "");
  68         String testClasses = System.getProperty("test.classes", "") + sep;
  69         String jdkLib = System.getProperty("test.jdk", ".") + sep + "lib" + sep;
  70 
  71         // Need to add SimpleProvider.jar and tools.jar to classpath.
  72         String classpath =
  73                 testClassPath + File.pathSeparator +
  74                 testClasses + "SimpleProvider.jar" + File.pathSeparator +
  75                 jdkLib + "tools.jar";
  76 
  77         String[] args = {
  78                 "-classpath",
  79                 classpath,
  80                 "ProviderTest$TestMain" };
  81         OutputAnalyzer output = ProcessTools.executeTestJvm(args);
  82         output.shouldHaveExitValue(0);
  83     }
  84 
  85     /**
  86      * Will build the SimpleProvider.jar.
  87      */
  88     private static void buildJar() throws Throwable {
  89         final String sep = File.separator;
  90         String testClasses = System.getProperty("test.classes", "?") + sep;
  91         String testSrc = System.getProperty("test.src", "?") + sep;
  92         String serviceDir = "META-INF" + sep + "services" + sep;
  93 
  94         RunnerUtil.createJar(
  95             "-cf", testClasses + "SimpleProvider.jar",
  96             "-C", testClasses, "SimpleProvider.class",
  97             "-C", testClasses, "SimpleVirtualMachine.class",
  98             "-C", testSrc,
  99             serviceDir + "com.sun.tools.attach.spi.AttachProvider");
 100     }
 101 
 102     /**
 103      * This is the actual test code that attaches to the running Application.
 104      * This class is run in a separate process.
 105      */
 106     public static class TestMain {
 107         public static void main(String args[]) throws Exception {
 108             // deal with internal builds where classes are loaded from the
 109             // 'classes' directory rather than rt.jar
 110             ClassLoader cl = AttachProvider.class.getClassLoader();
 111             if (cl != ClassLoader.getSystemClassLoader()) {
 112                 System.out.println("Attach API not loaded by system class loader - test skipped");
 113                 return;
 114             }
 115             VirtualMachine.attach("simple:1234").detach();
 116         }
 117     }
 118 }