|
1 /* |
|
2 * Copyright (c) 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 "requires public" |
|
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 RequiresPublicTest |
|
34 */ |
|
35 |
|
36 import java.nio.file.Files; |
|
37 import java.nio.file.Path; |
|
38 |
|
39 public class RequiresPublicTest extends ModuleTestBase { |
|
40 |
|
41 public static void main(String... args) throws Exception { |
|
42 RequiresPublicTest t = new RequiresPublicTest(); |
|
43 t.runTests(); |
|
44 } |
|
45 |
|
46 @Test |
|
47 void testJavaSE_OK(Path base) throws Exception { |
|
48 Path src = base.resolve("src"); |
|
49 tb.writeJavaFiles(src, |
|
50 "module m { requires java.se; }", |
|
51 "import java.awt.Frame;\n" // in java.se |
|
52 + "class Test {\n" |
|
53 + " Frame f;\n" |
|
54 + "}"); |
|
55 Path classes = base.resolve("classes"); |
|
56 Files.createDirectories(classes); |
|
57 |
|
58 tb.new JavacTask(ToolBox.Mode.CMDLINE) |
|
59 .files(findJavaFiles(src)) |
|
60 .outdir(classes) |
|
61 .run() |
|
62 .writeAll(); |
|
63 } |
|
64 |
|
65 @Test |
|
66 void testJavaSE_Fail(Path base) throws Exception { |
|
67 Path src = base.resolve("src"); |
|
68 tb.writeJavaFiles(src, |
|
69 "module m { requires java.se; }", |
|
70 "import com.sun.source.tree.Tree;\n" // not in java.se (in jdk.compiler) |
|
71 + "class Test {\n" |
|
72 + " Tree t;\n" |
|
73 + "}"); |
|
74 Path classes = base.resolve("classes"); |
|
75 Files.createDirectories(classes); |
|
76 |
|
77 String log = tb.new JavacTask(ToolBox.Mode.CMDLINE) |
|
78 .options("-XDrawDiagnostics") |
|
79 .files(findJavaFiles(src)) |
|
80 .outdir(classes.toString()) // should allow Path here |
|
81 .run(ToolBox.Expect.FAIL) |
|
82 .writeAll() |
|
83 .getOutput(ToolBox.OutputKind.DIRECT); |
|
84 |
|
85 if (!log.contains("Test.java:1:27: compiler.err.doesnt.exist: com.sun.source.tree")) |
|
86 throw new Exception("expected output not found"); |
|
87 } |
|
88 |
|
89 @Test |
|
90 void testComplex_OK(Path base) throws Exception { |
|
91 Path src = getComplexSrc(base, "", ""); |
|
92 Path classes = base.resolve("classes"); |
|
93 Files.createDirectories(classes); |
|
94 |
|
95 tb.new JavacTask(ToolBox.Mode.CMDLINE) |
|
96 .options("-modulesourcepath", src.toString()) |
|
97 .files(findJavaFiles(src)) |
|
98 .outdir(classes) |
|
99 .run() |
|
100 .writeAll(); |
|
101 } |
|
102 |
|
103 @Test |
|
104 void testComplex_Fail(Path base) throws Exception { |
|
105 Path src = getComplexSrc(base, |
|
106 "import p5.C5; import p6.C6; import p7.C7;\n", |
|
107 "C5 c5; C6 c6; C7 c7;\n"); |
|
108 Path classes = base.resolve("classes"); |
|
109 Files.createDirectories(classes); |
|
110 |
|
111 String log = tb.new JavacTask(ToolBox.Mode.CMDLINE) |
|
112 .options("-XDrawDiagnostics", |
|
113 "-modulesourcepath", src.toString()) |
|
114 .files(findJavaFiles(src)) |
|
115 .outdir(classes) |
|
116 .run(ToolBox.Expect.FAIL) |
|
117 .writeAll() |
|
118 .getOutput(ToolBox.OutputKind.DIRECT); |
|
119 |
|
120 String[] expect = { |
|
121 "C1.java:5:10: compiler.err.not.def.access.package.cant.access: p5.C5, p5", |
|
122 "C1.java:5:24: compiler.err.not.def.access.package.cant.access: p6.C6, p6", |
|
123 "C1.java:5:38: compiler.err.not.def.access.package.cant.access: p7.C7, p7", |
|
124 "C1.java:8:1: compiler.err.cant.resolve.location: kindname.class, C5, , , " |
|
125 + "(compiler.misc.location: kindname.class, p1.C1, null)", |
|
126 "C1.java:8:8: compiler.err.cant.resolve.location: kindname.class, C6, , , " |
|
127 + "(compiler.misc.location: kindname.class, p1.C1, null)", |
|
128 "C1.java:8:15: compiler.err.cant.resolve.location: kindname.class, C7, , , " |
|
129 + "(compiler.misc.location: kindname.class, p1.C1, null)" |
|
130 }; |
|
131 |
|
132 for (String e: expect) { |
|
133 if (!log.contains(e)) |
|
134 throw new Exception("expected output not found: " + e); |
|
135 } |
|
136 } |
|
137 |
|
138 /* |
|
139 * Set up the following module graph |
|
140 * m1 -> m2 => m3 => m4 -> m5 |
|
141 * -> m6 => m7 |
|
142 * where -> is requires, => is requires public |
|
143 */ |
|
144 Path getComplexSrc(Path base, String m1_extraImports, String m1_extraUses) throws Exception { |
|
145 Path src = base.resolve("src"); |
|
146 |
|
147 Path src_m1 = src.resolve("m1"); |
|
148 tb.writeJavaFiles(src_m1, |
|
149 "module m1 { requires m2; }", |
|
150 "package p1;\n" |
|
151 + "import p2.C2;\n" |
|
152 + "import p3.C3;\n" |
|
153 + "import p4.C4;\n" |
|
154 + m1_extraImports |
|
155 + "class C1 {\n" |
|
156 + " C2 c2; C3 c3; C4 c4;\n" |
|
157 + m1_extraUses |
|
158 + "}\n"); |
|
159 |
|
160 Path src_m2 = src.resolve("m2"); |
|
161 tb.writeJavaFiles(src_m2, |
|
162 "module m2 {\n" |
|
163 + " requires public m3;\n" |
|
164 + " requires m6;\n" |
|
165 + " exports p2;\n" |
|
166 + "}", |
|
167 "package p2;\n" |
|
168 + "public class C2 { }\n"); |
|
169 |
|
170 Path src_m3 = src.resolve("m3"); |
|
171 tb.writeJavaFiles(src_m3, |
|
172 "module m3 { requires public m4; exports p3; }", |
|
173 "package p3;\n" |
|
174 + "public class C3 { }\n"); |
|
175 |
|
176 Path src_m4 = src.resolve("m4"); |
|
177 tb.writeJavaFiles(src_m4, |
|
178 "module m4 { requires m5; exports p4; }", |
|
179 "package p4;\n" |
|
180 + "public class C4 { }\n"); |
|
181 |
|
182 Path src_m5 = src.resolve("m5"); |
|
183 tb.writeJavaFiles(src_m5, |
|
184 "module m5 { exports p5; }", |
|
185 "package p5;\n" |
|
186 + "public class C5 { }\n"); |
|
187 |
|
188 Path src_m6 = src.resolve("m6"); |
|
189 tb.writeJavaFiles(src_m6, |
|
190 "module m6 { requires public m7; exports p6; }", |
|
191 "package p6;\n" |
|
192 + "public class C6 { }\n"); |
|
193 |
|
194 Path src_m7 = src.resolve("m7"); |
|
195 tb.writeJavaFiles(src_m7, |
|
196 "module m7 { exports p7; }", |
|
197 "package p7;\n" |
|
198 + "public class C7 { }\n"); |
|
199 |
|
200 return src; |
|
201 } |
|
202 } |