1 /*
   2  * Copyright (c) 2015, 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  * @test
  26  * @requires vm.cds
  27  * @summary test that --patch-module works with CDS
  28  * @library /test/lib
  29  * @modules java.base/jdk.internal.misc
  30  *          jdk.jartool/sun.tools.jar
  31  * @build PatchModuleMain
  32  * @run driver PatchModuleCDS
  33  */
  34 
  35 import jdk.test.lib.compiler.InMemoryJavaCompiler;
  36 import jdk.test.lib.process.OutputAnalyzer;
  37 import jdk.test.lib.process.ProcessTools;
  38 
  39 public class PatchModuleCDS {
  40 
  41     public static void main(String args[]) throws Throwable {
  42 
  43         // Case 1: Test that --patch-module and -Xshare:dump are compatible
  44         String filename = "patch_module.jsa";
  45         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  46             "-XX:+UnlockDiagnosticVMOptions",
  47             "-XX:SharedArchiveFile=" + filename,
  48             "-Xshare:dump",
  49             "--patch-module=java.naming=no/such/directory",
  50             "-Xlog:class+path=info",
  51             "-version");
  52         new OutputAnalyzer(pb.start())
  53             // --patch-module is not supported during CDS dumping
  54             .shouldContain("Cannot use the following option when dumping the shared archive: --patch-module");
  55 
  56         // Case 2: Test that directory in --patch-module is supported for CDS dumping
  57         // Create a class file in the module java.base.
  58         String source = "package javax.naming.spi; "                +
  59                         "public class NamingManager { "             +
  60                         "    static { "                             +
  61                         "        System.out.println(\"I pass!\"); " +
  62                         "    } "                                    +
  63                         "}";
  64 
  65         ClassFileInstaller.writeClassToDisk("javax/naming/spi/NamingManager",
  66              InMemoryJavaCompiler.compile("javax.naming.spi.NamingManager", source, "--patch-module=java.naming"),
  67              System.getProperty("test.classes"));
  68 
  69         pb = ProcessTools.createJavaProcessBuilder(
  70             "-XX:+UnlockDiagnosticVMOptions",
  71             "-XX:SharedArchiveFile=" + filename,
  72             "-Xshare:dump",
  73             "--patch-module=java.naming=" + System.getProperty("test.classes"),
  74             "-Xlog:class+path=info",
  75             "-version");
  76         new OutputAnalyzer(pb.start())
  77             // --patch-module is not supported during CDS dumping
  78             .shouldContain("Cannot use the following option when dumping the shared archive: --patch-module");
  79 
  80         // Case 3a: Test CDS dumping with jar file in --patch-module
  81         BasicJarBuilder.build("javanaming", "javax/naming/spi/NamingManager");
  82         String moduleJar = BasicJarBuilder.getTestJar("javanaming.jar");
  83         pb = ProcessTools.createJavaProcessBuilder(
  84             "-XX:+UnlockDiagnosticVMOptions",
  85             "-XX:SharedArchiveFile=" + filename,
  86             "-Xshare:dump",
  87             "--patch-module=java.naming=" + moduleJar,
  88             "-Xlog:class+load",
  89             "-Xlog:class+path=info",
  90             "PatchModuleMain", "javax.naming.spi.NamingManager");
  91         new OutputAnalyzer(pb.start())
  92             // --patch-module is not supported during CDS dumping
  93             .shouldContain("Cannot use the following option when dumping the shared archive: --patch-module");
  94 
  95         // Case 3b: Test CDS run with jar file in --patch-module
  96         pb = ProcessTools.createJavaProcessBuilder(
  97             "-XX:+UnlockDiagnosticVMOptions",
  98             "-XX:SharedArchiveFile=" + filename,
  99             "-Xshare:auto",
 100             "--patch-module=java.naming=" + moduleJar,
 101             "-Xlog:class+load",
 102             "-Xlog:class+path=info",
 103             "PatchModuleMain", "javax.naming.spi.NamingManager");
 104         new OutputAnalyzer(pb.start())
 105             .shouldContain("I pass!")
 106             .shouldHaveExitValue(0);
 107     }
 108 }