Path: blob/master/test/jdk/java/nio/file/spi/SetDefaultProvider.java
41153 views
/*1* Copyright (c) 2008, 2018, 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* @modules jdk.jartool26* @library /test/lib27* @build SetDefaultProvider TestProvider m/* jdk.test.lib.process.ProcessTools28* @run testng/othervm SetDefaultProvider29* @summary Runs tests with -Djava.nio.file.spi.DefaultFileSystemProvider set on30* the command line to override the default file system provider31*/3233import java.io.File;34import java.io.IOException;35import java.nio.file.Files;36import java.nio.file.Path;37import java.nio.file.Paths;38import java.util.spi.ToolProvider;3940import jdk.test.lib.process.ProcessTools;4142import org.testng.annotations.BeforeTest;43import org.testng.annotations.Test;44import static org.testng.Assert.*;4546@Test47public class SetDefaultProvider {4849private static String SET_DEFAULT_FSP =50"-Djava.nio.file.spi.DefaultFileSystemProvider=TestProvider";5152private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")53.orElseThrow(() ->54new RuntimeException("jar tool not found")55);5657private static Path createTempDirectory(String prefix) throws IOException {58Path testDir = Paths.get(System.getProperty("test.dir", "."));59return Files.createTempDirectory(testDir, prefix);60}6162/**63* Test override of default FileSystemProvider with the main application64* on the class path.65*/66public void testClassPath() throws Exception {67String moduleClasses = moduleClasses();68String testClasses = System.getProperty("test.classes");69String classpath = moduleClasses + File.pathSeparator + testClasses;70int exitValue = exec(SET_DEFAULT_FSP, "-cp", classpath, "p.Main");71assertTrue(exitValue == 0);72}7374/**75* Test override of default FileSystemProvider with the main application76* on the module path as an exploded module.77*/78public void testExplodedModule() throws Exception {79String modulePath = System.getProperty("jdk.module.path");80int exitValue = exec(SET_DEFAULT_FSP, "-p", modulePath, "-m", "m/p.Main");81assertTrue(exitValue == 0);82}8384/**85* Test override of default FileSystemProvider with the main application86* on the module path as a modular JAR.87*/88public void testModularJar() throws Exception {89String jarFile = createModularJar();90int exitValue = exec(SET_DEFAULT_FSP, "-p", jarFile, "-m", "m/p.Main");91assertTrue(exitValue == 0);92}9394/**95* Test override of default FileSystemProvider where the main application96* is a module that is patched by an exploded patch.97*/98public void testExplodedModuleWithExplodedPatch() throws Exception {99Path patchdir = createTempDirectory("patch");100String modulePath = System.getProperty("jdk.module.path");101int exitValue = exec(SET_DEFAULT_FSP,102"--patch-module", "m=" + patchdir,103"-p", modulePath,104"-m", "m/p.Main");105assertTrue(exitValue == 0);106}107108/**109* Test override of default FileSystemProvider where the main application110* is a module that is patched by an exploded patch.111*/112public void testExplodedModuleWithJarPatch() throws Exception {113Path patchdir = createTempDirectory("patch");114Files.createDirectory(patchdir.resolve("m.properties"));115Path patch = createJarFile(patchdir);116String modulePath = System.getProperty("jdk.module.path");117int exitValue = exec(SET_DEFAULT_FSP,118"--patch-module", "m=" + patch,119"-p", modulePath,120"-m", "m/p.Main");121assertTrue(exitValue == 0);122}123124/**125* Returns the directory containing the classes for module "m".126*/127private String moduleClasses() {128String mp = System.getProperty("jdk.module.path");129for (String dir : mp.split(File.pathSeparator)) {130Path m = Paths.get(dir, "m");131if (Files.exists(m)) return m.toString();132}133assertFalse(true);134return null;135}136137/**138* Creates a modular JAR containing module "m".139*/140private String createModularJar() throws Exception {141Path dir = Paths.get(moduleClasses());142Path jar = createJarFile(dir);143return jar.toString();144}145146/**147* Creates a JAR file containing the entries in the given file tree.148*/149private Path createJarFile(Path dir) throws Exception {150Path jar = createTempDirectory("tmp").resolve("m.jar");151String[] args = { "--create", "--file=" + jar, "-C", dir.toString(), "." };152int ret = JAR_TOOL.run(System.out, System.out, args);153assertTrue(ret == 0);154return jar;155}156157/**158* Invokes the java launcher with the given arguments, returning the exit code.159*/160private int exec(String... args) throws Exception {161return ProcessTools.executeTestJava(args)162.outputTo(System.out)163.errorTo(System.out)164.getExitValue();165}166}167168169