jdk/test/java/io/File/Basic.java
changeset 45574 04189244d1b2
parent 13568 ce5ab758aeb5
equal deleted inserted replaced
45573:dea6ec006e78 45574:04189244d1b2
     1 /*
     1 /*
     2  * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /* @test
    24 /* @test
    25    @bug 4165666 4203706 4288670 4290024
    25  * @bug 4165666 4203706 4288670 4290024
    26    @summary Basic heartbeat test for File methods that access the filesystem
    26  * @summary Basic heartbeat test for File methods that access the filesystem
    27 
    27  * @build Basic Util
    28    @build Basic Util
    28  * @run main/othervm Basic
    29    @run shell basic.sh
       
    30  */
    29  */
    31 
    30 
       
    31 import java.io.FileOutputStream;
    32 import java.io.IOException;
    32 import java.io.IOException;
    33 import java.io.File;
    33 import java.io.File;
    34 import java.io.PrintStream;
    34 import java.io.PrintStream;
    35 import java.io.RandomAccessFile;
    35 import java.io.RandomAccessFile;
    36 
    36 
    37 
    37 
    38 public class Basic {
    38 public class Basic {
    39 
    39 
    40     static PrintStream out = System.err;
    40     static PrintStream out = System.err;
    41 
    41 
    42     static File nonExistantFile = new File("x.Basic.non");
       
    43     static File rwFile = new File("x.Basic.rw");
    42     static File rwFile = new File("x.Basic.rw");
    44     static File bigFile = new File("x.Basic.big");
    43     static File bigFile = new File("x.Basic.big");
    45     static File roFile = new File("x.Basic.ro");
    44     static File roFile = new File("x.Basic.ro");
    46     static File thisDir = new File(".");
    45     static File thisDir = new File(".");
    47     static File dir = new File("x.Basic.dir");
    46     static File dir = new File("x.Basic.dir");
    48     static File nonDir = new File("x.Basic.nonDir");
    47     static File dir2 = new File("x.Basic.dir2");
       
    48     static byte bytes[] = new byte[] {1, 2, 3, 4, 5, 6};
    49 
    49 
    50     static void showBoolean(String what, boolean value) {
    50     static void showBoolean(String what, boolean value) {
    51         out.println("  " + what + ": " + value);
    51         out.println("  " + what + ": " + value);
    52     }
    52     }
    53 
    53 
    73         if (!f.isFile()) fail(f, "is not a file");
    73         if (!f.isFile()) fail(f, "is not a file");
    74         if (f.isDirectory()) fail(f, "is a directory");
    74         if (f.isDirectory()) fail(f, "is a directory");
    75         if (!f.canRead()) fail(f, "is not readable");
    75         if (!f.canRead()) fail(f, "is not readable");
    76         if (!Util.isPrivileged() && f.canWrite() != writeable)
    76         if (!Util.isPrivileged() && f.canWrite() != writeable)
    77             fail(f, writeable ? "is not writeable" : "is writeable");
    77             fail(f, writeable ? "is not writeable" : "is writeable");
    78         int rwLen = 6;
       
    79         if (f.length() != length) fail(f, "has wrong length");
    78         if (f.length() != length) fail(f, "has wrong length");
    80     }
    79     }
    81 
    80 
    82     static void fail(File f, String why) throws Exception {
    81     static void fail(File f, String why) throws Exception {
    83         throw new Exception(f + " " + why);
    82         throw new Exception(f + " " + why);
    84     }
    83     }
    85 
    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 
    86     public static void main(String[] args) throws Exception {
   101     public static void main(String[] args) throws Exception {
    87 
   102         setup();
    88         show(nonExistantFile);
       
    89         if (nonExistantFile.exists()) fail(nonExistantFile, "exists");
       
    90 
   103 
    91         show(rwFile);
   104         show(rwFile);
    92         testFile(rwFile, true, 6);
   105         testFile(rwFile, true, bytes.length);
    93         rwFile.delete();
   106         rwFile.delete();
    94         if (rwFile.exists())
   107         if (rwFile.exists()) {
    95             fail(rwFile, "could not delete");
   108             fail(rwFile, "could not delete");
       
   109         }
    96 
   110 
    97         show(roFile);
   111         show(roFile);
    98         testFile(roFile, false, 0);
   112         testFile(roFile, false, 0);
    99 
   113 
   100         show(thisDir);
   114         show(thisDir);
   104         if (!thisDir.canRead()) fail(thisDir, "is readable");
   118         if (!thisDir.canRead()) fail(thisDir, "is readable");
   105         if (!thisDir.canWrite()) fail(thisDir, "is writeable");
   119         if (!thisDir.canWrite()) fail(thisDir, "is writeable");
   106         String[] fs = thisDir.list();
   120         String[] fs = thisDir.list();
   107         if (fs == null) fail(thisDir, "list() returned null");
   121         if (fs == null) fail(thisDir, "list() returned null");
   108         out.print("  [" + fs.length + "]");
   122         out.print("  [" + fs.length + "]");
   109         for (int i = 0; i < fs.length; i++)
   123         for (int i = 0; i < fs.length; i++) {
   110             out.print(" " + fs[i]);
   124             out.print(" " + fs[i]);
       
   125         }
   111         out.println();
   126         out.println();
   112         if (fs.length == 0) fail(thisDir, "is empty");
   127         if (fs.length == 0) fail(thisDir, "is empty");
   113 
   128 
   114         if (!nonExistantFile.createNewFile())
   129         if (!dir.mkdir() || !dir.exists() || !dir.isDirectory()) {
   115             fail(nonExistantFile, "could not create");
   130             fail(dir, "could not create");
   116         nonExistantFile.deleteOnExit();
   131         }
   117 
   132         if (!dir.renameTo(dir2)) {
   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");
   133             fail(dir, "failed to rename");
       
   134         }
       
   135         if (dir.exists() || !dir2.exists() || !dir2.isDirectory()) {
       
   136             fail(dir, "not renamed");
       
   137         }
   123 
   138 
   124         if (System.getProperty("os.name").equals("SunOS")
   139         if (System.getProperty("os.name").equals("SunOS")
   125             && System.getProperty("os.version").compareTo("5.6") >= 0) {
   140             && System.getProperty("os.version").compareTo("5.6") >= 0) {
   126             if (bigFile.exists()) {
   141             if (bigFile.exists()) {
   127                 bigFile.delete();
   142                 bigFile.delete();