1 /* |
|
2 * Copyright (c) 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 * @library /test/lib /test/hotspot/jtreg/runtime/appcds |
|
29 * @modules jdk.compiler |
|
30 * jdk.jartool/sun.tools.jar |
|
31 * jdk.jlink |
|
32 * @run driver ExportModule |
|
33 * @summary Tests involve exporting a module from the module path to a jar in the -cp. |
|
34 */ |
|
35 |
|
36 import java.io.File; |
|
37 import java.nio.file.Files; |
|
38 import java.nio.file.Path; |
|
39 import java.nio.file.Paths; |
|
40 |
|
41 import jdk.test.lib.compiler.CompilerUtils; |
|
42 import jdk.test.lib.process.OutputAnalyzer; |
|
43 import jdk.test.lib.Asserts; |
|
44 |
|
45 public class ExportModule { |
|
46 |
|
47 private static final Path USER_DIR = Paths.get(System.getProperty("user.dir")); |
|
48 |
|
49 private static final String TEST_SRC = System.getProperty("test.src"); |
|
50 |
|
51 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); |
|
52 private static final Path MODS_DIR = Paths.get("mods"); |
|
53 |
|
54 // the module name of the test module |
|
55 private static final String TEST_MODULE1 = "com.greetings"; |
|
56 private static final String TEST_MODULE2 = "org.astro"; |
|
57 |
|
58 // unnamed module package name |
|
59 private static final String PKG_NAME = "com.nomodule"; |
|
60 |
|
61 // the module main class |
|
62 private static final String MAIN_CLASS = "com.greetings.Main"; |
|
63 private static final String APP_CLASS = "org.astro.World"; |
|
64 |
|
65 // unnamed module main class |
|
66 private static final String UNNAMED_MAIN = "com.nomodule.Main"; |
|
67 |
|
68 private static Path moduleDir = null; |
|
69 private static Path moduleDir2 = null; |
|
70 private static Path appJar = null; |
|
71 private static Path appJar2 = null; |
|
72 |
|
73 public static void buildTestModule() throws Exception { |
|
74 |
|
75 // javac -d mods/$TESTMODULE src/$TESTMODULE/** |
|
76 JarBuilder.compileModule(SRC_DIR.resolve(TEST_MODULE2), |
|
77 MODS_DIR.resolve(TEST_MODULE2), |
|
78 null); |
|
79 |
|
80 // javac -d mods/$TESTMODULE --module-path MOD_DIR src/$TESTMODULE/** |
|
81 JarBuilder.compileModule(SRC_DIR.resolve(TEST_MODULE1), |
|
82 MODS_DIR.resolve(TEST_MODULE1), |
|
83 MODS_DIR.toString()); |
|
84 |
|
85 moduleDir = Files.createTempDirectory(USER_DIR, "mlib"); |
|
86 Path jar = moduleDir.resolve(TEST_MODULE2 + ".jar"); |
|
87 String classes = MODS_DIR.resolve(TEST_MODULE2).toString(); |
|
88 JarBuilder.createModularJar(jar.toString(), classes, null); |
|
89 |
|
90 moduleDir2 = Files.createTempDirectory(USER_DIR, "mlib2"); |
|
91 appJar = moduleDir2.resolve(TEST_MODULE1 + ".jar"); |
|
92 classes = MODS_DIR.resolve(TEST_MODULE1).toString(); |
|
93 JarBuilder.createModularJar(appJar.toString(), classes, MAIN_CLASS); |
|
94 |
|
95 // build a non-modular jar containing the main class which |
|
96 // requires the org.astro package |
|
97 boolean compiled |
|
98 = CompilerUtils.compile(SRC_DIR.resolve(PKG_NAME), |
|
99 MODS_DIR.resolve(PKG_NAME), |
|
100 "--module-path", MODS_DIR.toString(), |
|
101 "--add-modules", TEST_MODULE2, |
|
102 "--add-exports", "org.astro/org.astro=ALL-UNNAMED"); |
|
103 Asserts.assertTrue(compiled, "test package did not compile"); |
|
104 |
|
105 appJar2 = moduleDir2.resolve(PKG_NAME + ".jar"); |
|
106 classes = MODS_DIR.resolve(PKG_NAME).toString(); |
|
107 JarBuilder.createModularJar(appJar2.toString(), classes, null); |
|
108 } |
|
109 |
|
110 public static void main(String... args) throws Exception { |
|
111 // compile the modules and create the modular jar files |
|
112 buildTestModule(); |
|
113 String appClasses[] = {MAIN_CLASS, APP_CLASS}; |
|
114 // create an archive with the class in the org.astro module built in the |
|
115 // previous step and the main class from the modular jar in the -cp |
|
116 // note: the main class is in the modular jar in the -cp which requires |
|
117 // the module in the --module-path |
|
118 OutputAnalyzer output = TestCommon.createArchive( |
|
119 appJar.toString(), appClasses, |
|
120 "--module-path", moduleDir.toString(), |
|
121 "--add-modules", TEST_MODULE2, MAIN_CLASS); |
|
122 TestCommon.checkDump(output); |
|
123 |
|
124 // run it using the archive |
|
125 // both the main class and the class from the org.astro module should |
|
126 // be loaded from the archive |
|
127 TestCommon.run("-Xlog:class+load=trace", |
|
128 "-cp", appJar.toString(), |
|
129 "--module-path", moduleDir.toString(), |
|
130 "--add-modules", TEST_MODULE2, MAIN_CLASS) |
|
131 .assertNormalExit( |
|
132 "[class,load] org.astro.World source: shared objects file", |
|
133 "[class,load] com.greetings.Main source: shared objects file"); |
|
134 |
|
135 String appClasses2[] = {UNNAMED_MAIN, APP_CLASS}; |
|
136 // create an archive with the main class from a non-modular jar in the |
|
137 // -cp and the class from the org.astro module |
|
138 // note: the org.astro package needs to be exported to "ALL-UNNAMED" |
|
139 // module since the jar in the -cp is a non-modular jar and thus it is |
|
140 // unnmaed. |
|
141 output = TestCommon.createArchive( |
|
142 appJar2.toString(), appClasses2, |
|
143 "--module-path", moduleDir.toString(), |
|
144 "--add-modules", TEST_MODULE2, |
|
145 "--add-exports", "org.astro/org.astro=ALL-UNNAMED", |
|
146 UNNAMED_MAIN); |
|
147 TestCommon.checkDump(output); |
|
148 |
|
149 // both the main class and the class from the org.astro module should |
|
150 // be loaded from the archive |
|
151 TestCommon.run("-Xlog:class+load=trace", |
|
152 "-cp", appJar2.toString(), |
|
153 "--module-path", moduleDir.toString(), |
|
154 "--add-modules", TEST_MODULE2, |
|
155 "--add-exports", "org.astro/org.astro=ALL-UNNAMED", |
|
156 UNNAMED_MAIN) |
|
157 .assertNormalExit( |
|
158 "[class,load] org.astro.World source: shared objects file", |
|
159 "[class,load] com.nomodule.Main source: shared objects file"); |
|
160 } |
|
161 } |
|