test/java/io/File/Basic.java

Print this page




  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 (f.canWrite() != writeable)
  77             fail(f, writeable ? "is not writeable" : "is writeable");
  78         int rwLen = (File.separatorChar == '/' ? 6 : 7);
  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, File.separatorChar == '/' ? 6 : 7);
  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");




  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 (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");