8164035: compiler/profiling/spectrapredefineclass_classloaders/Launcher.java failing with Agent JAR not found or no Agent-Class attribute
authoriignatyev
Thu, 18 Aug 2016 11:26:35 +0300
changeset 40651 998e35976abf
parent 40650 1f280ba7058b
child 40652 9e6d111b4089
child 40862 3f9cd7a4bfa7
8164035: compiler/profiling/spectrapredefineclass_classloaders/Launcher.java failing with Agent JAR not found or no Agent-Class attribute Reviewed-by: kvn
hotspot/test/compiler/profiling/spectrapredefineclass/Agent.java
hotspot/test/compiler/profiling/spectrapredefineclass/Launcher.java
hotspot/test/compiler/profiling/spectrapredefineclass_classloaders/Agent.java
hotspot/test/compiler/profiling/spectrapredefineclass_classloaders/Launcher.java
--- a/hotspot/test/compiler/profiling/spectrapredefineclass/Agent.java	Wed Aug 17 22:34:33 2016 +0200
+++ b/hotspot/test/compiler/profiling/spectrapredefineclass/Agent.java	Thu Aug 18 11:26:35 2016 +0300
@@ -24,10 +24,12 @@
 package compiler.profiling.spectrapredefineclass;
 
 import com.sun.tools.attach.VirtualMachine;
+import jdk.test.lib.Utils;
 
 import java.lang.instrument.ClassFileTransformer;
 import java.lang.instrument.Instrumentation;
 import java.lang.management.ManagementFactory;
