1 /*
  2  * Copyright (c) 2023, 2026, 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  * @bug 8316969
 27  * @summary Test handling of module option (-m).
 28  * @requires vm.cds.write.archived.java.heap
 29  * @requires vm.flagless
 30  * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds
 31  * @run driver ModuleOption
 32  */
 33 
 34 import jdk.test.lib.process.OutputAnalyzer;
 35 
 36 public class ModuleOption {
 37     public static void main(String[] args) throws Exception {
 38         final String moduleOption = "jdk.httpserver/sun.net.httpserver.simpleserver.Main";
 39         final String incubatorModule = "jdk.incubator.vector";
 40         final String loggingOption = "-Xlog:aot=debug,aot+module=debug,aot+heap=info,cds=debug,module=trace";
 41         // Pattern of a module version string.
 42         // e.g. JDK 22:     "java 22"
 43         //      JDK 22.0.1: "java 22.0.1"
 44         final String versionPattern = "java.[0-9][0-9].*";
 45         String archiveName = TestCommon.getNewArchiveName("module-option");
 46         TestCommon.setCurrentArchiveName(archiveName);
 47 
 48         // dump a base archive with -m jdk.httpserver
 49         OutputAnalyzer oa = TestCommon.dumpBaseArchive(
 50             archiveName,
 51             loggingOption,
 52             "-m", moduleOption,
 53             "-version");
 54         oa.shouldHaveExitValue(0);
 55 
 56         // same module specified during runtime
 57         oa = TestCommon.execCommon(
 58             loggingOption,
 59             "-m", moduleOption,
 60             "-version");
 61         oa.shouldHaveExitValue(0)
 62           // version of the jdk.httpserver module, e.g. java 22-ea
 63           .shouldMatch(versionPattern)
 64           .shouldMatch("aot,module.*Restored from archive: entry.0x.*name jdk.httpserver");
 65 
 66         // different module specified during runtime
 67         oa = TestCommon.execCommon(
 68             loggingOption,
 69             "-m", "jdk.compiler/com.sun.tools.javac.Main",
 70             "-version");
 71         oa.shouldHaveExitValue(0)
 72           .shouldContain("Mismatched values for property jdk.module.main: runtime jdk.compiler dump time jdk.httpserver");
 73 
 74         // no module specified during runtime
 75         oa = TestCommon.execCommon(
 76             loggingOption,
 77             "-version");
 78         oa.shouldHaveExitValue(0)
 79           .shouldContain("Mismatched values for property jdk.module.main: jdk.httpserver specified during dump time but not during runtime");
 80 
 81         // dump an archive without the module option
 82         archiveName = TestCommon.getNewArchiveName("no-module-option");
 83         TestCommon.setCurrentArchiveName(archiveName);
 84         oa = TestCommon.dumpBaseArchive(
 85             archiveName,
 86             loggingOption,
 87             "-version");
 88         oa.shouldHaveExitValue(0);
 89 
 90         // run with module option
 91         oa = TestCommon.execCommon(
 92             loggingOption,
 93             "-m", moduleOption,
 94             "-version");
 95         oa.shouldHaveExitValue(0)
 96           .shouldContain("Mismatched values for property jdk.module.main: jdk.httpserver specified during runtime but not during dump time")
 97           // version of the jdk.httpserver module, e.g. java 22-ea
 98           .shouldMatch(versionPattern);
 99 
100         // dump an archive with an incubator module, -m jdk.incubator.vector
101         archiveName = TestCommon.getNewArchiveName("incubator-module");
102         TestCommon.setCurrentArchiveName(archiveName);
103         oa = TestCommon.dumpBaseArchive(
104             archiveName,
105             loggingOption,
106             "-m", incubatorModule,
107             "-version");
108         oa.shouldHaveExitValue(0)
109           // module graph won't be archived with an incubator module
110           .shouldContain("archivedBootLayer not available, disabling full module graph");
111 
112         // run with the same incubator module
113         oa = TestCommon.execCommon(
114             loggingOption,
115             "-m", incubatorModule,
116             "-version");
117         oa.shouldContain("full module graph: disabled")
118           // module is not restored from archive
119           .shouldContain("define_module(): creation of module: jdk.incubator.vector")
120           .shouldContain("WARNING: Using incubator modules: jdk.incubator.vector")
121           .shouldContain("module jdk.incubator.vector does not have a ModuleMainClass attribute, use -m <module>/<main-class>")
122           .shouldHaveExitValue(1);
123     }
124 }