< prev index next >

test/jdk/java/io/File/GetXSpace.java

Print this page

        

@@ -22,29 +22,29 @@
  */
 
 /**
  * @test
  * @bug 4057701 6286712 6364377
- * @requires (os.family == "linux" | os.family == "mac" | os.family == "windows")
- * @run build GetXSpace
- * @run shell GetXSpace.sh
+ * @requires (os.family == "linux" | os.family == "mac" |
+ *            os.family == "windows")
  * @summary Basic functionality of File.get-X-Space methods.
- * @key randomness
  */
 
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FilePermission;
 import java.io.InputStreamReader;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.FileStore;
+import java.nio.file.Path;
 import java.security.Permission;
 import java.util.ArrayList;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import static java.lang.System.err;
 import static java.lang.System.out;
 
 public class GetXSpace {
 
     private static SecurityManager [] sma = { null, new Allow(), new DenyFSA(),

@@ -59,10 +59,16 @@
 
     private static int fail = 0;
     private static int pass = 0;
     private static Throwable first;
 
+    static void reset() {
+        fail = 0;
+        pass = 0;
+        first = null;
+    }
+
     static void pass() {
         pass++;
     }
 
     static void fail(String p) {

@@ -336,22 +342,32 @@
         public void checkRead(String file) {
             throw new SecurityException(err);
         }
     }
 
-    private static void testFile(String dirName) {
+    private static int testFile(Path dir) {
+        String dirName = dir.toString();
         out.format("--- Testing %s%n", dirName);
         ArrayList<Space> l;
         try {
             l = space(dirName);
         } catch (IOException x) {
             throw new RuntimeException(dirName + " can't get file system information", x);
         }
         compare(l.get(0));
+
+        if (fail != 0) {
+            err.format("%d tests: %d failure(s); first: %s%n",
+                fail + pass, fail, first);
+        } else {
+            out.format("all %d tests passed%n", fail + pass);
     }
 
-    private static void testDF() {
+        return fail != 0 ? 1 : 0;
+    }
+
+    private static int testDF() {
         out.println("--- Testing df");
         // Find all of the partitions on the machine and verify that the size
         // returned by "df" is equivalent to File.getXSpace() values.
         ArrayList<Space> l;
         try {

@@ -379,22 +395,53 @@
                     compareZeroNonExist();
                     compareZeroExist();
                 }
             }
         }
-    }
 
-    public static void main(String [] args) {
-        if (args.length > 0) {
-            testFile(args[0]);
-        } else {
-            testDF();
-        }
+        System.setSecurityManager(null);
 
         if (fail != 0) {
-            throw new RuntimeException((fail + pass) + " tests: "
-                                       + fail + " failure(s), first", first);
+            err.format("%d tests: %d failure(s); first: %s%n",
+                fail + pass, fail, first);
         } else {
             out.format("all %d tests passed%n", fail + pass);
         }
+
+        return fail != 0 ? 1 : 0;
+    }
+
+    private static void perms(File file, boolean allow) throws IOException {
+        file.setExecutable(allow, false);
+        file.setReadable(allow, false);
+        file.setWritable(allow, false);
+    }
+
+    private static void deny(Path path) throws IOException {
+        perms(path.toFile(), false);
+    }
+
+    private static void allow(Path path) throws IOException {
+        perms(path.toFile(), true);
+    }
+
+    public static void main(String[] args) throws Exception {
+        int failedTests = testDF();
+        reset();
+
+        Path tmpDir = Files.createTempDirectory(null);
+        Path tmpSubdir = Files.createTempDirectory(tmpDir, null);
+        Path tmpFile = Files.createTempFile(tmpSubdir, "foo", null);
+
+        deny(tmpSubdir);
+        failedTests += testFile(tmpFile);
+
+        allow(tmpSubdir);
+        Files.delete(tmpFile);
+        Files.delete(tmpSubdir);
+        Files.delete(tmpDir);
+
+        if (failedTests > 0) {
+            throw new RuntimeException(failedTests + " test(s) failed");
+        }
     }
 }
< prev index next >