1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
|
/*
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8155026 8178011
* @summary Test automatic modules
* @library /tools/lib
* @modules
* java.desktop
* jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
* @build toolbox.ToolBox toolbox.JavacTask toolbox.JarTask ModuleTestBase
* @run main AutomaticModules
*/
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import toolbox.JarTask;
import toolbox.JavacTask;
import toolbox.Task;
public class AutomaticModules extends ModuleTestBase {
public static void main(String... args) throws Exception {
AutomaticModules t = new AutomaticModules();
t.runTests();
}
@Test
public void testSimple(Path base) throws Exception {
Path legacySrc = base.resolve("legacy-src");
tb.writeJavaFiles(legacySrc,
"package api; import java.awt.event.ActionListener; public abstract class Api implements ActionListener {}");
Path legacyClasses = base.resolve("legacy-classes");
Files.createDirectories(legacyClasses);
String log = new JavacTask(tb)
.options()
.outdir(legacyClasses)
.files(findJavaFiles(legacySrc))
.run()
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
if (!log.isEmpty()) {
throw new Exception("unexpected output: " + log);
}
Path modulePath = base.resolve("module-path");
Files.createDirectories(modulePath);
Path jar = modulePath.resolve("test-api-1.0.jar");
new JarTask(tb, jar)
.baseDir(legacyClasses)
.files("api/Api.class")
.run();
Path moduleSrc = base.resolve("module-src");
Path m1 = moduleSrc.resolve("m1x");
Path classes = base.resolve("classes");
Files.createDirectories(classes);
tb.writeJavaFiles(m1,
"module m1x { requires test.api; requires java.desktop; }",
"package impl; public class Impl { public void e(api.Api api) { api.actionPerformed(null); } }");
new JavacTask(tb)
.options("--module-source-path", moduleSrc.toString(), "--module-path", modulePath.toString())
.outdir(classes)
.files(findJavaFiles(moduleSrc))
.run()
.writeAll();
}
@Test
public void testUnnamedModule(Path base) throws Exception {
Path legacySrc = base.resolve("legacy-src");
tb.writeJavaFiles(legacySrc,
"package api; public abstract class Api { public void run(CharSequence str) { } private void run(base.Base base) { } }",
"package base; public interface Base { public void run(); }");
Path legacyClasses = base.resolve("legacy-classes");
Files.createDirectories(legacyClasses);
String log = new JavacTask(tb)
.options()
.outdir(legacyClasses)
.files(findJavaFiles(legacySrc))
.run()
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
if (!log.isEmpty()) {
throw new Exception("unexpected output: " + log);
}
Path modulePath = base.resolve("module-path");
Files.createDirectories(modulePath);
Path apiJar = modulePath.resolve("test-api-1.0.jar");
new JarTask(tb, apiJar)
.baseDir(legacyClasses)
.files("api/Api.class")
.run();
Path baseJar = base.resolve("base.jar");
new JarTask(tb, baseJar)
.baseDir(legacyClasses)
.files("base/Base.class")
.run();
Path moduleSrc = base.resolve("module-src");
Path m1 = moduleSrc.resolve("m1x");
Path classes = base.resolve("classes");
Files.createDirectories(classes);
tb.writeJavaFiles(m1,
"module m1x { requires test.api; }",
"package impl; public class Impl { public void e(api.Api api) { api.run(\"\"); } }");
new JavacTask(tb)
.options("--module-source-path", moduleSrc.toString(), "--module-path", modulePath.toString(), "--class-path", baseJar.toString())
.outdir(classes)
.files(findJavaFiles(moduleSrc))
.run()
.writeAll();
}
@Test
public void testModuleInfoFromClassFileDependsOnAutomatic(Path base) throws Exception {
Path automaticSrc = base.resolve("automaticSrc");
tb.writeJavaFiles(automaticSrc, "package api; public class Api {}");
Path automaticClasses = base.resolve("automaticClasses");
tb.createDirectories(automaticClasses);
String automaticLog = new JavacTask(tb)
.outdir(automaticClasses)
.files(findJavaFiles(automaticSrc))
.run()
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
if (!automaticLog.isEmpty())
throw new Exception("expected output not found: " + automaticLog);
Path modulePath = base.resolve("module-path");
Files.createDirectories(modulePath);
Path automaticJar = modulePath.resolve("automatic-1.0.jar");
new JarTask(tb, automaticJar)
.baseDir(automaticClasses)
.files("api/Api.class")
.run();
Path depSrc = base.resolve("depSrc");
Path depClasses = base.resolve("depClasses");
Files.createDirectories(depSrc);
Files.createDirectories(depClasses);
tb.writeJavaFiles(depSrc,
"module m1x { requires transitive automatic; }",
"package dep; public class Dep { api.Api api; }");
new JavacTask(tb)
.options("--module-path", modulePath.toString())
.outdir(depClasses)
.files(findJavaFiles(depSrc))
.run()
.writeAll();
Path moduleJar = modulePath.resolve("m1x.jar");
new JarTask(tb, moduleJar)
.baseDir(depClasses)
.files("module-info.class", "dep/Dep.class")
.run();
Path testSrc = base.resolve("testSrc");
Path testClasses = base.resolve("testClasses");
Files.createDirectories(testSrc);
Files.createDirectories(testClasses);
tb.writeJavaFiles(testSrc,
"module m2x { requires automatic; }",
"package test; public class Test { }");
new JavacTask(tb)
.options("--module-path", modulePath.toString())
.outdir(testClasses)
.files(findJavaFiles(testSrc))
.run()
.writeAll();
}
@Test
public void testAutomaticAndNamedModules(Path base) throws Exception {
Path modulePath = base.resolve("module-path");
Files.createDirectories(modulePath);
for (char c : new char[] {'A', 'B'}) {
Path automaticSrc = base.resolve("automaticSrc" + c);
tb.writeJavaFiles(automaticSrc, "package api" + c + "; public class Api {}");
Path automaticClasses = base.resolve("automaticClasses" + c);
tb.createDirectories(automaticClasses);
String automaticLog = new JavacTask(tb)
.outdir(automaticClasses)
.files(findJavaFiles(automaticSrc))
.run()
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
if (!automaticLog.isEmpty())
throw new Exception("expected output not found: " + automaticLog);
Path automaticJar = modulePath.resolve("automatic" + c + "-1.0.jar");
new JarTask(tb, automaticJar)
.baseDir(automaticClasses)
.files("api" + c + "/Api.class")
.run();
}
Path moduleSrc = base.resolve("module-src");
tb.writeJavaFiles(moduleSrc.resolve("m1x"),
"module m1x { requires static automaticA; }",
"package impl; public class Impl { apiA.Api a; apiB.Api b; m2x.M2 m;}");
tb.writeJavaFiles(moduleSrc.resolve("m2x"),
"module m2x { exports m2x; }",
"package m2x; public class M2 { }");
Path classes = base.resolve("classes");
Files.createDirectories(classes);
List<String> log = new JavacTask(tb)
.options("--module-source-path", moduleSrc.toString(),
"--module-path", modulePath.toString(),
"--add-modules", "automaticB",
"-XDrawDiagnostics")
.outdir(classes)
.files(findJavaFiles(moduleSrc))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected = Arrays.asList("Impl.java:1:59: compiler.err.package.not.visible: m2x, (compiler.misc.not.def.access.does.not.read: m1x, m2x, m2x)",
"1 error");
if (!expected.equals(log)) {
throw new Exception("expected output not found: " + log);
}
log = new JavacTask(tb)
.options("--module-source-path", moduleSrc.toString(),
"--module-path", modulePath.toString(),
"-XDrawDiagnostics")
.outdir(classes)
.files(findJavaFiles(moduleSrc))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
expected = Arrays.asList("Impl.java:1:59: compiler.err.package.not.visible: m2x, (compiler.misc.not.def.access.does.not.read: m1x, m2x, m2x)",
"1 error");
if (!expected.equals(log)) {
throw new Exception("expected output not found: " + log);
}
}
@Test
public void testWithTrailingVersion(Path base) throws Exception {
Path legacySrc = base.resolve("legacy-src");
tb.writeJavaFiles(legacySrc,
"package api; public class Api {}");
Path legacyClasses = base.resolve("legacy-classes");
Files.createDirectories(legacyClasses);
String log = new JavacTask(tb)
.options()
.outdir(legacyClasses)
.files(findJavaFiles(legacySrc))
.run()
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
if (!log.isEmpty()) {
throw new Exception("unexpected output: " + log);
}
Path modulePath = base.resolve("module-path");
Files.createDirectories(modulePath);
Path jar = modulePath.resolve("test1.jar");
new JarTask(tb, jar)
.baseDir(legacyClasses)
.files("api/Api.class")
.run();
Path moduleSrc = base.resolve("module-src");
Path m = moduleSrc.resolve("m");
Path classes = base.resolve("classes");
Files.createDirectories(classes);
tb.writeJavaFiles(m,
"module m { requires test1; }",
"package impl; public class Impl { public void e(api.Api api) { } }");
new JavacTask(tb)
.options("--module-source-path", moduleSrc.toString(), "--module-path", modulePath.toString())
.outdir(classes)
.files(findJavaFiles(moduleSrc))
.run()
.writeAll();
}
@Test
public void testMultipleAutomatic(Path base) throws Exception {
Path modulePath = base.resolve("module-path");
Files.createDirectories(modulePath);
for (char c : new char[] {'A', 'B'}) {
Path automaticSrc = base.resolve("automaticSrc" + c);
tb.writeJavaFiles(automaticSrc, "package api" + c + "; public class Api {}");
Path automaticClasses = base.resolve("automaticClasses" + c);
tb.createDirectories(automaticClasses);
String automaticLog = new JavacTask(tb)
.outdir(automaticClasses)
.files(findJavaFiles(automaticSrc))
.run()
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
if (!automaticLog.isEmpty())
throw new Exception("expected output not found: " + automaticLog);
Path automaticJar = modulePath.resolve("automatic" + c + "-1.0.jar");
new JarTask(tb, automaticJar)
.baseDir(automaticClasses)
.files("api" + c + "/Api.class")
.run();
}
Path src = base.resolve("src");
tb.writeJavaFiles(src.resolve("m1x"),
"package impl; public class Impl { apiA.Api a; apiB.Api b; }");
Path classes = base.resolve("classes");
Files.createDirectories(classes);
List<String> log = new JavacTask(tb)
.options("--module-path", modulePath.toString(),
"-XDrawDiagnostics")
.outdir(classes)
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected = Arrays.asList("Impl.java:1:35: compiler.err.package.not.visible: apiA, (compiler.misc.not.def.access.does.not.read.from.unnamed: apiA, automaticA)",
"Impl.java:1:47: compiler.err.package.not.visible: apiB, (compiler.misc.not.def.access.does.not.read.from.unnamed: apiB, automaticB)",
"2 errors");
if (!expected.equals(log)) {
throw new Exception("expected output not found: " + log);
}
new JavacTask(tb)
.options("--module-path", modulePath.toString(),
"--add-modules", "automaticA",
"-XDrawDiagnostics")
.outdir(classes)
.files(findJavaFiles(src))
.run()
.writeAll();
}
@Test
public void testLintRequireAutomatic(Path base) throws Exception {
Path modulePath = base.resolve("module-path");
Files.createDirectories(modulePath);
for (char c : new char[] {'A', 'B'}) {
Path automaticSrc = base.resolve("automaticSrc" + c);
tb.writeJavaFiles(automaticSrc, "package api" + c + "; public class Api {}");
Path automaticClasses = base.resolve("automaticClasses" + c);
tb.createDirectories(automaticClasses);
String automaticLog = new JavacTask(tb)
.outdir(automaticClasses)
.files(findJavaFiles(automaticSrc))
.run()
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
if (!automaticLog.isEmpty())
throw new Exception("expected output not found: " + automaticLog);
Path automaticJar = modulePath.resolve("automatic" + c + "-1.0.jar");
new JarTask(tb, automaticJar)
.baseDir(automaticClasses)
.files("api" + c + "/Api.class")
.run();
}
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"module m1x {\n" +
" requires transitive automaticA;\n" +
" requires automaticB;\n" +
"}");
Path classes = base.resolve("classes");
Files.createDirectories(classes);
List<String> expected;
List<String> log;
log = new JavacTask(tb)
.options("--source-path", src.toString(),
"--module-path", modulePath.toString(),
"-XDrawDiagnostics",
"-Werror")
.outdir(classes)
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
expected = Arrays.asList("module-info.java:2:25: compiler.warn.requires.transitive.automatic",
"- compiler.err.warnings.and.werror",
"1 error",
"1 warning");
if (!expected.equals(log)) {
throw new Exception("expected output not found: " + log);
}
log = new JavacTask(tb)
.options("--source-path", src.toString(),
"--module-path", modulePath.toString(),
"-Xlint:requires-automatic",
"-XDrawDiagnostics",
"-Werror")
.outdir(classes)
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
expected = Arrays.asList("module-info.java:2:25: compiler.warn.requires.transitive.automatic",
"module-info.java:3:14: compiler.warn.requires.automatic",
"- compiler.err.warnings.and.werror",
"1 error",
"2 warnings");
if (!expected.equals(log)) {
throw new Exception("expected output not found: " + log);
}
log = new JavacTask(tb)
.options("--source-path", src.toString(),
"--module-path", modulePath.toString(),
"-Xlint:-requires-transitive-automatic,requires-automatic",
"-XDrawDiagnostics",
"-Werror")
.outdir(classes)
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
expected = Arrays.asList("module-info.java:2:25: compiler.warn.requires.automatic",
"module-info.java:3:14: compiler.warn.requires.automatic",
"- compiler.err.warnings.and.werror",
"1 error",
"2 warnings");
if (!expected.equals(log)) {
throw new Exception("expected output not found: " + log);
}
new JavacTask(tb)
.options("--source-path", src.toString(),
"--module-path", modulePath.toString(),
"-Xlint:-requires-transitive-automatic",
"-XDrawDiagnostics",
"-Werror")
.outdir(classes)
.files(findJavaFiles(src))
.run(Task.Expect.SUCCESS)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
tb.writeJavaFiles(src,
"@SuppressWarnings(\"requires-transitive-automatic\")\n" +
"module m1x {\n" +
" requires transitive automaticA;\n" +
" requires automaticB;\n" +
"}");
new JavacTask(tb)
.options("--source-path", src.toString(),
"--module-path", modulePath.toString(),
"-XDrawDiagnostics",
"-Werror")
.outdir(classes)
.files(findJavaFiles(src))
.run(Task.Expect.SUCCESS)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
log = new JavacTask(tb)
.options("--source-path", src.toString(),
"--module-path", modulePath.toString(),
"-Xlint:requires-automatic",
"-XDrawDiagnostics",
"-Werror")
.outdir(classes)
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
expected = Arrays.asList("module-info.java:3:25: compiler.warn.requires.automatic",
"module-info.java:4:14: compiler.warn.requires.automatic",
"- compiler.err.warnings.and.werror",
"1 error",
"2 warnings");
if (!expected.equals(log)) {
throw new Exception("expected output not found: " + log);
}
tb.writeJavaFiles(src,
"@SuppressWarnings(\"requires-automatic\")\n" +
"module m1x {\n" +
" requires transitive automaticA;\n" +
" requires automaticB;\n" +
"}");
log = new JavacTask(tb)
.options("--source-path", src.toString(),
"--module-path", modulePath.toString(),
"-Xlint:requires-automatic",
"-XDrawDiagnostics",
"-Werror")
.outdir(classes)
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
expected = Arrays.asList("module-info.java:3:25: compiler.warn.requires.transitive.automatic",
"- compiler.err.warnings.and.werror",
"1 error",
"1 warning");
if (!expected.equals(log)) {
throw new Exception("expected output not found: " + log);
}
}
}
|