UsesTest.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 uses
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 UsesTest
34 */
35
36import java.nio.file.Files;
37import java.nio.file.Path;
38import java.util.Arrays;
39import java.util.Collection;
40import java.util.List;
41
42public class UsesTest extends ModuleTestBase {
43    public static void main(String... args) throws Exception {
44        UsesTest t = new UsesTest();
45        t.runTests();
46    }
47
48    @Test
49    void testSimple(Path base) throws Exception {
50        Path src = base.resolve("src");
51        tb.writeJavaFiles(src,
52                "module m { uses p.C; }",
53                "package p; public class C { }");
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 testSimpleInner(Path base) throws Exception {
66        Path src = base.resolve("src");
67        tb.writeJavaFiles(src,
68                "module m { uses p.C.Inner; }",
69                "package p; public class C { public class Inner { } }");
70        Path classes = base.resolve("classes");
71        Files.createDirectories(classes);
72
73        tb.new JavacTask()
74                .outdir(classes)
75                .files(findJavaFiles(src))
76                .run(ToolBox.Expect.SUCCESS)
77                .writeAll();
78    }
79
80    @Test
81    void testSimpleAnnotation(Path base) throws Exception {
82        Path src = base.resolve("src");
83        tb.writeJavaFiles(src,
84                "module m { uses p.C; }",
85                "package p; public @interface C { }");
86        Path classes = base.resolve("classes");
87        Files.createDirectories(classes);
88
89        tb.new JavacTask()
90                .outdir(classes)
91                .files(findJavaFiles(src))
92                .run(ToolBox.Expect.SUCCESS)
93                .writeAll();
94    }
95
96    @Test
97    void testPrivateService(Path base) throws Exception {
98        Path src = base.resolve("src");
99        tb.writeJavaFiles(src,
100                "module m { uses p.C.A; uses p.C; }",
101                "package p; public class C { protected class A { } }");
102
103        List<String> output = tb.new JavacTask()
104                .options("-XDrawDiagnostics")
105                .outdir(Files.createDirectories(base.resolve("classes")))
106                .files(findJavaFiles(src))
107                .run(ToolBox.Expect.FAIL)
108                .writeAll()
109                .getOutputLines(ToolBox.OutputKind.DIRECT);
110
111        List<String> expected = Arrays.asList("module-info.java:1:20: compiler.err.report.access: p.C.A, protected, p.C",
112                "1 error");
113        if (!output.containsAll(expected)) {
114            throw new Exception("Expected output not found");
115        }
116    }
117
118    @Test
119    void testMulti(Path base) throws Exception {
120        Path src = base.resolve("src");
121        tb.writeJavaFiles(src.resolve("m1"),
122                "module m1 { exports p; }",
123                "package p; public class C { }");
124        tb.writeJavaFiles(src.resolve("m2"),
125                "module m2 { requires m1; uses p.C; }");
126        Path modules = base.resolve("modules");
127        Files.createDirectories(modules);
128
129        tb.new JavacTask()
130                .options("-modulesourcepath", src.toString())
131                .outdir(modules)
132                .files(findJavaFiles(src))
133                .run(ToolBox.Expect.SUCCESS)
134                .writeAll();
135    }
136
137    @Test
138    void testMultiOnModulePath(Path base) throws Exception {
139        Path modules = base.resolve("modules");
140        new ModuleBuilder("m1")
141                .exports("p")
142                .classes("package p; public class C { }")
143                .build(modules);
144        new ModuleBuilder("m2")
145                .requires("m1")
146                .uses("p.C")
147                .write(modules);
148
149        tb.new JavacTask()
150                .options("-mp", modules.toString())
151                .outdir(modules)
152                .files(findJavaFiles(modules.resolve("m2")))
153                .run(ToolBox.Expect.SUCCESS)
154                .writeAll();
155    }
156
157    @Test
158    void testMultiOnModulePathInner(Path base) throws Exception {
159        Path modules = base.resolve("modules");
160        new ModuleBuilder("m1")
161                .exports("p")
162                .classes("package p; public class C { public class Inner { } }")
163                .build(modules);
164        new ModuleBuilder("m2")
165                .requires("m1")
166                .uses("p.C.Inner")
167                .write(modules);
168
169        tb.new JavacTask()
170                .options("-mp", modules.toString())
171                .outdir(modules)
172                .files(findJavaFiles(modules.resolve("m2")))
173                .run(ToolBox.Expect.SUCCESS)
174                .writeAll();
175    }
176
177    @Test
178    void testDuplicateUses(Path base) throws Exception {
179        Path src = base.resolve("src");
180        tb.writeJavaFiles(src.resolve("m"),
181                "module m { uses p.C; uses p.C; }",
182                "package p; public class C { }");
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        if (!output.containsAll(Arrays.asList(
193                "module-info.java:1:22: compiler.err.duplicate.uses: p.C"))) {
194            throw new Exception("Expected output not found");
195        }
196    }
197
198    @Test
199    void testServiceNotExist(Path base) throws Exception {
200        Path src = base.resolve("src");
201        tb.writeJavaFiles(src,
202                "module m { uses p.NotExist; }",
203                "package p; public class C { }");
204
205        List<String> output = tb.new JavacTask()
206                .outdir(Files.createDirectories(base.resolve("classes")))
207                .options("-XDrawDiagnostics")
208                .files(findJavaFiles(src))
209                .run(ToolBox.Expect.FAIL)
210                .writeAll()
211                .getOutputLines(ToolBox.OutputKind.DIRECT);
212        Collection<?> expected = Arrays.asList("module-info.java:1:18: compiler.err.cant.resolve.location: kindname.class, NotExist, , , (compiler.misc.location: kindname.package, p, null)",
213                "1 error");
214        if (!output.containsAll(expected)) {
215            throw new Exception("Expected output not found");
216        }
217    }
218
219    @Test
220    void testUsesUnexportedService(Path base) throws Exception {
221        Path src = base.resolve("src");
222        tb.writeJavaFiles(src.resolve("m1"),
223                "module m1 { }",
224                "package p; public class C { }");
225        tb.writeJavaFiles(src.resolve("m2"),
226                "module m2 { requires m1; uses p.C; }");
227
228        List<String> output = tb.new JavacTask()
229                .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
230                .outdir(Files.createDirectories(base.resolve("modules")))
231                .files(findJavaFiles(src))
232                .run(ToolBox.Expect.FAIL)
233                .writeAll()
234                .getOutputLines(ToolBox.OutputKind.DIRECT);
235
236        List<String> expected = Arrays.asList("module-info.java:1:32: compiler.err.not.def.access.package.cant.access: p.C, p",
237                "1 error");
238        if (!output.containsAll(expected)) {
239            throw new Exception("Expected output not found");
240        }
241    }
242
243    @Test
244    void testUsesUnexportedButProvidedService(Path base) throws Exception {
245        Path src = base.resolve("src");
246        tb.writeJavaFiles(src.resolve("m1"),
247                "module m1 { provides p.C with p.C; }",
248                "package p; public class C { }");
249        tb.writeJavaFiles(src.resolve("m2"),
250                "module m2 { requires m1; uses p.C; }");
251
252        List<String> output = tb.new JavacTask()
253                .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
254                .outdir(Files.createDirectories(base.resolve("modules")))
255                .files(findJavaFiles(src))
256                .run(ToolBox.Expect.FAIL)
257                .writeAll()
258                .getOutputLines(ToolBox.OutputKind.DIRECT);
259
260        List<String> expected = Arrays.asList("module-info.java:1:32: compiler.err.not.def.access.package.cant.access: p.C, p",
261                "1 error");
262        if (!output.containsAll(expected)) {
263            throw new Exception("Expected output not found");
264        }
265    }
266}
267