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
|
/*
* Copyright (c) 2015, 2017, 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
* @summary simple tests of module provides
* @bug 8168854 8172807
* @library /tools/lib
* @modules
* jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
* @build toolbox.ToolBox toolbox.JavacTask ModuleTestBase
* @run main ProvidesTest
*/
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import toolbox.JavacTask;
import toolbox.Task;
import toolbox.Task.Expect;
public class ProvidesTest extends ModuleTestBase {
public static void main(String... args) throws Exception {
ProvidesTest t = new ProvidesTest();
t.runTests();
}
@Test
public void testSimple(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"module m { provides p1.C1 with p2.C2; }",
"package p1; public class C1 { }",
"package p2; public class C2 extends p1.C1 { }");
Path classes = base.resolve("classes");
Files.createDirectories(classes);
new JavacTask(tb)
.outdir(classes)
.files(findJavaFiles(src))
.run(Task.Expect.SUCCESS)
.writeAll();
}
@Test
public void testMulti(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src.resolve("m1x"),
"module m1x { exports p1; }",
"package p1; public class C1 { }");
tb.writeJavaFiles(src.resolve("m2x"),
"module m2x { requires m1x; provides p1.C1 with p2.C2; }",
"package p2; public class C2 extends p1.C1 { }");
Path modules = base.resolve("modules");
Files.createDirectories(modules);
new JavacTask(tb)
.options("--module-source-path", src.toString())
.outdir(modules)
.files(findJavaFiles(src))
.run(Task.Expect.SUCCESS)
.writeAll();
}
@Test
public void testMissingWith(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"module m { provides p.C; }",
"package p; public class C { }");
Path classes = base.resolve("classes");
Files.createDirectories(classes);
String log = new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(classes)
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
if (!log.contains("module-info.java:1:24: compiler.err.expected: 'with'"))
throw new Exception("expected output not found");
}
@Test
public void testDuplicateImplementations1(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"module m { exports p1; exports p2; provides p1.C1 with p2.C2, p2.C2; }",
"package p1; public class C1 { }",
"package p2; public class C2 extends p1.C1 { }");
Path classes = base.resolve("classes");
Files.createDirectories(classes);
List<String> output = new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(classes)
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected = Arrays.asList(
"module-info.java:1:65: compiler.err.duplicate.provides: p1.C1, p2.C2",
"1 error");
if (!output.containsAll(expected)) {
throw new Exception("Expected output not found");
}
}
@Test
public void testDuplicateImplementations2(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"module m { exports p1; provides p1.C1 with p2.C2; provides p1.C1 with p2.C2; }",
"package p1; public class C1 { }",
"package p2; public class C2 extends p1.C1 { }");
Path classes = base.resolve("classes");
Files.createDirectories(classes);
List<String> output = new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(classes)
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected = Arrays.asList(
"module-info.java:1:62: compiler.err.repeated.provides.for.service: p1.C1",
"module-info.java:1:73: compiler.err.duplicate.provides: p1.C1, p2.C2",
"2 errors");
if (!output.containsAll(expected)) {
throw new Exception("Expected output not found");
}
}
@Test
public void testMissingService(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"module m { provides p.Missing with p.C; }",
"package p; public class C extends p.Missing { }");
List<String> output = new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected = Arrays.asList(
"C.java:1:36: compiler.err.cant.resolve.location: kindname.class, Missing, , , (compiler.misc.location: kindname.package, p, null)",
"module-info.java:1:22: compiler.err.cant.resolve.location: kindname.class, Missing, , , (compiler.misc.location: kindname.package, p, null)",
"2 errors");
if (!output.containsAll(expected)) {
throw new Exception("Expected output not found");
}
}
@Test
public void testProvidesFromAnotherModule(Path base) throws Exception {
Path modules = base.resolve("modules");
tb.writeJavaFiles(modules.resolve("M"),
"module M { exports p; }",
"package p; public class Service { }");
tb.writeJavaFiles(modules.resolve("L"),
"module L { requires M; provides p.Service with p.Service; }");
List<String> output = new JavacTask(tb)
.options("-XDrawDiagnostics",
"--module-source-path", modules.toString())
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(modules))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected = Arrays.asList(
"module-info.java:1:24: compiler.err.service.implementation.not.in.right.module: M",
"1 error");
if (!output.containsAll(expected)) {
throw new Exception("Expected output not found");
}
}
@Test
public void testServiceIsNotImplemented(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"module m { provides p.A with p.B; }",
"package p; public class A { }",
"package p; public class B { }");
List<String> output = new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected = Arrays.asList(
"module-info.java:1:31: compiler.err.service.implementation.must.be.subtype.of.service.interface",
"module-info.java:1:12: compiler.warn.service.provided.but.not.exported.or.used: p.A",
"1 error",
"1 warning");
if (!output.containsAll(expected)) {
throw new Exception("Expected output not found");
}
}
@Test
public void testMissingImplementation(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"module m { provides p.C with p.Impl; }",
"package p; public class C { }");
List<String> output = new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected = Arrays.asList("module-info.java:1:31: compiler.err.cant.resolve.location: kindname.class, Impl, , , (compiler.misc.location: kindname.package, p, null)",
"1 error");
if (!output.containsAll(expected)) {
throw new Exception("Expected output not found");
}
}
@Test
public void testSeveralImplementations(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"module m { provides p.C with p.Impl1, p.Impl2; }",
"package p; public class C { }",
"package p; public class Impl1 extends p.C { }",
"package p; public class Impl2 extends p.C { }");
new JavacTask(tb)
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(src))
.run(Task.Expect.SUCCESS)
.writeAll();
}
@Test
public void testRepeatedProvides(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"module m { exports p; provides p.C with p.Impl1; provides p.C with p.Impl2; }",
"package p; public class C { }",
"package p; public class Impl1 extends p.C { }",
"package p; public class Impl2 extends p.C { }");
List<String> output = new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected = Arrays.asList("module-info.java:1:60: compiler.err.repeated.provides.for.service: p.C",
"1 error");
if (!output.containsAll(expected)) {
throw new Exception("Expected output not found");
}
}
@Test
public void testOneImplementationsForServices(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"module m { provides p.Service1 with p.Impl; provides p.Service2 with p.Impl; }",
"package p; public interface Service1 { }",
"package p; public abstract class Service2 { }",
"package p; public class Impl extends p.Service2 implements p.Service1 { }");
new JavacTask(tb)
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(src))
.run(Task.Expect.SUCCESS)
.writeAll();
}
@Test
public void testAbstractImplementation(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"module m { provides p1.C1 with p2.C2; }",
"package p1; public class C1 { }",
"package p2; public abstract class C2 extends p1.C1 { }");
List<String> output = new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected = Arrays.asList(
"module-info.java:1:34: compiler.err.service.implementation.is.abstract: p2.C2");
if (!output.containsAll(expected)) {
throw new Exception("Expected output not found");
}
}
@Test
public void testInterfaceImplementation(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"module m { provides p1.Service with p2.Impl; }",
"package p1; public interface Service { }",
"package p2; public interface Impl extends p1.Service { }");
List<String> output = new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected = Arrays.asList(
"module-info.java:1:39: compiler.err.service.implementation.is.abstract: p2.Impl");
if (!output.containsAll(expected)) {
throw new Exception("Expected output not found");
}
}
@Test
public void testProtectedImplementation(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"module m { provides p1.C1 with p2.C2; }",
"package p1; public class C1 { }",
"package p2; class C2 extends p1.C1 { }");
List<String> output = new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected = Arrays.asList("module-info.java:1:34: compiler.err.not.def.public: p2.C2, p2",
"1 error");
if (!output.containsAll(expected)) {
throw new Exception("Expected output not found");
}
}
@Test
public void testNoNoArgConstructor(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"module m { uses p1.C1; provides p1.C1 with p2.C2; }",
"package p1; public class C1 { }",
"package p2; public class C2 extends p1.C1 { public C2(String str) { } }");
List<String> output = new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected = Arrays.asList(
"module-info.java:1:46: compiler.err.service.implementation.doesnt.have.a.no.args.constructor: p2.C2");
if (!output.containsAll(expected)) {
throw new Exception("Expected output not found");
}
}
@Test
public void testPrivateNoArgConstructor(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"module m { uses p1.C1; provides p1.C1 with p2.C2; }",
"package p1; public class C1 { }",
"package p2; public class C2 extends p1.C1 { private C2() { } }");
List<String> output = new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected = Arrays.asList(
"module-info.java:1:46: compiler.err.service.implementation.no.args.constructor.not.public: p2.C2");
if (!output.containsAll(expected)) {
throw new Exception("Expected output not found");
}
}
@Test
public void testServiceIndirectlyImplemented(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"module m { provides p1.C1 with p2.C3; }",
"package p1; public class C1 { }",
"package p2; public class C2 extends p1.C1 { }",
"package p2; public class C3 extends p2.C2 { }");
new JavacTask(tb)
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(src))
.run(Task.Expect.SUCCESS)
.writeAll();
}
@Test
public void testServiceImplementationInnerClass(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"module m { provides p1.C1 with p2.C2.Inner; }",
"package p1; public class C1 { }",
"package p2; public class C2 { public class Inner extends p1.C1 { } }");
List<String> output = new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expected = Arrays.asList(
"module-info.java:1:37: compiler.err.service.implementation.is.inner: p2.C2.Inner");
if (!output.containsAll(expected)) {
throw new Exception("Expected output not found");
}
}
@Test
public void testServiceDefinitionInnerClass(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"module m { provides p1.C1.InnerDefinition with p2.C2; }",
"package p1; public class C1 { public class InnerDefinition { } }",
"package p2; public class C2 extends p1.C1.InnerDefinition { public C2() { new p1.C1().super(); } }");
new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(src))
.run(Expect.SUCCESS)
.writeAll();
}
@Test
public void testFactory(Path base) throws Exception {
Path src = base.resolve("src");
tb.writeJavaFiles(src,
"module m { exports p1; provides p1.C1 with p2.C2; }",
"package p1; public interface C1 { }",
"package p2; public class C2 { public static p1.C1 provider() { return null; } }");
new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(src))
.run()
.writeAll()
.getOutput(Task.OutputKind.DIRECT);
List<String> output;
List<String> expected;
tb.writeJavaFiles(src,
"package p2; public class C2 { public p1.C1 provider() { return null; } }");
output = new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
expected = Arrays.asList("module-info.java:1:46: compiler.err.service.implementation.must.be.subtype.of.service.interface",
"1 error");
if (!expected.equals(output)) {
throw new Exception("Expected output not found. Output: " + output);
}
tb.writeJavaFiles(src,
"package p2; public class C2 { static p1.C1 provider() { return null; } }");
output = new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
expected = Arrays.asList("module-info.java:1:46: compiler.err.service.implementation.must.be.subtype.of.service.interface",
"1 error");
if (!expected.equals(output)) {
throw new Exception("Expected output not found. Output: " + output);
}
tb.writeJavaFiles(src,
"package p2; public class C2 { public static Object provider() { return null; } }");
output = new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
expected = Arrays.asList("module-info.java:1:46: compiler.err.service.implementation.provider.return.must.be.subtype.of.service.interface",
"1 error");
if (!expected.equals(output)) {
throw new Exception("Expected output not found. Output: " + output);
}
tb.writeJavaFiles(src,
"package p2; public class C2 { public static p1.C1 provider = new p1.C1() {}; }");
output = new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(Files.createDirectories(base.resolve("classes")))
.files(findJavaFiles(src))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
expected = Arrays.asList("module-info.java:1:46: compiler.err.service.implementation.must.be.subtype.of.service.interface",
"1 error");
if (!expected.equals(output)) {
throw new Exception("Expected output not found. Output: " + output);
}
}
}
|