ProvidesTest.java revision 3294:9adfb22ff08f
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 provides
27 * @library /tools/lib
28 * @modules
29 *      jdk.compiler/com.sun.tools.javac.api
30 *      jdk.compiler/com.sun.tools.javac.main
31 *      jdk.jdeps/com.sun.tools.javap
32 * @build ToolBox ModuleTestBase
33 * @run main ProvidesTest
34 */
35
36import java.nio.file.Files;
37import java.nio.file.Path;
38import java.util.Arrays;
39import java.util.List;
40
41public class ProvidesTest extends ModuleTestBase {
42    public static void main(String... args) throws Exception {
43        ProvidesTest t = new ProvidesTest();
44        t.runTests();
45    }
46
47    @Test
48    void testSimple(Path base) throws Exception {
49        Path src = base.resolve("src");
50        tb.writeJavaFiles(src,
51                "module m { provides p1.C1 with p2.C2; }",
52                "package p1; public class C1 { }",
53                "package p2; public class C2 extends p1.C1 { }");
54        Path classes = base.resolve("classes");
55        Files.createDirectories(classes);
56
57        tb.new JavacTask()
58                .outdir(classes)
59                .files(findJavaFiles(src))
60                .run(ToolBox.Expect.SUCCESS)
61                .writeAll();
62    }
63
64    @Test
65    void testMulti(Path base) throws Exception {
66        Path src = base.resolve("src");
67        tb.writeJavaFiles(src.resolve("m1"),
68                "module m1 { exports p1; }",
69                "package p1; public class C1 { }");
70        tb.writeJavaFiles(src.resolve("m2"),
71                "module m2 { requires m1; provides p1.C1 with p2.C2; }",
72                "package p2; public class C2 extends p1.C1 { }");
73        Path modules = base.resolve("modules");
74        Files.createDirectories(modules);
75
76        tb.new JavacTask()
77                .options("-modulesourcepath", src.toString())
78                .outdir(modules)
79                .files(findJavaFiles(src))
80                .run(ToolBox.Expect.SUCCESS)
81                .writeAll();
82
83    }
84
85    @Test
86    void testMissingWith(Path base) throws Exception {
87        Path src = base.resolve("src");
88        tb.writeJavaFiles(src,
89                "module m { provides p.C; }",
90                "package p; public class C { }");
91        Path classes = base.resolve("classes");
92        Files.createDirectories(classes);
93
94        String log = tb.new JavacTask()
95                .options("-XDrawDiagnostics")
96                .outdir(classes)
97                .files(findJavaFiles(src))
98                .run(ToolBox.Expect.FAIL)
99                .writeAll()
100                .getOutput(ToolBox.OutputKind.DIRECT);
101
102        if (!log.contains("module-info.java:1:24: compiler.err.expected: 'with'"))
103            throw new Exception("expected output not found");
104
105    }
106
107    @Test
108    void testDuplicateProvides(Path base) throws Exception {
109        Path src = base.resolve("src");
110        tb.writeJavaFiles(src,
111                "module m { provides p1.C1 with p2.C2; provides p1.C1 with p2.C2; }",
112                "package p1; public class C1 { }",
113                "package p2; public class C2 extends p1.C1 { }");
114        Path classes = base.resolve("classes");
115        Files.createDirectories(classes);
116
117        tb.new JavacTask()
118                .options("-XDrawDiagnostic")
119                .outdir(classes)
120                .files(findJavaFiles(src))
121                .run(ToolBox.Expect.FAIL)
122                .writeAll();
123    }
124
125    @Test
126    void testMissingService(Path base) throws Exception {
127        Path src = base.resolve("src");
128        tb.writeJavaFiles(src,
129                "module m { provides p.Missing with p.C; }",
130                "package p; public class C extends p.Missing { }");
131
132        List<String> output = tb.new JavacTask()
133                .options("-XDrawDiagnostics")
134                .outdir(Files.createDirectories(base.resolve("classes")))
135                .files(findJavaFiles(src))
136                .run(ToolBox.Expect.FAIL)
137                .writeAll()
138                .getOutputLines(ToolBox.OutputKind.DIRECT);
139
140        List<String> expected = Arrays.asList(
141                "C.java:1:36: compiler.err.cant.resolve.location: kindname.class, Missing, , , (compiler.misc.location: kindname.package, p, null)",
142                "module-info.java:1:22: compiler.err.cant.resolve.location: kindname.class, Missing, , , (compiler.misc.location: kindname.package, p, null)",
143                "module-info.java:1:37: compiler.err.service.implementation.doesnt.have.a.no.args.constructor: <any>",
144                "3 errors");
145        if (!output.containsAll(expected)) {
146            throw new Exception("Expected output not found");
147        }
148    }
149
150    @Test
151    void testProvidesFromAnotherModule(Path base) throws Exception {
152        Path modules = base.resolve("modules");
153        tb.writeJavaFiles(modules.resolve("M"),
154                "module M { exports p; }",
155                "package p; public class Service { }");
156        tb.writeJavaFiles(modules.resolve("L"),
157                "module L { requires M; provides p.Service with p.Service; }");
158
159        List<String> output = tb.new JavacTask()
160                .options("-XDrawDiagnostics",
161                        "-modulesourcepath", modules.toString())
162                .outdir(Files.createDirectories(base.resolve("classes")))
163                .files(findJavaFiles(modules))
164                .run(ToolBox.Expect.FAIL)
165                .writeAll()
166                .getOutputLines(ToolBox.OutputKind.DIRECT);
167        List<String> expected = Arrays.asList(
168                "module-info.java:1:24: compiler.err.service.implementation.not.in.right.module: M",
169                "1 error");
170        if (!output.containsAll(expected)) {
171            throw new Exception("Expected output not found");
172        }
173
174    }
175
176    @Test
177    void testServiceIsNotImplemented(Path base) throws Exception {
178        Path src = base.resolve("src");
179        tb.writeJavaFiles(src,
180                "module m { provides p.A with p.B; }",
181                "package p; public class A { }",
182                "package p; public class B { }");
183
184        List<String> output = tb.new JavacTask()
185                .options("-XDrawDiagnostics")
186                .outdir(Files.createDirectories(base.resolve("classes")))
187                .files(findJavaFiles(src))
188                .run(ToolBox.Expect.FAIL)
189                .writeAll()
190                .getOutputLines(ToolBox.OutputKind.DIRECT);
191
192        List<String> expected = Arrays.asList("module-info.java:1:31: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: p.B, p.A)",
193                "1 error");
194        if (!output.containsAll(expected)) {
195            throw new Exception("Expected output not found");
196        }
197    }
198
199    @Test
200    void testMissingImplementation(Path base) throws Exception {
201        Path src = base.resolve("src");
202        tb.writeJavaFiles(src,
203                "module m { provides p.C with p.Impl; }",
204                "package p; public class C { }");
205
206        List<String> output = tb.new JavacTask()
207                .options("-XDrawDiagnostics")
208                .outdir(Files.createDirectories(base.resolve("classes")))
209                .files(findJavaFiles(src))
210                .run(ToolBox.Expect.FAIL)
211                .writeAll()
212                .getOutputLines(ToolBox.OutputKind.DIRECT);
213
214        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)",
215                "1 error");
216        if (!output.containsAll(expected)) {
217            throw new Exception("Expected output not found");
218        }
219    }
220
221    @Test
222    void testSeveralImplementations(Path base) throws Exception {
223        Path src = base.resolve("src");
224        tb.writeJavaFiles(src,
225                "module m { provides p.C with p.Impl1; provides p.C with p.Impl2; }",
226                "package p; public class C { }",
227                "package p; public class Impl1 extends p.C { }",
228                "package p; public class Impl2 extends p.C { }");
229
230        tb.new JavacTask()
231                .outdir(Files.createDirectories(base.resolve("classes")))
232                .files(findJavaFiles(src))
233                .run(ToolBox.Expect.SUCCESS)
234                .writeAll();
235    }
236
237    @Test
238    void testOneImplementationsForServices(Path base) throws Exception {
239        Path src = base.resolve("src");
240        tb.writeJavaFiles(src,
241                "module m { provides p.Service1 with p.Impl; provides p.Service2 with p.Impl; }",
242                "package p; public interface Service1 { }",
243                "package p; public abstract class Service2 { }",
244                "package p; public class Impl extends p.Service2 implements p.Service1 { }");
245
246        tb.new JavacTask()
247                .outdir(Files.createDirectories(base.resolve("classes")))
248                .files(findJavaFiles(src))
249                .run(ToolBox.Expect.SUCCESS)
250                .writeAll();
251    }
252
253    @Test
254    void testAbstractImplementation(Path base) throws Exception {
255        Path src = base.resolve("src");
256        tb.writeJavaFiles(src,
257                "module m { provides p1.C1 with p2.C2; }",
258                "package p1; public class C1 { }",
259                "package p2; public abstract class C2 extends p1.C1 { }");
260
261        List<String> output = tb.new JavacTask()
262                .options("-XDrawDiagnostics")
263                .outdir(Files.createDirectories(base.resolve("classes")))
264                .files(findJavaFiles(src))
265                .run(ToolBox.Expect.FAIL)
266                .writeAll()
267                .getOutputLines(ToolBox.OutputKind.DIRECT);
268
269        List<String> expected = Arrays.asList(
270                "module-info.java:1:34: compiler.err.service.implementation.is.abstract: p2.C2");
271        if (!output.containsAll(expected)) {
272            throw new Exception("Expected output not found");
273        }
274    }
275
276    @Test
277    void testInterfaceImplementation(Path base) throws Exception {
278        Path src = base.resolve("src");
279        tb.writeJavaFiles(src,
280                "module m { provides p1.Service with p2.Impl; }",
281                "package p1; public interface Service { }",
282                "package p2; public interface Impl extends p1.Service { }");
283
284        List<String> output = tb.new JavacTask()
285                .options("-XDrawDiagnostics")
286                .outdir(Files.createDirectories(base.resolve("classes")))
287                .files(findJavaFiles(src))
288                .run(ToolBox.Expect.FAIL)
289                .writeAll()
290                .getOutputLines(ToolBox.OutputKind.DIRECT);
291
292        List<String> expected = Arrays.asList(
293                "module-info.java:1:39: compiler.err.service.implementation.is.abstract: p2.Impl");
294        if (!output.containsAll(expected)) {
295            throw new Exception("Expected output not found");
296        }
297    }
298
299    @Test
300    void testProtectedImplementation(Path base) throws Exception {
301        Path src = base.resolve("src");
302        tb.writeJavaFiles(src,
303                "module m { provides p1.C1 with p2.C2; }",
304                "package p1; public class C1 { }",
305                "package p2; class C2 extends p1.C1 { }");
306
307        List<String> output = tb.new JavacTask()
308                .options("-XDrawDiagnostics")
309                .outdir(Files.createDirectories(base.resolve("classes")))
310                .files(findJavaFiles(src))
311                .run(ToolBox.Expect.FAIL)
312                .writeAll()
313                .getOutputLines(ToolBox.OutputKind.DIRECT);
314
315        List<String> expected = Arrays.asList("module-info.java:1:34: compiler.err.not.def.public.cant.access: p2.C2, p2",
316                "1 error");
317        if (!output.containsAll(expected)) {
318            throw new Exception("Expected output not found");
319        }
320    }
321
322    @Test
323    void testNoNoArgConstructor(Path base) throws Exception {
324        Path src = base.resolve("src");
325        tb.writeJavaFiles(src,
326                "module m { uses p1.C1; provides p1.C1 with p2.C2; }",
327                "package p1; public class C1 { }",
328                "package p2; public class C2 extends p1.C1 { public C2(String str) { } }");
329
330        List<String> output = tb.new JavacTask()
331                .options("-XDrawDiagnostics")
332                .outdir(Files.createDirectories(base.resolve("classes")))
333                .files(findJavaFiles(src))
334                .run(ToolBox.Expect.FAIL)
335                .writeAll()
336                .getOutputLines(ToolBox.OutputKind.DIRECT);
337
338        List<String> expected = Arrays.asList(
339                "module-info.java:1:46: compiler.err.service.implementation.doesnt.have.a.no.args.constructor: p2.C2");
340        if (!output.containsAll(expected)) {
341            throw new Exception("Expected output not found");
342        }
343    }
344
345    @Test
346    void testPrivateNoArgConstructor(Path base) throws Exception {
347        Path src = base.resolve("src");
348        tb.writeJavaFiles(src,
349                "module m { uses p1.C1; provides p1.C1 with p2.C2; }",
350                "package p1; public class C1 { }",
351                "package p2; public class C2 extends p1.C1 { private C2() { } }");
352
353        List<String> output = tb.new JavacTask()
354                .options("-XDrawDiagnostics")
355                .outdir(Files.createDirectories(base.resolve("classes")))
356                .files(findJavaFiles(src))
357                .run(ToolBox.Expect.FAIL)
358                .writeAll()
359                .getOutputLines(ToolBox.OutputKind.DIRECT);
360
361        List<String> expected = Arrays.asList(
362                "module-info.java:1:46: compiler.err.service.implementation.no.args.constructor.not.public: p2.C2");
363        if (!output.containsAll(expected)) {
364            throw new Exception("Expected output not found");
365        }
366    }
367
368    @Test
369    void testServiceIndirectlyImplemented(Path base) throws Exception {
370        Path src = base.resolve("src");
371        tb.writeJavaFiles(src,
372                "module m { provides p1.C1 with p2.C3; }",
373                "package p1; public class C1 { }",
374                "package p2; public class C2 extends p1.C1 {  }",
375                "package p2; public class C3 extends p2.C2 {  }");
376
377        tb.new JavacTask()
378                .outdir(Files.createDirectories(base.resolve("classes")))
379                .files(findJavaFiles(src))
380                .run(ToolBox.Expect.SUCCESS)
381                .writeAll();
382    }
383
384    @Test
385    void testServiceImplementationInnerClass(Path base) throws Exception {
386        Path src = base.resolve("src");
387        tb.writeJavaFiles(src,
388                "module m { provides p1.C1 with p2.C2.Inner; }",
389                "package p1; public class C1 { }",
390                "package p2; public class C2  { public class Inner extends p1.C1 { } }");
391
392        List<String> output = tb.new JavacTask()
393                .options("-XDrawDiagnostics")
394                .outdir(Files.createDirectories(base.resolve("classes")))
395                .files(findJavaFiles(src))
396                .run(ToolBox.Expect.FAIL)
397                .writeAll()
398                .getOutputLines(ToolBox.OutputKind.DIRECT);
399
400        List<String> expected = Arrays.asList(
401                "module-info.java:1:37: compiler.err.service.implementation.is.inner: p2.C2.Inner");
402        if (!output.containsAll(expected)) {
403            throw new Exception("Expected output not found");
404        }
405    }
406
407    @Test
408    void testServiceDefinitionInnerClass(Path base) throws Exception {
409        Path src = base.resolve("src");
410        tb.writeJavaFiles(src,
411                "module m { provides p1.C1.InnerDefinition with p2.C2; }",
412                "package p1; public class C1 { public class InnerDefinition { } }",
413                "package p2; public class C2 extends p1.C1.InnerDefinition { }");
414
415        List<String> output = tb.new JavacTask()
416                .options("-XDrawDiagnostics")
417                .outdir(Files.createDirectories(base.resolve("classes")))
418                .files(findJavaFiles(src))
419                .run(ToolBox.Expect.FAIL)
420                .writeAll()
421                .getOutputLines(ToolBox.OutputKind.DIRECT);
422
423        List<String> expected = Arrays.asList(
424                "module-info.java:1:26: compiler.err.service.definition.is.inner: p1.C1.InnerDefinition",
425                "module-info.java:1:12: compiler.warn.service.provided.but.not.exported.or.used: p1.C1.InnerDefinition",
426                "C2.java:1:20: compiler.err.encl.class.required: p1.C1.InnerDefinition",
427                "2 errors",
428                "1 warning");
429        if (!output.containsAll(expected)) {
430            throw new Exception("Expected output not found");
431        }
432    }
433}
434