+import java.nio.file.Paths;
 import java.security.ProtectionDomain;
 
 class A {
@@ -67,8 +69,7 @@
 }
 
 public class Agent implements ClassFileTransformer {
-
-
+    public static final String AGENT_JAR = Paths.get(Utils.TEST_CLASSES, "agent.jar").toString();
     static public boolean m2(A a) {
         boolean res = false;
         if (a.getClass() == B.class) {
@@ -95,7 +96,7 @@
         // Redefine class
         try {
             VirtualMachine vm = VirtualMachine.attach(pid);
-            vm.loadAgent(System.getProperty("test.classes",".") + "/agent.jar", "");
+            vm.loadAgent(AGENT_JAR, "");
             vm.detach();
         } catch (Exception e) {
             throw new RuntimeException(e);
--- a/hotspot/test/compiler/profiling/spectrapredefineclass/Launcher.java	Wed Aug 17 22:34:33 2016 +0200
+++ b/hotspot/test/compiler/profiling/spectrapredefineclass/Launcher.java	Thu Aug 18 11:26:35 2016 +0300
@@ -28,7 +28,7 @@
  * @modules java.base/jdk.internal.misc
  *          java.instrument
  *          java.management
- * @build compiler.profiling.spectrapredefineclass_classloaders.Agent
+ * @build compiler.profiling.spectrapredefineclass.Agent
  * @run driver ClassFileInstaller compiler.profiling.spectrapredefineclass.Agent
  * @run driver compiler.profiling.spectrapredefineclass.Launcher
  * @run main/othervm -XX:-TieredCompilation -XX:-BackgroundCompilation
@@ -39,23 +39,33 @@
 
 package compiler.profiling.spectrapredefineclass;
 
-import jdk.test.lib.JDKToolFinder;
+import jdk.test.lib.JDKToolLauncher;
+import jdk.test.lib.OutputAnalyzer;
 
 import java.io.File;
+import java.io.IOException;
 import java.io.PrintWriter;
 
 public class Launcher {
+    private static final String MANIFEST = "MANIFEST.MF";
     public static void main(String[] args) throws Exception  {
+        try (PrintWriter pw = new PrintWriter(MANIFEST)) {
+            pw.println("Agent-Class: " + Agent.class.getName());
+            pw.println("Can-Retransform-Classes: true");
+        }
 
-      PrintWriter pw = new PrintWriter("MANIFEST.MF");
-      pw.println("Agent-Class: " + Launcher.class.getPackage().getName() +".Agent");
-      pw.println("Can-Retransform-Classes: true");
-      pw.close();
+        JDKToolLauncher jar = JDKToolLauncher.create("jar")
+                .addToolArg("cmf")
+                .addToolArg(MANIFEST)
+                .addToolArg(Agent.AGENT_JAR)
+                .addToolArg(Agent.class.getName().replace('.', File.separatorChar) + ".class");
 
-      ProcessBuilder pb = new ProcessBuilder();
-      pb.command(new String[] { JDKToolFinder.getJDKTool("jar"), "cmf", "MANIFEST.MF",
-              System.getProperty("test.classes",".") + "/agent.jar",
-              "compiler/profiling/spectrapredefineclass/Agent.class".replace('/', File.separatorChar)});
-      pb.start().waitFor();
+        ProcessBuilder pb = new ProcessBuilder(jar.getCommand());
+        try {
+            OutputAnalyzer output = new OutputAnalyzer(pb.start());
+            output.shouldHaveExitValue(0);
+        } catch (IOException ex) {
+            throw new Error("TESTBUG: jar failed.", ex);
+        }
     }
 }
--- a/hotspot/test/compiler/profiling/spectrapredefineclass_classloaders/Agent.java	Wed Aug 17 22:34:33 2016 +0200
+++ b/hotspot/test/compiler/profiling/spectrapredefineclass_classloaders/Agent.java	Thu Aug 18 11:26:35 2016 +0300
@@ -24,6 +24,7 @@
 package compiler.profiling.spectrapredefineclass_classloaders;
 
 import com.sun.tools.attach.VirtualMachine;
+import jdk.test.lib.Utils;
 
 import java.lang.instrument.ClassFileTransformer;
 import java.lang.instrument.Instrumentation;
@@ -32,14 +33,16 @@
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLClassLoader;
+import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.security.ProtectionDomain;
 
 public class Agent implements ClassFileTransformer {
+    public static final String AGENT_JAR = Paths.get(Utils.TEST_CLASSES, "agent.jar").toString();
     public static ClassLoader newClassLoader() {
         try {
             return new URLClassLoader(new URL[] {
-                    Paths.get(System.getProperty("test.classes",".")).toUri().toURL(),
+                    Paths.get(Utils.TEST_CLASSES).toUri().toURL(),
             }, null);
         } catch (MalformedURLException e){
             throw new RuntimeException("Unexpected URL conversion failure", e);
@@ -76,7 +79,7 @@
         for (int i = 0; i < 2; i++) {
             try {
                 VirtualMachine vm = VirtualMachine.attach(pid);
-                vm.loadAgent(System.getProperty("test.classes",".") + "/agent.jar", "");
+                vm.loadAgent(AGENT_JAR, "");
                 vm.detach();
             } catch (Exception e) {
                 throw new RuntimeException(e);
--- a/hotspot/test/compiler/profiling/spectrapredefineclass_classloaders/Launcher.java	Wed Aug 17 22:34:33 2016 +0200
+++ b/hotspot/test/compiler/profiling/spectrapredefineclass_classloaders/Launcher.java	Thu Aug 18 11:26:35 2016 +0300
@@ -39,26 +39,36 @@
  *                   -XX:ReservedCodeCacheSize=3M
  *                   compiler.profiling.spectrapredefineclass_classloaders.Agent
  */
+
 package compiler.profiling.spectrapredefineclass_classloaders;
 
-import jdk.test.lib.JDKToolFinder;
+import jdk.test.lib.JDKToolLauncher;
+import jdk.test.lib.OutputAnalyzer;
 
 import java.io.File;
+import java.io.IOException;
 import java.io.PrintWriter;
 
 public class Launcher {
-    public static void main(String[] args) throws Exception {
-
-        PrintWriter pw = new PrintWriter("MANIFEST.MF");
+    private static final String MANIFEST = "MANIFEST.MF";
+    public static void main(String[] args) throws Exception  {
+        try (PrintWriter pw = new PrintWriter(MANIFEST)) {
+            pw.println("Agent-Class: " + Agent.class.getName());
+            pw.println("Can-Retransform-Classes: true");
+        }
 
-        pw.println("Agent-Class: " + Launcher.class.getPackage().getName() + ".Agent");
-        pw.println("Can-Retransform-Classes: true");
-        pw.close();
+        JDKToolLauncher jar = JDKToolLauncher.create("jar")
+                .addToolArg("cmf")
+                .addToolArg(MANIFEST)
+                .addToolArg(Agent.AGENT_JAR)
+                .addToolArg(Agent.class.getName().replace('.', File.separatorChar) + ".class");
 
-        ProcessBuilder pb = new ProcessBuilder();
-        pb.command(new String[]{JDKToolFinder.getJDKTool("jar"), "cmf", "MANIFEST.MF",
-                System.getProperty("test.classes", ".") + "/agent.jar",
-                "compiler/profiling/spectrapredefineclass/Agent.class".replace('/', File.separatorChar)});
-        pb.start().waitFor();
+        ProcessBuilder pb = new ProcessBuilder(jar.getCommand());
+        try {
+            OutputAnalyzer output = new OutputAnalyzer(pb.start());
+            output.shouldHaveExitValue(0);
+        } catch (IOException ex) {
+            throw new Error("TESTBUG: jar failed.", ex);
+        }
     }
 }