Path: blob/master/test/jdk/java/nio/file/spi/SetDefaultProvider.java
66645 views
/*1* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 826634526* @modules jdk.jartool27* @library /test/lib28* @build SetDefaultProvider TestProvider m/* jdk.test.lib.process.ProcessTools29* @run testng/othervm SetDefaultProvider30* @summary Runs tests with -Djava.nio.file.spi.DefaultFileSystemProvider set on31* the command line to override the default file system provider32*/3334import java.io.File;35import java.io.IOException;36import java.nio.file.Files;37import java.nio.file.Path;38import java.nio.file.Paths;39import java.util.spi.ToolProvider;4041import jdk.test.lib.process.ProcessTools;4243import org.testng.annotations.BeforeTest;44import org.testng.annotations.Test;45import static org.testng.Assert.*;4647@Test48public class SetDefaultProvider {4950private static String SET_DEFAULT_FSP =51"-Djava.nio.file.spi.DefaultFileSystemProvider=TestProvider";5253private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")54.orElseThrow(() ->55new RuntimeException("jar tool not found")56);5758private static Path createTempDirectory(String prefix) throws IOException {59Path testDir = Paths.get(System.getProperty("test.dir", "."));60return Files.createTempDirectory(testDir, prefix);61}6263/**64* Test override of default FileSystemProvider with the main application65* on the class path.66*/67public void testClassPath() throws Exception {68String moduleClasses = moduleClasses();69String testClasses = System.getProperty("test.classes");70String classpath = moduleClasses + File.pathSeparator + testClasses;71int exitValue = exec(SET_DEFAULT_FSP, "-cp", classpath, "p.Main");72assertTrue(exitValue == 0);73}7475/**76* Test override of default FileSystemProvider with the main application77* on the class path and a SecurityManager enabled.78*/79public void testClassPathWithSecurityManager() throws Exception {80String moduleClasses = moduleClasses();81String testClasses = System.getProperty("test.classes");82String classpath = moduleClasses + File.pathSeparator + testClasses;83String policyFile = System.getProperty("test.src", ".")84+ File.separator + "fs.policy";85int exitValue = exec(SET_DEFAULT_FSP, "-cp", classpath,86"-Dtest.classes=" + testClasses, "-Djava.security.manager",87"-Djava.security.policy==" + policyFile, "p.Main");88assertTrue(exitValue == 0);89}9091/**92* Test override of default FileSystemProvider with the main application93* on the module path as an exploded module.94*/95public void testExplodedModule() throws Exception {96String modulePath = System.getProperty("jdk.module.path");97int exitValue = exec(SET_DEFAULT_FSP, "-p", modulePath, "-m", "m/p.Main");98assertTrue(exitValue == 0);99}100101/**102* Test override of default FileSystemProvider with the main application103* on the module path as a modular JAR.104*/105public void testModularJar() throws Exception {106String jarFile = createModularJar();107int exitValue = exec(SET_DEFAULT_FSP, "-p", jarFile, "-m", "m/p.Main");108assertTrue(exitValue == 0);109}110111/**112* Test override of default FileSystemProvider where the main application113* is a module that is patched by an exploded patch.114*/115public void testExplodedModuleWithExplodedPatch() throws Exception {116Path patchdir = createTempDirectory("patch");117String modulePath = System.getProperty("jdk.module.path");118int exitValue = exec(SET_DEFAULT_FSP,119"--patch-module", "m=" + patchdir,120"-p", modulePath,121"-m", "m/p.Main");122assertTrue(exitValue == 0);123}124125/**126* Test override of default FileSystemProvider where the main application127* is a module that is patched by an exploded patch.128*/129public void testExplodedModuleWithJarPatch() throws Exception {130Path patchdir = createTempDirectory("patch");131Files.createDirectory(patchdir.resolve("m.properties"));132Path patch = createJarFile(patchdir);133String modulePath = System.getProperty("jdk.module.path");134int exitValue = exec(SET_DEFAULT_FSP,135"--patch-module", "m=" + patch,136"-p", modulePath,137"-m", "m/p.Main");138assertTrue(exitValue == 0);139}140141/**142* Returns the directory containing the classes for module "m".143*/144private String moduleClasses() {145String mp = System.getProperty("jdk.module.path");146for (String dir : mp.split(File.pathSeparator)) {147Path m = Paths.get(dir, "m");148if (Files.exists(m)) return m.toString();149}150assertFalse(true);151return null;152}153154/**155* Creates a modular JAR containing module "m".156*/157private String createModularJar() throws Exception {158Path dir = Paths.get(moduleClasses());159Path jar = createJarFile(dir);160return jar.toString();161}162163/**164* Creates a JAR file containing the entries in the given file tree.165*/166private Path createJarFile(Path dir) throws Exception {167Path jar = createTempDirectory("tmp").resolve("m.jar");168String[] args = { "--create", "--file=" + jar, "-C", dir.toString(), "." };169int ret = JAR_TOOL.run(System.out, System.out, args);170assertTrue(ret == 0);171return jar;172}173174/**175* Invokes the java launcher with the given arguments, returning the exit code.176*/177private int exec(String... args) throws Exception {178return ProcessTools.executeTestJava(args)179.outputTo(System.out)180.errorTo(System.out)181.getExitValue();182}183}184185186