Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/java/nio/file/spi/SetDefaultProvider.java
66645 views
1
/*
2
* Copyright (c) 2008, 2021, 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
* @bug 8266345
27
* @modules jdk.jartool
28
* @library /test/lib
29
* @build SetDefaultProvider TestProvider m/* jdk.test.lib.process.ProcessTools
30
* @run testng/othervm SetDefaultProvider
31
* @summary Runs tests with -Djava.nio.file.spi.DefaultFileSystemProvider set on
32
* the command line to override the default file system provider
33
*/
34
35
import java.io.File;
36
import java.io.IOException;
37
import java.nio.file.Files;
38
import java.nio.file.Path;
39
import java.nio.file.Paths;
40
import java.util.spi.ToolProvider;
41
42
import jdk.test.lib.process.ProcessTools;
43
44
import org.testng.annotations.BeforeTest;
45
import org.testng.annotations.Test;
46
import static org.testng.Assert.*;
47
48
@Test
49
public class SetDefaultProvider {
50
51
private static String SET_DEFAULT_FSP =
52
"-Djava.nio.file.spi.DefaultFileSystemProvider=TestProvider";
53
54
private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
55
.orElseThrow(() ->
56
new RuntimeException("jar tool not found")
57
);
58
59
private static Path createTempDirectory(String prefix) throws IOException {
60
Path testDir = Paths.get(System.getProperty("test.dir", "."));
61
return Files.createTempDirectory(testDir, prefix);
62
}
63
64
/**
65
* Test override of default FileSystemProvider with the main application
66
* on the class path.
67
*/
68
public void testClassPath() throws Exception {
69
String moduleClasses = moduleClasses();
70
String testClasses = System.getProperty("test.classes");
71
String classpath = moduleClasses + File.pathSeparator + testClasses;
72
int exitValue = exec(SET_DEFAULT_FSP, "-cp", classpath, "p.Main");
73
assertTrue(exitValue == 0);
74
}
75
76
/**
77
* Test override of default FileSystemProvider with the main application
78
* on the class path and a SecurityManager enabled.
79
*/
80
public void testClassPathWithSecurityManager() throws Exception {
81
String moduleClasses = moduleClasses();
82
String testClasses = System.getProperty("test.classes");
83
String classpath = moduleClasses + File.pathSeparator + testClasses;
84
String policyFile = System.getProperty("test.src", ".")
85
+ File.separator + "fs.policy";
86
int exitValue = exec(SET_DEFAULT_FSP, "-cp", classpath,
87
"-Dtest.classes=" + testClasses, "-Djava.security.manager",
88
"-Djava.security.policy==" + policyFile, "p.Main");
89
assertTrue(exitValue == 0);
90
}
91
92
/**
93
* Test override of default FileSystemProvider with the main application
94
* on the module path as an exploded module.
95
*/
96
public void testExplodedModule() throws Exception {
97
String modulePath = System.getProperty("jdk.module.path");
98
int exitValue = exec(SET_DEFAULT_FSP, "-p", modulePath, "-m", "m/p.Main");
99
assertTrue(exitValue == 0);
100
}
101
102
/**
103
* Test override of default FileSystemProvider with the main application
104
* on the module path as a modular JAR.
105
*/
106
public void testModularJar() throws Exception {
107
String jarFile = createModularJar();
108
int exitValue = exec(SET_DEFAULT_FSP, "-p", jarFile, "-m", "m/p.Main");
109
assertTrue(exitValue == 0);
110
}
111
112
/**
113
* Test override of default FileSystemProvider where the main application
114
* is a module that is patched by an exploded patch.
115
*/
116
public void testExplodedModuleWithExplodedPatch() throws Exception {
117
Path patchdir = createTempDirectory("patch");
118
String modulePath = System.getProperty("jdk.module.path");
119
int exitValue = exec(SET_DEFAULT_FSP,
120
"--patch-module", "m=" + patchdir,
121
"-p", modulePath,
122
"-m", "m/p.Main");
123
assertTrue(exitValue == 0);
124
}
125
126
/**
127
* Test override of default FileSystemProvider where the main application
128
* is a module that is patched by an exploded patch.
129
*/
130
public void testExplodedModuleWithJarPatch() throws Exception {
131
Path patchdir = createTempDirectory("patch");
132
Files.createDirectory(patchdir.resolve("m.properties"));
133
Path patch = createJarFile(patchdir);
134
String modulePath = System.getProperty("jdk.module.path");
135
int exitValue = exec(SET_DEFAULT_FSP,
136
"--patch-module", "m=" + patch,
137
"-p", modulePath,
138
"-m", "m/p.Main");
139
assertTrue(exitValue == 0);
140
}
141
142
/**
143
* Returns the directory containing the classes for module "m".
144
*/
145
private String moduleClasses() {
146
String mp = System.getProperty("jdk.module.path");
147
for (String dir : mp.split(File.pathSeparator)) {
148
Path m = Paths.get(dir, "m");
149
if (Files.exists(m)) return m.toString();
150
}
151
assertFalse(true);
152
return null;
153
}
154
155
/**
156
* Creates a modular JAR containing module "m".
157
*/
158
private String createModularJar() throws Exception {
159
Path dir = Paths.get(moduleClasses());
160
Path jar = createJarFile(dir);
161
return jar.toString();
162
}
163
164
/**
165
* Creates a JAR file containing the entries in the given file tree.
166
*/
167
private Path createJarFile(Path dir) throws Exception {
168
Path jar = createTempDirectory("tmp").resolve("m.jar");
169
String[] args = { "--create", "--file=" + jar, "-C", dir.toString(), "." };
170
int ret = JAR_TOOL.run(System.out, System.out, args);
171
assertTrue(ret == 0);
172
return jar;
173
}
174
175
/**
176
* Invokes the java launcher with the given arguments, returning the exit code.
177
*/
178
private int exec(String... args) throws Exception {
179
return ProcessTools.executeTestJava(args)
180
.outputTo(System.out)
181
.errorTo(System.out)
182
.getExitValue();
183
}
184
}
185
186