8141609: Need test for jrtfs that runs on JDK 8 to target a JDK 9 image
authoralanb
Wed, 30 Mar 2016 19:56:34 +0100
changeset 36741 aaf41d8104ce
parent 36740 f80cd68bc7f4
child 36742 286b583dc969
8141609: Need test for jrtfs that runs on JDK 8 to target a JDK 9 image Reviewed-by: alanb, sundar Contributed-by: felix.yang@oracle.com
jdk/test/Makefile
jdk/test/jdk/internal/jrtfs/remote/Main.java
jdk/test/jdk/internal/jrtfs/remote/RemoteRuntimeImageTest.java
--- a/jdk/test/Makefile	Wed Mar 30 12:47:54 2016 -0400
+++ b/jdk/test/Makefile	Wed Mar 30 19:56:34 2016 +0100
@@ -302,6 +302,8 @@
 # Set the max memory for jtreg control vm
 JTREG_MEMORY_OPTION = -J-Xmx512m
 JTREG_BASIC_OPTIONS += $(JTREG_MEMORY_OPTION)
+# Give tests access to JT_JAVA, see JDK-8141609
+JTREG_BASIC_OPTIONS += -e:JDK8_HOME=${JT_JAVA}
 # Add any extra options
 JTREG_BASIC_OPTIONS += $(EXTRA_JTREG_OPTIONS)
 # Set other vm and test options
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/jdk/internal/jrtfs/remote/Main.java	Wed Mar 30 19:56:34 2016 +0100
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.nio.file.FileSystem;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.stream.Stream;
+
+/**
+ * Basic jrt file system functionality testing
+ *
+ */
+public class Main {
+
+    public static void main(String[] args) throws Exception {
+        String javaHome = args[0];
+        FileSystem fs = null;
+        boolean isInstalled = false;
+        if (args.length == 2) {
+            fs = createFsByInstalledProvider();
+            isInstalled = true;
+        } else {
+            fs = createFsWithURLClassloader(javaHome);
+        }
+
+        Path mods = fs.getPath("/modules");
+        try (Stream<Path> stream = Files.walk(mods)) {
+            stream.forEach(path -> {
+                path.getFileName();
+            });
+        } finally {
+            try {
+                fs.close();
+            } catch (UnsupportedOperationException e) {
+                if (!isInstalled) {
+                    throw new RuntimeException(
+                        "UnsupportedOperationException is thrown unexpectedly");
+                }
+            }
+        }
+    }
+
+    private static FileSystem createFsWithURLClassloader(String javaHome) throws IOException{
+        URL url = Paths.get(javaHome, "jrt-fs.jar").toUri().toURL();
+        URLClassLoader loader = new URLClassLoader(new URL[] { url });
+        return FileSystems.newFileSystem(URI.create("jrt:/"), null, loader);
+    }
+
+    private static FileSystem createFsByInstalledProvider() throws IOException {
+        return FileSystems.getFileSystem(URI.create("jrt:/"));
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/jdk/internal/jrtfs/remote/RemoteRuntimeImageTest.java	Wed Mar 30 19:56:34 2016 +0100
@@ -0,0 +1,124 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8141609
+ * @summary Verify JDK 8 can use jrt-fs.jar to work with jrt file system.
+ * @run main RemoteRuntimeImageTest
+ */
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Properties;
+public class RemoteRuntimeImageTest {
+    //the jrt-fs.jar shipped together with jdk
+    private static final String JRTFS_JAR = "jrt-fs.jar";
+    private static final String SRC_DIR = System.getProperty("test.src");
+    private static final String CLASSES_DIR = "classes";
+    private static final String TEST_JAVAHOME = System.getProperty("test.jdk");
+
+    public static void main(String[] args) throws Exception {
+        // By default, set to ${JT_JAVA}
+        String jdk8Home = System.getenv("JDK8_HOME");
+        if (jdk8Home == null || jdk8Home.isEmpty()) {
+            System.err.println("Failed to locate JDK8 with system "
+                + "environment variable 'JDK8_HOME'. Skip testing!");
+            return;
+        }
+
+        Path jdk8Path = getJdk8Path(jdk8Home);
+        if (!isJdk8(jdk8Path)) {
+            System.err.println("This test is only for JDK 8. Skip testing");
+            return;
+        }
+
+        String java = jdk8Path.resolve("bin/java").toAbsolutePath().toString();
+        String javac = jdk8Path.resolve("bin/javac").toAbsolutePath().toString();
+
+        Files.createDirectories(Paths.get(".", CLASSES_DIR));
+        String jrtJar = Paths.get(TEST_JAVAHOME, JRTFS_JAR).toAbsolutePath().toString();
+
+        // Compose command-lines for compiling and executing tests
+        List<List<String>> cmds = Arrays.asList(
+                // Commands to compile test classes
+                Arrays.asList(javac, "-d", CLASSES_DIR, "-cp", jrtJar,
+                        SRC_DIR + File.separatorChar + "Main.java"),
+                // Run test
+                Arrays.asList(java, "-cp", CLASSES_DIR, "Main", TEST_JAVAHOME),
+                // Run test with jrtfs.jar in class path,
+                // which means to install jrt FileSystem provider
+                Arrays.asList(java, "-cp", CLASSES_DIR + File.pathSeparatorChar + jrtJar,
+                        "Main", TEST_JAVAHOME, "installed")
+                );
+
+        cmds.forEach(cmd -> execCmd(cmd));
+    }
+
+    private static void execCmd(List<String> command){
+        System.out.println();
+        System.out.println("Executing command: " + command);
+        Process p = null;
+        try {
+            p = new ProcessBuilder(command).inheritIO().start();
+            p.waitFor();
+            int rc = p.exitValue();
+            if (rc != 0) {
+                throw new RuntimeException("Unexpected exit code:" + rc);
+            }
+        } catch (IOException | InterruptedException e) {
+            throw new RuntimeException(e);
+        } finally {
+            if (p != null && p.isAlive()){
+                p.destroy();
+            }
+        }
+    }
+
+    private static Path getJdk8Path(String jdk8Home) {
+        Path jdk8Path = Paths.get(jdk8Home);
+        // It is possible to point to the path of java executable by ${JT_JAVA}
+        return Files.isDirectory(jdk8Path)? jdk8Path : jdk8Path.getParent().getParent();
+    }
+
+    private static boolean isJdk8(Path jdk8Home) throws FileNotFoundException, IOException {
+        File file = jdk8Home.resolve("release").toFile();
+        Properties props = new Properties();
+        try (FileInputStream in = new FileInputStream(file)) {
+            props.load(in);
+        }
+
+        String version = props.getProperty("JAVA_VERSION", "");
+        System.out.println("JAVA_VERSION is " + version);
+        return version.startsWith("\"1.8");
+    }
+}
+