author | jjg |
Thu, 26 May 2016 10:45:13 -0700 | |
changeset 38611 | b5a479aff686 |
parent 37758 | 3ecf9b414e05 |
child 38916 | d118cb2cffab |
permissions | -rw-r--r-- |
36526 | 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 |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
26 |
* @summary tests for module declarations |
36526 | 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 |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
32 |
* @build toolbox.ToolBox toolbox.JavacTask ModuleTestBase |
36526 | 33 |
* @run main ModuleInfoTest |
34 |
*/ |
|
35 |
||
36 |
import java.nio.file.Files; |
|
37 |
import java.nio.file.Path; |
|
38 |
||
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
39 |
import toolbox.JavacTask; |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
40 |
import toolbox.Task; |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
41 |
import toolbox.ToolBox; |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
42 |
|
36526 | 43 |
public class ModuleInfoTest extends ModuleTestBase { |
44 |
||
45 |
public static void main(String... args) throws Exception { |
|
46 |
ModuleInfoTest t = new ModuleInfoTest(); |
|
47 |
t.runTests(); |
|
48 |
} |
|
49 |
||
50 |
/** |
|
51 |
* Check error message if module declaration not in module-info.java. |
|
52 |
*/ |
|
53 |
@Test |
|
37758 | 54 |
public void testModuleDeclNotInModuleJava(Path base) throws Exception { |
36526 | 55 |
Path src = base.resolve("src"); |
56 |
tb.writeFile(src.resolve("M.java"), "module M { }"); |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
57 |
String log = new JavacTask(tb) |
36526 | 58 |
.options("-XDrawDiagnostics") |
59 |
.files(findJavaFiles(src)) |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
60 |
.run(Task.Expect.FAIL) |
36526 | 61 |
.writeAll() |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
62 |
.getOutput(Task.OutputKind.DIRECT); |
36526 | 63 |
|
64 |
if (!log.contains("M.java:1:1: compiler.err.module.decl.sb.in.module-info.java")) |
|
65 |
throw new Exception("expected output not found"); |
|
66 |
} |
|
67 |
||
68 |
/** |
|
69 |
* Verify that a package private class can be put in module-info.java. |
|
70 |
*/ |
|
71 |
@Test |
|
37758 | 72 |
public void testNotModuleDeclInModuleJava_1(Path base) throws Exception { |
36526 | 73 |
Path src = base.resolve("src"); |
74 |
tb.writeFile(src.resolve("module-info.java"), "class C { }"); |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
75 |
new JavacTask(tb) |
36526 | 76 |
.options("-XDrawDiagnostics") |
77 |
.files(findJavaFiles(src)) |
|
78 |
.run() |
|
79 |
.writeAll(); |
|
80 |
} |
|
81 |
||
82 |
/** |
|
83 |
* Verify that a public class cannot be put in module-info.java. |
|
84 |
*/ |
|
85 |
@Test |
|
37758 | 86 |
public void testNotModuleDeclInModuleJava_2(Path base) throws Exception { |
36526 | 87 |
Path src = base.resolve("src"); |
88 |
tb.writeFile(src.resolve("module-info.java"), "public class C { }"); |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
89 |
String log = new JavacTask(tb) |
36526 | 90 |
.options("-XDrawDiagnostics") |
91 |
.files(findJavaFiles(src)) |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
92 |
.run(Task.Expect.FAIL) |
36526 | 93 |
.writeAll() |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
94 |
.getOutput(Task.OutputKind.DIRECT); |
36526 | 95 |
|
96 |
if (!log.contains("module-info.java:1:8: compiler.err.class.public.should.be.in.file: C")) |
|
97 |
throw new Exception("expected output not found"); |
|
98 |
} |
|
99 |
||
100 |
/** |
|
101 |
* Verify that only one module decl can be put in module-info.java. |
|
102 |
*/ |
|
103 |
@Test |
|
37758 | 104 |
public void testSingleModuleDecl(Path base) throws Exception { |
36526 | 105 |
Path src = base.resolve("src"); |
106 |
tb.writeJavaFiles(src, "module M1 { } /*...*/ module M2 { }"); |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
107 |
String log = new JavacTask(tb) |
36526 | 108 |
.options("-XDrawDiagnostics") |
109 |
.files(findJavaFiles(src)) |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
110 |
.run(Task.Expect.FAIL) |
36526 | 111 |
.writeAll() |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
112 |
.getOutput(Task.OutputKind.DIRECT); |
36526 | 113 |
|
114 |
if (!log.contains("module-info.java:1:14: compiler.err.expected: token.end-of-input")) |
|
115 |
throw new Exception("expected output not found"); |
|
116 |
} |
|
117 |
||
118 |
/** |
|
119 |
* Verify that missing requires are reported. |
|
120 |
*/ |
|
121 |
@Test |
|
37758 | 122 |
public void testRequiresNotFound(Path base) throws Exception { |
36526 | 123 |
Path src = base.resolve("src"); |
124 |
tb.writeJavaFiles(src, "module M1 { requires M2; }"); |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
125 |
String log = new JavacTask(tb) |
36526 | 126 |
.options("-XDrawDiagnostics") |
127 |
.files(findJavaFiles(src)) |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
128 |
.run(Task.Expect.FAIL) |
36526 | 129 |
.writeAll() |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
130 |
.getOutput(Task.OutputKind.DIRECT); |
36526 | 131 |
|
132 |
if (!log.contains("module-info.java:1:22: compiler.err.module.not.found: M2")) |
|
133 |
throw new Exception("expected output not found"); |
|
134 |
} |
|
135 |
||
136 |
/** |
|
137 |
* Verify that missing exports are reported. |
|
138 |
*/ |
|
139 |
@Test |
|
37758 | 140 |
public void testExportsNotFound(Path base) throws Exception { |
36526 | 141 |
Path src = base.resolve("src"); |
142 |
tb.writeJavaFiles(src, "module M1 { exports p to M2; }"); |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
143 |
String log = new JavacTask(tb) |
36526 | 144 |
.options("-XDrawDiagnostics") |
145 |
.files(findJavaFiles(src)) |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
146 |
.run(Task.Expect.FAIL) |
36526 | 147 |
.writeAll() |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
148 |
.getOutput(Task.OutputKind.DIRECT); |
36526 | 149 |
|
150 |
if (!log.contains("module-info.java:1:26: compiler.err.module.not.found: M2")) |
|
151 |
throw new Exception("expected output not found"); |
|
152 |
} |
|
153 |
||
154 |
/** |
|
155 |
* Verify that a simple loop is detected. |
|
156 |
*/ |
|
157 |
@Test |
|
37758 | 158 |
public void testRequiresSelf(Path base) throws Exception { |
36526 | 159 |
Path src = base.resolve("src"); |
160 |
tb.writeJavaFiles(src, "module M { requires M; }"); |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
161 |
String log = new JavacTask(tb) |
36526 | 162 |
.options("-XDrawDiagnostics") |
163 |
.files(findJavaFiles(src)) |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
164 |
.run(Task.Expect.FAIL) |
36526 | 165 |
.writeAll() |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
166 |
.getOutput(Task.OutputKind.DIRECT); |
36526 | 167 |
|
168 |
if (!log.contains("module-info.java:1:21: compiler.err.cyclic.requires: M")) |
|
169 |
throw new Exception("expected output not found"); |
|
170 |
} |
|
171 |
||
172 |
/** |
|
173 |
* Verify that a multi-module loop is detected. |
|
174 |
*/ |
|
175 |
@Test |
|
37758 | 176 |
public void testRequiresLoop(Path base) throws Exception { |
36526 | 177 |
Path src = base.resolve("src"); |
178 |
Path src_m1 = src.resolve("m1"); |
|
179 |
tb.writeFile(src_m1.resolve("module-info.java"), "module m1 { requires m2; }"); |
|
180 |
Path src_m2 = src.resolve("m2"); |
|
181 |
tb.writeFile(src_m2.resolve("module-info.java"), "module m2 { requires m3; }"); |
|
182 |
Path src_m3 = src.resolve("m3"); |
|
183 |
tb.writeFile(src_m3.resolve("module-info.java"), "module m3 { requires m1; }"); |
|
184 |
||
185 |
Path classes = base.resolve("classes"); |
|
186 |
Files.createDirectories(classes); |
|
187 |
||
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
188 |
String log = new JavacTask(tb) |
36526 | 189 |
.options("-XDrawDiagnostics", "-modulesourcepath", src.toString()) |
190 |
.outdir(classes) |
|
191 |
.files(findJavaFiles(src)) |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
192 |
.run(Task.Expect.FAIL) |
36526 | 193 |
.writeAll() |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
194 |
.getOutput(Task.OutputKind.DIRECT); |
36526 | 195 |
|
196 |
if (!log.contains("module-info.java:1:22: compiler.err.cyclic.requires: m3")) |
|
197 |
throw new Exception("expected output not found"); |
|
198 |
} |
|
199 |
||
200 |
/** |
|
201 |
* Verify that a multi-module loop is detected. |
|
202 |
*/ |
|
203 |
@Test |
|
37758 | 204 |
public void testRequiresPublicLoop(Path base) throws Exception { |
36526 | 205 |
Path src = base.resolve("src"); |
206 |
Path src_m1 = src.resolve("m1"); |
|
207 |
tb.writeFile(src_m1.resolve("module-info.java"), "module m1 { requires m2; }"); |
|
208 |
Path src_m2 = src.resolve("m2"); |
|
209 |
tb.writeFile(src_m2.resolve("module-info.java"), "module m2 { requires public m3; }"); |
|
210 |
Path src_m3 = src.resolve("m3"); |
|
211 |
tb.writeFile(src_m3.resolve("module-info.java"), "module m3 { requires m1; }"); |
|
212 |
||
213 |
Path classes = base.resolve("classes"); |
|
214 |
Files.createDirectories(classes); |
|
215 |
||
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
216 |
String log = new JavacTask(tb) |
36526 | 217 |
.options("-XDrawDiagnostics", "-modulesourcepath", src.toString()) |
218 |
.outdir(classes) |
|
219 |
.files(findJavaFiles(src)) |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
220 |
.run(Task.Expect.FAIL) |
36526 | 221 |
.writeAll() |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
222 |
.getOutput(Task.OutputKind.DIRECT); |
36526 | 223 |
|
224 |
if (!log.contains("module-info.java:1:29: compiler.err.cyclic.requires: m3")) |
|
225 |
throw new Exception("expected output not found"); |
|
226 |
} |
|
227 |
||
228 |
/** |
|
229 |
* Verify that duplicate requires are detected. |
|
230 |
*/ |
|
231 |
@Test |
|
37758 | 232 |
public void testDuplicateRequires(Path base) throws Exception { |
36526 | 233 |
Path src = base.resolve("src"); |
234 |
Path src_m1 = src.resolve("m1"); |
|
235 |
tb.writeFile(src_m1.resolve("module-info.java"), "module m1 { }"); |
|
236 |
Path src_m2 = src.resolve("m2"); |
|
237 |
tb.writeFile(src_m2.resolve("module-info.java"), "module m2 { requires m1; requires m1; }"); |
|
238 |
||
239 |
Path classes = base.resolve("classes"); |
|
240 |
Files.createDirectories(classes); |
|
241 |
||
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
242 |
String log = new JavacTask(tb) |
36526 | 243 |
.options("-XDrawDiagnostics", "-modulesourcepath", src.toString()) |
244 |
.outdir(classes) |
|
245 |
.files(findJavaFiles(src)) |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
246 |
.run(Task.Expect.FAIL) |
36526 | 247 |
.writeAll() |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
248 |
.getOutput(Task.OutputKind.DIRECT); |
36526 | 249 |
|
250 |
if (!log.contains("module-info.java:1:35: compiler.err.duplicate.requires: m1")) |
|
251 |
throw new Exception("expected output not found"); |
|
252 |
} |
|
253 |
||
254 |
/** |
|
255 |
* Verify that duplicate exported packages are detected. |
|
256 |
*/ |
|
257 |
@Test |
|
37758 | 258 |
public void testDuplicateExports_packages(Path base) throws Exception { |
36526 | 259 |
Path src = base.resolve("src"); |
260 |
tb.writeJavaFiles(src, "module m1 { exports p; exports p; }"); |
|
261 |
||
262 |
Path classes = base.resolve("classes"); |
|
263 |
Files.createDirectories(classes); |
|
264 |
||
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
265 |
String log = new JavacTask(tb) |
36526 | 266 |
.options("-XDrawDiagnostics") |
267 |
.outdir(classes) |
|
268 |
.files(findJavaFiles(src)) |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
269 |
.run(Task.Expect.FAIL) |
36526 | 270 |
.writeAll() |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
271 |
.getOutput(Task.OutputKind.DIRECT); |
36526 | 272 |
|
273 |
if (!log.contains("module-info.java:1:32: compiler.err.duplicate.exports: p")) |
|
274 |
throw new Exception("expected output not found"); |
|
275 |
} |
|
276 |
||
277 |
/** |
|
278 |
* Verify that duplicate exported packages are detected. |
|
279 |
*/ |
|
280 |
@Test |
|
37758 | 281 |
public void testDuplicateExports_packages2(Path base) throws Exception { |
36526 | 282 |
Path src = base.resolve("src"); |
283 |
tb.writeJavaFiles(src.resolve("m1"), "module m1 { exports p; exports p to m2; }"); |
|
284 |
tb.writeJavaFiles(src.resolve("m2"), "module m2 { }"); |
|
285 |
||
286 |
Path classes = base.resolve("classes"); |
|
287 |
Files.createDirectories(classes); |
|
288 |
||
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
289 |
String log = new JavacTask(tb) |
36526 | 290 |
.options("-XDrawDiagnostics", "-modulesourcepath", src.toString()) |
291 |
.outdir(classes) |
|
292 |
.files(findJavaFiles(src)) |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
293 |
.run(Task.Expect.FAIL) |
36526 | 294 |
.writeAll() |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
295 |
.getOutput(Task.OutputKind.DIRECT); |
36526 | 296 |
|
297 |
if (!log.contains("module-info.java:1:32: compiler.err.duplicate.exports: p")) |
|
298 |
throw new Exception("expected output not found"); |
|
299 |
} |
|
300 |
||
301 |
/** |
|
302 |
* Verify that duplicate exported packages are detected. |
|
303 |
*/ |
|
304 |
@Test |
|
37758 | 305 |
public void testDuplicateExports_modules(Path base) throws Exception { |
36526 | 306 |
Path src = base.resolve("src"); |
307 |
Path src_m1 = src.resolve("m1"); |
|
308 |
tb.writeFile(src_m1.resolve("module-info.java"), "module m1 { }"); |
|
309 |
Path src_m2 = src.resolve("m2"); |
|
310 |
tb.writeFile(src_m2.resolve("module-info.java"), "module m2 { exports p to m1, m1; }"); |
|
311 |
||
312 |
Path classes = base.resolve("classes"); |
|
313 |
Files.createDirectories(classes); |
|
314 |
||
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
315 |
String log = new JavacTask(tb) |
36526 | 316 |
.options("-XDrawDiagnostics", "-modulesourcepath", src.toString()) |
317 |
.outdir(classes) |
|
318 |
.files(findJavaFiles(src)) |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
319 |
.run(Task.Expect.FAIL) |
36526 | 320 |
.writeAll() |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
321 |
.getOutput(Task.OutputKind.DIRECT); |
36526 | 322 |
|
323 |
if (!log.contains("module-info.java:1:30: compiler.err.duplicate.exports: m1")) |
|
324 |
throw new Exception("expected output not found"); |
|
325 |
} |
|
326 |
} |