test/jdk/java/io/File/Basic.java
author dholmes
Wed, 06 Nov 2019 21:18:42 -0500
changeset 58956 9a0a5e70eeb2
parent 47216 71c04702a3d5
permissions -rw-r--r--
8233454: Test fails with assert(!is_init_completed(), "should only happen during init") after JDK-8229516 Reviewed-by: jiefu, dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
45574
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
     2
 * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4655
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4655
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4655
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
45574
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
    25
 * @bug 4165666 4203706 4288670 4290024
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
    26
 * @summary Basic heartbeat test for File methods that access the filesystem
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
    27
 * @build Basic Util
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
    28
 * @run main/othervm Basic
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
45574
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
    31
import java.io.FileOutputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.RandomAccessFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class Basic {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static PrintStream out = System.err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static File rwFile = new File("x.Basic.rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static File bigFile = new File("x.Basic.big");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static File roFile = new File("x.Basic.ro");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static File thisDir = new File(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static File dir = new File("x.Basic.dir");
45574
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
    47
    static File dir2 = new File("x.Basic.dir2");
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
    48
    static byte bytes[] = new byte[] {1, 2, 3, 4, 5, 6};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static void showBoolean(String what, boolean value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        out.println("  " + what + ": " + value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static void showLong(String what, long value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        out.println("  " + what + ": " + value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static void show(File f) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        out.println(f + ": ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        showBoolean("exists", f.exists());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        showBoolean("isFile", f.isFile());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        showBoolean("isDirectory", f.isDirectory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        showBoolean("canRead", f.canRead());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        showBoolean("canWrite", f.canWrite());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        showLong("lastModified", f.lastModified());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        showLong("length", f.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static void testFile(File f, boolean writeable, long length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (!f.exists()) fail(f, "does not exist");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (!f.isFile()) fail(f, "is not a file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (f.isDirectory()) fail(f, "is a directory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (!f.canRead()) fail(f, "is not readable");
13568
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 7668
diff changeset
    76
        if (!Util.isPrivileged() && f.canWrite() != writeable)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            fail(f, writeable ? "is not writeable" : "is writeable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (f.length() != length) fail(f, "has wrong length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    static void fail(File f, String why) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        throw new Exception(f + " " + why);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
45574
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
    85
    static void setup() throws Exception {
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
    86
        rwFile.delete();
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
    87
        bigFile.delete();
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
    88
        roFile.delete();
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
    89
        thisDir.delete();
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
    90
        dir.delete();
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
    91
        dir2.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
45574
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
    93
        try (FileOutputStream fos = new FileOutputStream(rwFile)) {
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
    94
            fos.write(bytes);
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
    95
        }
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
    96
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
    97
        roFile.createNewFile();
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
    98
        roFile.setReadOnly();
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
    99
    }
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
   100
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
   101
    public static void main(String[] args) throws Exception {
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
   102
        setup();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        show(rwFile);
45574
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
   105
        testFile(rwFile, true, bytes.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        rwFile.delete();
45574
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
   107
        if (rwFile.exists()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            fail(rwFile, "could not delete");
45574
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
   109
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        show(roFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        testFile(roFile, false, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        show(thisDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (!thisDir.exists()) fail(thisDir, "does not exist");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (thisDir.isFile()) fail(thisDir, "is a file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (!thisDir.isDirectory()) fail(thisDir, "is not a directory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (!thisDir.canRead()) fail(thisDir, "is readable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (!thisDir.canWrite()) fail(thisDir, "is writeable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        String[] fs = thisDir.list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (fs == null) fail(thisDir, "list() returned null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        out.print("  [" + fs.length + "]");
45574
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
   123
        for (int i = 0; i < fs.length; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            out.print(" " + fs[i]);
45574
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
   125
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (fs.length == 0) fail(thisDir, "is empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
45574
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
   129
        if (!dir.mkdir() || !dir.exists() || !dir.isDirectory()) {
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
   130
            fail(dir, "could not create");
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
   131
        }
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
   132
        if (!dir.renameTo(dir2)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            fail(dir, "failed to rename");
45574
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
   134
        }
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
   135
        if (dir.exists() || !dir2.exists() || !dir2.isDirectory()) {
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
   136
            fail(dir, "not renamed");
04189244d1b2 8181478: Refactor java/io shell tests to plain java tests
mli
parents: 13568
diff changeset
   137
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (System.getProperty("os.name").equals("SunOS")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            && System.getProperty("os.version").compareTo("5.6") >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if (bigFile.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                bigFile.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                if (bigFile.exists())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    fail(bigFile, "could not delete");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            RandomAccessFile raf = new RandomAccessFile(bigFile, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            long big = ((long)Integer.MAX_VALUE) * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                raf.seek(big);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                raf.write('x');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                show(bigFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                testFile(bigFile, true, big + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                raf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            bigFile.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            if (bigFile.exists())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                fail(bigFile, "could not delete");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            System.err.println("NOTE: Large files not supported on this system");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
}