hotspot/test/runtime/modules/PatchModule/PatchModuleCDS.java
changeset 41281 e1dc38ba642f
parent 40631 ed82623d7831
child 42876 ff8ff9dcccec
equal deleted inserted replaced
41279:5a7c83da4a2d 41281:e1dc38ba642f
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
       
    26  * @summary test that --patch-module works with CDS
    26  * @library /test/lib
    27  * @library /test/lib
    27  * @modules java.base/jdk.internal.misc
    28  * @modules java.base/jdk.internal.misc
       
    29  *          jdk.jartool/sun.tools.jar
       
    30  * @build PatchModuleMain
    28  * @run main PatchModuleCDS
    31  * @run main PatchModuleCDS
    29  */
    32  */
    30 
    33 
    31 import java.io.File;
    34 import java.io.File;
       
    35 import jdk.test.lib.InMemoryJavaCompiler;
    32 import jdk.test.lib.process.OutputAnalyzer;
    36 import jdk.test.lib.process.OutputAnalyzer;
    33 import jdk.test.lib.process.ProcessTools;
    37 import jdk.test.lib.process.ProcessTools;
    34 
    38 
    35 public class PatchModuleCDS {
    39 public class PatchModuleCDS {
    36 
    40 
    37     public static void main(String args[]) throws Throwable {
    41     public static void main(String args[]) throws Throwable {
    38         System.out.println("Test that --patch-module and -Xshare:dump are incompatibable");
       
    39         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("--patch-module=java.naming=mods/java.naming", "-Xshare:dump");
       
    40         OutputAnalyzer output = new OutputAnalyzer(pb.start());
       
    41         output.shouldContain("Cannot use the following option when dumping the shared archive: --patch-module");
       
    42 
    42 
    43         System.out.println("Test that --patch-module and -Xshare:on are incompatibable");
    43         // Case 1: Test that --patch-module and -Xshare:dump are compatible
    44         String filename = "patch_module.jsa";
    44         String filename = "patch_module.jsa";
    45         pb = ProcessTools.createJavaProcessBuilder(
    45         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
    46             "-XX:+UnlockDiagnosticVMOptions",
    46             "-XX:+UnlockDiagnosticVMOptions",
    47             "-XX:SharedArchiveFile=" + filename,
    47             "-XX:SharedArchiveFile=" + filename,
    48             "-Xshare:dump");
    48             "-Xshare:dump",
    49         output = new OutputAnalyzer(pb.start());
    49             "--patch-module=java.naming=no/such/directory",
    50         output.shouldContain("ro space:"); // Make sure archive got created.
    50             "-Xlog:class+path=info",
       
    51             "-version");
       
    52         new OutputAnalyzer(pb.start())
       
    53             .shouldContain("ro space:"); // Make sure archive got created.
       
    54 
       
    55        // Case 2: Test that only jar file in --patch-module is supported for CDS dumping
       
    56         // Create a class file in the module java.base.
       
    57         String source = "package javax.naming.spi; "                +
       
    58                         "public class NamingManager { "             +
       
    59                         "    static { "                             +
       
    60                         "        System.out.println(\"I pass!\"); " +
       
    61                         "    } "                                    +
       
    62                         "}";
       
    63 
       
    64         ClassFileInstaller.writeClassToDisk("javax/naming/spi/NamingManager",
       
    65              InMemoryJavaCompiler.compile("javax.naming.spi.NamingManager", source, "-Xmodule:java.naming"),
       
    66              System.getProperty("test.classes"));
    51 
    67 
    52         pb = ProcessTools.createJavaProcessBuilder(
    68         pb = ProcessTools.createJavaProcessBuilder(
    53             "-XX:+UnlockDiagnosticVMOptions",
    69             "-XX:+UnlockDiagnosticVMOptions",
    54             "-XX:SharedArchiveFile=" + filename,
    70             "-XX:SharedArchiveFile=" + filename,
    55             "-Xshare:on",
    71             "-Xshare:dump",
    56             "--patch-module=java.naming=mods/java.naming",
    72             "--patch-module=java.base=" + System.getProperty("test.classes"),
       
    73             "-Xlog:class+path=info",
    57             "-version");
    74             "-version");
    58         output = new OutputAnalyzer(pb.start());
    75         new OutputAnalyzer(pb.start())
    59         output.shouldContain("The shared archive file cannot be used with --patch-module");
    76             .shouldContain("--patch-module requires a regular file during dumping");
    60 
    77 
    61         output.shouldHaveExitValue(1);
    78         // Case 3a: Test CDS dumping with jar file in --patch-module
       
    79         BasicJarBuilder.build("javanaming", "javax/naming/spi/NamingManager");
       
    80         String moduleJar = BasicJarBuilder.getTestJar("javanaming.jar");
       
    81         pb = ProcessTools.createJavaProcessBuilder(
       
    82             "-XX:+UnlockDiagnosticVMOptions",
       
    83             "-XX:SharedArchiveFile=" + filename,
       
    84             "-Xshare:dump",
       
    85             "--patch-module=java.naming=" + moduleJar,
       
    86             "-Xlog:class+load",
       
    87             "-Xlog:class+path=info",
       
    88             "PatchModuleMain", "javax.naming.spi.NamingManager");
       
    89         new OutputAnalyzer(pb.start())
       
    90             .shouldContain("ro space:"); // Make sure archive got created.
       
    91 
       
    92         // Case 3b: Test CDS run with jar file in --patch-module
       
    93         pb = ProcessTools.createJavaProcessBuilder(
       
    94             "-XX:+UnlockDiagnosticVMOptions",
       
    95             "-XX:SharedArchiveFile=" + filename,
       
    96             "-Xshare:auto",
       
    97             "--patch-module=java.naming=" + moduleJar,
       
    98             "-Xlog:class+load",
       
    99             "-Xlog:class+path=info",
       
   100             "PatchModuleMain", "javax.naming.spi.NamingManager");
       
   101         new OutputAnalyzer(pb.start())
       
   102             .shouldContain("I pass!")
       
   103             .shouldHaveExitValue(0);
    62     }
   104     }
    63 }
   105 }