ModulePathTest.java revision 3980:f34b5b81ef55
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 * @bug 8142968 8174104 27 * @summary tests for --module-path 28 * @library /tools/lib 29 * @modules 30 * jdk.compiler/com.sun.tools.javac.api 31 * jdk.compiler/com.sun.tools.javac.main 32 * jdk.jdeps/com.sun.tools.javap 33 * jdk.jlink 34 * @build toolbox.ToolBox toolbox.JarTask toolbox.JavacTask toolbox.ModuleBuilder 35 * ModuleTestBase 36 * @run main ModulePathTest 37 */ 38 39import java.io.File; 40import java.io.IOException; 41import java.nio.file.Files; 42import java.nio.file.Path; 43import java.util.spi.ToolProvider; 44 45import toolbox.JarTask; 46import toolbox.JavacTask; 47import toolbox.ModuleBuilder; 48import toolbox.Task; 49import toolbox.ToolBox; 50 51public class ModulePathTest extends ModuleTestBase { 52 53 public static final String PATH_SEP = File.pathSeparator; 54 55 public static void main(String... args) throws Exception { 56 ModulePathTest t = new ModulePathTest(); 57 t.runTests(); 58 } 59 60 @Test 61 public void testNotExistsOnPath_noWarn(Path base) throws Exception { 62 Path src = base.resolve("src"); 63 tb.writeJavaFiles(src, "class C { }"); 64 65 String log = new JavacTask(tb, Task.Mode.CMDLINE) 66 .options("-XDrawDiagnostics", 67 "--module-path", "doesNotExist") 68 .files(findJavaFiles(src)) 69 .run(Task.Expect.SUCCESS) 70 .writeAll() 71 .getOutput(Task.OutputKind.DIRECT); 72 73 if (!log.isEmpty()) 74 throw new Exception("unexpected output"); 75 } 76 77 @Test 78 public void testNotExistsOnPath_warn(Path base) throws Exception { 79 Path src = base.resolve("src"); 80 tb.writeJavaFiles(src, "class C { }"); 81 82 String log = new JavacTask(tb, Task.Mode.CMDLINE) 83 .options("-XDrawDiagnostics", 84 "-Xlint:path", 85 "--module-path", "doesNotExist") 86 .files(findJavaFiles(src)) 87 .run(Task.Expect.SUCCESS) 88 .writeAll() 89 .getOutput(Task.OutputKind.DIRECT); 90 91 if (!log.contains("- compiler.warn.path.element.not.found: doesNotExist")) 92 throw new Exception("expected output not found"); 93 } 94 95 @Test 96 public void testNotADirOnPath_1(Path base) throws Exception { 97 Path src = base.resolve("src"); 98 tb.writeJavaFiles(src, "class C { }"); 99 tb.writeFile("dummy.txt", ""); 100 101 String log = new JavacTask(tb, Task.Mode.CMDLINE) 102 .options("-XDrawDiagnostics", 103 "--module-path", "dummy.txt") 104 .files(findJavaFiles(src)) 105 .run(Task.Expect.FAIL) 106 .writeAll() 107 .getOutput(Task.OutputKind.DIRECT); 108 109 if (!log.contains("- compiler.err.illegal.argument.for.option: --module-path, dummy.txt")) 110 throw new Exception("expected output not found"); 111 } 112 113 @Test 114 public void testNotADirOnPath_2(Path base) throws Exception { 115 Path src = base.resolve("src"); 116 tb.writeJavaFiles(src, "class C { }"); 117 tb.writeFile("dummy.jimage", ""); 118 119 String log = new JavacTask(tb, Task.Mode.CMDLINE) 120 .options("-XDrawDiagnostics", 121 "--module-path", "dummy.jimage") 122 .files(findJavaFiles(src)) 123 .run(Task.Expect.FAIL) 124 .writeAll() 125 .getOutput(Task.OutputKind.DIRECT); 126 127 if (!log.contains("- compiler.err.illegal.argument.for.option: --module-path, dummy.jimage")) 128 throw new Exception("expected output not found"); 129 } 130 131 @Test 132 public void testExplodedModuleOnPath(Path base) throws Exception { 133 Path modSrc = base.resolve("modSrc"); 134 tb.writeJavaFiles(modSrc, 135 "module m1x { exports p; }", 136 "package p; public class CC { }"); 137 Path modClasses = base.resolve("modClasses"); 138 Files.createDirectories(modClasses); 139 140 new JavacTask(tb, Task.Mode.CMDLINE) 141 .outdir(modClasses) 142 .files(findJavaFiles(modSrc)) 143 .run() 144 .writeAll(); 145 146 Path src = base.resolve("src"); 147 tb.writeJavaFiles(src, 148 "module m { requires m1x ; }", 149 "class C { }"); 150 Path classes = base.resolve("classes"); 151 Files.createDirectories(classes); 152 153 new JavacTask(tb, Task.Mode.CMDLINE) 154 .outdir(classes) 155 .options("--module-path", modClasses.toString()) 156 .files(findJavaFiles(src)) 157 .run() 158 .writeAll(); 159 } 160 161 @Test 162 public void testBadExplodedModuleOnPath(Path base) throws Exception { 163 Path modClasses = base.resolve("modClasses"); 164 tb.writeFile(modClasses.resolve("module-info.class"), "module m1x { }"); 165 166 Path src = base.resolve("src"); 167 tb.writeJavaFiles(src, 168 "module m { requires m1x ; }", 169 "class C { }"); 170 Path classes = base.resolve("classes"); 171 Files.createDirectories(classes); 172 173 String log = new JavacTask(tb, Task.Mode.CMDLINE) 174 .outdir(classes) 175 .options("-XDrawDiagnostics", 176 "--module-path", modClasses.toString()) 177 .files(findJavaFiles(src)) 178 .run(Task.Expect.FAIL) 179 .writeAll() 180 .getOutput(Task.OutputKind.DIRECT); 181 182 if (!log.contains("- compiler.err.locn.bad.module-info: " + modClasses.toString())) 183 throw new Exception("expected output not found"); 184 } 185 186 @Test 187 public void testAutoJarOnPath(Path base) throws Exception { 188 Path jarSrc = base.resolve("jarSrc"); 189 tb.writeJavaFiles(jarSrc, 190 "package p; public class CC { }"); 191 Path jarClasses = base.resolve("jarClasses"); 192 Files.createDirectories(jarClasses); 193 194 new JavacTask(tb, Task.Mode.CMDLINE) 195 .outdir(jarClasses) 196 .files(findJavaFiles(jarSrc)) 197 .run() 198 .writeAll(); 199 200 Path moduleJar = base.resolve("a.jar"); 201 new JarTask(tb, moduleJar) 202 .baseDir(jarClasses) 203 .files("p/CC.class") 204 .run(); 205 206 Path src = base.resolve("src"); 207 tb.writeJavaFiles(src, "class C { p.CC cc; }"); 208 Path classes = base.resolve("classes"); 209 Files.createDirectories(classes); 210 211 new JavacTask(tb, Task.Mode.CMDLINE) 212 .outdir(classes) 213 .options("--module-path", moduleJar.toString(), "--add-modules", "a") 214 .files(findJavaFiles(src)) 215 .run() 216 .writeAll(); 217 } 218 219 @Test 220 public void testModJarOnPath(Path base) throws Exception { 221 Path jarSrc = base.resolve("jarSrc"); 222 tb.writeJavaFiles(jarSrc, 223 "module m1x { exports p; }", 224 "package p; public class CC { }"); 225 Path jarClasses = base.resolve("jarClasses"); 226 Files.createDirectories(jarClasses); 227 228 new JavacTask(tb, Task.Mode.CMDLINE) 229 .outdir(jarClasses) 230 .files(findJavaFiles(jarSrc)) 231 .run() 232 .writeAll(); 233 234 Path moduleJar = base.resolve("myModule.jar"); // deliberately not m1 235 new JarTask(tb, moduleJar) 236 .baseDir(jarClasses) 237 .files("module-info.class", "p/CC.class") 238 .run(); 239 240 Path src = base.resolve("src"); 241 tb.writeJavaFiles(src, 242 "module m { requires m1x ; }", 243 "class C { }"); 244 Path classes = base.resolve("classes"); 245 Files.createDirectories(classes); 246 247 new JavacTask(tb, Task.Mode.CMDLINE) 248 .outdir(classes) 249 .options("--module-path", moduleJar.toString()) 250 .files(findJavaFiles(src)) 251 .run() 252 .writeAll(); 253 } 254 255 @Test 256 public void testBadJarOnPath(Path base) throws Exception { 257 Path src = base.resolve("src"); 258 tb.writeJavaFiles(src, "class C { }"); 259 tb.writeFile("dummy.jar", ""); 260 261 String log = new JavacTask(tb, Task.Mode.CMDLINE) 262 .options("-XDrawDiagnostics", 263 "--module-path", "dummy.jar") 264 .files(findJavaFiles(src)) 265 .run(Task.Expect.FAIL) 266 .writeAll() 267 .getOutput(Task.OutputKind.DIRECT); 268 269 if (!log.contains("- compiler.err.locn.cant.read.file: dummy.jar")) 270 throw new Exception("expected output not found"); 271 } 272 273 @Test 274 public void testJModOnPath(Path base) throws Exception { 275 Path jmodSrc = base.resolve("jmodSrc"); 276 tb.writeJavaFiles(jmodSrc, 277 "module m1x { exports p; }", 278 "package p; public class CC { }"); 279 Path jmodClasses = base.resolve("jmodClasses"); 280 Files.createDirectories(jmodClasses); 281 282 new JavacTask(tb, Task.Mode.CMDLINE) 283 .outdir(jmodClasses) 284 .files(findJavaFiles(jmodSrc)) 285 .run() 286 .writeAll(); 287 288 Path jmod = base.resolve("myModule.jmod"); // deliberately not m1 289 jmod(jmodClasses, jmod); 290 291 Path src = base.resolve("src"); 292 tb.writeJavaFiles(src, 293 "module m { requires m1x ; }", 294 "class C { }"); 295 Path classes = base.resolve("classes"); 296 Files.createDirectories(classes); 297 298 new JavacTask(tb, Task.Mode.CMDLINE) 299 .outdir(classes) 300 .options("--module-path", jmod.toString()) 301 .files(findJavaFiles(src)) 302 .run() 303 .writeAll(); 304 } 305 306 @Test 307 public void testBadJModOnPath(Path base) throws Exception { 308 Path src = base.resolve("src"); 309 tb.writeJavaFiles(src, "class C { }"); 310 tb.writeFile("dummy.jmod", ""); 311 312 String log = new JavacTask(tb, Task.Mode.CMDLINE) 313 .options("-XDrawDiagnostics", 314 "--module-path", "dummy.jmod") 315 .files(findJavaFiles(src)) 316 .run(Task.Expect.FAIL) 317 .writeAll() 318 .getOutput(Task.OutputKind.DIRECT); 319 320 if (!log.contains("- compiler.err.locn.cant.read.file: dummy.jmod")) 321 throw new Exception("expected output not found"); 322 } 323 324 @Test 325 public void relativePath(Path base) throws Exception { 326 Path modules = base.resolve("modules"); 327 new ModuleBuilder(tb, "m1x").build(modules); 328 329 Path src = base.resolve("src"); 330 tb.writeJavaFiles(src, "module m2x { requires m1x; }", "class A { }"); 331 332 new JavacTask(tb, Task.Mode.CMDLINE) 333 .options("-XDrawDiagnostics", 334 "--module-path", modules + "/./../modules") 335 .files(findJavaFiles(src)) 336 .run() 337 .writeAll(); 338 } 339 340 @Test 341 public void duplicatePaths_1(Path base) throws Exception { 342 Path modules = base.resolve("modules"); 343 new ModuleBuilder(tb, "m1x").build(modules); 344 345 Path src = base.resolve("src"); 346 tb.writeJavaFiles(src, "module m2x { requires m1x; }", "class A { }"); 347 348 new JavacTask(tb, Task.Mode.CMDLINE) 349 .options("-XDrawDiagnostics", 350 "--module-path", modules + "/./../modules" + PATH_SEP + modules) 351 .files(findJavaFiles(src)) 352 .run() 353 .writeAll(); 354 } 355 356 @Test 357 public void duplicatePaths_2(Path base) throws Exception { 358 Path modules = base.resolve("modules"); 359 new ModuleBuilder(tb, "m1x").build(modules); 360 361 Path src = base.resolve("src"); 362 tb.writeJavaFiles(src, "module m2x { requires m1x; }", "class A { }"); 363 364 new JavacTask(tb, Task.Mode.CMDLINE) 365 .options("-XDrawDiagnostics", 366 "--module-path", modules.toString(), 367 "--module-path", modules.toString()) 368 .files(findJavaFiles(src)) 369 .run() 370 .writeAll(); 371 } 372 373 @Test 374 public void oneModuleHidesAnother(Path base) throws Exception { 375 Path modules = base.resolve("modules"); 376 new ModuleBuilder(tb, "m1x") 377 .exports("pkg1") 378 .classes("package pkg1; public class E { }") 379 .build(modules); 380 381 Path deepModuleDirSrc = base.resolve("deepModuleDirSrc"); 382 Path deepModuleDir = modules.resolve("deepModuleDir"); 383 new ModuleBuilder(tb, "m1x") 384 .exports("pkg2") 385 .classes("package pkg2; public class E { }") 386 .build(deepModuleDirSrc, deepModuleDir); 387 388 Path src = base.resolve("src"); 389 tb.writeJavaFiles(src, "module m2x { requires m1x; }", " package p; class A { void main() { pkg2.E.class.getName(); } }"); 390 391 new JavacTask(tb, Task.Mode.CMDLINE) 392 .options("-XDrawDiagnostics", 393 "--module-path", deepModuleDir + PATH_SEP + modules) 394 .files(findJavaFiles(src)) 395 .run() 396 .writeAll(); 397 } 398 399 @Test 400 public void modulesInDifferentContainers(Path base) throws Exception { 401 Path modules = base.resolve("modules"); 402 new ModuleBuilder(tb, "m1x") 403 .exports("one") 404 .classes("package one; public class A { }") 405 .build(modules); 406 407 new ModuleBuilder(tb, "m2x") 408 .requires("m1x", modules) 409 .build(base.resolve("tmp")); 410 jar(base.resolve("tmp/m2x"), modules.resolve("m2x.jar")); 411 412 new ModuleBuilder(tb, "m3x") 413 .requires("m2x", modules) 414 .build(base.resolve("tmp")); 415 jmod(base.resolve("tmp/m3x"), modules.resolve("m3x.jmod")); 416 417 Path src = base.resolve("src"); 418 tb.writeJavaFiles(src, "module m { requires m3x; requires m2x; requires m1x; }", 419 "package p; class A { void main() { one.A.class.getName(); } }"); 420 421 new JavacTask(tb, Task.Mode.CMDLINE) 422 .options("-XDrawDiagnostics", 423 "--module-path", modules.toString()) 424 .files(findJavaFiles(src)) 425 .run() 426 .writeAll(); 427 } 428 429 private void jar(Path dir, Path jar) throws IOException { 430 new JarTask(tb, jar) 431 .baseDir(dir) 432 .files(".") 433 .run() 434 .writeAll(); 435 } 436 437 private void jmod(Path dir, Path jmod) throws Exception { 438 String[] args = { 439 "create", 440 "--class-path", dir.toString(), 441 jmod.toString() 442 }; 443 ToolProvider jmodTool = ToolProvider.findFirst("jmod").orElseThrow(() -> 444 new RuntimeException("jmod tool not found") 445 ); 446 jmodTool.run(System.out, System.err, args); 447 } 448} 449