|
21 | 21 | * questions. |
22 | 22 | */ |
23 | 23 |
|
24 | | -/** |
| 24 | +/* |
25 | 25 | * @test |
26 | 26 | * @bug 4313887 7006126 8142968 8178380 8183320 8210112 8266345 8263940 |
27 | | - * @modules jdk.jartool |
| 27 | + * @modules jdk.jartool jdk.jlink |
28 | 28 | * @library /test/lib |
29 | | - * @build SetDefaultProvider TestProvider m/* jdk.test.lib.process.ProcessTools |
30 | | - * @run testng/othervm SetDefaultProvider |
| 29 | + * @build testfsp/* testapp/* |
| 30 | + * @run junit SetDefaultProvider |
31 | 31 | * @summary Runs tests with -Djava.nio.file.spi.DefaultFileSystemProvider set on |
32 | 32 | * the command line to override the default file system provider |
33 | 33 | */ |
|
37 | 37 | import java.nio.file.Files; |
38 | 38 | import java.nio.file.Path; |
39 | 39 | import java.nio.file.Paths; |
| 40 | +import java.util.Arrays; |
40 | 41 | import java.util.ArrayList; |
41 | 42 | import java.util.List; |
42 | 43 | import java.util.spi.ToolProvider; |
43 | 44 | import java.util.stream.Stream; |
44 | 45 |
|
45 | 46 | import jdk.test.lib.process.ProcessTools; |
| 47 | +import org.junit.jupiter.api.Test; |
| 48 | +import org.junit.jupiter.api.Disabled; |
| 49 | +import org.junit.jupiter.api.BeforeAll; |
| 50 | +import static org.junit.jupiter.api.Assertions.*; |
46 | 51 |
|
47 | | -import org.testng.annotations.Test; |
48 | | -import static org.testng.Assert.*; |
49 | | - |
50 | | -@Test |
51 | | -public class SetDefaultProvider { |
| 52 | +class SetDefaultProvider { |
52 | 53 |
|
53 | 54 | private static final String SET_DEFAULT_FSP = |
54 | | - "-Djava.nio.file.spi.DefaultFileSystemProvider=TestProvider"; |
| 55 | + "-Djava.nio.file.spi.DefaultFileSystemProvider=testfsp.TestProvider"; |
55 | 56 |
|
56 | 57 | private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar") |
57 | 58 | .orElseThrow(() -> |
58 | 59 | new RuntimeException("jar tool not found") |
59 | 60 | ); |
60 | 61 |
|
61 | | - private static Path createTempDirectory(String prefix) throws IOException { |
62 | | - Path testDir = Paths.get(System.getProperty("test.dir", ".")); |
63 | | - return Files.createTempDirectory(testDir, prefix); |
| 62 | + private static final String TESTFSP = "testfsp"; |
| 63 | + private static final String TESTAPP = "testapp"; |
| 64 | + private static final String TESTAPP_MAIN = TESTAPP + ".Main"; |
| 65 | + |
| 66 | + // directory containing testfsp class files |
| 67 | + private static String TESTFSP_CLASSES; |
| 68 | + |
| 69 | + // directory containing testapp class files |
| 70 | + private static String TESTAPP_CLASSES; |
| 71 | + |
| 72 | + @BeforeAll |
| 73 | + static void setup() { |
| 74 | + TESTFSP_CLASSES = classes(TESTFSP); |
| 75 | + TESTAPP_CLASSES = classes(TESTAPP); |
64 | 76 | } |
65 | 77 |
|
66 | 78 | /** |
67 | | - * Test override of default FileSystemProvider with the main application |
68 | | - * on the class path. |
| 79 | + * Test file system provider exploded on the class path. |
69 | 80 | */ |
70 | | - public void testClassPath() throws Exception { |
71 | | - String moduleClasses = moduleClasses(); |
72 | | - String testClasses = System.getProperty("test.classes"); |
73 | | - String classpath = moduleClasses + File.pathSeparator + testClasses; |
74 | | - int exitValue = exec(SET_DEFAULT_FSP, "-cp", classpath, "p.Main"); |
75 | | - assertEquals(exitValue, 0); |
| 81 | + @Test |
| 82 | + void testFspOnClassPath1() throws Exception { |
| 83 | + exec(SET_DEFAULT_FSP, |
| 84 | + "-cp", ofClasspath(TESTFSP_CLASSES, TESTAPP_CLASSES), |
| 85 | + TESTAPP_MAIN); |
76 | 86 | } |
77 | 87 |
|
78 | 88 | /** |
79 | | - * Test override of default FileSystemProvider with a |
80 | | - * FileSystemProvider jar and the main application on the class path. |
| 89 | + * Test file system provider in JAR file on the class path. |
81 | 90 | */ |
82 | | - public void testClassPathWithFileSystemProviderJar() throws Exception { |
83 | | - String testClasses = System.getProperty("test.classes"); |
84 | | - Path jar = Path.of("testFileSystemProvider.jar"); |
85 | | - Files.deleteIfExists(jar); |
86 | | - createFileSystemProviderJar(jar, Path.of(testClasses)); |
87 | | - String classpath = jar + File.pathSeparator + testClasses |
88 | | - + File.separator + "modules" + File.separator + "m"; |
89 | | - int exitValue = exec(SET_DEFAULT_FSP, "-cp", classpath, "p.Main"); |
90 | | - assertEquals(exitValue, 0); |
| 91 | + @Test |
| 92 | + void testFspOnClassPath2() throws Exception { |
| 93 | + String jarFile = createJar("fsp.jar", TESTFSP_CLASSES); |
| 94 | + exec(SET_DEFAULT_FSP, |
| 95 | + "-cp", ofClasspath(jarFile, TESTAPP_CLASSES), |
| 96 | + TESTAPP_MAIN); |
91 | 97 | } |
92 | 98 |
|
93 | 99 | /** |
94 | | - * Creates a JAR containing the FileSystemProvider used to override the |
95 | | - * default FileSystemProvider |
| 100 | + * Test file system provider in exploded module on the module path. |
96 | 101 | */ |
97 | | - private void createFileSystemProviderJar(Path jar, Path dir) throws IOException { |
| 102 | + @Test |
| 103 | + void testFspOnModulePath1() throws Exception { |
| 104 | + exec(SET_DEFAULT_FSP, |
| 105 | + "-p", TESTFSP_CLASSES, |
| 106 | + "--add-modules", TESTFSP, |
| 107 | + "-cp", TESTAPP_CLASSES, |
| 108 | + TESTAPP_MAIN); |
| 109 | + } |
98 | 110 |
|
99 | | - List<String> args = new ArrayList<>(); |
100 | | - args.add("--create"); |
101 | | - args.add("--file=" + jar); |
102 | | - try (Stream<Path> stream = Files.list(dir)) { |
103 | | - List<String> paths = stream |
104 | | - .map(path -> path.getFileName().toString()) |
105 | | - .filter(f -> f.startsWith("TestProvider")) |
106 | | - .toList(); |
107 | | - for(var p : paths) { |
108 | | - args.add("-C"); |
109 | | - args.add(dir.toString()); |
110 | | - args.add(p); |
111 | | - } |
112 | | - } |
113 | | - int ret = JAR_TOOL.run(System.out, System.out, args.toArray(new String[0])); |
114 | | - assertEquals(ret, 0); |
| 111 | + /** |
| 112 | + * Test file system provider in modular JAR on the module path. |
| 113 | + */ |
| 114 | + @Test |
| 115 | + void testFspOnModulePath2() throws Exception { |
| 116 | + String jarFile = createJar("fsp.jar", TESTFSP_CLASSES); |
| 117 | + exec(SET_DEFAULT_FSP, |
| 118 | + "-p", jarFile, |
| 119 | + "--add-modules", TESTFSP, |
| 120 | + "-cp", TESTAPP_CLASSES, |
| 121 | + TESTAPP_MAIN); |
115 | 122 | } |
116 | 123 |
|
117 | 124 | /** |
118 | | - * Test override of default FileSystemProvider with the main application |
119 | | - * on the module path as an exploded module. |
| 125 | + * Test file system provider linked into run-time image. |
120 | 126 | */ |
121 | | - public void testExplodedModule() throws Exception { |
122 | | - String modulePath = System.getProperty("jdk.module.path"); |
123 | | - int exitValue = exec(SET_DEFAULT_FSP, "-p", modulePath, "-m", "m/p.Main"); |
124 | | - assertEquals(exitValue, 0); |
| 127 | + @Disabled |
| 128 | + @Test |
| 129 | + void testFspInRuntimeImage() throws Exception { |
| 130 | + String image = "image"; |
| 131 | + |
| 132 | + ToolProvider jlink = ToolProvider.findFirst("jlink").orElseThrow(); |
| 133 | + String[] jlinkCmd = { |
| 134 | + "--module-path", TESTFSP_CLASSES, |
| 135 | + "--add-modules", TESTFSP, |
| 136 | + "--output", image |
| 137 | + }; |
| 138 | + int exitCode = jlink.run(System.out, System.err, jlinkCmd); |
| 139 | + assertEquals(0, exitCode); |
| 140 | + |
| 141 | + String[] javaCmd = { |
| 142 | + Path.of(image, "bin", "java").toString(), |
| 143 | + SET_DEFAULT_FSP, |
| 144 | + "--add-modules", TESTFSP, |
| 145 | + "-cp", TESTAPP_CLASSES, |
| 146 | + TESTAPP_MAIN |
| 147 | + }; |
| 148 | + var pb = new ProcessBuilder(javaCmd); |
| 149 | + ProcessTools.executeProcess(pb) |
| 150 | + .outputTo(System.out) |
| 151 | + .errorTo(System.err) |
| 152 | + .shouldHaveExitValue(0); |
125 | 153 | } |
126 | 154 |
|
127 | 155 | /** |
128 | | - * Test override of default FileSystemProvider with the main application |
129 | | - * on the module path as a modular JAR. |
| 156 | + * Test file system provider on class path, application in exploded module on module path. |
130 | 157 | */ |
131 | | - public void testModularJar() throws Exception { |
132 | | - String jarFile = createModularJar(); |
133 | | - int exitValue = exec(SET_DEFAULT_FSP, "-p", jarFile, "-m", "m/p.Main"); |
134 | | - assertEquals(exitValue, 0); |
| 158 | + @Test |
| 159 | + void testAppOnModulePath1() throws Exception { |
| 160 | + exec(SET_DEFAULT_FSP, |
| 161 | + "-p", TESTAPP_CLASSES, |
| 162 | + "-cp", TESTFSP_CLASSES, |
| 163 | + "-m", TESTAPP + "/" + TESTAPP_MAIN); |
135 | 164 | } |
136 | 165 |
|
137 | 166 | /** |
138 | | - * Test override of default FileSystemProvider where the main application |
139 | | - * is a module that is patched by an exploded patch. |
| 167 | + * Test file system provider on class path, application in modular JAR on module path. |
140 | 168 | */ |
141 | | - public void testExplodedModuleWithExplodedPatch() throws Exception { |
| 169 | + @Test |
| 170 | + void testAppOnModulePath2() throws Exception { |
| 171 | + String jarFile = createJar("testapp.jar", TESTAPP_CLASSES); |
| 172 | + exec(SET_DEFAULT_FSP, |
| 173 | + "-cp", TESTFSP_CLASSES, |
| 174 | + "-p", jarFile, |
| 175 | + "-m", TESTAPP + "/" + TESTAPP_MAIN); |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Test file system provider on class path, application in modular JAR on module path |
| 180 | + * that is patched with exploded patch. |
| 181 | + */ |
| 182 | + @Test |
| 183 | + void testPatchedAppOnModulePath1() throws Exception { |
142 | 184 | Path patchdir = createTempDirectory("patch"); |
143 | | - String modulePath = System.getProperty("jdk.module.path"); |
144 | | - int exitValue = exec(SET_DEFAULT_FSP, |
145 | | - "--patch-module", "m=" + patchdir, |
146 | | - "-p", modulePath, |
147 | | - "-m", "m/p.Main"); |
148 | | - assertEquals(exitValue, 0); |
| 185 | + Files.createFile(patchdir.resolve("aoo.properties")); |
| 186 | + exec(SET_DEFAULT_FSP, |
| 187 | + "--patch-module", TESTAPP + "=" + patchdir, |
| 188 | + "-p", TESTAPP_CLASSES, |
| 189 | + "-cp", TESTFSP_CLASSES, |
| 190 | + "-m", TESTAPP + "/" + TESTAPP_MAIN); |
149 | 191 | } |
150 | 192 |
|
151 | 193 | /** |
152 | | - * Test override of default FileSystemProvider where the main application |
153 | | - * is a module that is patched by an exploded patch. |
| 194 | + * Test file system provider on class path, application in modular JAR on module path |
| 195 | + * that is patched with patch in JAR file. |
154 | 196 | */ |
155 | | - public void testExplodedModuleWithJarPatch() throws Exception { |
| 197 | + @Test |
| 198 | + void testPatchedAppOnModulePath2() throws Exception { |
156 | 199 | Path patchdir = createTempDirectory("patch"); |
157 | | - Files.createDirectory(patchdir.resolve("m.properties")); |
158 | | - Path patch = createJarFile(patchdir); |
159 | | - String modulePath = System.getProperty("jdk.module.path"); |
160 | | - int exitValue = exec(SET_DEFAULT_FSP, |
161 | | - "--patch-module", "m=" + patch, |
162 | | - "-p", modulePath, |
163 | | - "-m", "m/p.Main"); |
164 | | - assertEquals(exitValue, 0); |
| 200 | + Files.createFile(patchdir.resolve("app.properties")); |
| 201 | + String jarFile = createJar("patch.jar", patchdir.toString()); |
| 202 | + exec(SET_DEFAULT_FSP, |
| 203 | + "--patch-module", TESTAPP + "=" + jarFile, |
| 204 | + "-p", TESTAPP_CLASSES, |
| 205 | + "-cp", TESTFSP_CLASSES, |
| 206 | + "-m", TESTAPP + "/" + TESTAPP_MAIN); |
165 | 207 | } |
166 | 208 |
|
167 | 209 | /** |
168 | | - * Returns the directory containing the classes for module "m". |
| 210 | + * Returns the directory containing the classes for the given module. |
169 | 211 | */ |
170 | | - private String moduleClasses() { |
| 212 | + private static String classes(String mn) { |
171 | 213 | String mp = System.getProperty("jdk.module.path"); |
172 | | - for (String dir : mp.split(File.pathSeparator)) { |
173 | | - Path m = Paths.get(dir, "m"); |
174 | | - if (Files.exists(m)) return m.toString(); |
175 | | - } |
176 | | - fail(); |
177 | | - return null; |
| 214 | + return Arrays.stream(mp.split(File.pathSeparator)) |
| 215 | + .map(e -> Path.of(e, mn)) |
| 216 | + .filter(Files::isDirectory) |
| 217 | + .findAny() |
| 218 | + .map(Path::toString) |
| 219 | + .orElseThrow(); |
178 | 220 | } |
179 | 221 |
|
180 | 222 | /** |
181 | | - * Creates a modular JAR containing module "m". |
| 223 | + * Returns a class path from the given paths. |
182 | 224 | */ |
183 | | - private String createModularJar() throws Exception { |
184 | | - Path dir = Paths.get(moduleClasses()); |
185 | | - Path jar = createJarFile(dir); |
186 | | - return jar.toString(); |
| 225 | + private String ofClasspath(String... paths) { |
| 226 | + return String.join(File.pathSeparator, paths); |
187 | 227 | } |
188 | 228 |
|
189 | 229 | /** |
190 | | - * Creates a JAR file containing the entries in the given file tree. |
| 230 | + * Creates a JAR file from the contains of the given directory. |
191 | 231 | */ |
192 | | - private Path createJarFile(Path dir) throws Exception { |
193 | | - Path jar = createTempDirectory("tmp").resolve("m.jar"); |
194 | | - String[] args = { "--create", "--file=" + jar, "-C", dir.toString(), "." }; |
195 | | - int ret = JAR_TOOL.run(System.out, System.out, args); |
| 232 | + private String createJar(String jar, String dir) throws IOException { |
| 233 | + List<String> args = new ArrayList<>(); |
| 234 | + args.add("--create"); |
| 235 | + args.add("--file=" + jar); |
| 236 | + args.add("-C"); |
| 237 | + args.add(dir); |
| 238 | + args.add("."); |
| 239 | + int ret = JAR_TOOL.run(System.err, System.err, args.toArray(new String[0])); |
196 | 240 | assertEquals(ret, 0); |
197 | 241 | return jar; |
198 | 242 | } |
199 | 243 |
|
200 | 244 | /** |
201 | | - * Invokes the java launcher with the given arguments, returning the exit code. |
| 245 | + * Create a temporary directory with the given prefix in the current directory. |
202 | 246 | */ |
203 | | - private int exec(String... args) throws Exception { |
204 | | - return ProcessTools.executeTestJava(args) |
205 | | - .outputTo(System.out) |
206 | | - .errorTo(System.out) |
207 | | - .getExitValue(); |
| 247 | + private static Path createTempDirectory(String prefix) throws IOException { |
| 248 | + return Files.createTempDirectory(Path.of("."), prefix); |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + * Invokes the java launcher with the given arguments, throws if the non-0 is returned. |
| 253 | + */ |
| 254 | + private void exec(String... args) throws Exception { |
| 255 | + ProcessTools.executeTestJava(args) |
| 256 | + .outputTo(System.err) |
| 257 | + .errorTo(System.err) |
| 258 | + .shouldHaveExitValue(0); |
208 | 259 | } |
209 | 260 | } |
0 commit comments