8181413: Refactor test/sun/net/www/protocol/jar/jarbug/run.sh to plain java tests
authorxiaofeya
Mon, 05 Jun 2017 18:02:51 -0700
changeset 45388 6406324c5b40
parent 45387 179605f4d367
child 45389 213e7518bab8
8181413: Refactor test/sun/net/www/protocol/jar/jarbug/run.sh to plain java tests Reviewed-by: psandoz
jdk/test/sun/net/www/protocol/jar/jarbug/TestDriver.java
jdk/test/sun/net/www/protocol/jar/jarbug/run.sh
jdk/test/sun/net/www/protocol/jar/jarbug/src/test/RunAllTests.java
jdk/test/sun/net/www/protocol/jar/jarbug/src/test/TestBug4361044.java
jdk/test/sun/net/www/protocol/jar/jarbug/src/test/TestBug4523159.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/sun/net/www/protocol/jar/jarbug/TestDriver.java	Mon Jun 05 18:02:51 2017 -0700
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2017, 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 4361044 4388202 4418643 4523159 4730642
+ * @library /test/lib
+ *          /lib/testlibrary
+ * @modules jdk.compiler
+ * @build src.test.src.TestDriver CompilerUtils JarUtils
+ *        jdk.test.lib.JDKToolFinder
+ *        jdk.test.lib.process.*
+ * @summary various resource and classloading bugs related to jar files
+ * @run main/othervm TestDriver
+ */
+
+import jdk.test.lib.JDKToolFinder;
+import jdk.test.lib.process.ProcessTools;
+
+import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
+
+public class TestDriver {
+    public static void main(String[] args) throws Throwable {
+        Path srcDir = Paths.get(System.getProperty("test.src"));
+        Path targetDir = Paths.get(System.getProperty("user.dir"));
+        Path jar1SrcDir = srcDir.resolve("src").resolve("jar1");
+        Path jar1TargetDir =  targetDir.resolve("jar1");
+        Path ectJar1Dir = srcDir.resolve("etc").resolve("jar1");
+        Path jarFile = targetDir.resolve("jar1.jar");
+        Path[]  files= new Path[] {
+                Paths.get("res1.txt"), Paths.get("jar1", "bundle.properties")
+        };
+
+        // Copy files to target directory and change permission
+        for (Path file : files) {
+            Path dest = jar1TargetDir.resolve(file);
+            Files.createDirectories(dest.getParent());
+            Files.copy(ectJar1Dir.resolve(file), dest, REPLACE_EXISTING);
+        }
+
+        // Compile and build jar1.jar
+        ProcessTools.executeCommand("chmod", "-R", "u+w", "./jar1")
+                    .outputTo(System.out)
+                    .errorTo(System.out)
+                    .shouldHaveExitValue(0);
+        CompilerUtils.compile(jar1SrcDir, jar1TargetDir);
+        JarUtils.createJarFile(jarFile, jar1TargetDir);
+
+        // Compile test files
+        CompilerUtils.compile(srcDir.resolve("src").resolve("test"), targetDir);
+
+        // Run tests
+        String java = JDKToolFinder.getTestJDKTool("java");
+        String cp = targetDir.toString() + File.pathSeparator + jarFile;
+        String[] tests = new String[]{"TestBug4361044", "TestBug4523159"};
+        for (String test : tests) {
+            ProcessTools.executeCommand(java, "-cp", cp, test)
+                        .outputTo(System.out)
+                        .errorTo(System.out)
+                        .shouldHaveExitValue(0);
+        }
+    }
+}
--- a/jdk/test/sun/net/www/protocol/jar/jarbug/run.sh	Mon Jun 05 14:07:07 2017 +0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,84 +0,0 @@
-#! /bin/sh
-
-#
-# Copyright (c) 2005, 2012, 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 4361044 4388202 4418643 4523159 4730642
-# @summary various resource and classloading bugs related to jar files
-#set -x
-DEST=`pwd`
-
-OS=`uname -s`
-case "$OS" in
-  SunOS | Linux | Darwin | AIX )
-    PS=":"
-    FS="/"
-    CHMOD="${FS}bin${FS}chmod"
-    ;;
-  Windows* )
-    PS=";"
-    FS="\\"
-    CHMOD="chmod"
-    ;;
-  CYGWIN* )
-    PS=";"
-    FS="/"
-    CHMOD="chmod"
-    #
-    # javac does not like /cygdrive produced by `pwd`.
-    #
-    DEST=`cygpath -d ${DEST}`
-    ;;
-  * )
-    echo "Unrecognized system!"
-    exit 1;
-    ;;
-esac
-
-#
-# build jar1
-#
-mkdir -p ${DEST}${FS}jar1
-cd ${TESTSRC}${FS}etc${FS}jar1
-cp -r . ${DEST}${FS}jar1
-${CHMOD} -R u+w ${DEST}${FS}jar1
-${COMPILEJAVA}${FS}bin${FS}javac ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} -d ${DEST}${FS}jar1 \
-    ${TESTSRC}${FS}src${FS}jar1${FS}LoadResourceBundle.java
-${COMPILEJAVA}${FS}bin${FS}javac ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} -d ${DEST}${FS}jar1 \
-    ${TESTSRC}${FS}src${FS}jar1${FS}GetResource.java
-cd ${DEST}${FS}jar1
-${COMPILEJAVA}${FS}bin${FS}jar ${TESTTOOLVMOPTS} cfM jar1.jar jar1 res1.txt
-mv jar1.jar ..
-#
-# build the test sources and run them
-#
-${COMPILEJAVA}${FS}bin${FS}javac ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} -d ${DEST} ${TESTSRC}${FS}src${FS}test${FS}*.java
-cd ${DEST}
-${TESTJAVA}${FS}bin${FS}java ${TESTVMOPTS} RunAllTests
-result=$?
-if [ "$result" -ne "0" ]; then
-    exit 1
-fi
-rm -rf *
-exit 0
--- a/jdk/test/sun/net/www/protocol/jar/jarbug/src/test/RunAllTests.java	Mon Jun 05 14:07:07 2017 +0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,30 +0,0 @@
-/*
- * Copyright (c) 2005, 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.
- */
-
-public class RunAllTests {
-        public static void main(String[] args) throws Exception {
-                String[] noArgs = new String[0];
-                TestBug4361044.main(noArgs);
-                TestBug4523159.main(noArgs);
-        }
-}
--- a/jdk/test/sun/net/www/protocol/jar/jarbug/src/test/TestBug4361044.java	Mon Jun 05 14:07:07 2017 +0800
+++ b/jdk/test/sun/net/www/protocol/jar/jarbug/src/test/TestBug4361044.java	Mon Jun 05 18:02:51 2017 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2007, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2017, 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
@@ -21,9 +21,9 @@
  * questions.
  */
 
