< prev index next >
test/java/io/File/Basic.java
Print this page
@@ -1,7 +1,7 @@
/*
- * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
@@ -20,44 +20,46 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
- @bug 4165666 4203706 4288670 4290024
- @summary Basic heartbeat test for File methods that access the filesystem
-
- @build Basic Util
- @run shell basic.sh
+ * @bug 4165666 4203706 4288670 4290024
+ * @summary Basic heartbeat test for File methods that access the filesystem
+ * @build Basic Util NonExistentDriver
+ * @run driver NonExistentDriver
+ * @run main/othervm Basic
*/
+import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;
import java.io.PrintStream;
import java.io.RandomAccessFile;
public class Basic {
static PrintStream out = System.err;
- static File nonExistantFile = new File("x.Basic.non");
static File rwFile = new File("x.Basic.rw");
static File bigFile = new File("x.Basic.big");
static File roFile = new File("x.Basic.ro");
static File thisDir = new File(".");
static File dir = new File("x.Basic.dir");
+ static File dir2 = new File("x.Basic.dir2");
static File nonDir = new File("x.Basic.nonDir");
+ static byte bytes[] = new byte[] {1, 2, 3, 4, 5, 6};
static void showBoolean(String what, boolean value) {
out.println(" " + what + ": " + value);
}
static void showLong(String what, long value) {
out.println(" " + what + ": " + value);
}
- static void show(File f) throws Exception {
+ public static void show(File f) throws Exception {
out.println(f + ": ");
showBoolean("exists", f.exists());
showBoolean("isFile", f.isFile());
showBoolean("isDirectory", f.isDirectory());
showBoolean("canRead", f.canRead());
@@ -73,28 +75,45 @@
if (!f.isFile()) fail(f, "is not a file");
if (f.isDirectory()) fail(f, "is a directory");
if (!f.canRead()) fail(f, "is not readable");
if (!Util.isPrivileged() && f.canWrite() != writeable)
fail(f, writeable ? "is not writeable" : "is writeable");
- int rwLen = 6;
if (f.length() != length) fail(f, "has wrong length");
}
- static void fail(File f, String why) throws Exception {
+ public static void fail(File f, String why) throws Exception {
throw new Exception(f + " " + why);
}
- public static void main(String[] args) throws Exception {
+ static void setup() throws Exception {
+ rwFile.delete();
+ bigFile.delete();
+ roFile.delete();
+ thisDir.delete();
+ dir.delete();
+ dir2.delete();
+ nonDir.delete();
- show(nonExistantFile);
- if (nonExistantFile.exists()) fail(nonExistantFile, "exists");
+ try (FileOutputStream fos = new FileOutputStream(rwFile)) {
+ fos.write(bytes);
+ }
+
+ roFile.createNewFile();
+ roFile.setReadOnly();
+
+ dir.mkdir();
+ }
+
+ public static void main(String[] args) throws Exception {
+ setup();
show(rwFile);
- testFile(rwFile, true, 6);
+ testFile(rwFile, true, bytes.length);
rwFile.delete();
- if (rwFile.exists())
+ if (rwFile.exists()) {
fail(rwFile, "could not delete");
+ }
show(roFile);
testFile(roFile, false, 0);
show(thisDir);
@@ -104,24 +123,33 @@
if (!thisDir.canRead()) fail(thisDir, "is readable");
if (!thisDir.canWrite()) fail(thisDir, "is writeable");
String[] fs = thisDir.list();
if (fs == null) fail(thisDir, "list() returned null");
out.print(" [" + fs.length + "]");
- for (int i = 0; i < fs.length; i++)
+ for (int i = 0; i < fs.length; i++) {
out.print(" " + fs[i]);
+ }
out.println();
if (fs.length == 0) fail(thisDir, "is empty");
- if (!nonExistantFile.createNewFile())
- fail(nonExistantFile, "could not create");
- nonExistantFile.deleteOnExit();
+ if (NonExistentDriver.nonExistentFile.exists()) {
+ fail(NonExistentDriver.nonExistentFile, "not deleted");
+ }
- if (!nonDir.mkdir())
+ if (!nonDir.mkdir()) {
fail(nonDir, "could not create");
+ }
+ if (!nonDir.exists() || !nonDir.isDirectory()) {
+ fail(nonDir, "not created");
+ }
- if (!dir.renameTo(new File("x.Basic.dir2")))
+ if (!dir.renameTo(dir2)) {
fail(dir, "failed to rename");
+ }
+ if (dir.exists() || !dir2.exists() || !dir2.isDirectory()) {
+ fail(dir, "not renamed");
+ }
if (System.getProperty("os.name").equals("SunOS")
&& System.getProperty("os.version").compareTo("5.6") >= 0) {
if (bigFile.exists()) {
bigFile.delete();
< prev index next >