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