Path: blob/master/test/jdk/java/io/File/Basic.java
41149 views
/*1* Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4165666 4203706 4288670 429002425* @summary Basic heartbeat test for File methods that access the filesystem26* @build Basic Util27* @run main/othervm Basic28*/2930import java.io.FileOutputStream;31import java.io.IOException;32import java.io.File;33import java.io.PrintStream;34import java.io.RandomAccessFile;353637public class Basic {3839static PrintStream out = System.err;4041static File rwFile = new File("x.Basic.rw");42static File bigFile = new File("x.Basic.big");43static File roFile = new File("x.Basic.ro");44static File thisDir = new File(".");45static File dir = new File("x.Basic.dir");46static File dir2 = new File("x.Basic.dir2");47static byte bytes[] = new byte[] {1, 2, 3, 4, 5, 6};4849static void showBoolean(String what, boolean value) {50out.println(" " + what + ": " + value);51}5253static void showLong(String what, long value) {54out.println(" " + what + ": " + value);55}5657static void show(File f) throws Exception {58out.println(f + ": ");59showBoolean("exists", f.exists());60showBoolean("isFile", f.isFile());61showBoolean("isDirectory", f.isDirectory());62showBoolean("canRead", f.canRead());63showBoolean("canWrite", f.canWrite());64showLong("lastModified", f.lastModified());65showLong("length", f.length());66}6768static void testFile(File f, boolean writeable, long length)69throws Exception70{71if (!f.exists()) fail(f, "does not exist");72if (!f.isFile()) fail(f, "is not a file");73if (f.isDirectory()) fail(f, "is a directory");74if (!f.canRead()) fail(f, "is not readable");75if (!Util.isPrivileged() && f.canWrite() != writeable)76fail(f, writeable ? "is not writeable" : "is writeable");77if (f.length() != length) fail(f, "has wrong length");78}7980static void fail(File f, String why) throws Exception {81throw new Exception(f + " " + why);82}8384static void setup() throws Exception {85rwFile.delete();86bigFile.delete();87roFile.delete();88thisDir.delete();89dir.delete();90dir2.delete();9192try (FileOutputStream fos = new FileOutputStream(rwFile)) {93fos.write(bytes);94}9596roFile.createNewFile();97roFile.setReadOnly();98}99100public static void main(String[] args) throws Exception {101setup();102103show(rwFile);104testFile(rwFile, true, bytes.length);105rwFile.delete();106if (rwFile.exists()) {107fail(rwFile, "could not delete");108}109110show(roFile);111testFile(roFile, false, 0);112113show(thisDir);114if (!thisDir.exists()) fail(thisDir, "does not exist");115if (thisDir.isFile()) fail(thisDir, "is a file");116if (!thisDir.isDirectory()) fail(thisDir, "is not a directory");117if (!thisDir.canRead()) fail(thisDir, "is readable");118if (!thisDir.canWrite()) fail(thisDir, "is writeable");119String[] fs = thisDir.list();120if (fs == null) fail(thisDir, "list() returned null");121out.print(" [" + fs.length + "]");122for (int i = 0; i < fs.length; i++) {123out.print(" " + fs[i]);124}125out.println();126if (fs.length == 0) fail(thisDir, "is empty");127128if (!dir.mkdir() || !dir.exists() || !dir.isDirectory()) {129fail(dir, "could not create");130}131if (!dir.renameTo(dir2)) {132fail(dir, "failed to rename");133}134if (dir.exists() || !dir2.exists() || !dir2.isDirectory()) {135fail(dir, "not renamed");136}137138System.err.println("NOTE: Large files not supported on this system");139}140141}142143144