1/*
2 * Copyright (c) 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 8174243
27 * @summary incorrect error message for nested service provider
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 WrongErrorMessageForNestedServiceProviderTest
34 */
35
36import java.nio.file.Files;
37import java.nio.file.Path;
38import java.util.Arrays;
39import java.util.List;
40
41import toolbox.JavacTask;
42import toolbox.Task;
43import toolbox.ToolBox;
44
45public class WrongErrorMessageForNestedServiceProviderTest extends ModuleTestBase {
46    public static void main(String... args) throws Exception {
47        WrongErrorMessageForNestedServiceProviderTest t = new WrongErrorMessageForNestedServiceProviderTest();
48        t.runTests();
49    }
50
51    private static final String twoServicesModuleDef =
52            "module m {\n" +
53            "    exports example;\n" +
54            "    provides example.SomeService with example.ServiceImpl;\n" +
55            "    provides example.SomeServiceOuter with example.Outer.ServiceImplOuter;\n" +
56            "}";
57
58    private static final String someServiceInt =
59            "package example;\n" +
60            "public interface SomeService {\n" +
61            "    public void foo();\n" +
62            "}";
63
64    private static final String someServiceIntOuter =
65            "package example;\n" +
66            "public interface SomeServiceOuter {\n" +
67            "    public void foo();\n" +
68            "}";
69
70    @Test
71    public void testPositive(Path base) throws Exception {
72        Path src = base.resolve("src");
73        tb.writeJavaFiles(src,
74                twoServicesModuleDef,
75                someServiceInt,
76                someServiceIntOuter,
77                "package example;\n" +
78                "public class ServiceImpl implements example.SomeService {\n" +
79                "    public ServiceImpl() {}\n" +
80                "    public void foo() {}\n" +
81                "}",
82
83                "package example;\n" +
84                "class Outer {\n" +
85                "    public static class ServiceImplOuter implements example.SomeServiceOuter {\n" +
86                "        public ServiceImplOuter() {}\n" +
87                "        public void foo() {}\n" +
88                "    }\n" +
89                "}");
90        Path classes = base.resolve("classes");
91        Files.createDirectories(classes);
92
93        List<String> output = new JavacTask(tb)
94                .outdir(classes)
95                .options("-Werror", "-XDrawDiagnostics")
96                .files(findJavaFiles(src))
97                .run(Task.Expect.SUCCESS)
98                .writeAll()
99                .getOutputLines(Task.OutputKind.DIRECT);
100        List<String> expected = Arrays.asList("");
101        if (!output.containsAll(expected)) {
102            throw new Exception("Expected output not found");
103        }
104    }
105
106    @Test
107    public void testNegative(Path base) throws Exception {
108        Path src = base.resolve("src");
109        tb.writeJavaFiles(src,
110                twoServicesModuleDef,
111                someServiceInt,
112                someServiceIntOuter,
113
114                "package example;\n" +
115                "class ServiceImpl implements example.SomeService {\n" +
116                "    public ServiceImpl() {}\n" +
117                "    public void foo() {}\n" +
118                "}",
119
120                "package example;\n" +
121                "class Outer {\n" +
122                "    static class ServiceImplOuter implements example.SomeServiceOuter {\n" +
123                "        public ServiceImplOuter() {}\n" +
124                "        public void foo() {}\n" +
125                "    }\n" +
126                "}");
127        Path classes = base.resolve("classes");
128        Files.createDirectories(classes);
129
130        List<String> output = new JavacTask(tb)
131                .outdir(classes)
132                .options("-Werror", "-XDrawDiagnostics")
133                .files(findJavaFiles(src))
134                .run(Task.Expect.FAIL)
135                .writeAll()
136                .getOutputLines(Task.OutputKind.DIRECT);
137        List<String> expected = Arrays.asList(
138                "module-info.java:3:46: compiler.err.not.def.public: example.ServiceImpl, example",
139                "module-info.java:4:57: compiler.err.not.def.public: example.Outer.ServiceImplOuter, example.Outer",
140                "2 errors");
141        if (!output.containsAll(expected)) {
142            throw new Exception("Expected output not found");
143        }
144    }
145
146    @Test
147    public void testClassWrappedByPrivateClass(Path base) throws Exception {
148        Path src = base.resolve("src");
149        tb.writeJavaFiles(src,
150                "module m {\n" +
151                "    exports example;\n" +
152                "    provides example.SomeServiceOuter with example.Outer1.Outer2.ServiceImplOuter;\n" +
153                "}",
154
155                someServiceIntOuter,
156
157                "package example;\n" +
158                "class Outer1 {\n" +
159                "    static private class Outer2 {\n" +
160                "        public static class ServiceImplOuter implements example.SomeServiceOuter {\n" +
161                "            public ServiceImplOuter() {}\n" +
162                "            public void foo() {}\n" +
163                "        }\n" +
164                "    }\n" +
165                "}");
166        Path classes = base.resolve("classes");
167        Files.createDirectories(classes);
168
169        List<String> output = new JavacTask(tb)
170                .outdir(classes)
171                .options("-Werror", "-XDrawDiagnostics")
172                .files(findJavaFiles(src))
173                .run(Task.Expect.SUCCESS)
174                .writeAll()
175                .getOutputLines(Task.OutputKind.DIRECT);
176        List<String> expected = Arrays.asList("");
177        if (!output.containsAll(expected)) {
178            throw new Exception("Expected output not found");
179        }
180    }
181}
182