author | jjg |
Thu, 31 Mar 2016 15:20:50 -0700 | |
changeset 36778 | e04318f39f92 |
parent 36526 | 3b41f1c69604 |
child 37758 | 3ecf9b414e05 |
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 |
|
26 |
* @summary test module/package conflicts |
|
27 |
* @library /tools/lib |
|
28 |
* @modules |
|
29 |
* jdk.compiler/com.sun.tools.javac.api |
|
30 |
* jdk.compiler/com.sun.tools.javac.main |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
31 |
* @build toolbox.ToolBox toolbox.JavacTask ModuleTestBase |
36526 | 32 |
* @run main PackageConflictTest |
33 |
*/ |
|
34 |
||
35 |
import java.nio.file.Files; |
|
36 |
import java.nio.file.Path; |
|
37 |
import java.util.Arrays; |
|
38 |
import java.util.List; |
|
39 |
||
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
40 |
import toolbox.JavacTask; |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
41 |
import toolbox.Task; |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
42 |
import toolbox.ToolBox; |
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
43 |
|
36526 | 44 |
public class PackageConflictTest extends ModuleTestBase { |
45 |
public static void main(String... args) throws Exception { |
|
46 |
PackageConflictTest t = new PackageConflictTest(); |
|
47 |
t.runTests(); |
|
48 |
} |
|
49 |
||
50 |
@Test |
|
51 |
void testSimple(Path base) throws Exception { |
|
52 |
Path src = base.resolve("src"); |
|
53 |
tb.writeJavaFiles(src, |
|
54 |
"package java.util; public class MyList { }"); |
|
55 |
Path classes = base.resolve("classes"); |
|
56 |
Files.createDirectories(classes); |
|
57 |
||
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
58 |
String log = new JavacTask(tb) |
36526 | 59 |
.options("-XDrawDiagnostics") |
60 |
.outdir(classes) |
|
61 |
.files(findJavaFiles(src)) |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
62 |
.run(Task.Expect.FAIL) |
36526 | 63 |
.writeAll() |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
64 |
.getOutput(Task.OutputKind.DIRECT); |
36526 | 65 |
|
66 |
if (!log.contains("MyList.java:1:1: compiler.err.package.in.other.module: java.base")) |
|
67 |
throw new Exception("expected output not found"); |
|
68 |
} |
|
69 |
||
70 |
@Test |
|
71 |
void testDisjoint(Path base) throws Exception { |
|
72 |
Path m1 = base.resolve("m1"); |
|
73 |
Path m2 = base.resolve("m2"); |
|
74 |
tb.writeJavaFiles(m1, |
|
75 |
"module m1 { }", |
|
76 |
"package test; public class A { }"); |
|
77 |
tb.writeJavaFiles(m2, |
|
78 |
"module m2 { }", |
|
79 |
"package test; public class B { }"); |
|
80 |
Path classes = base.resolve("classes"); |
|
81 |
Files.createDirectories(classes); |
|
82 |
||
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
83 |
new JavacTask(tb) |
36526 | 84 |
.options("-Werror", "-modulesourcepath", base.toString()) |
85 |
.outdir(classes) |
|
86 |
.files(findJavaFiles(base)) |
|
87 |
.run() |
|
88 |
.writeAll(); |
|
89 |
} |
|
90 |
||
91 |
@Test |
|
92 |
void testConflictInDependencies(Path base) throws Exception { |
|
93 |
Path m1 = base.resolve("m1"); |
|
94 |
Path m2 = base.resolve("m2"); |
|
95 |
Path m3 = base.resolve("m3"); |
|
96 |
tb.writeJavaFiles(m1, |
|
97 |
"module m1 { exports test; }", |
|
98 |
"package test; public class A { }"); |
|
99 |
tb.writeJavaFiles(m2, |
|
100 |
"module m2 { exports test; }", |
|
101 |
"package test; public class B { }"); |
|
102 |
tb.writeJavaFiles(m3, |
|
103 |
"module m3 { requires m1; requires m2; }", |
|
104 |
"package impl; public class Impl { }"); |
|
105 |
Path classes = base.resolve("classes"); |
|
106 |
Files.createDirectories(classes); |
|
107 |
||
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
108 |
List<String> log = new JavacTask(tb) |
36526 | 109 |
.options("-XDrawDiagnostics", "-modulesourcepath", base.toString()) |
110 |
.outdir(classes) |
|
111 |
.files(findJavaFiles(base)) |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
112 |
.run(Task.Expect.FAIL) |
36526 | 113 |
.writeAll() |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
114 |
.getOutputLines(Task.OutputKind.DIRECT); |
36526 | 115 |
|
116 |
List<String> expected = |
|
117 |
Arrays.asList("module-info.java:1:1: compiler.err.package.clash.from.requires: m3, test, m1, m2", |
|
118 |
"1 error"); |
|
119 |
||
120 |
if (!expected.equals(log)) { |
|
121 |
throw new AssertionError("Unexpected output: " + log); |
|
122 |
} |
|
123 |
} |
|
124 |
||
125 |
@Test |
|
126 |
void testSimple2(Path base) throws Exception { |
|
127 |
Path modules = base.resolve("modules"); |
|
128 |
new ModuleBuilder("N") |
|
129 |
.exports("pack") |
|
130 |
.classes("package pack; public class A { }") |
|
131 |
.build(modules); |
|
132 |
new ModuleBuilder("M") |
|
133 |
.requires("N") |
|
134 |
.classes("package pack; public class B { pack.A f; }") |
|
135 |
.write(modules); |
|
136 |
||
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
137 |
String log = new JavacTask(tb) |
36526 | 138 |
.options("-XDrawDiagnostics", "-mp", modules.toString()) |
139 |
.outdir(Files.createDirectories(base.resolve("classes"))) |
|
140 |
.files(findJavaFiles(modules.resolve("M"))) |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
141 |
.run(Task.Expect.FAIL) |
36526 | 142 |
.writeAll() |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
143 |
.getOutput(Task.OutputKind.DIRECT); |
36526 | 144 |
|
145 |
if (!log.contains("B.java:1:1: compiler.err.package.in.other.module: N")) |
|
146 |
throw new Exception("expected output not found"); |
|
147 |
} |
|
148 |
||
149 |
@Test |
|
150 |
void testPrivateConflict(Path base) throws Exception { |
|
151 |
Path modules = base.resolve("modules"); |
|
152 |
new ModuleBuilder("N") |
|
153 |
.exports("publ") |
|
154 |
.classes("package pack; public class A { }") |
|
155 |
.classes("package publ; public class B { }") |
|
156 |
.write(modules); |
|
157 |
new ModuleBuilder("M") |
|
158 |
.requires("N") |
|
159 |
.classes("package pack; public class C { publ.B b; }") |
|
160 |
.write(modules); |
|
161 |
||
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
162 |
String log = new JavacTask(tb) |
36526 | 163 |
.options("-XDrawDiagnostics", "-modulesourcepath", modules + "/*/src") |
164 |
.outdir(Files.createDirectories(base.resolve("classes"))) |
|
165 |
.files(findJavaFiles(modules)) |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
166 |
.run(Task.Expect.SUCCESS) |
36526 | 167 |
.writeAll() |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
168 |
.getOutput(Task.OutputKind.DIRECT); |
36526 | 169 |
|
170 |
if (!log.contains("")) |
|
171 |
throw new Exception("unexpected output not found"); |
|
172 |
||
173 |
} |
|
174 |
||
175 |
@Test |
|
176 |
void testPrivateConflictOnModulePath(Path base) throws Exception { |
|
177 |
Path modules = base.resolve("modules"); |
|
178 |
new ModuleBuilder("N") |
|
179 |
.exports("publ") |
|
180 |
.classes("package pack; public class A { }") |
|
181 |
.classes("package publ; public class B { }") |
|
182 |
.build(modules); |
|
183 |
new ModuleBuilder("M") |
|
184 |
.requires("N") |
|
185 |
.classes("package pack; public class C { publ.B b; }") |
|
186 |
.write(modules); |
|
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", "-mp", modules.toString()) |
190 |
.outdir(Files.createDirectories(base.resolve("classes"))) |
|
191 |
.files(findJavaFiles(modules.resolve("M"))) |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
192 |
.run(Task.Expect.SUCCESS) |
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("")) |
|
197 |
throw new Exception("expected output not found"); |
|
198 |
||
199 |
} |
|
200 |
||
201 |
@Test |
|
202 |
void testRequiresConflictExports(Path base) throws Exception { |
|
203 |
Path modules = base.resolve("modules"); |
|
204 |
new ModuleBuilder("M") |
|
205 |
.exports("pack") |
|
206 |
.classes("package pack; public class A { }") |
|
207 |
.build(modules); |
|
208 |
new ModuleBuilder("N") |
|
209 |
.exports("pack") |
|
210 |
.classes("package pack; public class B { }") |
|
211 |
.build(modules); |
|
212 |
new ModuleBuilder("K") |
|
213 |
.requires("M") |
|
214 |
.requires("N") |
|
215 |
.classes("package pkg; public class C { pack.A a; pack.B b; }") |
|
216 |
.write(modules); |
|
217 |
||
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
218 |
List<String> log = new JavacTask(tb) |
36526 | 219 |
.options("-XDrawDiagnostics", "-mp", modules.toString()) |
220 |
.outdir(Files.createDirectories(base.resolve("classes"))) |
|
221 |
.files(findJavaFiles(modules.resolve("K"))) |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
222 |
.run(Task.Expect.FAIL) |
36526 | 223 |
.writeAll() |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
224 |
.getOutputLines(Task.OutputKind.DIRECT); |
36526 | 225 |
|
226 |
List<String> expected = |
|
227 |
Arrays.asList("module-info.java:1:1: compiler.err.package.clash.from.requires: K, pack, M, N", |
|
228 |
"1 error"); |
|
229 |
if (!log.containsAll(expected)) |
|
230 |
throw new Exception("expected output not found"); |
|
231 |
} |
|
232 |
||
233 |
@Test |
|
234 |
void testQulifiedExportsToDifferentModules(Path base) throws Exception { |
|
235 |
Path modules = base.resolve("modules"); |
|
236 |
new ModuleBuilder("U").write(modules); |
|
237 |
new ModuleBuilder("M") |
|
238 |
.exports("pkg to U") |
|
239 |
.classes("package pkg; public class A { public static boolean flagM; }") |
|
240 |
.write(modules); |
|
241 |
new ModuleBuilder("N") |
|
242 |
.exports("pkg to K") |
|
243 |
.classes("package pkg; public class A { public static boolean flagN; }") |
|
244 |
.write(modules); |
|
245 |
ModuleBuilder moduleK = new ModuleBuilder("K"); |
|
246 |
moduleK.requires("M") |
|
247 |
.requires("N") |
|
248 |
.classes("package p; public class DependsOnN { boolean f = pkg.A.flagN; } ") |
|
249 |
.write(modules); |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
250 |
new JavacTask(tb) |
36526 | 251 |
.options("-modulesourcepath", modules + "/*/src") |
252 |
.outdir(Files.createDirectories(base.resolve("classes"))) |
|
253 |
.files(findJavaFiles(modules.resolve("K"))) |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
254 |
.run(Task.Expect.SUCCESS) |
36526 | 255 |
.writeAll(); |
256 |
||
257 |
//negative case |
|
258 |
moduleK.classes("package pkg; public class DuplicatePackage { } ") |
|
259 |
.classes("package p; public class DependsOnM { boolean f = pkg.A.flagM; } ") |
|
260 |
.write(modules); |
|
261 |
||
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
262 |
List<String> output = new JavacTask(tb) |
36526 | 263 |
.options("-XDrawDiagnostics", |
264 |
"-modulesourcepath", modules + "/*/src") |
|
265 |
.outdir(Files.createDirectories(base.resolve("classes"))) |
|
266 |
.files(findJavaFiles(modules.resolve("K"))) |
|
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
267 |
.run(Task.Expect.FAIL) |
36526 | 268 |
.writeAll() |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
269 |
.getOutputLines(Task.OutputKind.DIRECT); |
36526 | 270 |
|
271 |
List<String> expected = Arrays.asList( |
|
272 |
"DependsOnM.java:1:55: compiler.err.cant.resolve.location: kindname.variable, flagM, , , (compiler.misc.location: kindname.class, pkg.A, null)"); |
|
273 |
if (!output.containsAll(expected)) { |
|
274 |
throw new Exception("expected output not found"); |
|
275 |
} |
|
276 |
} |
|
277 |
} |