Skip to content

Commit 23d6f74

Browse files
author
Alan Bateman
committed
8346463: Add test coverage for deploying the default provider as a module
Reviewed-by: bpb
1 parent 484229e commit 23d6f74

File tree

7 files changed

+199
-121
lines changed

7 files changed

+199
-121
lines changed

test/jdk/java/net/UnixDomainSocketAddress/AddressTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,7 +24,7 @@
2424
/**
2525
* @test
2626
* @bug 8231358
27-
* @compile ../../nio/file/spi/TestProvider.java AddressTest.java
27+
* @compile ../../nio/file/spi/testfsp/testfsp/TestProvider.java AddressTest.java
2828
* @run testng/othervm AddressTest
2929
*/
3030

@@ -51,8 +51,8 @@ public class AddressTest {
5151

5252
@Test
5353
public static void runTest() throws Exception {
54-
TestProvider prov = new TestProvider(FileSystems.getDefault().provider());
55-
Path path = prov.getPath(URI.create("file:/"));
54+
var fsp = new testfsp.TestProvider(FileSystems.getDefault().provider());
55+
Path path = fsp.getPath(URI.create("file:/"));
5656
assertThrows(IAE, () -> UnixDomainSocketAddress.of(path));
5757
}
5858
}

test/jdk/java/nio/file/spi/SetDefaultProvider.java

+158-107
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
* questions.
2222
*/
2323

24-
/**
24+
/*
2525
* @test
2626
* @bug 4313887 7006126 8142968 8178380 8183320 8210112 8266345 8263940
27-
* @modules jdk.jartool
27+
* @modules jdk.jartool jdk.jlink
2828
* @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
3131
* @summary Runs tests with -Djava.nio.file.spi.DefaultFileSystemProvider set on
3232
* the command line to override the default file system provider
3333
*/
@@ -37,173 +37,224 @@
3737
import java.nio.file.Files;
3838
import java.nio.file.Path;
3939
import java.nio.file.Paths;
40+
import java.util.Arrays;
4041
import java.util.ArrayList;
4142
import java.util.List;
4243
import java.util.spi.ToolProvider;
4344
import java.util.stream.Stream;
4445

4546
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.*;
4651

47-
import org.testng.annotations.Test;
48-
import static org.testng.Assert.*;
49-
50-
@Test
51-
public class SetDefaultProvider {
52+
class SetDefaultProvider {
5253

5354
private static final String SET_DEFAULT_FSP =
54-
"-Djava.nio.file.spi.DefaultFileSystemProvider=TestProvider";
55+
"-Djava.nio.file.spi.DefaultFileSystemProvider=testfsp.TestProvider";
5556

5657
private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
5758
.orElseThrow(() ->
5859
new RuntimeException("jar tool not found")
5960
);
6061

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);
6476
}
6577

6678
/**
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.
6980
*/
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);
7686
}
7787

7888
/**
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.
8190
*/
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);
9197
}
9298

9399
/**
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.
96101
*/
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+
}
98110

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);
115122
}
116123

117124
/**
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.
120126
*/
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);
125153
}
126154

127155
/**
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.
130157
*/
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);
135164
}
136165

137166
/**
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.
140168
*/
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 {
142184
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);
149191
}
150192

151193
/**
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.
154196
*/
155-
public void testExplodedModuleWithJarPatch() throws Exception {
197+
@Test
198+
void testPatchedAppOnModulePath2() throws Exception {
156199
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);
165207
}
166208

167209
/**
168-
* Returns the directory containing the classes for module "m".
210+
* Returns the directory containing the classes for the given module.
169211
*/
170-
private String moduleClasses() {
212+
private static String classes(String mn) {
171213
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();
178220
}
179221

180222
/**
181-
* Creates a modular JAR containing module "m".
223+
* Returns a class path from the given paths.
182224
*/
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);
187227
}
188228

189229
/**
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.
191231
*/
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]));
196240
assertEquals(ret, 0);
197241
return jar;
198242
}
199243

200244
/**
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.
202246
*/
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);
208259
}
209260
}

0 commit comments

Comments
 (0)