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