< prev index next >
test/jdk/java/io/File/GetXSpace.java
Print this page
rev 59964 : imported patch 6501010
*** 45,122 ****
public class GetXSpace {
private static SecurityManager [] sma = { null, new Allow(), new DenyFSA(),
new DenyRead() };
! private static final String name = System.getProperty("os.name");
! private static final String dfFormat;
! static {
! if (name.equals("Linux") || name.contains("OS X")) {
// FileSystem Total Used Available Use% MountedOn
! dfFormat = "([^\\s]+)\\s+(\\d+)\\s+\\d+\\s+(\\d+)\\s+\\d+%\\s+([^\\s]+)";
! } else if (name.startsWith("Windows")) {
! // Drive (MountedOn) Available/Total
! dfFormat = "([^\\s]+)\\s+\\(([^\\s]+)\\)\\s+(\\d+)\\/(\\d+)\\s+";
! } else {
! throw new RuntimeException("unrecognized system:"
! + " os.name == " + name);
! }
! }
! private static Pattern dfPattern = Pattern.compile(dfFormat);
private static int fail = 0;
private static int pass = 0;
private static Throwable first;
static void pass() {
pass++;
}
static void fail(String p) {
- if (first == null)
setFirst(p);
System.err.format("FAILED: %s%n", p);
fail++;
}
static void fail(String p, long exp, String cmp, long got) {
String s = String.format("'%s': %d %s %d", p, exp, cmp, got);
- if (first == null)
setFirst(s);
System.err.format("FAILED: %s%n", s);
fail++;
}
private static void fail(String p, Class ex) {
String s = String.format("'%s': expected %s - FAILED%n", p, ex.getName());
- if (first == null)
setFirst(s);
System.err.format("FAILED: %s%n", s);
fail++;
}
private static void setFirst(String s) {
! try {
! throw new RuntimeException(s);
! } catch (RuntimeException x) {
! first = x;
}
}
private static class Space {
private static final long KSIZE = 1024;
! private String name;
! private long total;
! private long free;
Space(String total, String free, String name) {
try {
this.total = Long.valueOf(total) * KSIZE;
this.free = Long.valueOf(free) * KSIZE;
} catch (NumberFormatException x) {
! // the regex should have caught this
! assert false;
}
this.name = name;
}
String name() { return name; }
--- 45,104 ----
public class GetXSpace {
private static SecurityManager [] sma = { null, new Allow(), new DenyFSA(),
new DenyRead() };
! private static final String osName = System.getProperty("os.name");
// FileSystem Total Used Available Use% MountedOn
! private static final Pattern dfPattern = Pattern.compile("([^\\s]+)\\s+(\\d+)\\s+\\d+\\s+(\\d+)\\s+\\d+%\\s+([^\\s].*)\n");
private static int fail = 0;
private static int pass = 0;
private static Throwable first;
static void pass() {
pass++;
}
static void fail(String p) {
setFirst(p);
System.err.format("FAILED: %s%n", p);
fail++;
}
static void fail(String p, long exp, String cmp, long got) {
String s = String.format("'%s': %d %s %d", p, exp, cmp, got);
setFirst(s);
System.err.format("FAILED: %s%n", s);
fail++;
}
private static void fail(String p, Class ex) {
String s = String.format("'%s': expected %s - FAILED%n", p, ex.getName());
setFirst(s);
System.err.format("FAILED: %s%n", s);
fail++;
}
private static void setFirst(String s) {
! if (first == null) {
! first = new RuntimeException(s);
}
}
private static class Space {
private static final long KSIZE = 1024;
! private final String name;
! private final long total;
! private final long free;
Space(String total, String free, String name) {
try {
this.total = Long.valueOf(total) * KSIZE;
this.free = Long.valueOf(free) * KSIZE;
} catch (NumberFormatException x) {
! throw new RuntimeException("the regex should have caught this", x);
}
this.name = name;
}
String name() { return name; }
*** 128,167 ****
public String toString() {
return String.format("%s (%d/%d)", name, free, total);
}
}
! private static ArrayList space(String f) throws IOException {
! ArrayList al = new ArrayList();
- Process p = null;
String cmd = "df -k -P" + (f == null ? "" : " " + f);
! p = Runtime.getRuntime().exec(cmd);
! BufferedReader in = new BufferedReader
! (new InputStreamReader(p.getInputStream()));
String s;
int i = 0;
- StringBuilder sb = new StringBuilder();
while ((s = in.readLine()) != null) {
// skip header
! if (i++ == 0 && !name.startsWith("Windows")) continue;
sb.append(s).append("\n");
}
Matcher m = dfPattern.matcher(sb);
int j = 0;
while (j < sb.length()) {
if (m.find(j)) {
- if (!name.startsWith("Windows")) {
// swap can change while this test is running
if (!m.group(1).equals("swap")) {
! String name = (f == null ? m.group(4): f);
! al.add(new Space(m.group(2), m.group(3), name));;
}
! } else {
! String name = (f == null ? m.group(2) : f);
! al.add(new Space(m.group(4), m.group(3), name ));;
}
j = m.end() + 1;
} else {
throw new RuntimeException("unrecognized df output format: "
+ "charAt(" + j + ") = '"
--- 110,148 ----
public String toString() {
return String.format("%s (%d/%d)", name, free, total);
}
}
! private static ArrayList<Space> space(String f) throws IOException {
! ArrayList<Space> al = new ArrayList<>();
String cmd = "df -k -P" + (f == null ? "" : " " + f);
! StringBuilder sb = new StringBuilder();
! Process p = Runtime.getRuntime().exec(cmd);
! try (BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
String s;
int i = 0;
while ((s = in.readLine()) != null) {
// skip header
! if (i++ == 0) continue;
sb.append(s).append("\n");
}
+ }
+ out.println(sb);
Matcher m = dfPattern.matcher(sb);
int j = 0;
while (j < sb.length()) {
if (m.find(j)) {
// swap can change while this test is running
if (!m.group(1).equals("swap")) {
! String name = f;
! if (name == null) {
! // cygwin's df lists windows path as FileSystem (1st group)
! name = osName.startsWith("Windows") ? m.group(1) : m.group(4);
}
! al.add(new Space(m.group(2), m.group(3), name));;
}
j = m.end() + 1;
} else {
throw new RuntimeException("unrecognized df output format: "
+ "charAt(" + j + ") = '"
*** 172,182 ****
if (al.size() == 0) {
// df did not produce output
String name = (f == null ? "" : f);
al.add(new Space("0", "0", name));
}
- in.close();
return al;
}
private static void tryCatch(Space s) {
out.format("%s:%n", s.name());
--- 153,162 ----
*** 218,283 ****
String fmt = " %-4s total= %12d free = %12d usable = %12d%n";
out.format(fmt, "df", s.total(), 0, s.free());
out.format(fmt, "getX", ts, fs, us);
// if the file system can dynamically change size, this check will fail
! if (ts != s.total())
fail(s.name(), s.total(), "!=", ts);
! else
pass();
// unix df returns statvfs.f_bavail
! long tsp = (!name.startsWith("Windows") ? us : fs);
! if (!s.woomFree(tsp))
fail(s.name(), s.free(), "??", tsp);
! else
pass();
! if (fs > s.total())
fail(s.name(), s.total(), ">", fs);
! else
pass();
! if (us > s.total())
fail(s.name(), s.total(), ">", us);
! else
pass();
}
private static String FILE_PREFIX = "/getSpace.";
private static void compareZeroNonExist() {
File f;
while (true) {
f = new File(FILE_PREFIX + Math.random());
! if (f.exists())
continue;
break;
}
long [] s = { f.getTotalSpace(), f.getFreeSpace(), f.getUsableSpace() };
for (int i = 0; i < s.length; i++) {
! if (s[i] != 0L)
fail(f.getName(), s[i], "!=", 0L);
! else
pass();
}
}
private static void compareZeroExist() {
try {
File f = File.createTempFile("tmp", null, new File("."));
long [] s = { f.getTotalSpace(), f.getFreeSpace(), f.getUsableSpace() };
for (int i = 0; i < s.length; i++) {
! if (s[i] == 0L)
fail(f.getName(), s[i], "==", 0L);
! else
pass();
}
} catch (IOException x) {
fail("Couldn't create temp file for test");
}
}
private static class Allow extends SecurityManager {
--- 198,271 ----
String fmt = " %-4s total= %12d free = %12d usable = %12d%n";
out.format(fmt, "df", s.total(), 0, s.free());
out.format(fmt, "getX", ts, fs, us);
// if the file system can dynamically change size, this check will fail
! if (ts != s.total()) {
fail(s.name(), s.total(), "!=", ts);
! } else {
pass();
+ }
// unix df returns statvfs.f_bavail
! long tsp = (!osName.startsWith("Windows") ? us : fs);
! if (!s.woomFree(tsp)) {
fail(s.name(), s.free(), "??", tsp);
! } else {
pass();
+ }
! if (fs > s.total()) {
fail(s.name(), s.total(), ">", fs);
! } else {
pass();
+ }
! if (us > s.total()) {
fail(s.name(), s.total(), ">", us);
! } else {
pass();
}
+ }
private static String FILE_PREFIX = "/getSpace.";
private static void compareZeroNonExist() {
File f;
while (true) {
f = new File(FILE_PREFIX + Math.random());
! if (f.exists()) {
continue;
+ }
break;
}
long [] s = { f.getTotalSpace(), f.getFreeSpace(), f.getUsableSpace() };
for (int i = 0; i < s.length; i++) {
! if (s[i] != 0L) {
fail(f.getName(), s[i], "!=", 0L);
! } else {
pass();
}
}
+ }
private static void compareZeroExist() {
try {
File f = File.createTempFile("tmp", null, new File("."));
long [] s = { f.getTotalSpace(), f.getFreeSpace(), f.getUsableSpace() };
for (int i = 0; i < s.length; i++) {
! if (s[i] == 0L) {
fail(f.getName(), s[i], "==", 0L);
! } else {
pass();
}
+ }
} catch (IOException x) {
+ x.printStackTrace();
fail("Couldn't create temp file for test");
}
}
private static class Allow extends SecurityManager {
*** 326,349 ****
}
}
private static void testFile(String dirName) {
out.format("--- Testing %s%n", dirName);
! ArrayList l;
try {
l = space(dirName);
} catch (IOException x) {
throw new RuntimeException(dirName + " can't get file system information", x);
}
! compare((GetXSpace.Space) l.get(0));
}
private static void testDF() {
! out.format("--- Testing df");
// Find all of the partitions on the machine and verify that the size
// returned by "df" is equivalent to File.getXSpace() values.
! ArrayList l;
try {
l = space(null);
} catch (IOException x) {
throw new RuntimeException("can't get file system information", x);
}
--- 314,337 ----
}
}
private static void testFile(String dirName) {
out.format("--- Testing %s%n", dirName);
! ArrayList<Space> l;
try {
l = space(dirName);
} catch (IOException x) {
throw new RuntimeException(dirName + " can't get file system information", x);
}
! compare(l.get(0));
}
private static void testDF() {
! out.println("--- Testing df");
// Find all of the partitions on the machine and verify that the size
// returned by "df" is equivalent to File.getXSpace() values.
! ArrayList<Space> l;
try {
l = space(null);
} catch (IOException x) {
throw new RuntimeException("can't get file system information", x);
}
*** 357,368 ****
throw new RuntimeException("Test configuration error "
+ " - can't set security manager");
out.format("%nSecurityManager = %s%n" ,
(sm == null ? "null" : sm.getClass().getName()));
! for (int j = 0; j < l.size(); j++) {
! Space s = (GetXSpace.Space) l.get(j);
if (sm instanceof Deny) {
tryCatch(s);
} else {
compare(s);
compareZeroNonExist();
--- 345,355 ----
throw new RuntimeException("Test configuration error "
+ " - can't set security manager");
out.format("%nSecurityManager = %s%n" ,
(sm == null ? "null" : sm.getClass().getName()));
! for (var s : l) {
if (sm instanceof Deny) {
tryCatch(s);
} else {
compare(s);
compareZeroNonExist();
*** 377,388 ****
testFile(args[0]);
} else {
testDF();
}
! if (fail != 0)
throw new RuntimeException((fail + pass) + " tests: "
+ fail + " failure(s), first", first);
! else
out.format("all %d tests passed%n", fail + pass);
}
}
--- 364,376 ----
testFile(args[0]);
} else {
testDF();
}
! if (fail != 0) {
throw new RuntimeException((fail + pass) + " tests: "
+ fail + " failure(s), first", first);
! } else {
out.format("all %d tests passed%n", fail + pass);
}
+ }
}
< prev index next >