UsesTest.java revision 3741:1fc501869aa8
1/*
2 * Copyright (c) 2015, 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 * @summary simple tests of module uses
27 * @library /tools/lib
28 * @modules
29 *      jdk.compiler/com.sun.tools.javac.api
30 *      jdk.compiler/com.sun.tools.javac.main
31 * @build toolbox.ToolBox toolbox.JavacTask toolbox.ModuleBuilder ModuleTestBase
32 * @run main UsesTest
33 */
34
35import java.nio.file.Files;
36import java.nio.file.Path;
37import java.util.Arrays;
38import java.util.Collection;
39import java.util.List;
40
41import toolbox.JavacTask;
42import toolbox.ModuleBuilder;
43import toolbox.Task;
44import toolbox.ToolBox;
45
46public class UsesTest extends ModuleTestBase {
47    public static void main(String... args) throws Exception {
48        UsesTest t = new UsesTest();
49        t.runTests();
50    }
51
52    @Test
53    public void testSimple(Path base) throws Exception {
54        Path src = base.resolve("src");
55        tb.writeJavaFiles(src,
56                "module m { uses p.C; }",
57                "package p; public class C { }");
58        Path classes = base.resolve("classes");
59        Files.createDirectories(classes);
60
61        new JavacTask(tb)
62                .outdir(classes)
63                .files(findJavaFiles(src))
64                .run(Task.Expect.SUCCESS)
65                .writeAll();
66    }
67
68    @Test
69    public void testSimpleInner(Path base) throws Exception {
70        Path src = base.resolve("src");
71        tb.writeJavaFiles(src,
72                "module m { uses p.C.Inner; }",
73                "package p; public class C { public class Inner { } }");
74        Path classes = base.resolve("classes");
75        Files.createDirectories(classes);
76
77        new JavacTask(tb)
78                .outdir(classes)
79                .files(findJavaFiles(src))
80                .run(Task.Expect.SUCCESS)
81                .writeAll();
82    }
83
84    @Test
85    public void testEnumAsAService(Path base) throws Exception {
86        Path src = base.resolve("src");
87        tb.writeJavaFiles(src,
88                "module m { uses pkg.EnumST; }",
89                "package pkg; public enum EnumST {A, B}");
90        Path classes = base.resolve("classes");
91        Files.createDirectories(classes);
92
93        List<String> output = new JavacTask(tb)
94                .options("-XDrawDiagnostics")
95                .outdir(classes)
96                .files(tb.findJavaFiles(src))
97                .run(Task.Expect.FAIL)
98                .writeAll()
99                .getOutputLines(Task.OutputKind.DIRECT);
100        List<String> expected = Arrays.asList("module-info.java:1:20: compiler.err.service.definition.is.enum: pkg.EnumST",
101                "1 error");
102        if (!output.containsAll(expected)) {
103            throw new Exception("Expected output not found");
104        }
105    }
106
107    @Test
108    public void testSimpleAnnotation(Path base) throws Exception {
109        Path src = base.resolve("src");
110        tb.writeJavaFiles(src,
111                "module m { uses p.C; }",
112                "package p; public @interface C { }");
113        Path classes = base.resolve("classes");
114        Files.createDirectories(classes);
115
116        new JavacTask(tb)
117                .outdir(classes)
118                .files(findJavaFiles(src))
119                .run(Task.Expect.SUCCESS)
120                .writeAll();
121    }
122
123    @Test
124    public void testPrivateService(Path base) throws Exception {
125        Path src = base.resolve("src");
126        tb.writeJavaFiles(src,
127                "module m { uses p.C.A; uses p.C; }",
128                "package p; public class C { protected class A { } }");
129
130        List<String> output = new JavacTask(tb)
131                .options("-XDrawDiagnostics")
132                .outdir(Files.createDirectories(base.resolve("classes")))
133                .files(findJavaFiles(src))
134                .run(Task.Expect.FAIL)
135                .writeAll()
136                .getOutputLines(Task.OutputKind.DIRECT);
137
138        List<String> expected = Arrays.asList("module-info.java:1:20: compiler.err.report.access: p.C.A, protected, p.C",
139                "1 error");
140        if (!output.containsAll(expected)) {
141            throw new Exception("Expected output not found");
142        }
143    }
144
145    @Test
146    public void testMulti(Path base) throws Exception {
147        Path src = base.resolve("src");
148        tb.writeJavaFiles(src.resolve("m1"),
149                "module m1 { exports p; }",
150                "package p; public class C { }");
151        tb.writeJavaFiles(src.resolve("m2"),
152                "module m2 { requires m1; uses p.C; }");
153        Path modules = base.resolve("modules");
154        Files.createDirectories(modules);
155
156        new JavacTask(tb)
157                .options("--module-source-path", src.toString())
158                .outdir(modules)
159                .files(findJavaFiles(src))
160                .run(Task.Expect.SUCCESS)
161                .writeAll();
162    }
163
164    @Test
165    public void testMultiOnModulePath(Path base) throws Exception {
166        Path modSrc = base.resolve("modSrc");
167        Path modules = base.resolve("modules");
168        new ModuleBuilder(tb, "m1")
169                .exports("p")
170                .classes("package p; public class C { }")
171                .build(modules);
172        new ModuleBuilder(tb, "m2")
173                .requires("m1")
174                .uses("p.C")
175                .write(modSrc);
176
177        new JavacTask(tb)
178                .options("-p", modules.toString())
179                .outdir(modules)
180                .files(findJavaFiles(modSrc.resolve("m2")))
181                .run(Task.Expect.SUCCESS)
182                .writeAll();
183    }
184
185    @Test
186    public void testMultiOnModulePathInner(Path base) throws Exception {
187        Path modSrc = base.resolve("modSrc");
188        Path modules = base.resolve("modules");
189        new ModuleBuilder(tb, "m1")
190                .exports("p")
191                .classes("package p; public class C { public class Inner { } }")
192                .build(modules);
193        new ModuleBuilder(tb, "m2")
194                .requires("m1")
195                .uses("p.C.Inner")
196                .write(modSrc);
197
198        new JavacTask(tb)
199                .options("-p", modules.toString())
200                .outdir(modules)
201                .files(findJavaFiles(modSrc.resolve("m2")))
202                .run(Task.Expect.SUCCESS)
203                .writeAll();
204    }
205
206    @Test
207    public void testDuplicateUses(Path base) throws Exception {
208        Path src = base.resolve("src");
209        tb.writeJavaFiles(src.resolve("m"),
210                "module m { uses p.C; uses p.C; }",
211                "package p; public class C { }");
212
213        List<String> output = new JavacTask(tb)
214                .options("-XDrawDiagnostics")
215                .outdir(Files.createDirectories(base.resolve("classes")))
216                .files(findJavaFiles(src))
217                .run(Task.Expect.FAIL)
218                .writeAll()
219                .getOutputLines(Task.OutputKind.DIRECT);
220
221        if (!output.containsAll(Arrays.asList(
222                "module-info.java:1:22: compiler.err.duplicate.uses: p.C"))) {
223            throw new Exception("Expected output not found");
224        }
225    }
226
227    @Test
228    public void testServiceNotExist(Path base) throws Exception {
229        Path src = base.resolve("src");
230        tb.writeJavaFiles(src,
231                "module m { uses p.NotExist; }",
232                "package p; public class C { }");
233
234        List<String> output = new JavacTask(tb)
235                .outdir(Files.createDirectories(base.resolve("classes")))
236                .options("-XDrawDiagnostics")
237                .files(findJavaFiles(src))
238                .run(Task.Expect.FAIL)
239                .writeAll()
240                .getOutputLines(Task.OutputKind.DIRECT);
241        Collection<?> expected = Arrays.asList("module-info.java:1:18: compiler.err.cant.resolve.location: kindname.class, NotExist, , , (compiler.misc.location: kindname.package, p, null)",
242                "1 error");
243        if (!output.containsAll(expected)) {
244            throw new Exception("Expected output not found");
245        }
246    }
247
248    @Test
249    public void testUsesUnexportedService(Path base) throws Exception {
250        Path src = base.resolve("src");
251        tb.writeJavaFiles(src.resolve("m1"),
252                "module m1 { }",
253                "package p; public class C { }");
254        tb.writeJavaFiles(src.resolve("m2"),
255                "module m2 { requires m1; uses p.C; }");
256
257        List<String> output = new JavacTask(tb)
258                .options("-XDrawDiagnostics", "--module-source-path", src.toString())
259                .outdir(Files.createDirectories(base.resolve("modules")))
260                .files(findJavaFiles(src))
261                .run(Task.Expect.FAIL)
262                .writeAll()
263                .getOutputLines(Task.OutputKind.DIRECT);
264
265        List<String> expected = Arrays.asList("module-info.java:1:32: compiler.err.not.def.access.package.cant.access: p.C, p",
266                "1 error");
267        if (!output.containsAll(expected)) {
268            throw new Exception("Expected output not found");
269        }
270    }
271
272    @Test
273    public void testUsesUnexportedButProvidedService(Path base) throws Exception {
274        Path src = base.resolve("src");
275        tb.writeJavaFiles(src.resolve("m1"),
276                "module m1 { provides p.C with p.C; }",
277                "package p; public class C { }");
278        tb.writeJavaFiles(src.resolve("m2"),
279                "module m2 { requires m1; uses p.C; }");
280
281        List<String> output = new JavacTask(tb)
282                .options("-XDrawDiagnostics", "--module-source-path", src.toString())
283                .outdir(Files.createDirectories(base.resolve("modules")))
284                .files(findJavaFiles(src))
285                .run(Task.Expect.FAIL)
286                .writeAll()
287                .getOutputLines(Task.OutputKind.DIRECT);
288
289        List<String> expected = Arrays.asList("module-info.java:1:32: compiler.err.not.def.access.package.cant.access: p.C, p",
290                "1 error");
291        if (!output.containsAll(expected)) {
292            throw new Exception("Expected output not found");
293        }
294    }
295}
296