36511
|
1 |
/*
|
|
2 |
* Copyright (c) 2014, 2016, 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 |
* @library /lib/testlibrary
|
|
27 |
* @modules jdk.jlink/jdk.tools.jmod
|
|
28 |
* jdk.compiler
|
|
29 |
* @build AddModsTest CompilerUtils jdk.testlibrary.*
|
|
30 |
* @run testng AddModsTest
|
|
31 |
* @summary Basic test for java -addmods
|
|
32 |
*/
|
|
33 |
|
|
34 |
import java.nio.file.Path;
|
|
35 |
import java.nio.file.Paths;
|
|
36 |
|
|
37 |
import static jdk.testlibrary.ProcessTools.*;
|
|
38 |
|
|
39 |
import org.testng.annotations.BeforeTest;
|
|
40 |
import org.testng.annotations.Test;
|
|
41 |
import static org.testng.Assert.*;
|
|
42 |
|
|
43 |
|
|
44 |
@Test
|
|
45 |
public class AddModsTest {
|
|
46 |
|
|
47 |
private static final String TEST_SRC = System.getProperty("test.src");
|
|
48 |
|
|
49 |
private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
|
|
50 |
private static final Path MODS_DIR = Paths.get("mods");
|
|
51 |
|
|
52 |
// the module name of the library module
|
|
53 |
private static final String LIB_MODULE = "lib";
|
|
54 |
|
|
55 |
// application source directory
|
|
56 |
private static final String APP_SRC = "app";
|
|
57 |
|
|
58 |
// application is compiled to classes
|
|
59 |
private static final Path CLASSES_DIR = Paths.get("classes");
|
|
60 |
|
|
61 |
// application main class
|
|
62 |
private static final String MAIN_CLASS = "app.Main";
|
|
63 |
|
|
64 |
|
|
65 |
@BeforeTest
|
|
66 |
public void compile() throws Exception {
|
|
67 |
|
|
68 |
// javac -d mods/$LIB_MODULE src/$LIB_MODULE/**
|
|
69 |
boolean compiled = CompilerUtils.compile(
|
|
70 |
SRC_DIR.resolve(LIB_MODULE),
|
|
71 |
MODS_DIR.resolve(LIB_MODULE)
|
|
72 |
);
|
|
73 |
assertTrue(compiled, "library module did not compile");
|
|
74 |
|
|
75 |
// javac -d classes -mp mods src/$APP_DIR/**
|
|
76 |
compiled = CompilerUtils.compile(
|
|
77 |
SRC_DIR.resolve(APP_SRC),
|
|
78 |
CLASSES_DIR,
|
|
79 |
"-mp", MODS_DIR.toString(),
|
|
80 |
"-addmods", LIB_MODULE
|
|
81 |
);
|
|
82 |
assertTrue(compiled, "app did not compile");
|
|
83 |
}
|
|
84 |
|
|
85 |
|
|
86 |
/**
|
|
87 |
* Basic test of -addmods ALL-SYSTEM, using the output of -listmods to
|
|
88 |
* check that the a sample of the system modules are resolved.
|
|
89 |
*/
|
|
90 |
public void testAddSystemModules() throws Exception {
|
|
91 |
|
|
92 |
executeTestJava("-addmods", "ALL-SYSTEM",
|
|
93 |
"-listmods",
|
|
94 |
"-m", "java.base")
|
|
95 |
.outputTo(System.out)
|
|
96 |
.errorTo(System.out)
|
|
97 |
.shouldContain("java.sql")
|
|
98 |
.shouldContain("java.corba");
|
|
99 |
|
|
100 |
// no exit value to check as -m java.base will likely fail
|
|
101 |
}
|
|
102 |
|
|
103 |
|
|
104 |
/**
|
|
105 |
* Run application on class path that makes use of module on the
|
|
106 |
* application module path. Uses {@code -addmods lib}
|
|
107 |
*/
|
|
108 |
public void testRunWithAddMods() throws Exception {
|
|
109 |
|
|
110 |
// java -mp mods -addmods lib -cp classes app.Main
|
|
111 |
int exitValue
|
|
112 |
= executeTestJava("-mp", MODS_DIR.toString(),
|
|
113 |
"-addmods", LIB_MODULE,
|
|
114 |
"-cp", CLASSES_DIR.toString(),
|
|
115 |
MAIN_CLASS)
|
|
116 |
.outputTo(System.out)
|
|
117 |
.errorTo(System.out)
|
|
118 |
.getExitValue();
|
|
119 |
|
|
120 |
assertTrue(exitValue == 0);
|
|
121 |
|
|
122 |
}
|
|
123 |
|
|
124 |
/**
|
|
125 |
* Run application on class path that makes use of module on the
|
|
126 |
* application module path. Uses {@code -addmods ALL-MODULE-PATH}.
|
|
127 |
*/
|
|
128 |
public void testAddAllModulePath() throws Exception {
|
|
129 |
|
|
130 |
// java -mp mods -addmods lib -cp classes app.Main
|
|
131 |
int exitValue
|
|
132 |
= executeTestJava("-mp", MODS_DIR.toString(),
|
|
133 |
"-addmods", "ALL-MODULE-PATH",
|
|
134 |
"-cp", CLASSES_DIR.toString(),
|
|
135 |
MAIN_CLASS)
|
|
136 |
.outputTo(System.out)
|
|
137 |
.errorTo(System.out)
|
|
138 |
.getExitValue();
|
|
139 |
|
|
140 |
assertTrue(exitValue == 0);
|
|
141 |
|
|
142 |
}
|
|
143 |
|
|
144 |
|
|
145 |
/**
|
|
146 |
* Run application on class path that makes use of module on the
|
|
147 |
* application module path. Does not use -addmods and so will
|
|
148 |
* fail at run-time.
|
|
149 |
*/
|
|
150 |
public void testRunMissingAddMods() throws Exception {
|
|
151 |
|
|
152 |
// java -mp mods -cp classes app.Main
|
|
153 |
int exitValue
|
|
154 |
= executeTestJava("-mp", MODS_DIR.toString(),
|
|
155 |
"-cp", CLASSES_DIR.toString(),
|
|
156 |
MAIN_CLASS)
|
|
157 |
.outputTo(System.out)
|
|
158 |
.errorTo(System.out)
|
|
159 |
.getExitValue();
|
|
160 |
|
|
161 |
// CNFE or other error/exception
|
|
162 |
assertTrue(exitValue != 0);
|
|
163 |
|
|
164 |
}
|
|
165 |
|
|
166 |
|
|
167 |
/**
|
|
168 |
* Attempt to run with a bad module name specified to -addmods
|
|
169 |
*/
|
|
170 |
public void testRunWithBadAddMods() throws Exception {
|
|
171 |
|
|
172 |
// java -mp mods -addmods,DoesNotExist lib -cp classes app.Main
|
|
173 |
int exitValue
|
|
174 |
= executeTestJava("-mp", MODS_DIR.toString(),
|
|
175 |
"-addmods", LIB_MODULE + ",DoesNotExist",
|
|
176 |
"-cp", CLASSES_DIR.toString(),
|
|
177 |
MAIN_CLASS)
|
|
178 |
.outputTo(System.out)
|
|
179 |
.errorTo(System.out)
|
|
180 |
.getExitValue();
|
|
181 |
|
|
182 |
assertTrue(exitValue != 0);
|
|
183 |
|
|
184 |
}
|
|
185 |
|
|
186 |
}
|