author | dl |
Fri, 03 Feb 2017 13:24:59 -0800 | |
changeset 43522 | f9c6f543c4db |
parent 42338 | a60f280f803c |
child 43712 | 5dfd0950317c |
permissions | -rw-r--r-- |
36511 | 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 Negative tests for jlink |
|
27 |
* @bug 8130861 |
|
28 |
* @author Andrei Eremeev |
|
29 |
* @library ../lib |
|
30 |
* @modules java.base/jdk.internal.jimage |
|
31 |
* java.base/jdk.internal.module |
|
32 |
* jdk.jdeps/com.sun.tools.classfile |
|
33 |
* jdk.jlink/jdk.tools.jlink.internal |
|
34 |
* jdk.jlink/jdk.tools.jmod |
|
35 |
* jdk.jlink/jdk.tools.jimage |
|
36 |
* jdk.compiler |
|
37 |
* @build tests.* |
|
38 |
* @run testng JLinkNegativeTest |
|
39 |
*/ |
|
40 |
||
41 |
import java.io.IOException; |
|
42 |
import java.io.OutputStream; |
|
43 |
import java.lang.module.ModuleDescriptor; |
|
44 |
import java.nio.file.FileVisitResult; |
|
45 |
import java.nio.file.Files; |
|
46 |
import java.nio.file.Path; |
|
47 |
import java.nio.file.Paths; |
|
48 |
import java.nio.file.SimpleFileVisitor; |
|
49 |
import java.nio.file.attribute.BasicFileAttributes; |
|
50 |
import java.util.Arrays; |
|
51 |
import java.util.Collections; |
|
52 |
import java.util.List; |
|
53 |
||
54 |
import jdk.internal.module.ModuleInfoWriter; |
|
55 |
import org.testng.SkipException; |
|
56 |
import org.testng.annotations.BeforeClass; |
|
57 |
import org.testng.annotations.Test; |
|
58 |
import tests.Helper; |
|
59 |
import tests.JImageGenerator; |
|
60 |
import tests.JImageGenerator.InMemoryFile; |
|
61 |
import tests.Result; |
|
62 |
||
63 |
@Test |
|
64 |
public class JLinkNegativeTest { |
|
65 |
||
66 |
private Helper helper; |
|
67 |
||
68 |
@BeforeClass |
|
69 |
public void setUp() throws IOException { |
|
70 |
helper = Helper.newHelper(); |
|
71 |
if (helper == null) { |
|
72 |
throw new SkipException("Not run"); |
|
73 |
} |
|
74 |
helper.generateDefaultModules(); |
|
75 |
} |
|
76 |
||
77 |
private void deleteDirectory(Path dir) throws IOException { |
|
78 |
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() { |
|
79 |
@Override |
|
80 |
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
|
81 |
Files.delete(file); |
|
82 |
return FileVisitResult.CONTINUE; |
|
83 |
} |
|
84 |
||
85 |
@Override |
|
86 |
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { |
|
87 |
Files.delete(dir); |
|
88 |
return FileVisitResult.CONTINUE; |
|
89 |
} |
|
90 |
}); |
|
91 |
} |
|
92 |
||
93 |
public void testModuleNotExist() { |
|
94 |
helper.generateDefaultImage("failure1").assertFailure("Error: Module failure1 not found"); |
|
95 |
} |
|
96 |
||
97 |
public void testNotExistInAddMods() { |
|
40261
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
36511
diff
changeset
|
98 |
// cannot find jmod from --add-modules |
36511 | 99 |
JImageGenerator.getJLinkTask() |
100 |
.modulePath(".") |
|
101 |
.addMods("not_exist") |
|
102 |
.output(helper.getImageDir().resolve("failure2")) |
|
103 |
.call().assertFailure("Error: Module not_exist not found"); |
|
104 |
} |
|
105 |
||
106 |
public void test() throws IOException { |
|
107 |
helper.generateDefaultJModule("failure3"); |
|
108 |
Path image = helper.generateDefaultImage("failure3").assertSuccess(); |
|
109 |
JImageGenerator.getJLinkTask() |
|
110 |
.modulePath(helper.defaultModulePath()) |
|
111 |
.output(image) |
|
112 |
.addMods("leaf1") |
|
113 |
.limitMods("leaf1") |
|
114 |
.call().assertFailure("Error: directory already exists: .*failure3.image(\n|\r|.)*"); |
|
115 |
} |
|
116 |
||
117 |
public void testOutputIsFile() throws IOException { |
|
118 |
// output == file |
|
119 |
Path image = helper.createNewImageDir("failure4"); |
|
120 |
Files.createFile(image); |
|
121 |
JImageGenerator.getJLinkTask() |
|
122 |
.modulePath(helper.defaultModulePath()) |
|
123 |
.output(image) |
|
124 |
.addMods("leaf1") |
|
125 |
.call().assertFailure("Error: directory already exists: .*failure4.image(\n|\r|.)*"); |
|
126 |
} |
|
127 |
||
128 |
public void testModuleNotFound() { |
|
129 |
// limit module is not found |
|
130 |
Path imageFile = helper.createNewImageDir("test"); |
|
131 |
JImageGenerator.getJLinkTask() |
|
132 |
.output(imageFile) |
|
133 |
.addMods("leaf1") |
|
134 |
.limitMods("leaf1") |
|
135 |
.limitMods("failure5") |
|
136 |
.modulePath(helper.defaultModulePath()) |
|
137 |
.call().assertFailure("Error: Module failure5 not found"); |
|
138 |
} |
|
139 |
||
140 |
public void testJmodIsDir() throws IOException { |
|
141 |
Path imageFile = helper.createNewImageDir("test"); |
|
142 |
Path dirJmod = helper.createNewJmodFile("dir"); |
|
143 |
Files.createDirectory(dirJmod); |
|
144 |
try { |
|
145 |
JImageGenerator.getJLinkTask() |
|
146 |
.output(imageFile) |
|
147 |
.addMods("dir") |
|
148 |
.modulePath(helper.defaultModulePath()) |
|
149 |
.call().assertFailure("Error: Module dir not found"); |
|
150 |
} finally { |
|
151 |
deleteDirectory(dirJmod); |
|
152 |
} |
|
153 |
} |
|
154 |
||
155 |
public void testJarIsDir() throws IOException { |
|
156 |
Path imageFile = helper.createNewImageDir("test"); |
|
157 |
Path dirJar = helper.createNewJarFile("dir"); |
|
158 |
Files.createDirectory(dirJar); |
|
159 |
try { |
|
160 |
JImageGenerator.getJLinkTask() |
|
161 |
.output(imageFile) |
|
162 |
.addMods("dir") |
|
163 |
.modulePath(helper.defaultModulePath()) |
|
164 |
.call().assertFailure("Error: Module dir not found"); |
|
165 |
} finally { |
|
166 |
deleteDirectory(dirJar); |
|
167 |
} |
|
168 |
} |
|
169 |
||
170 |
public void testMalformedJar() throws IOException { |
|
171 |
Path imageFile = helper.createNewImageDir("test"); |
|
172 |
Path jar = helper.createNewJarFile("not_zip"); |
|
173 |
Files.createFile(jar); |
|
174 |
try { |
|
175 |
JImageGenerator.getJLinkTask() |
|
176 |
.output(imageFile) |
|
177 |
.addMods("not_zip") |
|
178 |
.modulePath(helper.defaultModulePath()) |
|
179 |
.call().assertFailure("Error: java.util.zip.ZipException: zip file is empty"); |
|
180 |
} finally { |
|
181 |
deleteDirectory(jar); |
|
182 |
} |
|
183 |
} |
|
184 |
||
185 |
public void testMalformedJmod() throws IOException { |
|
186 |
Path imageFile = helper.createNewImageDir("test"); |
|
187 |
Path jmod = helper.createNewJmodFile("not_zip"); |
|
188 |
Files.createFile(jmod); |
|
189 |
try { |
|
190 |
JImageGenerator.getJLinkTask() |
|
191 |
.output(imageFile) |
|
192 |
.addMods("not_zip") |
|
193 |
.modulePath(helper.defaultModulePath()) |
|
41352 | 194 |
.call().assertFailure("Error: java.io.IOException: Invalid jmod file"); |
36511 | 195 |
} finally { |
196 |
deleteDirectory(jmod); |
|
197 |
} |
|
198 |
} |
|
199 |
||
200 |
// Temporarily exclude; the jmod tool can no longer be used to create a jmod |
|
201 |
// with a class in the unnamed package. Find another way, or remove. |
|
202 |
// public void testAddDefaultPackage() throws IOException { |
|
203 |
// String moduleName = "hacked1"; |
|
204 |
// Path module = helper.generateModuleCompiledClasses(helper.getJmodSrcDir(), helper.getJmodClassesDir(), |
|
205 |
// moduleName, Arrays.asList("hacked1.Main", "A", "B"), "leaf1"); |
|
206 |
// JImageGenerator |
|
207 |
// .getJModTask() |
|
208 |
// .addClassPath(module) |
|
209 |
// .jmod(helper.getJmodDir().resolve(moduleName + ".jmod")) |
|
210 |
// .create().assertSuccess(); |
|
211 |
// Path image = helper.generateDefaultImage(moduleName).assertSuccess(); |
|
212 |
// helper.checkImage(image, moduleName, null, null); |
|
213 |
// } |
|
214 |
||
215 |
public void testAddSomeTopLevelFiles() throws IOException { |
|
216 |
String moduleName = "hacked2"; |
|
217 |
Path module = helper.generateModuleCompiledClasses(helper.getJmodSrcDir(), helper.getJmodClassesDir(), |
|
218 |
moduleName); |
|
219 |
Files.createFile(module.resolve("top-level-file")); |
|
220 |
Path jmod = JImageGenerator |
|
221 |
.getJModTask() |
|
222 |
.addClassPath(module) |
|
223 |
.jmod(helper.getJmodDir().resolve(moduleName + ".jmod")) |
|
224 |
.create().assertSuccess(); |
|
225 |
try { |
|
226 |
Path image = helper.generateDefaultImage(moduleName).assertSuccess(); |
|
227 |
helper.checkImage(image, moduleName, null, null); |
|
228 |
} finally { |
|
229 |
deleteDirectory(jmod); |
|
230 |
} |
|
231 |
} |
|
232 |
||
233 |
public void testAddNonStandardSection() throws IOException { |
|
234 |
String moduleName = "hacked3"; |
|
235 |
Path module = helper.generateDefaultJModule(moduleName).assertSuccess(); |
|
236 |
JImageGenerator.addFiles(module, new InMemoryFile("unknown/A.class", new byte[0])); |
|
237 |
try { |
|
238 |
Result result = helper.generateDefaultImage(moduleName); |
|
41352 | 239 |
System.err.println(result.getMessage()); |
240 |
if (result.getExitCode() == 0) { |
|
36511 | 241 |
throw new AssertionError("Crash expected"); |
242 |
} |
|
243 |
} finally { |
|
244 |
deleteDirectory(module); |
|
245 |
} |
|
246 |
} |
|
247 |
||
248 |
@Test(enabled = true) |
|
249 |
public void testSectionsAreFiles() throws IOException { |
|
41352 | 250 |
String moduleName = "hacked4"; |
36511 | 251 |
Path jmod = helper.generateDefaultJModule(moduleName).assertSuccess(); |
252 |
JImageGenerator.addFiles(jmod, |
|
253 |
new InMemoryFile("/native", new byte[0]), |
|
254 |
new InMemoryFile("/conf", new byte[0]), |
|
255 |
new InMemoryFile("/bin", new byte[0])); |
|
256 |
try { |
|
257 |
Result result = helper.generateDefaultImage(moduleName); |
|
41352 | 258 |
System.err.println(result.getMessage()); |
259 |
if (result.getExitCode() == 0) { |
|
36511 | 260 |
throw new AssertionError("Crash expected"); |
261 |
} |
|
262 |
} finally { |
|
263 |
deleteDirectory(jmod); |
|
264 |
} |
|
265 |
} |
|
266 |
||
267 |
public void testDuplicateModule1() throws IOException { |
|
268 |
String moduleName1 = "dupRes1Jmod1"; |
|
269 |
String moduleName2 = "dupRes1Jmod2"; |
|
270 |
List<String> classNames = Arrays.asList("java.A", "javax.B"); |
|
271 |
Path module1 = helper.generateModuleCompiledClasses( |
|
272 |
helper.getJmodSrcDir(), helper.getJmodClassesDir(), moduleName1, classNames); |
|
273 |
Path module2 = helper.generateModuleCompiledClasses( |
|
274 |
helper.getJmodSrcDir(), helper.getJmodClassesDir(), moduleName2, classNames); |
|
275 |
||
276 |
try (OutputStream out = Files.newOutputStream(module2.resolve("module-info.class"))) { |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41352
diff
changeset
|
277 |
ModuleInfoWriter.write(ModuleDescriptor.module(moduleName1) |
36511 | 278 |
.requires("java.base").build(), out); |
279 |
} |
|
280 |
||
281 |
Path jmod1 = JImageGenerator.getJModTask() |
|
282 |
.addClassPath(module1) |
|
283 |
.jmod(helper.createNewJmodFile(moduleName1)) |
|
284 |
.create() |
|
285 |
.assertSuccess(); |
|
286 |
Path jmod2 = JImageGenerator.getJModTask() |
|
287 |
.addClassPath(module2) |
|
288 |
.jmod(helper.createNewJmodFile(moduleName2)) |
|
289 |
.create() |
|
290 |
.assertSuccess(); |
|
291 |
try { |
|
292 |
helper.generateDefaultImage(moduleName1) |
|
293 |
.assertFailure("Error: Two versions of module dupRes1Jmod1 found in"); |
|
294 |
} finally { |
|
295 |
deleteDirectory(jmod1); |
|
296 |
deleteDirectory(jmod2); |
|
297 |
} |
|
298 |
} |
|
299 |
||
300 |
public void testDuplicateModule2() throws IOException { |
|
301 |
String moduleName = "dupRes2Jmod"; |
|
302 |
List<String> classNames = Arrays.asList("java.A", "javax.B"); |
|
303 |
Path module1 = helper.generateModuleCompiledClasses( |
|
304 |
helper.getJmodSrcDir(), helper.getJmodClassesDir(), moduleName, classNames); |
|
305 |
Path module2 = helper.generateModuleCompiledClasses( |
|
306 |
helper.getJarSrcDir(), helper.getJarClassesDir(), moduleName, classNames); |
|
307 |
||
308 |
Path jmod = JImageGenerator.getJModTask() |
|
309 |
.addClassPath(module1) |
|
310 |
.jmod(helper.createNewJmodFile(moduleName)) |
|
311 |
.create() |
|
312 |
.assertSuccess(); |
|
313 |
Path jar = JImageGenerator.createJarFile(helper.getJarDir().resolve(moduleName + ".jar"), module2); |
|
314 |
Path newJar = helper.getJmodDir().resolve(jar.getFileName()); |
|
315 |
Files.move(jar, newJar); |
|
316 |
try { |
|
317 |
helper.generateDefaultImage(moduleName) |
|
318 |
.assertFailure("Error: Two versions of module dupRes2Jmod found in"); |
|
319 |
} finally { |
|
320 |
deleteDirectory(jmod); |
|
321 |
deleteDirectory(newJar); |
|
322 |
} |
|
323 |
} |
|
324 |
||
325 |
public void testDuplicateModule3() throws IOException { |
|
326 |
String moduleName1 = "dupRes3Jar1"; |
|
327 |
String moduleName2 = "dupRes3Jar2"; |
|
328 |
List<String> classNames = Arrays.asList("java.A", "javax.B"); |
|
329 |
Path module1 = helper.generateModuleCompiledClasses( |
|
330 |
helper.getJarSrcDir(), helper.getJarClassesDir(), moduleName1, classNames); |
|
331 |
Path module2 = helper.generateModuleCompiledClasses( |
|
332 |
helper.getJarSrcDir(), helper.getJarClassesDir(), moduleName2, classNames); |
|
333 |
||
334 |
try (OutputStream out = Files.newOutputStream(module2.resolve("module-info.class"))) { |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41352
diff
changeset
|
335 |
ModuleInfoWriter.write(ModuleDescriptor.module(moduleName1) |
36511 | 336 |
.requires("java.base").build(), out); |
337 |
} |
|
338 |
||
339 |
Path jar1 = JImageGenerator.createJarFile(helper.getJarDir().resolve(moduleName1 + ".jar"), module1); |
|
340 |
Path jar2 = JImageGenerator.createJarFile(helper.getJarDir().resolve(moduleName2 + ".jar"), module2); |
|
341 |
try { |
|
342 |
helper.generateDefaultImage(moduleName1) |
|
343 |
.assertFailure("Error: Two versions of module dupRes3Jar1 found in"); |
|
344 |
} finally { |
|
345 |
deleteDirectory(jar1); |
|
346 |
deleteDirectory(jar2); |
|
347 |
} |
|
348 |
} |
|
349 |
} |