ProvidesTest.java revision 3294:9adfb22ff08f
1159979Sglebius/*
2159979Sglebius * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3159979Sglebius * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4159979Sglebius *
5159979Sglebius * This code is free software; you can redistribute it and/or modify it
6159979Sglebius * under the terms of the GNU General Public License version 2 only, as
7159979Sglebius * published by the Free Software Foundation.
8159979Sglebius *
9159979Sglebius * This code is distributed in the hope that it will be useful, but WITHOUT
10159979Sglebius * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11159979Sglebius * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12159979Sglebius * version 2 for more details (a copy is included in the LICENSE file that
13159979Sglebius * accompanied this code).
14159979Sglebius *
15159979Sglebius * You should have received a copy of the GNU General Public License version
16159979Sglebius * 2 along with this work; if not, write to the Free Software Foundation,
17159979Sglebius * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18159979Sglebius *
19159979Sglebius * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20159979Sglebius * or visit www.oracle.com if you need additional information or have any
21159979Sglebius * questions.
22159979Sglebius */
23159979Sglebius
24159979Sglebius/**
25159979Sglebius * @test
26159979Sglebius * @summary simple tests of module provides
27159979Sglebius * @library /tools/lib
28159979Sglebius * @modules
29159979Sglebius *      jdk.compiler/com.sun.tools.javac.api
30159979Sglebius *      jdk.compiler/com.sun.tools.javac.main
31159979Sglebius *      jdk.jdeps/com.sun.tools.javap
32159979Sglebius * @build ToolBox ModuleTestBase
33159979Sglebius * @run main ProvidesTest
34159979Sglebius */
35159979Sglebius
36159979Sglebiusimport java.nio.file.Files;
37159979Sglebiusimport java.nio.file.Path;
38159979Sglebiusimport java.util.Arrays;
39159979Sglebiusimport java.util.List;
40159979Sglebius
41159979Sglebiuspublic class ProvidesTest extends ModuleTestBase {
42159979Sglebius    public static void main(String... args) throws Exception {
43159979Sglebius        ProvidesTest t = new ProvidesTest();
44159979Sglebius        t.runTests();
45159979Sglebius    }
46159979Sglebius
47159979Sglebius    @Test
48159979Sglebius    void testSimple(Path base) throws Exception {
49159979Sglebius        Path src = base.resolve("src");
50159979Sglebius        tb.writeJavaFiles(src,
51159979Sglebius                "module m { provides p1.C1 with p2.C2; }",
52159979Sglebius                "package p1; public class C1 { }",
53159979Sglebius                "package p2; public class C2 extends p1.C1 { }");
54159979Sglebius        Path classes = base.resolve("classes");
55159979Sglebius        Files.createDirectories(classes);
56159979Sglebius
57159979Sglebius        tb.new JavacTask()
58159979Sglebius                .outdir(classes)
59159979Sglebius                .files(findJavaFiles(src))
60159979Sglebius                .run(ToolBox.Expect.SUCCESS)
61159979Sglebius                .writeAll();
62159979Sglebius    }
63159979Sglebius
64159979Sglebius    @Test
65159979Sglebius    void testMulti(Path base) throws Exception {
66159979Sglebius        Path src = base.resolve("src");
67159979Sglebius        tb.writeJavaFiles(src.resolve("m1"),
68159979Sglebius                "module m1 { exports p1; }",
69159979Sglebius                "package p1; public class C1 { }");
70159979Sglebius        tb.writeJavaFiles(src.resolve("m2"),
71159979Sglebius                "module m2 { requires m1; provides p1.C1 with p2.C2; }",
72159979Sglebius                "package p2; public class C2 extends p1.C1 { }");
73159979Sglebius        Path modules = base.resolve("modules");
74159979Sglebius        Files.createDirectories(modules);
75159979Sglebius
76159979Sglebius        tb.new JavacTask()
77159979Sglebius                .options("-modulesourcepath", src.toString())
78159979Sglebius                .outdir(modules)
79159979Sglebius                .files(findJavaFiles(src))
80159979Sglebius                .run(ToolBox.Expect.SUCCESS)
81159979Sglebius                .writeAll();
82159979Sglebius
83159979Sglebius    }
84159979Sglebius
85159979Sglebius    @Test
86159979Sglebius    void testMissingWith(Path base) throws Exception {
87159979Sglebius        Path src = base.resolve("src");
88159979Sglebius        tb.writeJavaFiles(src,
89159979Sglebius                "module m { provides p.C; }",
90159979Sglebius                "package p; public class C { }");
91159979Sglebius        Path classes = base.resolve("classes");
92159979Sglebius        Files.createDirectories(classes);
93159979Sglebius
94159979Sglebius        String log = tb.new JavacTask()
95159979Sglebius                .options("-XDrawDiagnostics")
96159979Sglebius                .outdir(classes)
97159979Sglebius                .files(findJavaFiles(src))
98159979Sglebius                .run(ToolBox.Expect.FAIL)
99159979Sglebius                .writeAll()
100159979Sglebius                .getOutput(ToolBox.OutputKind.DIRECT);
101159979Sglebius
102159979Sglebius        if (!log.contains("module-info.java:1:24: compiler.err.expected: 'with'"))
103159979Sglebius            throw new Exception("expected output not found");
104159979Sglebius
105159979Sglebius    }
106159979Sglebius
107159979Sglebius    @Test
108159979Sglebius    void testDuplicateProvides(Path base) throws Exception {
109159979Sglebius        Path src = base.resolve("src");
110159979Sglebius        tb.writeJavaFiles(src,
111159979Sglebius                "module m { provides p1.C1 with p2.C2; provides p1.C1 with p2.C2; }",
112159979Sglebius                "package p1; public class C1 { }",
113159979Sglebius                "package p2; public class C2 extends p1.C1 { }");
114159979Sglebius        Path classes = base.resolve("classes");
115159979Sglebius        Files.createDirectories(classes);
116159979Sglebius
117159979Sglebius        tb.new JavacTask()
118159979Sglebius                .options("-XDrawDiagnostic")
119159979Sglebius                .outdir(classes)
120159979Sglebius                .files(findJavaFiles(src))
121159979Sglebius                .run(ToolBox.Expect.FAIL)
122159979Sglebius                .writeAll();
123159979Sglebius    }
124159979Sglebius
125159979Sglebius    @Test
126159979Sglebius    void testMissingService(Path base) throws Exception {
127159979Sglebius        Path src = base.resolve("src");
128159979Sglebius        tb.writeJavaFiles(src,
129159979Sglebius                "module m { provides p.Missing with p.C; }",
130159979Sglebius                "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