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
  28  * @run main/othervm Basic
  29  */
  30 
  31 import java.io.FileOutputStream;
  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 rwFile = new File("x.Basic.rw");
  43     static File bigFile = new File("x.Basic.big");
  44     static File roFile = new File("x.Basic.ro");
  45     static File thisDir = new File(".");
  46     static File dir = new File("x.Basic.dir");
  47     static File dir2 = new File("x.Basic.dir2");
  48     static byte bytes[] = new byte[] {1, 2, 3, 4, 5, 6};
  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         if (f.length() != length) fail(f, "has wrong length");
  79     }
  80 
  81     static void fail(File f, String why) throws Exception {
  82         throw new Exception(f + " " + why);
  83     }
  84 
  85     static void setup() throws Exception {
  86         rwFile.delete();
  87         bigFile.delete();
  88         roFile.delete();
  89         thisDir.delete();
  90         dir.delete();
  91         dir2.delete();
  92 
  93         try (FileOutputStream fos = new FileOutputStream(rwFile)) {
  94             fos.write(bytes);
  95         }
  96 
  97         roFile.createNewFile();
  98         roFile.setReadOnly();
  99     }
 100 
 101     public static void main(String[] args) throws Exception {
 102         setup();
 103 
 104         show(rwFile);
 105         testFile(rwFile, true, bytes.length);
 106         rwFile.delete();
 107         if (rwFile.exists()) {
 108             fail(rwFile, "could not delete");
 109         }
 110 
 111         show(roFile);
 112         testFile(roFile, false, 0);
 113 
 114         show(thisDir);
 115         if (!thisDir.exists()) fail(thisDir, "does not exist");
 116         if (thisDir.isFile()) fail(thisDir, "is a file");
 117         if (!thisDir.isDirectory()) fail(thisDir, "is not a directory");
 118         if (!thisDir.canRead()) fail(thisDir, "is readable");
 119         if (!thisDir.canWrite()) fail(thisDir, "is writeable");
 120         String[] fs = thisDir.list();
 121         if (fs == null) fail(thisDir, "list() returned null");
 122         out.print("  [" + fs.length + "]");
 123         for (int i = 0; i < fs.length; i++) {
 124             out.print(" " + fs[i]);
 125         }
 126         out.println();
 127         if (fs.length == 0) fail(thisDir, "is empty");
 128 
 129         if (!dir.mkdir() || !dir.exists() || !dir.isDirectory()) {
 130             fail(dir, "could not create");
 131         }
 132         if (!dir.renameTo(dir2)) {
 133             fail(dir, "failed to rename");
 134         }
 135         if (dir.exists() || !dir2.exists() || !dir2.isDirectory()) {
 136             fail(dir, "not renamed");
 137         }
 138 
 139         System.err.println("NOTE: Large files not supported on this system");
 140     }
 141 
 142 }