< prev index next >

test/java/io/File/Basic.java

Print this page


   1 /*
   2  * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25    @bug 4165666 4203706 4288670 4290024
  26    @summary Basic heartbeat test for File methods that access the filesystem
  27 
  28    @build Basic Util
  29    @run shell basic.sh
  30  */
  31 

  32 import java.io.IOException;
  33 import java.io.File;
  34 import java.io.PrintStream;
  35 import java.io.RandomAccessFile;
  36 
  37 
  38 public class Basic {
  39 
  40     static PrintStream out = System.err;
  41 
  42     static File nonExistantFile = new File("x.Basic.non");
  43     static File rwFile = new File("x.Basic.rw");
  44     static File bigFile = new File("x.Basic.big");
  45     static File roFile = new File("x.Basic.ro");
  46     static File thisDir = new File(".");
  47     static File dir = new File("x.Basic.dir");

  48     static File nonDir = new File("x.Basic.nonDir");

  49 
  50     static void showBoolean(String what, boolean value) {
  51         out.println("  " + what + ": " + value);
  52     }
  53 
  54     static void showLong(String what, long value) {
  55         out.println("  " + what + ": " + value);
  56     }
  57 
  58     static void show(File f) throws Exception {
  59         out.println(f + ": ");
  60         showBoolean("exists", f.exists());
  61         showBoolean("isFile", f.isFile());
  62         showBoolean("isDirectory", f.isDirectory());
  63         showBoolean("canRead", f.canRead());
  64         showBoolean("canWrite", f.canWrite());
  65         showLong("lastModified", f.lastModified());
  66         showLong("length", f.length());
  67     }
  68 
  69     static void testFile(File f, boolean writeable, long length)
  70         throws Exception
  71     {
  72         if (!f.exists()) fail(f, "does not exist");
  73         if (!f.isFile()) fail(f, "is not a file");
  74         if (f.isDirectory()) fail(f, "is a directory");
  75         if (!f.canRead()) fail(f, "is not readable");
  76         if (!Util.isPrivileged() && f.canWrite() != writeable)
  77             fail(f, writeable ? "is not writeable" : "is writeable");
  78         int rwLen = 6;
  79         if (f.length() != length) fail(f, "has wrong length");
  80     }
  81 
  82     static void fail(File f, String why) throws Exception {
  83         throw new Exception(f + " " + why);
  84     }
  85 
  86     public static void main(String[] args) throws Exception {







  87 
  88         show(nonExistantFile);
  89         if (nonExistantFile.exists()) fail(nonExistantFile, "exists");










  90 
  91         show(rwFile);
  92         testFile(rwFile, true, 6);
  93         rwFile.delete();
  94         if (rwFile.exists())
  95             fail(rwFile, "could not delete");

  96 
  97         show(roFile);
  98         testFile(roFile, false, 0);
  99 
 100         show(thisDir);
 101         if (!thisDir.exists()) fail(thisDir, "does not exist");
 102         if (thisDir.isFile()) fail(thisDir, "is a file");
 103         if (!thisDir.isDirectory()) fail(thisDir, "is not a directory");
 104         if (!thisDir.canRead()) fail(thisDir, "is readable");
 105         if (!thisDir.canWrite()) fail(thisDir, "is writeable");
 106         String[] fs = thisDir.list();
 107         if (fs == null) fail(thisDir, "list() returned null");
 108         out.print("  [" + fs.length + "]");
 109         for (int i = 0; i < fs.length; i++)
 110             out.print(" " + fs[i]);

 111         out.println();
 112         if (fs.length == 0) fail(thisDir, "is empty");
 113 
 114         if (!nonExistantFile.createNewFile())
 115             fail(nonExistantFile, "could not create");
 116         nonExistantFile.deleteOnExit();
 117 
 118         if (!nonDir.mkdir())
 119             fail(nonDir, "could not create");




 120 
 121         if (!dir.renameTo(new File("x.Basic.dir2")))
 122             fail(dir, "failed to rename");




 123 
 124         if (System.getProperty("os.name").equals("SunOS")
 125             && System.getProperty("os.version").compareTo("5.6") >= 0) {
 126             if (bigFile.exists()) {
 127                 bigFile.delete();
 128                 if (bigFile.exists())
 129                     fail(bigFile, "could not delete");
 130             }
 131             RandomAccessFile raf = new RandomAccessFile(bigFile, "rw");
 132             long big = ((long)Integer.MAX_VALUE) * 2;
 133             try {
 134                 raf.seek(big);
 135                 raf.write('x');
 136                 show(bigFile);
 137                 testFile(bigFile, true, big + 1);
 138             } finally {
 139                 raf.close();
 140             }
 141             bigFile.delete();
 142             if (bigFile.exists())
   1 /*
   2  * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 4165666 4203706 4288670 4290024
  26  * @summary Basic heartbeat test for File methods that access the filesystem
  27  * @build Basic Util NonExistentDriver
  28  * @run driver NonExistentDriver
  29  * @run main/othervm Basic
  30  */
  31 
  32 import java.io.FileOutputStream;
  33 import java.io.IOException;
  34 import java.io.File;
  35 import java.io.PrintStream;
  36 import java.io.RandomAccessFile;
  37 
  38 
  39 public class Basic {
  40 
  41     static PrintStream out = System.err;
  42 

  43     static File rwFile = new File("x.Basic.rw");
  44     static File bigFile = new File("x.Basic.big");
  45     static File roFile = new File("x.Basic.ro");
  46     static File thisDir = new File(".");
  47     static File dir = new File("x.Basic.dir");
  48     static File dir2 = new File("x.Basic.dir2");
  49     static File nonDir = new File("x.Basic.nonDir");
  50     static byte bytes[] = new byte[] {1, 2, 3, 4, 5, 6};
  51 
  52     static void showBoolean(String what, boolean value) {
  53         out.println("  " + what + ": " + value);
  54     }
  55 
  56     static void showLong(String what, long value) {
  57         out.println("  " + what + ": " + value);
  58     }
  59 
  60     public static void show(File f) throws Exception {
  61         out.println(f + ": ");
  62         showBoolean("exists", f.exists());
  63         showBoolean("isFile", f.isFile());
  64         showBoolean("isDirectory", f.isDirectory());
  65         showBoolean("canRead", f.canRead());
  66         showBoolean("canWrite", f.canWrite());
  67         showLong("lastModified", f.lastModified());
  68         showLong("length", f.length());
  69     }
  70 
  71     static void testFile(File f, boolean writeable, long length)
  72         throws Exception
  73     {
  74         if (!f.exists()) fail(f, "does not exist");
  75         if (!f.isFile()) fail(f, "is not a file");
  76         if (f.isDirectory()) fail(f, "is a directory");
  77         if (!f.canRead()) fail(f, "is not readable");
  78         if (!Util.isPrivileged() && f.canWrite() != writeable)
  79             fail(f, writeable ? "is not writeable" : "is writeable");

  80         if (f.length() != length) fail(f, "has wrong length");
  81     }
  82 
  83     public static void fail(File f, String why) throws Exception {
  84         throw new Exception(f + " " + why);
  85     }
  86 
  87     static void setup() throws Exception {
  88         rwFile.delete();
  89         bigFile.delete();
  90         roFile.delete();
  91         thisDir.delete();
  92         dir.delete();
  93         dir2.delete();
  94         nonDir.delete();
  95 
  96         try (FileOutputStream fos = new FileOutputStream(rwFile)) {
  97             fos.write(bytes);
  98         }
  99 
 100         roFile.createNewFile();
 101         roFile.setReadOnly();
 102 
 103         dir.mkdir();
 104     }
 105 
 106     public static void main(String[] args) throws Exception {
 107         setup();
 108 
 109         show(rwFile);
 110         testFile(rwFile, true, bytes.length);
 111         rwFile.delete();
 112         if (rwFile.exists()) {
 113             fail(rwFile, "could not delete");
 114         }
 115 
 116         show(roFile);
 117         testFile(roFile, false, 0);
 118 
 119         show(thisDir);
 120         if (!thisDir.exists()) fail(thisDir, "does not exist");
 121         if (thisDir.isFile()) fail(thisDir, "is a file");
 122         if (!thisDir.isDirectory()) fail(thisDir, "is not a directory");
 123         if (!thisDir.canRead()) fail(thisDir, "is readable");
 124         if (!thisDir.canWrite()) fail(thisDir, "is writeable");
 125         String[] fs = thisDir.list();
 126         if (fs == null) fail(thisDir, "list() returned null");
 127         out.print("  [" + fs.length + "]");
 128         for (int i = 0; i < fs.length; i++) {
 129             out.print(" " + fs[i]);
 130         }
 131         out.println();
 132         if (fs.length == 0) fail(thisDir, "is empty");
 133 
 134         if (NonExistentDriver.nonExistentFile.exists()) {
 135             fail(NonExistentDriver.nonExistentFile, "not deleted");
 136         }
 137 
 138         if (!nonDir.mkdir()) {
 139             fail(nonDir, "could not create");
 140         }
 141         if (!nonDir.exists() || !nonDir.isDirectory()) {
 142             fail(nonDir, "not created");
 143         }
 144 
 145         if (!dir.renameTo(dir2)) {
 146             fail(dir, "failed to rename");
 147         }
 148         if (dir.exists() || !dir2.exists() || !dir2.isDirectory()) {
 149             fail(dir, "not renamed");
 150         }
 151 
 152         if (System.getProperty("os.name").equals("SunOS")
 153             && System.getProperty("os.version").compareTo("5.6") >= 0) {
 154             if (bigFile.exists()) {
 155                 bigFile.delete();
 156                 if (bigFile.exists())
 157                     fail(bigFile, "could not delete");
 158             }
 159             RandomAccessFile raf = new RandomAccessFile(bigFile, "rw");
 160             long big = ((long)Integer.MAX_VALUE) * 2;
 161             try {
 162                 raf.seek(big);
 163                 raf.write('x');
 164                 show(bigFile);
 165                 testFile(bigFile, true, big + 1);
 166             } finally {
 167                 raf.close();
 168             }
 169             bigFile.delete();
 170             if (bigFile.exists())
< prev index next >