Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/File/Basic.java
38821 views
/*1* Copyright (c) 1998, 2012, 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 filesystem2627@build Basic Util28@run shell basic.sh29*/3031import 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 nonExistantFile = new File("x.Basic.non");42static File rwFile = new File("x.Basic.rw");43static File bigFile = new File("x.Basic.big");44static File roFile = new File("x.Basic.ro");45static File thisDir = new File(".");46static File dir = new File("x.Basic.dir");47static File nonDir = new File("x.Basic.nonDir");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");77int rwLen = 6;78if (f.length() != length) fail(f, "has wrong length");79}8081static void fail(File f, String why) throws Exception {82throw new Exception(f + " " + why);83}8485public static void main(String[] args) throws Exception {8687show(nonExistantFile);88if (nonExistantFile.exists()) fail(nonExistantFile, "exists");8990show(rwFile);91testFile(rwFile, true, 6);92rwFile.delete();93if (rwFile.exists())94fail(rwFile, "could not delete");9596show(roFile);97testFile(roFile, false, 0);9899show(thisDir);100if (!thisDir.exists()) fail(thisDir, "does not exist");101if (thisDir.isFile()) fail(thisDir, "is a file");102if (!thisDir.isDirectory()) fail(thisDir, "is not a directory");103if (!thisDir.canRead()) fail(thisDir, "is readable");104if (!thisDir.canWrite()) fail(thisDir, "is writeable");105String[] fs = thisDir.list();106if (fs == null) fail(thisDir, "list() returned null");107out.print(" [" + fs.length + "]");108for (int i = 0; i < fs.length; i++)109out.print(" " + fs[i]);110out.println();111if (fs.length == 0) fail(thisDir, "is empty");112113if (!nonExistantFile.createNewFile())114fail(nonExistantFile, "could not create");115nonExistantFile.deleteOnExit();116117if (!nonDir.mkdir())118fail(nonDir, "could not create");119120if (!dir.renameTo(new File("x.Basic.dir2")))121fail(dir, "failed to rename");122123if (System.getProperty("os.name").equals("SunOS")124&& System.getProperty("os.version").compareTo("5.6") >= 0) {125if (bigFile.exists()) {126bigFile.delete();127if (bigFile.exists())128fail(bigFile, "could not delete");129}130RandomAccessFile raf = new RandomAccessFile(bigFile, "rw");131long big = ((long)Integer.MAX_VALUE) * 2;132try {133raf.seek(big);134raf.write('x');135show(bigFile);136testFile(bigFile, true, big + 1);137} finally {138raf.close();139}140bigFile.delete();141if (bigFile.exists())142fail(bigFile, "could not delete");143} else {144System.err.println("NOTE: Large files not supported on this system");145}146147}148149}150151152