ResolveTest.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2013, 2015, 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 tests for module resolution
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 ResolveTest
34 */
35
36import java.nio.file.*;
37
38public class ResolveTest extends ModuleTestBase {
39    public static void main(String... args) throws Exception {
40        ResolveTest t = new ResolveTest();
41        t.runTests();
42    }
43
44    @Test
45    void testMissingSimpleTypeUnnamedModule(Path base) throws Exception {
46        Path src = base.resolve("src");
47        tb.writeJavaFiles(src, "class C { D d; }");
48
49        String log = tb.new JavacTask()
50                .options("-XDrawDiagnostics")
51                .files(findJavaFiles(src))
52                .run(ToolBox.Expect.FAIL)
53                .writeAll()
54                .getOutput(ToolBox.OutputKind.DIRECT);
55
56        if (!log.contains("C.java:1:11: compiler.err.cant.resolve.location: "
57                + "kindname.class, D, , , (compiler.misc.location: kindname.class, C, null)"))
58            throw new Exception("expected output not found");
59    }
60
61    @Test
62    void testMissingSimpleTypeNamedModule(Path base) throws Exception {
63        Path src = base.resolve("src");
64        tb.writeJavaFiles(src,
65                "module m { }",
66                "class C { D d; }");
67
68        String log = tb.new JavacTask()
69                .options("-XDrawDiagnostics")
70                .files(findJavaFiles(src))
71                .run(ToolBox.Expect.FAIL)
72                .writeAll()
73                .getOutput(ToolBox.OutputKind.DIRECT);
74
75        if (!log.contains("C.java:1:11: compiler.err.cant.resolve.location: "
76                + "kindname.class, D, , , (compiler.misc.location: kindname.class, C, null)"))
77            throw new Exception("expected output not found");
78    }
79
80    @Test
81    void testUnexportedTypeUnreadableModule(Path base) throws Exception {
82        Path src = base.resolve("src");
83        tb.writeJavaFiles(src.resolve("m1"),
84                "module m1 { }",
85                "package p1; public class C1 { }");
86        tb.writeJavaFiles(src.resolve("m2"),
87                "module m2 { }",
88                "package p2; public class C2 { p1.C1 c; }");
89        Path modules = base.resolve("modules");
90        Files.createDirectories(modules);
91
92        String log = tb.new JavacTask()
93                .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
94                .outdir(modules)
95                .files(findJavaFiles(src))
96                .run(ToolBox.Expect.FAIL)
97                .writeAll()
98                .getOutput(ToolBox.OutputKind.DIRECT);
99
100        if (!log.contains("C2.java:1:33: compiler.err.not.def.access.package.cant.access: p1.C1, p1"))
101            throw new Exception("expected output not found");
102    }
103
104    @Test
105    void testUnexportedTypeReadableModule(Path base) throws Exception {
106        Path src = base.resolve("src");
107        tb.writeJavaFiles(src.resolve("m1"),
108                "module m1 { }",
109                "package p1; public class C1 { }");
110        tb.writeJavaFiles(src.resolve("m2"),
111                "module m2 { requires m1; }",
112                "package p2; public class C2 { p1.C1 c; }");
113        Path modules = base.resolve("modules");
114        Files.createDirectories(modules);
115
116        String log = tb.new JavacTask()
117                .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
118                .outdir(modules)
119                .files(findJavaFiles(src))
120                .run(ToolBox.Expect.FAIL)
121                .writeAll()
122                .getOutput(ToolBox.OutputKind.DIRECT);
123
124        if (!log.contains("C2.java:1:33: compiler.err.not.def.access.package.cant.access: p1.C1, p1"))
125            throw new Exception("expected output not found");
126    }
127
128    @Test
129    void testQualifiedExportedTypeReadableModule(Path base) throws Exception {
130        Path src = base.resolve("src");
131        tb.writeJavaFiles(src.resolve("m1"),
132                "module m1 { exports p1 to m3; }",
133                "package p1; public class C1 { }");
134        tb.writeJavaFiles(src.resolve("m2"),
135                "module m2 { requires m1; }",
136                "package p2; public class C2 { p1.C1 c; }");
137        tb.writeJavaFiles(src.resolve("m3"),
138                "module m3 { requires m1; }");
139        Path modules = base.resolve("modules");
140        Files.createDirectories(modules);
141
142        String log = tb.new JavacTask()
143                .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
144                .outdir(modules)
145                .files(findJavaFiles(src))
146                .run(ToolBox.Expect.FAIL)
147                .writeAll()
148                .getOutput(ToolBox.OutputKind.DIRECT);
149
150        if (!log.contains("C2.java:1:33: compiler.err.not.def.access.package.cant.access: p1.C1, p1"))
151            throw new Exception("expected output not found");
152    }
153
154    @Test
155    void testExportedTypeUnreadableModule(Path base) throws Exception {
156        Path src = base.resolve("src");
157        tb.writeJavaFiles(src.resolve("m1"),
158                "module m1 { exports p1; }",
159                "package p1; public class C1 { }");
160        tb.writeJavaFiles(src.resolve("m2"),
161                "module m2 { }",
162                "package p2; public class C2 { p1.C1 c; }");
163        Path modules = base.resolve("modules");
164        Files.createDirectories(modules);
165
166        String log = tb.new JavacTask()
167                .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
168                .outdir(modules)
169                .files(findJavaFiles(src))
170                .run(ToolBox.Expect.FAIL)
171                .writeAll()
172                .getOutput(ToolBox.OutputKind.DIRECT);
173
174        if (!log.contains("C2.java:1:33: compiler.err.not.def.access.package.cant.access: p1.C1, p1"))
175            throw new Exception("expected output not found");
176    }
177
178    @Test
179    void testExportedTypeReadableModule(Path base) throws Exception {
180        Path src = base.resolve("src");
181        tb.writeJavaFiles(src.resolve("m1"),
182                "module m1 { exports p1; }",
183                "package p1; public class C1 { }");
184        tb.writeJavaFiles(src.resolve("m2"),
185                "module m2 { requires m1; }",
186                "package p2; public class C2 { p1.C1 c; }");
187        Path modules = base.resolve("modules");
188        Files.createDirectories(modules);
189
190        tb.new JavacTask()
191                .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
192                .outdir(modules)
193                .files(findJavaFiles(src))
194                .run()
195                .writeAll();
196    }
197
198    @Test
199    void testExportedTypeReadableModule2(Path base) throws Exception {
200        Path src = base.resolve("src");
201        tb.writeJavaFiles(src.resolve("m1"),
202                "module m1 { exports p1 to m2; }",
203                "package p1; public class C1 { }");
204        tb.writeJavaFiles(src.resolve("m2"),
205                "module m2 { requires m1; }",
206                "package p2; public class C2 { p1.C1 c; }");
207        Path modules = base.resolve("modules");
208        Files.createDirectories(modules);
209
210        tb.new JavacTask()
211                .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
212                .outdir(modules)
213                .files(findJavaFiles(src))
214                .run()
215                .writeAll();
216    }
217}
218