8181478: Refactor java/io shell tests to plain java tests
authormli
Mon, 19 Jun 2017 18:02:06 -0700
changeset 45574 04189244d1b2
parent 45573 dea6ec006e78
child 45575 022acef7b83b
8181478: Refactor java/io shell tests to plain java tests Reviewed-by: alanb, psandoz
jdk/test/java/io/File/Basic.java
jdk/test/java/io/File/basic.sh
jdk/test/java/io/FileOutputStream/FileOpen.sh
jdk/test/java/io/FileOutputStream/FileOpenNeg.java
jdk/test/java/io/FileOutputStream/FileOpenPos.java
jdk/test/java/io/FileOutputStream/FileOpenTest.java
--- a/jdk/test/java/io/File/Basic.java	Mon Jun 19 11:52:01 2017 +0200
+++ b/jdk/test/java/io/File/Basic.java	Mon Jun 19 18:02:06 2017 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 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,13 +22,13 @@
  */
 
 /* @test
-   @bug 4165666 4203706 4288670 4290024
-   @summary Basic heartbeat test for File methods that access the filesystem
-
-   @build Basic Util
-   @run shell basic.sh
+ * @bug 4165666 4203706 4288670 4290024
+ * @summary Basic heartbeat test for File methods that access the filesystem
+ * @build Basic Util
+ * @run main/othervm Basic
  */
 
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.File;
 import java.io.PrintStream;
@@ -39,13 +39,13 @@
 
     static PrintStream out = System.err;
 
-    static File nonExistantFile = new File("x.Basic.non");
     static File rwFile = new File("x.Basic.rw");
     static File bigFile = new File("x.Basic.big");
     static File roFile = new File("x.Basic.ro");
     static File thisDir = new File(".");
     static File dir = new File("x.Basic.dir");
-    static File nonDir = new File("x.Basic.nonDir");
+    static File dir2 = new File("x.Basic.dir2");
+    static byte bytes[] = new byte[] {1, 2, 3, 4, 5, 6};
 
     static void showBoolean(String what, boolean value) {
         out.println("  " + what + ": " + value);
@@ -75,7 +75,6 @@
         if (!f.canRead()) fail(f, "is not readable");
         if (!Util.isPrivileged() && f.canWrite() != writeable)
             fail(f, writeable ? "is not writeable" : "is writeable");
-        int rwLen = 6;
         if (f.length() != length) fail(f, "has wrong length");
     }
 
@@ -83,16 +82,31 @@
         throw new Exception(f + " " + why);
     }
 
