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.compiler
28 * @build AddExportsTest CompilerUtils jdk.testlibrary.*
29 * @run testng AddExportsTest
30 * @summary Basic tests for java --add-exports
31 */
32
33 import java.nio.file.Path;
34 import java.nio.file.Paths;
35
36 import static jdk.testlibrary.ProcessTools.*;
37
38 import org.testng.annotations.BeforeTest;
39 import org.testng.annotations.DataProvider;
40 import org.testng.annotations.Test;
41 import static org.testng.Assert.*;
42
43
44 @Test
45 public class AddExportsTest {
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 private static final Path UPGRADE_MODS_DIRS = Paths.get("upgrademods");
52
53 // test module m1 that uses Unsafe
54 private static final String TEST1_MODULE = "m1";
55 private static final String TEST1_MAIN_CLASS = "jdk.test1.Main";
56
88 "--add-exports", "java.transaction/javax.transaction.internal=m2");
89 assertTrue(compiled, "module " + TEST2_MODULE + " did not compile");
90
91 // javac -d mods/m3 src/m3/**
92 compiled = CompilerUtils.compile(
93 SRC_DIR.resolve(TEST3_MODULE),
94 MODS_DIR.resolve(TEST3_MODULE));
95 assertTrue(compiled, "module " + TEST3_MODULE + " did not compile");
96
97 // javac -d mods/m4 src/m4/**
98 compiled = CompilerUtils.compile(
99 SRC_DIR.resolve(TEST4_MODULE),
100 MODS_DIR.resolve(TEST4_MODULE));
101 assertTrue(compiled, "module " + TEST4_MODULE + " did not compile");
102 }
103
104 /**
105 * Sanity check with -version
106 */
107 public void testSanity() throws Exception {
108
109 int exitValue
110 = executeTestJava("--add-exports", "java.base/jdk.internal.reflect=ALL-UNNAMED",
111 "-version")
112 .outputTo(System.out)
113 .errorTo(System.out)
114 .getExitValue();
115
116 assertTrue(exitValue == 0);
117 }
118
119
120 /**
121 * Run class path application that uses jdk.internal.misc.Unsafe
122 */
123 public void testUnnamedModule() throws Exception {
124
125 // java --add-exports java.base/jdk.internal.misc=ALL-UNNAMED \
126 // -cp mods/$TESTMODULE jdk.test.UsesUnsafe
127
128 String classpath = MODS_DIR.resolve(TEST1_MODULE).toString();
129 int exitValue
130 = executeTestJava("--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED",
131 "-cp", classpath,
132 TEST1_MAIN_CLASS)
133 .outputTo(System.out)
134 .errorTo(System.out)
135 .getExitValue();
136
137 assertTrue(exitValue == 0);
138 }
139
140
141 /**
142 * Run named module that uses jdk.internal.misc.Unsafe
143 */
144 public void testNamedModule() throws Exception {
145
146 // java --add-exports java.base/jdk.internal.misc=test \
147 // --module-path mods -m $TESTMODULE/$MAIN_CLASS
148
149 String mid = TEST1_MODULE + "/" + TEST1_MAIN_CLASS;
150 int exitValue =
151 executeTestJava("--add-exports", "java.base/jdk.internal.misc=" + TEST1_MODULE,
152 "--module-path", MODS_DIR.toString(),
153 "-m", mid)
154 .outputTo(System.out)
155 .errorTo(System.out)
156 .getExitValue();
157
158 assertTrue(exitValue == 0);
159 }
160
161
162 /**
163 * Test --add-exports with upgraded module
164 */
165 public void testWithUpgradedModule() throws Exception {
166
167 // java --add-exports java.transaction/javax.transaction.internal=m2
168 // --upgrade-module-path upgrademods --module-path mods -m ...
169 String mid = TEST2_MODULE + "/" + TEST2_MAIN_CLASS;
170 int exitValue = executeTestJava(
171 "--add-exports", "java.transaction/javax.transaction.internal=m2",
172 "--upgrade-module-path", UPGRADE_MODS_DIRS.toString(),
173 "--module-path", MODS_DIR.toString(),
174 "-m", mid)
175 .outputTo(System.out)
176 .errorTo(System.out)
177 .getExitValue();
178
179 assertTrue(exitValue == 0);
180 }
181
182
183 /**
184 * Test --add-exports with module that is added to the set of root modules
185 * with --add-modules.
186 */
187 public void testWithAddMods() throws Exception {
188
189 // java --add-exports m4/jdk.test4=m3 --module-path mods -m ...
190 String mid = TEST3_MODULE + "/" + TEST3_MAIN_CLASS;
191 int exitValue = executeTestJava(
192 "--add-exports", "m4/jdk.test4=m3",
193 "--module-path", MODS_DIR.toString(),
194 "--add-modules", TEST4_MODULE,
195 "-m", mid)
196 .outputTo(System.out)
197 .errorTo(System.out)
198 .getExitValue();
199
200 assertTrue(exitValue == 0);
201 }
202
203
204 /**
205 * --add-exports can only be specified once
206 */
207 public void testWithDuplicateOption() throws Exception {
208
209 int exitValue
210 = executeTestJava("--add-exports", "java.base/jdk.internal.reflect=ALL-UNNAMED",
211 "--add-exports", "java.base/jdk.internal.reflect=ALL-UNNAMED",
212 "-version")
213 .outputTo(System.out)
214 .errorTo(System.out)
215 .shouldContain("specified more than once")
216 .getExitValue();
217
218 assertTrue(exitValue != 0);
219 }
220
221
222 /**
223 * Exercise --add-exports with bad values
224 */
225 @Test(dataProvider = "badvalues")
226 public void testWithBadValue(String value, String ignore) throws Exception {
227
228 // --add-exports $VALUE -version
229 int exitValue =
230 executeTestJava("--add-exports", value,
231 "-version")
232 .outputTo(System.out)
233 .errorTo(System.out)
234 .getExitValue();
235
236 assertTrue(exitValue != 0);
237 }
238
239 @DataProvider(name = "badvalues")
240 public Object[][] badValues() {
241 return new Object[][]{
242
243 { "java.base/jdk.internal.misc", null }, // missing target
244 { "java.base/jdk.internal.misc=sun.monkey", null }, // unknown target
245 { "java.monkey/sun.monkey=ALL-UNNAMED", null }, // unknown module
246 { "java.base/sun.monkey=ALL-UNNAMED", null }, // unknown package
247 { "java.monkey/sun.monkey=ALL-UNNAMED", null }, // unknown module/package
248 { "java.base=ALL-UNNAMED", null }, // missing package
249 { "java.base/=ALL-UNNAMED", null } // missing package
250
251 };
252 }
253 }
|
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.compiler
28 * @build AddExportsTest CompilerUtils jdk.testlibrary.* jdk.testlibrary.tasks.JavaTask
29 * @run testng AddExportsTest
30 * @summary Basic tests for java --add-exports
31 */
32
33 import java.nio.file.Path;
34 import java.nio.file.Paths;
35
36 import static jdk.testlibrary.ProcessTools.*;
37 import jdk.testlibrary.tasks.JavaTask;
38 import jdk.testlibrary.tasks.Task;
39
40 import org.testng.annotations.BeforeTest;
41 import org.testng.annotations.DataProvider;
42 import org.testng.annotations.Test;
43 import static org.testng.Assert.*;
44
45
46 @Test
47 public class AddExportsTest {
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 private static final Path UPGRADE_MODS_DIRS = Paths.get("upgrademods");
54
55 // test module m1 that uses Unsafe
56 private static final String TEST1_MODULE = "m1";
57 private static final String TEST1_MAIN_CLASS = "jdk.test1.Main";
58
90 "--add-exports", "java.transaction/javax.transaction.internal=m2");
91 assertTrue(compiled, "module " + TEST2_MODULE + " did not compile");
92
93 // javac -d mods/m3 src/m3/**
94 compiled = CompilerUtils.compile(
95 SRC_DIR.resolve(TEST3_MODULE),
96 MODS_DIR.resolve(TEST3_MODULE));
97 assertTrue(compiled, "module " + TEST3_MODULE + " did not compile");
98
99 // javac -d mods/m4 src/m4/**
100 compiled = CompilerUtils.compile(
101 SRC_DIR.resolve(TEST4_MODULE),
102 MODS_DIR.resolve(TEST4_MODULE));
103 assertTrue(compiled, "module " + TEST4_MODULE + " did not compile");
104 }
105
106 /**
107 * Sanity check with -version
108 */
109 public void testSanity() throws Exception {
110 new JavaTask()
111 .addExports("java.base/jdk.internal.reflect=ALL-UNNAMED")
112 .vmOptions("-version")
113 .run();
114 }
115
116
117 /**
118 * Run class path application that uses jdk.internal.misc.Unsafe
119 */
120 public void testUnnamedModule() throws Exception {
121
122 // java --add-exports java.base/jdk.internal.misc=ALL-UNNAMED \
123 // -cp mods/$TESTMODULE jdk.test.UsesUnsafe
124
125 new JavaTask()
126 .addExports("java.base/jdk.internal.misc=ALL-UNNAMED")
127 .vmOptions("-version")
128 .classpath(MODS_DIR.resolve(TEST1_MODULE).toString())
129 .className(TEST1_MAIN_CLASS)
130 .run();
131 }
132
133
134 /**
135 * Run named module that uses jdk.internal.misc.Unsafe
136 */
137 public void testNamedModule() throws Exception {
138
139 // java --add-exports java.base/jdk.internal.misc=test \
140 // --module-path mods -m $TESTMODULE/$MAIN_CLASS
141
142 new JavaTask()
143 .addExports("java.base/jdk.internal.misc=" + TEST1_MODULE)
144 .modulepath(MODS_DIR.toString())
145 .moduleName(TEST1_MODULE).className(TEST1_MAIN_CLASS)
146 .run();
147 }
148
149
150 /**
151 * Test --add-exports with upgraded module
152 */
153 public void testWithUpgradedModule() throws Exception {
154
155 // java --add-exports java.transaction/javax.transaction.internal=m2
156 // --upgrade-module-path upgrademods --module-path mods -m ...
157 new JavaTask().ignoreStandardModuleOptions()
158 .addExports("java.transaction/javax.transaction.internal=m2")
159 .vmOptions("--upgrade-module-path", UPGRADE_MODS_DIRS.toString())
160 .modulepath(MODS_DIR.toString())
161 .moduleName(TEST2_MODULE).className(TEST2_MAIN_CLASS)
162 .run();
163 }
164
165
166 /**
167 * Test --add-exports with module that is added to the set of root modules
168 * with --add-modules.
169 */
170 public void testWithAddMods() throws Exception {
171
172 // java --add-exports m4/jdk.test4=m3 --module-path mods -m ...
173 new JavaTask().ignoreStandardModuleOptions()
174 .addExports("m4/jdk.test4=m3").addModules(TEST4_MODULE)
175 .modulepath(MODS_DIR.toString())
176 .moduleName(TEST3_MODULE).className(TEST3_MAIN_CLASS)
177 .run();
178 }
179
180
181 /**
182 * --add-exports can only be specified once
183 */
184 public void testWithDuplicateOption() throws Exception {
185 new JavaTask()
186 .vmOptions(
187 "--add-exports", "java.base/jdk.internal.reflect=ALL-UNNAMED",
188 "--add-exports", "java.base/jdk.internal.reflect=ALL-UNNAMED",
189 "-version")
190 .run(Task.Expect.FAIL);
191 }
192
193
194 /**
195 * Exercise --add-exports with bad values
196 */
197 @Test(dataProvider = "badvalues")
198 public void testWithBadValue(String value, String ignore) throws Exception {
199 // --add-exports $VALUE -version
200 new JavaTask()
201 .addExports(value)
202 .vmOptions("-version")
203 .run(Task.Expect.FAIL);
204 }
205
206 @DataProvider(name = "badvalues")
207 public Object[][] badValues() {
208 return new Object[][]{
209
210 { "java.base/jdk.internal.misc", null }, // missing target
211 { "java.base/jdk.internal.misc=sun.monkey", null }, // unknown target
212 { "java.monkey/sun.monkey=ALL-UNNAMED", null }, // unknown module
213 { "java.base/sun.monkey=ALL-UNNAMED", null }, // unknown package
214 { "java.monkey/sun.monkey=ALL-UNNAMED", null }, // unknown module/package
215 { "java.base=ALL-UNNAMED", null }, // missing package
216 { "java.base/=ALL-UNNAMED", null } // missing package
217
218 };
219 }
220 }
|