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