changeset 45465:75b876952ec1

8181080: Refactor several sun/net shell tests to plain java tests Reviewed-by: chegar, alanb
author xiaofeya
date Sun, 11 Jun 2017 18:36:23 -0700
parents 7d02d9bb39d1
children faf1c55d2046
files jdk/test/sun/net/InetAddress/nameservice/dns/CNameTest.java jdk/test/sun/net/InetAddress/nameservice/dns/cname.sh jdk/test/sun/net/ftp/MarkResetTest.java jdk/test/sun/net/ftp/MarkResetTest.sh jdk/test/sun/net/www/protocol/file/DirPermissionDenied.java jdk/test/sun/net/www/protocol/file/DirPermissionDenied.sh jdk/test/sun/net/www/protocol/jrt/OtherResourcesTest.java jdk/test/sun/net/www/protocol/jrt/other_resources.sh
diffstat 8 files changed, 192 insertions(+), 241 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/sun/net/InetAddress/nameservice/dns/CNameTest.java	Sun Jun 11 18:36:23 2017 -0700
@@ -0,0 +1,73 @@
+/*
+ * 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.
+ */
+
+import jdk.test.lib.process.ProcessTools;
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
+
+/**
+ * @test
+ * @bug 4763315
+ * @modules java.naming
+ * @library /test/lib
+ * @build CanonicalName Lookup jdk.test.lib.process.*
+ * @run main/othervm/timeout=120 CNameTest
+ * @summary Test DNS provider's handling of CNAME records
+ */
+public class CNameTest {
+    private static final String HOST = "www-proxy.us.oracle.com";
+    private static final String POLICY = "grant {" + System.lineSeparator() +
+            " permission java.net .SocketPermission \"${HOST}\", \"resolve\";" +
+            System.lineSeparator() + "};";
+
+    public static void main(String[] args) throws Exception {
+        // Prerequisite check
+        int rc = ProcessTools.executeTestJava("CanonicalName", HOST)
+                             .outputTo(System.out)
+                             .errorTo(System.out)
+                             .getExitValue();
+        if (rc != 0) {
+            System.out.println("DNS not configured or host doesn't" +
+                    " resolve to CNAME record");
+            return;
+        }
+
+        // Tests - with & without security manager
+        Path policy = Paths.get(".", "java.policy");
+        Files.write(policy, POLICY.getBytes(), StandardOpenOption.CREATE_NEW);
+        String[] opts = new String[]{
+                "-Dsun.net.spi.nameservice.provider.1=dns,sun",
+                "-Djava.security.manager -Djava.security.policy=" + policy
+        };
+        for (String opt : opts) {
+            ProcessTools.executeTestJava(opt, "Lookup", HOST)
+                        .outputTo(System.out)
+                        .errorTo(System.err)
+                        .shouldHaveExitValue(0);
+        }
+    }
+}
+
--- a/jdk/test/sun/net/InetAddress/nameservice/dns/cname.sh	Fri Jun 09 21:34:36 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,81 +0,0 @@
-#!/bin/sh
-
-#
-# Copyright (c) 2002, 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 4763315
-# @modules java.naming
-# @build CanonicalName Lookup
-# @run shell/timeout=120 cname.sh
-# @summary Test DNS provider's handling of CNAME records
-
-
-# The host that we try to resolve
-
-HOST=www-proxy.us.oracle.com
-
-# fail gracefully if DNS is not configured or there
-# isn't a CNAME record.
-
-CLASSPATH=${TESTCLASSES}
-export CLASSPATH
-JAVA="${TESTJAVA}/bin/java"
-
-sh -xc "$JAVA ${TESTVMOPTS} CanonicalName $HOST" 2>&1
-if [ $? != 0 ]; then
-    echo "DNS not configured or host doesn't resolve to CNAME record"
-    exit 0
-fi
-
-failures=0
-
-go() {
-    echo ''
-    sh -xc "$JAVA ${TESTVMOPTS} $1 Lookup $2" 2>&1
-    if [ $? != 0 ]; then failures=`expr $failures + 1`; fi
-}
-
-# Tests - with & without security manager
-
-POLICY=java.policy
-echo "grant {" > ${POLICY}
-echo " permission java.net.SocketPermission \"${HOST}\", \"resolve\";" >> ${POLICY}
-echo "};" >> ${POLICY}
-
-np="-Dsun.net.spi.nameservice.provider.1=dns,sun"
-sm="-Djava.security.manager -Djava.security.policy=${POLICY}"
-
-go "" "$HOST"
-go "$sm" "$HOST"
-
-
-#
-# Results
-#
-echo ''
-if [ $failures -gt 0 ];
-  then echo "$failures test(s) failed";
-  else echo "All test(s) passed"; fi
-exit $failures
--- a/jdk/test/sun/net/ftp/MarkResetTest.java	Fri Jun 09 21:34:36 2017 +0000
+++ b/jdk/test/sun/net/ftp/MarkResetTest.java	Sun Jun 11 18:36:23 2017 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 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
@@ -22,15 +22,30 @@
  */
 
 /*
- *
- * run from MarkResetTest.sh
+ * @test
+ * @bug 4673103
+ * @run main/othervm/timeout=140 MarkResetTest
+ * @summary URLConnection.getContent() hangs over FTP for DOC, PPT, XLS files
  */
 
