1/*
2 * Copyright (c) 2015, 2017, 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 main PatchModuleCDS
33 */
34
35import jdk.test.lib.compiler.InMemoryJavaCompiler;
36import jdk.test.lib.process.OutputAnalyzer;
37import jdk.test.lib.process.ProcessTools;
38
39public 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            .shouldContain("ro space:"); // Make sure archive got created.
54
55        // Case 2: Test that directory 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, "--patch-module=java.naming"),
66             System.getProperty("test.classes"));
67
68        pb = ProcessTools.createJavaProcessBuilder(
69            "-XX:+UnlockDiagnosticVMOptions",
70            "-XX:SharedArchiveFile=" + filename,
71            "-Xshare:dump",
72            "--patch-module=java.naming=" + System.getProperty("test.classes"),
73            "-Xlog:class+path=info",
74            "-version");
75        new OutputAnalyzer(pb.start())
76            .shouldContain("ro space:"); // Make sure archive got created.
77
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);
104    }
105}
106