-    public static void main(String[] args) throws Exception {
+    static void setup() throws Exception {
+        rwFile.delete();
+        bigFile.delete();
+        roFile.delete();
+        thisDir.delete();
+        dir.delete();
+        dir2.delete();
 
-        show(nonExistantFile);
-        if (nonExistantFile.exists()) fail(nonExistantFile, "exists");
+        try (FileOutputStream fos = new FileOutputStream(rwFile)) {
+            fos.write(bytes);
+        }
+
+        roFile.createNewFile();
+        roFile.setReadOnly();
+    }
+
+    public static void main(String[] args) throws Exception {
+        setup();
 
         show(rwFile);
-        testFile(rwFile, true, 6);
+        testFile(rwFile, true, bytes.length);
         rwFile.delete();
-        if (rwFile.exists())
+        if (rwFile.exists()) {
             fail(rwFile, "could not delete");
+        }
 
         show(roFile);
         testFile(roFile, false, 0);
@@ -106,20 +120,21 @@
         String[] fs = thisDir.list();
         if (fs == null) fail(thisDir, "list() returned null");
         out.print("  [" + fs.length + "]");
-        for (int i = 0; i < fs.length; i++)
+        for (int i = 0; i < fs.length; i++) {
             out.print(" " + fs[i]);
+        }
         out.println();
         if (fs.length == 0) fail(thisDir, "is empty");
 
-        if (!nonExistantFile.createNewFile())
-            fail(nonExistantFile, "could not create");
-        nonExistantFile.deleteOnExit();
-
-        if (!nonDir.mkdir())
-            fail(nonDir, "could not create");
-
-        if (!dir.renameTo(new File("x.Basic.dir2")))
+        if (!dir.mkdir() || !dir.exists() || !dir.isDirectory()) {
+            fail(dir, "could not create");
+        }
+        if (!dir.renameTo(dir2)) {
             fail(dir, "failed to rename");
+        }
+        if (dir.exists() || !dir2.exists() || !dir2.isDirectory()) {
+            fail(dir, "not renamed");
+        }
 
         if (System.getProperty("os.name").equals("SunOS")
             && System.getProperty("os.version").compareTo("5.6") >= 0) {
--- a/jdk/test/java/io/File/basic.sh	Mon Jun 19 11:52:01 2017 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,56 +0,0 @@
-#! /bin/sh
-
-#
-# Copyright (c) 1998, 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.
-#
-
-#
-
-if [ "x$TESTJAVA" = x ]; then
-  TESTJAVA=$1; shift
-  TESTCLASSES=.
-fi
-
-rm -rf x.Basic.*
-rm -f x.Basic.non
-printf "%s" "xyzzyN" > x.Basic.rw
-touch x.Basic.ro
-OS=`uname -s`
-case "$OS" in
-  Windows_* | CYGWIN*)
-    attrib +R x.Basic.ro
-    ;;
-  *)
-    chmod ugo-w x.Basic.ro
-    ;;
-esac
-mkdir x.Basic.dir
-if $TESTJAVA/bin/java ${TESTVMOPTS} $* -classpath "$TESTCLASSES" Basic; then
-  [ -f x.Basic.rw ] && (echo "x.Basic.rw not deleted"; exit 1)
-  ([ -d x.Basic.dir ] || [ \! -d x.Basic.dir2 ]) \
-    && (echo "x.Basic.dir not renamed"; exit 1)
-  [ \! -d x.Basic.nonDir ] && (echo "x.Basic.nonDir not created"; exit 1)
-  [ -f x.Basic.non ] && (echo "x.Basic.non not deleted"; exit 1)
-  exit 0
-else
-  exit 1
-fi
--- a/jdk/test/java/io/FileOutputStream/FileOpen.sh	Mon Jun 19 11:52:01 2017 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,78 +0,0 @@
-#!/bin/sh
-
-#
-# Copyright (c) 2006, 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 6364894
-# @run shell FileOpen.sh
-# @summary Test to ensure that opening of hidden Vs non-hidden,
-# read/write Vs read-only files for writing works as expected.
-
-
-# We use a TMP directory on a local disk because this test
-# requires that the file to be tested be present on the local disk,
-# not on a samba mounted drive or on a drive that is mapped.
-# The cmd 'attrib' works only on the local files.
-TMP="C:\TEMP"
-hfile=${TMP}"\random_file1.txt"
-ATTRIB=${SystemRoot}"\system32\attrib.exe"
-
-OS=`uname -s`
-case "$OS" in
-    Windows_* )
-	if [ ! -d ${TMP} ] ; then 
-           echo "Could not find the directory-" ${TMP} "- passing test"
-	   exit 0;
-	fi
-	${COMPILEJAVA}/bin/javac ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} -d . \
-	    ${TESTSRC}\\FileOpenPos.java
-	${COMPILEJAVA}/bin/javac ${TESTJAVACOPTS} ${TESTTOOLVMOPTS} -d . \
-	    ${TESTSRC}\\FileOpenNeg.java
-
-	echo "Opening Writable Normal File.."
-	${TESTJAVA}/bin/java ${TESTVMOPTS} FileOpenPos ${hfile}
-
-	echo "Opening Writable Hidden File.."
-	${ATTRIB} +h ${hfile}
-	${TESTJAVA}/bin/java ${TESTVMOPTS} FileOpenNeg ${hfile}
-
-	echo "Opening Read-Only Normal File.."
-	${ATTRIB} -h ${hfile}
-	${ATTRIB} +r ${hfile}
-	${TESTJAVA}/bin/java ${TESTVMOPTS} FileOpenNeg ${hfile}
-
-	echo "Opening Read-Only Hidden File.." 
-	${ATTRIB} +h ${hfile}
-	${TESTJAVA}/bin/java ${TESTVMOPTS} FileOpenNeg ${hfile}
-
-        rm -f ${hfile}
-	exit
-        ;;
-
-    * )
-        echo "This test is not intended for this OS - passing test"
-	exit 0
-        ;;
-esac
--- a/jdk/test/java/io/FileOutputStream/FileOpenNeg.java	Mon Jun 19 11:52:01 2017 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,45 +0,0 @@
-/*
- * Copyright (c) 2006, 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.*;
-
-public class FileOpenNeg {
-
-    public static void main( String[] args) throws Exception {
-        boolean openForWrite = true;
-
-        File f = new File(args[0]);
-        try {
-            FileOutputStream fs = new FileOutputStream(f);
-            fs.write(1);
-            fs.close();
-        } catch( IOException e ) {
-            System.out.println("Caught the Exception as expected");
-            e.printStackTrace(System.out);
-            openForWrite = false;
-        }
-        if (openForWrite && !f.canWrite()) {
-            throw new Exception("Able to open READ-ONLY file for WRITING!");
-        }
-    }
-}
--- a/jdk/test/java/io/FileOutputStream/FileOpenPos.java	Mon Jun 19 11:52:01 2017 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,37 +0,0 @@
-/*
- * Copyright (c) 2006, 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.*;
-
-public class FileOpenPos {
-
-    public static void main( String[] args)
-        throws IOException {
-        File f = new File(args[0]);
-        FileOutputStream fs = new FileOutputStream(f);
-        fs.write(1);
-        fs.close();
-        System.out.println("Can Write ?" + f.canWrite());
-        System.out.println("The File was successfully opened");
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/io/FileOutputStream/FileOpenTest.java	Mon Jun 19 18:02:06 2017 -0700
@@ -0,0 +1,82 @@
+/*
+ * 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 6364894
+ * @requires (os.family == "windows")
+ * @library /test/lib
+ * @build jdk.test.lib.Asserts
+ * @run main FileOpenTest
+ * @summary Test to ensure that opening of hidden Vs non-hidden,
+ *          read/write Vs read-only files for writing works as expected.
+ */
+
+import java.io.File;
+import java.io.IOException;
+import java.io.FileOutputStream;
+import java.nio.file.Files;
+
+import static jdk.test.lib.Asserts.assertTrue;
+
+public class FileOpenTest {
+
+    private static File tmpFile;
+
+    public static void main(String args[]) throws Exception {
+        try {
+            tmpFile = File.createTempFile("FileOpenTest", "suffix");
+
+            // Opening Writable Normal File..
+            test(true);
+
+            // Opening Writable Hidden File..
+            Files.setAttribute(tmpFile.toPath(), "dos:hidden", true);
+            test(false);
+
+            // Opening Read-Only Hidden File..
+            Files.setAttribute(tmpFile.toPath(), "dos:hidden", false);
+            tmpFile.setReadOnly();
+            test(false);
+
+            // Opening Read-Only Normal File..
+            Files.setAttribute(tmpFile.toPath(), "dos:hidden", true);
+            test(false);
+        } finally {
+            tmpFile.delete();
+        }
+    }
+
+    private static void test(boolean writable) throws Exception {
+
+        try (FileOutputStream fs = new FileOutputStream(tmpFile)) {
+            fs.write(1);
+            assertTrue(writable, "Able to open READ-ONLY file for WRITING!");
+            assertTrue(tmpFile.canWrite(), "Able to open READ-ONLY file for WRITING!");
+        } catch(IOException e) {
+            assertTrue(!writable, "Unable to open non-READ-ONLY file for WRITING!");
+            System.out.println("Caught the Exception as expected");
+            e.printStackTrace(System.out);
+        }
+    }
+}