-import java.io.*;
-import java.net.*;
-import java.util.jar.*;
+import java.io.File;
+import java.net.URL;
+import java.net.URLClassLoader;
 
 /*
  * ResourceBundle from jar not found if jar exists in path
@@ -38,40 +38,17 @@
 public class TestBug4361044 extends JarTest
 {
     public void run(String[] args) throws Exception {
-        if (args.length == 0 ) {  // execute the test in another vm.
-            System.out.println("Test: " + getClass().getName());
-            Process process = Runtime.getRuntime().exec(javaCmd + " TestBug4361044 -test");
-
-            BufferedReader isReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
-            BufferedReader esReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
-            Redirector outRedirector = new Redirector(isReader, System.out);
-            Redirector errRedirector = new Redirector(esReader, System.err);
-
-            (new Thread(outRedirector)).start();
-            (new Thread(errRedirector)).start();
-
-            process.waitFor();
-
-            // Delete any remaining files from the test
-            File testDir = new File(tmpdir + File.separator + getClass().getName());
-            deleteRecursively(testDir);
-
-            if (outRedirector.getHasReadData() || errRedirector.getHasReadData())
-                throw new RuntimeException("Failed: No output should have been received from the process");
-
-        } else {   // run the test.
-            File tmp = createTempDir();
-            try {
-                File dir = new File(tmp, "dir!name");
-                dir.mkdir();
-                File testFile = copyResource(dir, "jar1.jar");
-                URL[] urls = new URL[1];
-                urls[0] = new URL("jar:" + testFile.toURL() + "!/");
-                URLClassLoader loader = new URLClassLoader(urls);
-                loader.loadClass("jar1.LoadResourceBundle").newInstance();
-            } finally {
-                deleteRecursively(tmp);
-            }
+        File tmp = createTempDir();
+        try {
+            File dir = new File(tmp, "dir!name");
+            dir.mkdir();
+            File testFile = copyResource(dir, "jar1.jar");
+            URL[] urls = new URL[1];
+            urls[0] = new URL("jar:" + testFile.toURI().toURL() + "!/");
+            URLClassLoader loader = new URLClassLoader(urls);
+            loader.loadClass("jar1.LoadResourceBundle").newInstance();
+        } finally {
+            deleteRecursively(tmp);
         }
     }
 
--- a/jdk/test/sun/net/www/protocol/jar/jarbug/src/test/TestBug4523159.java	Mon Jun 05 14:07:07 2017 +0800
+++ b/jdk/test/sun/net/www/protocol/jar/jarbug/src/test/TestBug4523159.java	Mon Jun 05 18:02:51 2017 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2007, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2017, 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
@@ -21,9 +21,12 @@
  * questions.
  */
 
