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