1 /*
   2  * Copyright (c) 2014, 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 
  24 /*
  25  * @test
  26  * @summary Redefine a class with an UnresolvedClass reference in the constant pool.
  27  * @bug 8035150
  28  * @library /test/lib
  29  * @modules java.base/jdk.internal.misc
  30  *          java.compiler
  31  *          java.instrument
  32  *          java.management
  33  *          jdk.jartool/sun.tools.jar
  34  *          jdk.jvmstat/sun.jvmstat.monitor
  35  * @build UnresolvedClassAgent
  36  * @run main TestRedefineWithUnresolvedClass
  37  */
  38 
  39 import java.io.File;
  40 import java.util.Arrays;
  41 
  42 import jdk.test.lib.process.OutputAnalyzer;
  43 import jdk.test.lib.process.ProcessTools;
  44 
  45 public class TestRedefineWithUnresolvedClass {
  46 
  47     final static String slash = File.separator;
  48     final static String testClasses = System.getProperty("test.classes") + slash;
  49 
  50     public static void main(String... args) throws Throwable {
  51         // delete this class to cause a NoClassDefFoundError
  52         File unresolved = new File(testClasses, "MyUnresolvedClass.class");
  53         if (unresolved.exists() && !unresolved.delete()) {
  54             throw new Exception("Could not delete: " + unresolved);
  55         }
  56 
  57         // build the javaagent
  58         buildJar("UnresolvedClassAgent");
  59 
  60         // launch a VM with the javaagent
  61         launchTest();
  62     }
  63 
  64     private static void buildJar(String jarName) throws Throwable {
  65         String testSrc = System.getProperty("test.src", "?") + slash;
  66 
  67         String jarPath = String.format("%s%s.jar", testClasses, jarName);
  68         String manifestPath = String.format("%s%s.mf", testSrc, jarName);
  69         String className = String.format("%s.class", jarName);
  70 
  71         String[] args = new String[] {"-cfm", jarPath, manifestPath, "-C", testClasses, className};
  72 
  73         System.out.println("Running jar " + Arrays.toString(args));
  74         sun.tools.jar.Main jarTool = new sun.tools.jar.Main(System.out, System.err, "jar");
  75         if (!jarTool.run(args)) {
  76             throw new Exception("jar failed: args=" + Arrays.toString(args));
  77         }
  78     }
  79 
  80     private static void launchTest() throws Throwable {
  81         String[] args = {
  82             "-javaagent:" + testClasses + "UnresolvedClassAgent.jar",
  83             "-Dtest.classes=" + testClasses,
  84             "UnresolvedClassAgent" };
  85         OutputAnalyzer output = ProcessTools.executeTestJvm(args);
  86         output.shouldHaveExitValue(0);
  87     }
  88 }