|
1 /* |
|
2 * Copyright (c) 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 * @bug 8145013 |
|
27 * @summary Javac doesn't report warnings/errors if module provides unexported service and doesn't use it itself |
|
28 * @library /tools/lib |
|
29 * @modules |
|
30 * jdk.compiler/com.sun.tools.javac.api |
|
31 * jdk.compiler/com.sun.tools.javac.main |
|
32 * @build toolbox.ToolBox toolbox.JavacTask ModuleTestBase |
|
33 * @run main ServiceProvidedButNotExportedOrUsedTest |
|
34 */ |
|
35 |
|
36 import java.nio.file.Files; |
|
37 import java.nio.file.Path; |
|
38 import java.util.Arrays; |
|
39 import java.util.List; |
|
40 |
|
41 import toolbox.JavacTask; |
|
42 import toolbox.Task; |
|
43 import toolbox.ToolBox; |
|
44 |
|
45 public class ServiceProvidedButNotExportedOrUsedTest extends ModuleTestBase { |
|
46 public static void main(String... args) throws Exception { |
|
47 ServiceProvidedButNotExportedOrUsedTest t = new ServiceProvidedButNotExportedOrUsedTest(); |
|
48 t.runTests(); |
|
49 } |
|
50 |
|
51 @Test |
|
52 public void testWarning(Path base) throws Exception { |
|
53 Path src = base.resolve("src"); |
|
54 tb.writeJavaFiles(src, |
|
55 "module m { provides p1.C1 with p2.C2; }", |
|
56 "package p1; public class C1 { }", |
|
57 "package p2; public class C2 extends p1.C1 { }"); |
|
58 Path classes = base.resolve("classes"); |
|
59 Files.createDirectories(classes); |
|
60 |
|
61 List<String> output = new JavacTask(tb) |
|
62 .outdir(classes) |
|
63 .options("-Werror", "-XDrawDiagnostics") |
|
64 .files(findJavaFiles(src)) |
|
65 .run(Task.Expect.FAIL) |
|
66 .writeAll() |
|
67 .getOutputLines(Task.OutputKind.DIRECT); |
|
68 List<String> expected = Arrays.asList( |
|
69 "module-info.java:1:12: compiler.warn.service.provided.but.not.exported.or.used: p1.C1", |
|
70 "- compiler.err.warnings.and.werror", |
|
71 "1 error", |
|
72 "1 warning"); |
|
73 if (!output.containsAll(expected)) { |
|
74 throw new Exception("Expected output not found"); |
|
75 } |
|
76 } |
|
77 |
|
78 @Test |
|
79 public void testImplementationMustBeInSameModuleAsProvidesDirective(Path base) throws Exception { |
|
80 Path src = base.resolve("src"); |
|
81 tb.writeJavaFiles(src.resolve("m1x"), |
|
82 "module m1x { exports p1; }", |
|
83 "package p1; public class C1 { }"); |
|
84 tb.writeJavaFiles(src.resolve("m2x"), |
|
85 "module m2x { requires m1x; requires m3x; provides p1.C1 with p2.C2; }"); |
|
86 tb.writeJavaFiles(src.resolve("m3x"), |
|
87 "module m3x { requires m1x; exports p2; }", |
|
88 "package p2; public class C2 extends p1.C1 { }"); |
|
89 Path modules = base.resolve("modules"); |
|
90 Files.createDirectories(modules); |
|
91 |
|
92 List<String> output = new JavacTask(tb) |
|
93 .options("-XDrawDiagnostics", "--module-source-path", src.toString()) |
|
94 .outdir(modules) |
|
95 .files(findJavaFiles(src)) |
|
96 .run(Task.Expect.FAIL) |
|
97 .writeAll() |
|
98 .getOutputLines(Task.OutputKind.DIRECT); |
|
99 List<String> expected = Arrays.asList( |
|
100 "module-info.java:1:42: compiler.err.service.implementation.not.in.right.module: m3x", |
|
101 "1 error"); |
|
102 if (!output.containsAll(expected)) { |
|
103 throw new Exception("Expected output not found"); |
|
104 } |
|
105 } |
|
106 } |