-import java.io.*;
-import java.net.*;
-import java.util.regex.*;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.URL;
+import java.net.URLConnection;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 
 public class MarkResetTest {
+    private static final String FILE_NAME = "EncDec.doc";
 
     /**
      * A class that simulates, on a separate, an FTP server.
@@ -388,7 +403,9 @@
     }
 
     public static void main(String[] args) throws Exception {
-        MarkResetTest test = new MarkResetTest();
+        Files.copy(Paths.get(System.getProperty("test.src"), FILE_NAME),
+                Paths.get(".", FILE_NAME));
+        new MarkResetTest();
     }
 
     public MarkResetTest() {
@@ -402,9 +419,8 @@
                 port = server.getPort();
             }
 
-            String filename = "EncDec.doc";
-            URL url = new URL("ftp://localhost:" + port + "/" +
-                                filename);
+
+            URL url = new URL("ftp://localhost:" + port + "/" + FILE_NAME);
 
             URLConnection con = url.openConnection();
             System.out.println("getContent: " + con.getContent());
@@ -438,7 +454,7 @@
             server.interrupt();
 
             // Did we pass ?
-            if (len != (new File(filename)).length()) {
+            if (len != (new File(FILE_NAME)).length()) {
                 throw new Exception("Failed to read the file correctly");
             }
             System.out.println("PASSED: File read correctly");
--- a/jdk/test/sun/net/ftp/MarkResetTest.sh	Fri Jun 09 21:34:36 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,55 +0,0 @@
-#
-# Copyright (c) 2002, 2013, 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 4673103
-# @run shell/timeout=140 MarkResetTest.sh
-# @summary URLConnection.getContent() hangs over FTP for DOC, PPT, XLS files
-
-OS=`uname -s`
-case "$OS" in
-  SunOS | Linux | Darwin | AIX )
-    PS=":"
-    FS="/"
-    ;;
-  CYGWIN* )
-    PS=";"
-    FS="/"
-    ;;
-  Windows* )
-    PS=";"
-    FS="\\"
-    ;;
-  * )
-    echo "Unrecognized system!"
-    exit 1;
-    ;;
-esac
-
-${COMPILEJAVA}${FS}bin${FS}javac ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} -d . ${TESTSRC}${FS}MarkResetTest.java
-
-# ftp server used by the test requires the file to be present
-# in this directory
-cp ${TESTSRC}${FS}EncDec.doc .
-
-${TESTJAVA}${FS}bin${FS}java ${TESTVMOPTS} MarkResetTest
--- a/jdk/test/sun/net/www/protocol/file/DirPermissionDenied.java	Fri Jun 09 21:34:36 2017 +0000
+++ b/jdk/test/sun/net/www/protocol/file/DirPermissionDenied.java	Sun Jun 11 18:36:23 2017 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 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,14 +21,36 @@
  * questions.
  */
 
+/**
+ * @test
+ * @bug 6977851
+ * @summary NPE from FileURLConnection.connect
+ * @library /test/lib
+ * @build DirPermissionDenied jdk.test.lib.process.*
+ *        jdk.test.lib.util.FileUtils
+ * @run testng DirPermissionDenied
+ */
+
+import java.io.IOException;
+import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLConnection;
-import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 
+import jdk.test.lib.process.ProcessTools;
+import jdk.test.lib.util.FileUtils;
+import org.testng.annotations.AfterTest;
+import org.testng.annotations.Test;
+import org.testng.annotations.BeforeTest;
 public class DirPermissionDenied {
-    public static void main(String[] args) throws Exception {
-        URL url = new URL("file:" + args[0]);
+    private static final Path TEST_DIR = Paths.get(
+            "DirPermissionDeniedDirectory");
 
+    @Test
+    public void doTest() throws MalformedURLException {
+        URL url = new URL(TEST_DIR.toUri().toString());
         try {
             URLConnection uc = url.openConnection();
             uc.connect();
@@ -56,4 +78,20 @@
             throw new RuntimeException("Failed " + e);
         }
     }
+
+    @BeforeTest
+    public void setup() throws Throwable {
+        // mkdir and chmod "333"
+        Files.createDirectories(TEST_DIR);
+        ProcessTools.executeCommand("chmod", "333", TEST_DIR.toString())
+                    .outputTo(System.out)
+                    .errorTo(System.out)
+                    .shouldHaveExitValue(0);
+    }
+
+    @AfterTest
+    public void tearDown() throws IOException {
+        FileUtils.deleteFileIfExistsWithRetry(TEST_DIR);
+    }
 }
+
--- a/jdk/test/sun/net/www/protocol/file/DirPermissionDenied.sh	Fri Jun 09 21:34:36 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,45 +0,0 @@
-#
-# Copyright (c) 2010, 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 6977851
-# @summary NPE from FileURLConnection.connect
-# @build DirPermissionDenied
-# @run shell DirPermissionDenied.sh
-
-TESTDIR="${TESTCLASSES}/DirPermissionDeniedDirectory"
-echo ${TESTDIR}
-
-rm -rf ${TESTDIR}
-mkdir -p ${TESTDIR}
-chmod 333 ${TESTDIR}
-
-$TESTJAVA/bin/java ${TESTVMOPTS} -classpath $TESTCLASSES DirPermissionDenied ${TESTDIR}
-result=$?
-
-# Add back read access for user, otherwise not removable on some systems
-chmod u+r ${TESTDIR}
-
-rm -rf ${TESTDIR}
-exit $result
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/sun/net/www/protocol/jrt/OtherResourcesTest.java	Sun Jun 11 18:36:23 2017 -0700
@@ -0,0 +1,50 @@
+/*
+ * 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.
+ */
+
+import jdk.test.lib.JDKToolFinder;
+import static jdk.test.lib.process.ProcessTools.executeCommand;
+
+/**
+ * @test
+ * @bug 8142968
+ * @summary Access a jrt:/ resource in an observable module that is not in
+ *          the boot layer and hence not known to the built-in class loaders.
+ *          This test is intended to run with --limit-modules.
+ * @library /test/lib
+ * @build OtherResources OtherResourcesTest
+ *        jdk.test.lib.JDKToolFinder
+ *        jdk.test.lib.process.*
+ * @run main OtherResourcesTest
+ */
+public class OtherResourcesTest {
+    public static void main(String[] args) throws Throwable {
+        String classes = System.getProperty("test.classes");
+        executeCommand(JDKToolFinder.getTestJDKTool("java"),
+                       "--limit-modules", "java.base",
+                       "-cp", classes, "OtherResources")
+                      .outputTo(System.out)
+                      .errorTo(System.out)
+                      .shouldHaveExitValue(0);
+    }
+}
+
--- a/jdk/test/sun/net/www/protocol/jrt/other_resources.sh	Fri Jun 09 21:34:36 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,45 +0,0 @@
-#
-# Copyright (c) 2015, 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
-# @build OtherResources
-# @run shell other_resources.sh
-# @summary Access a jrt:/ resource in an observable module that is not in
-#  the boot layer and hence not known to the built-in class loaders. This
-#  test is a shell test because the run tag doesn't support --limit-modules.
-
-set -e
-
-if [ -z "$TESTJAVA" ]; then
-  if [ $# -lt 1 ]; then exit 1; fi
-  TESTJAVA="$1"; shift
-  COMPILEJAVA="${TESTJAVA}"
-  TESTSRC="`pwd`"
-  TESTCLASSES="`pwd`"
-fi
-
-JAVA="$TESTJAVA/bin/java ${TESTVMOPTS}"
-$JAVA --limit-modules java.base -cp $TESTCLASSES OtherResources
-
-exit 0
-