-import java.io.*;
-import java.net.*;
-import java.util.jar.*;
+import java.io.File;
+import java.net.JarURLConnection;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
 
 /*
  * Issuing a getResourceAsStream() call will throw an exception if:
@@ -40,53 +43,30 @@
 public class TestBug4523159 extends JarTest
 {
     public void run(String[] args) throws Exception {
-        if (args.length == 0 ) {  // execute the test in another vm.
-            System.out.println("Test: " + getClass().getName());
-            Process process = Runtime.getRuntime().exec(javaCmd + " TestBug4523159 -test");
-
-            BufferedReader isReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
-            BufferedReader esReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
-            Redirector outRedirector = new Redirector(isReader, System.out);
-            Redirector errRedirector = new Redirector(esReader, System.err);
-
-            (new Thread(outRedirector)).start();
-            (new Thread(errRedirector)).start();
-
-            process.waitFor();
-
-            // Delete any remaining files from the test
-            File testDir = new File(tmpdir + File.separator + getClass().getName());
-            deleteRecursively(testDir);
-
-            if (outRedirector.getHasReadData() || errRedirector.getHasReadData())
-                throw new RuntimeException("Failed: No output should have been received from the process");
+        File tmp = createTempDir();
+        try {
+            File dir = new File(tmp, "dir!name");
+            dir.mkdir();
+            File testFile = copyResource(dir, "jar1.jar");
 
-        } else { // run the test.
-            File tmp = createTempDir();
-            try {
-                File dir = new File(tmp, "dir!name");
-                dir.mkdir();
-                File testFile = copyResource(dir, "jar1.jar");
+            // Case 1: direct access
+            URL url = new URL("jar:" + testFile.toURI().toURL() + "!/res1.txt");
+            JarURLConnection conn = (JarURLConnection) url.openConnection();
+            JarFile file = conn.getJarFile();
+            JarEntry entry = conn.getJarEntry();
+            byte[] buffer = readFully(file.getInputStream(entry));
+            String str = new String(buffer);
+            if (!str.equals("This is jar 1\n")) {
+                throw (new Exception("resource content invalid"));
+            }
 
-                // Case 1: direct access
-                URL url = new URL("jar:" + testFile.toURL() + "!/res1.txt");
-                JarURLConnection conn = (JarURLConnection) url.openConnection();
-                JarFile file = conn.getJarFile();
-                JarEntry entry = conn.getJarEntry();
-                byte[] buffer = readFully(file.getInputStream(entry));
-                String str = new String(buffer);
-                if (!str.equals("This is jar 1\n")) {
-                    throw(new Exception("resource content invalid"));
-                }
-
-                // Case 2: indirect access
-                URL[] urls = new URL[1];
-                urls[0] = new URL("jar:" + testFile.toURL() + "!/");
-                URLClassLoader loader = new URLClassLoader(urls);
-                loader.loadClass("jar1.GetResource").newInstance();
-            } finally {
-                deleteRecursively(tmp);
-            }
+            // Case 2: indirect access
+            URL[] urls = new URL[1];
+            urls[0] = new URL("jar:" + testFile.toURI().toURL()  + "!/");
+            URLClassLoader loader = new URLClassLoader(urls);
+            loader.loadClass("jar1.GetResource").newInstance();
+        } finally {
+            deleteRecursively(tmp);
         }
     }