author | clanger |
Fri, 22 Nov 2019 09:25:09 +0100 | |
changeset 59216 | 47c879f478d2 |
parent 51977 | a8862960c19f |
permissions | -rw-r--r-- |
45004 | 1 |
/* |
59216
47c879f478d2
8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents:
51977
diff
changeset
|
2 |
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. |
45004 | 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 |
* @modules jdk.zipfs |
|
51977
a8862960c19f
8211171: move JarUtils to top-level testlibrary
iignatyev
parents:
47216
diff
changeset
|
27 |
* @library /test/lib |
a8862960c19f
8211171: move JarUtils to top-level testlibrary
iignatyev
parents:
47216
diff
changeset
|
28 |
* @build ModulesInCustomFileSystem m1/* m2/* |
a8862960c19f
8211171: move JarUtils to top-level testlibrary
iignatyev
parents:
47216
diff
changeset
|
29 |
* jdk.test.lib.util.JarUtils |
45004 | 30 |
* @run testng/othervm ModulesInCustomFileSystem |
31 |
* @summary Test ModuleFinder to find modules in a custom file system |
|
32 |
*/ |
|
33 |
||
34 |
import java.io.File; |
|
35 |
import java.lang.module.Configuration; |
|
36 |
import java.lang.module.ModuleFinder; |
|
37 |
import java.lang.module.ModuleReader; |
|
38 |
import java.lang.module.ModuleReference; |
|
39 |
import java.lang.reflect.Method; |
|
40 |
import java.nio.file.FileSystem; |
|
41 |
import java.nio.file.FileSystems; |
|
42 |
import java.nio.file.Files; |
|
43 |
import java.nio.file.Path; |
|
44 |
import java.nio.file.Paths; |
|
45 |
import java.util.Set; |
|
46 |
||
51977
a8862960c19f
8211171: move JarUtils to top-level testlibrary
iignatyev
parents:
47216
diff
changeset
|
47 |
import jdk.test.lib.util.JarUtils; |
a8862960c19f
8211171: move JarUtils to top-level testlibrary
iignatyev
parents:
47216
diff
changeset
|
48 |
|
45004 | 49 |
import org.testng.annotations.Test; |
50 |
import static org.testng.Assert.*; |
|
51 |
||
52 |
@Test |
|
53 |
public class ModulesInCustomFileSystem { |
|
45833
882f4885ec80
8183536: Update jdk tests to allow for unique test classes directory
alanb
parents:
45004
diff
changeset
|
54 |
private static final Path HERE = Paths.get(""); |
45004 | 55 |
|
56 |
/** |
|
59216
47c879f478d2
8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents:
51977
diff
changeset
|
57 |
* Test exploded modules in a Zip file system. |
45004 | 58 |
*/ |
59216
47c879f478d2
8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents:
51977
diff
changeset
|
59 |
public void testExplodedModulesInZipFileSystem() throws Exception { |
45004 | 60 |
Path m1 = findModuleDirectory("m1"); |
61 |
Path m2 = findModuleDirectory("m2"); |
|
62 |
Path mlib = m1.getParent(); |
|
63 |
assertEquals(mlib, m2.getParent()); |
|
64 |
||
65 |
// create JAR file containing m1/** and m2/** |
|
45833
882f4885ec80
8183536: Update jdk tests to allow for unique test classes directory
alanb
parents:
45004
diff
changeset
|
66 |
Path jar = Files.createTempDirectory(HERE, "mlib").resolve("modules.jar"); |
45004 | 67 |
JarUtils.createJarFile(jar, mlib); |
59216
47c879f478d2
8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents:
51977
diff
changeset
|
68 |
testZipFileSystem(jar); |
45004 | 69 |
} |
70 |
||
71 |
/** |
|
59216
47c879f478d2
8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents:
51977
diff
changeset
|
72 |
* Test modular JARs in a Zip file system. |
45004 | 73 |
*/ |
59216
47c879f478d2
8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents:
51977
diff
changeset
|
74 |
public void testModularJARsInZipFileSystem() throws Exception { |
45004 | 75 |
Path m1 = findModuleDirectory("m1"); |
76 |
Path m2 = findModuleDirectory("m2"); |
|
45833
882f4885ec80
8183536: Update jdk tests to allow for unique test classes directory
alanb
parents:
45004
diff
changeset
|
77 |
Path contents = Files.createTempDirectory(HERE, "contents"); |
45004 | 78 |
JarUtils.createJarFile(contents.resolve("m1.jar"), m1); |
79 |
JarUtils.createJarFile(contents.resolve("m2.jar"), m2); |
|
80 |
||
81 |
// create JAR file containing m1.jar and m2.jar |
|
45833
882f4885ec80
8183536: Update jdk tests to allow for unique test classes directory
alanb
parents:
45004
diff
changeset
|
82 |
Path jar = Files.createTempDirectory(HERE, "mlib").resolve("modules.jar"); |
45004 | 83 |
JarUtils.createJarFile(jar, contents); |
59216
47c879f478d2
8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents:
51977
diff
changeset
|
84 |
testZipFileSystem(jar); |
45004 | 85 |
} |
86 |
||
87 |
/** |
|
88 |
* Opens a JAR file as a file system |
|
89 |
*/ |
|
59216
47c879f478d2
8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents:
51977
diff
changeset
|
90 |
private void testZipFileSystem(Path zip) throws Exception { |
47c879f478d2
8234089: (zipfs) Remove classes JarFileSystemProvider and JarFileSystem
clanger
parents:
51977
diff
changeset
|
91 |
try (FileSystem fs = FileSystems.newFileSystem(zip)) { |
45004 | 92 |
// ModuleFinder to find modules in top-level directory |
93 |
Path top = fs.getPath("/"); |
|
94 |
ModuleFinder finder = ModuleFinder.of(top); |
|
95 |
||
96 |
// list the modules |
|
97 |
listAllModules(finder); |
|
98 |
||
99 |
// load modules into child layer, invoking m1/p.Main |
|
100 |
loadAndRunModule(finder); |
|
101 |
} |
|
102 |
} |
|
103 |
||
104 |
/** |
|
105 |
* List all modules that the finder finds and the resources in the module. |
|
106 |
*/ |
|
107 |
private void listAllModules(ModuleFinder finder) throws Exception { |
|
108 |
for (ModuleReference mref : finder.findAll()) { |
|
109 |
System.out.println(mref.descriptor()); |
|
110 |
try (ModuleReader reader = mref.open()) { |
|
111 |
reader.list().forEach(name -> System.out.format(" %s%n", name)); |
|
112 |
} |
|
113 |
} |
|
114 |
} |
|
115 |
||
116 |
/** |
|
117 |
* Creates a child layer with m1 and m2, invokes m1/p.Main to ensure that |
|
118 |
* classes can be loaded. |
|
119 |
*/ |
|
120 |
private void loadAndRunModule(ModuleFinder finder) throws Exception { |
|
121 |
ModuleLayer bootLayer = ModuleLayer.boot(); |
|
122 |
Configuration cf = bootLayer.configuration() |
|
123 |
.resolve(finder, ModuleFinder.of(), Set.of("m1")); |
|
124 |
ClassLoader scl = ClassLoader.getSystemClassLoader(); |
|
125 |
ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, scl); |
|
126 |
Class<?> c = layer.findLoader("m1").loadClass("p.Main"); |
|
127 |
Method m = c.getMethod("main", String[].class); |
|
128 |
m.invoke(null, (Object)new String[0]); |
|
129 |
} |
|
130 |
||
131 |
/** |
|
132 |
* Find the directory for a module on the module path |
|
133 |
*/ |
|
134 |
private Path findModuleDirectory(String name) { |
|
135 |
String mp = System.getProperty("jdk.module.path"); |
|
136 |
for (String element : mp.split(File.pathSeparator)) { |
|
137 |
Path dir = Paths.get(element).resolve(name); |
|
138 |
if (Files.exists(dir)) { |
|
139 |
return dir; |
|
140 |
} |
|
141 |
} |
|
142 |
assertFalse(true); |
|
143 |
return null; |
|
144 |
} |
|
145 |
} |