1 /*
   2  * Copyright (c) 2016, 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 8141609 8154403
  27  * @summary Verify JDK 8 can use jrt-fs.jar to work with jrt file system.
  28  * @run main RemoteRuntimeImageTest
  29  */
  30 
  31 import java.io.File;
  32 import java.io.FileInputStream;
  33 import java.io.FileNotFoundException;
  34 import java.io.IOException;
  35 import java.nio.file.FileSystems;
  36 import java.nio.file.Files;
  37 import java.nio.file.Path;
  38 import java.nio.file.Paths;
  39 import java.util.Arrays;
  40 import java.util.List;
  41 import java.util.Properties;
  42 public class RemoteRuntimeImageTest {
  43     //the jrt-fs.jar shipped together with jdk
  44     private static final String JRTFS_JAR = "jrt-fs.jar";
  45     private static final String SRC_DIR = System.getProperty("test.src");
  46     private static final String CLASSES_DIR = "classes";
  47     private static final String TEST_JAVAHOME = System.getProperty("test.jdk");
  48 
  49     public static void main(String[] args) throws Exception {
  50         // By default, set to ${JT_JAVA}
  51         String jdk8Home = System.getenv("JDK8_HOME");
  52         if (jdk8Home == null || jdk8Home.isEmpty()) {
  53             System.err.println("Failed to locate JDK8 with system "
  54                 + "environment variable 'JDK8_HOME'. Skip testing!");
  55             return;
  56         }
  57 
  58         Path jdk8Path = getJdk8Path(jdk8Home);
  59         if (!isJdk8(jdk8Path)) {
  60             System.err.println("This test is only for JDK 8. Skip testing");
  61             return;
  62         }
  63 
  64         String java = jdk8Path.resolve("bin/java").toAbsolutePath().toString();
  65         String javac = jdk8Path.resolve("bin/javac").toAbsolutePath().toString();
  66         Files.createDirectories(Paths.get(".", CLASSES_DIR));
  67         String jrtJar = Paths.get(TEST_JAVAHOME, JRTFS_JAR).toAbsolutePath().toString();
  68 
  69         // Compose command-lines for compiling and executing tests
  70         List<List<String>> cmds = Arrays.asList(
  71                 // Commands to compile test classes
  72                 Arrays.asList(javac, "-d", CLASSES_DIR, "-cp", jrtJar,
  73                         SRC_DIR + File.separatorChar + "Main.java"),
  74                 // Run test
  75                 Arrays.asList(java, "-cp", CLASSES_DIR, "Main", TEST_JAVAHOME),
  76                 // Run test with jrtfs.jar in class path,
  77                 // which means to install jrt FileSystem provider
  78                 Arrays.asList(java, "-cp", CLASSES_DIR + File.pathSeparatorChar + jrtJar,
  79                         "Main", TEST_JAVAHOME, "installed")
  80                 );
  81 
  82         cmds.forEach(cmd -> execCmd(cmd));
  83     }
  84 
  85     private static void execCmd(List<String> command){
  86         System.out.println();
  87         System.out.println("Executing command: " + command);
  88         Process p = null;
  89         try {
  90             p = new ProcessBuilder(command).inheritIO().start();
  91             p.waitFor();
  92             int rc = p.exitValue();
  93             if (rc != 0) {
  94                 throw new RuntimeException("Unexpected exit code:" + rc);
  95             }
  96         } catch (IOException | InterruptedException e) {
  97             throw new RuntimeException(e);
  98         } finally {
  99             if (p != null && p.isAlive()){
 100                 p.destroy();
 101             }
 102         }
 103     }
 104 
 105     private static Path getJdk8Path(String jdk8Home) {
 106         Path jdk8Path = Paths.get(jdk8Home);
 107         // It is possible to point to the path of java executable by ${JT_JAVA}
 108         return Files.isDirectory(jdk8Path)? jdk8Path : jdk8Path.getParent().getParent();
 109     }
 110 
 111     private static boolean isJdk8(Path jdk8Home) throws FileNotFoundException, IOException {
 112         File file = jdk8Home.resolve("release").toFile();
 113         Properties props = new Properties();
 114         try (FileInputStream in = new FileInputStream(file)) {
 115             props.load(in);
 116         }
 117 
 118         String version = props.getProperty("JAVA_VERSION", "");
 119         System.out.println("JAVA_VERSION is " + version);
 120         return version.startsWith("\"1.8");
 121     }
 122 }
 123