1 /*
   2  * Copyright (c) 2016, 2018, 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 /*
  26  * @test
  27  * @requires vm.cds
  28  * @summary the class in the -cp is a subclass of the class in --patch-module. The
  29  *          patched class should be used at runtime.
  30  * @library ../..
  31  * @library /test/hotspot/jtreg/testlibrary
  32  * @library /test/lib
  33  * @modules java.base/jdk.internal.misc
  34  *          jdk.jartool/sun.tools.jar
  35  * @build PatchMain
  36  * @run main SubClassOfPatchedClass
  37  */
  38 
  39 import java.io.File;
  40 import jdk.test.lib.compiler.InMemoryJavaCompiler;
  41 import jdk.test.lib.process.OutputAnalyzer;
  42 
  43 public class SubClassOfPatchedClass {
  44     private static String moduleJar;
  45     private static String appJar;
  46 
  47     public static void main(String args[]) throws Throwable {
  48 
  49         // Create a class file in the module java.naming. This class file
  50         // will be put in the javanaming.jar file.
  51         String source = "package javax.naming; "                +
  52                         "public class Reference { "             +
  53                         "    static { "                             +
  54                         "        System.out.println(\"I pass!\"); " +
  55                         "    } "                                    +
  56                         "}";
  57 
  58         String classDir = System.getProperty("test.classes");
  59 
  60         ClassFileInstaller.writeClassToDisk("javax/naming/Reference",
  61              InMemoryJavaCompiler.compile("javax.naming.Reference", source, "--patch-module=java.naming"),
  62              classDir);
  63 
  64         // Build the jar file that will be used for the module "java.naming".
  65         JarBuilder.build("javanaming", "javax/naming/Reference");
  66         moduleJar = TestCommon.getTestJar("javanaming.jar");
  67 
  68         String source2 = "package mypackage; "                +
  69                         "public class MyReference extends javax.naming.Reference { "             +
  70                         "    static { "                             +
  71                         "        System.out.println(\"MyReference!\"); " +
  72                         "    } "                                    +
  73                         "    public MyReference(String mystring) { " +
  74                         "        super(mystring); " +
  75                         "    } " +
  76                         "}";
  77         ClassFileInstaller.writeClassToDisk("mypackage/MyReference",
  78              InMemoryJavaCompiler.compile("mypackage.MyReference", source2),
  79              classDir);
  80 
  81         JarBuilder.build("myjavanaming", "mypackage/MyReference");
  82         appJar = TestCommon.getTestJar("myjavanaming.jar");
  83 
  84         System.out.println("Test dumping with --patch-module");
  85         OutputAnalyzer output =
  86             TestCommon.dump(appJar,
  87                 TestCommon.list("javax/naming/Reference", "mypackage/MyReference"),
  88                 "--patch-module=java.naming=" + moduleJar,
  89                 "-Xlog:class+load",
  90                 "PatchMain", "javax.naming.Reference", "mypackage.MyReference");
  91         output.shouldHaveExitValue(1)
  92               .shouldContain("Cannot use the following option when dumping the shared archive: --patch-module");
  93 
  94         String classPath = appJar + File.pathSeparator + classDir;
  95         System.out.println("classPath: " + classPath);
  96         TestCommon.run(
  97             "-XX:+UnlockDiagnosticVMOptions",
  98             "-cp", classPath,
  99             "--patch-module=java.naming=" + moduleJar,
 100             "-Xlog:class+load",
 101             "PatchMain", "javax.naming.Reference", "mypackage.MyReference")
 102             .assertSilentlyDisabledCDS(0, "MyReference source: file:", "I pass!");
 103     }
 104 }