📄 filesystemutilstestcase.java
字号:
String lines =
" Volume in drive C is HDD\n" +
" Volume Serial Number is XXXX-YYYY\n" +
"\n" +
" Directory of C:\\Documents and Settings\\empty" +
"\n";
FileSystemUtils fsu = new MockFileSystemUtils(1, lines);
try {
fsu.freeSpaceWindows("C:");
fail();
} catch (IOException ex) {}
}
//-----------------------------------------------------------------------
public void testGetFreeSpaceUnix_String_EmptyPath() throws Exception {
String lines =
"Filesystem 1K-blocks Used Available Use% Mounted on\n" +
"xxx:/home/users/s 14428928 12956424 1472504 90% /home/users/s";
FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
try {
fsu.freeSpaceUnix("", false, false);
fail();
} catch (IllegalArgumentException ex) {}
try {
fsu.freeSpaceUnix("", true, false);
fail();
} catch (IllegalArgumentException ex) {}
try {
fsu.freeSpaceUnix("", true, true);
fail();
} catch (IllegalArgumentException ex) {}
try {
fsu.freeSpaceUnix("", false, true);
fail();
} catch (IllegalArgumentException ex) {}
}
public void testGetFreeSpaceUnix_String_NormalResponseLinux() throws Exception {
// from Sourceforge 'GNU bash, version 2.05b.0(1)-release (i386-redhat-linux-gnu)'
String lines =
"Filesystem 1K-blocks Used Available Use% Mounted on\n" +
"/dev/xxx 497944 308528 189416 62% /";
FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
assertEquals(189416L, fsu.freeSpaceUnix("/", false, false));
}
public void testGetFreeSpaceUnix_String_NormalResponseFreeBSD() throws Exception {
// from Apache 'FreeBSD 6.1-RELEASE (SMP-turbo)'
String lines =
"Filesystem 1K-blocks Used Avail Capacity Mounted on\n" +
"/dev/xxxxxx 128990 102902 15770 87% /";
FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
assertEquals(15770L, fsu.freeSpaceUnix("/", false, false));
}
//-----------------------------------------------------------------------
public void testGetFreeSpaceUnix_String_NormalResponseKbLinux() throws Exception {
// from Sourceforge 'GNU bash, version 2.05b.0(1)-release (i386-redhat-linux-gnu)'
// df, df -k and df -kP are all identical
String lines =
"Filesystem 1K-blocks Used Available Use% Mounted on\n" +
"/dev/xxx 497944 308528 189416 62% /";
FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
assertEquals(189416L, fsu.freeSpaceUnix("/", true, false));
}
public void testGetFreeSpaceUnix_String_NormalResponseKbFreeBSD() throws Exception {
// from Apache 'FreeBSD 6.1-RELEASE (SMP-turbo)'
// df and df -k are identical, but df -kP uses 512 blocks (not relevant as not used)
String lines =
"Filesystem 1K-blocks Used Avail Capacity Mounted on\n" +
"/dev/xxxxxx 128990 102902 15770 87% /";
FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
assertEquals(15770L, fsu.freeSpaceUnix("/", true, false));
}
public void testGetFreeSpaceUnix_String_NormalResponseKbSolaris() throws Exception {
// from IO-91 - ' SunOS et 5.10 Generic_118822-25 sun4u sparc SUNW,Ultra-4'
// non-kb response does not contain free space - see IO-91
String lines =
"Filesystem kbytes used avail capacity Mounted on\n" +
"/dev/dsk/x0x0x0x0 1350955 815754 481163 63%";
FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
assertEquals(481163L, fsu.freeSpaceUnix("/dev/dsk/x0x0x0x0", true, false));
}
public void testGetFreeSpaceUnix_String_LongResponse() throws Exception {
String lines =
"Filesystem 1K-blocks Used Available Use% Mounted on\n" +
"xxx-yyyyyyy-zzz:/home/users/s\n" +
" 14428928 12956424 1472504 90% /home/users/s";
FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s", false, false));
}
public void testGetFreeSpaceUnix_String_LongResponseKb() throws Exception {
String lines =
"Filesystem 1K-blocks Used Available Use% Mounted on\n" +
"xxx-yyyyyyy-zzz:/home/users/s\n" +
" 14428928 12956424 1472504 90% /home/users/s";
FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
assertEquals(1472504L, fsu.freeSpaceUnix("/home/users/s", true, false));
}
public void testGetFreeSpaceUnix_String_EmptyResponse() throws Exception {
String lines = "";
FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
try {
fsu.freeSpaceUnix("/home/users/s", false, false);
fail();
} catch (IOException ex) {}
try {
fsu.freeSpaceUnix("/home/users/s", true, false);
fail();
} catch (IOException ex) {}
try {
fsu.freeSpaceUnix("/home/users/s", false, true);
fail();
} catch (IOException ex) {}
try {
fsu.freeSpaceUnix("/home/users/s", true, true);
fail();
} catch (IOException ex) {}
}
public void testGetFreeSpaceUnix_String_InvalidResponse1() throws Exception {
String lines =
"Filesystem 1K-blocks Used Available Use% Mounted on\n" +
" 14428928 12956424 100";
FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
try {
fsu.freeSpaceUnix("/home/users/s", false, false);
fail();
} catch (IOException ex) {}
try {
fsu.freeSpaceUnix("/home/users/s", true, false);
fail();
} catch (IOException ex) {}
try {
fsu.freeSpaceUnix("/home/users/s", false, true);
fail();
} catch (IOException ex) {}
try {
fsu.freeSpaceUnix("/home/users/s", true, true);
fail();
} catch (IOException ex) {}
}
public void testGetFreeSpaceUnix_String_InvalidResponse2() throws Exception {
String lines =
"Filesystem 1K-blocks Used Available Use% Mounted on\n" +
"xxx:/home/users/s 14428928 12956424 nnnnnnn 90% /home/users/s";
FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
try {
fsu.freeSpaceUnix("/home/users/s", false, false);
fail();
} catch (IOException ex) {}
try {
fsu.freeSpaceUnix("/home/users/s", true, false);
fail();
} catch (IOException ex) {}
try {
fsu.freeSpaceUnix("/home/users/s", false, true);
fail();
} catch (IOException ex) {}
try {
fsu.freeSpaceUnix("/home/users/s", true, true);
fail();
} catch (IOException ex) {}
}
public void testGetFreeSpaceUnix_String_InvalidResponse3() throws Exception {
String lines =
"Filesystem 1K-blocks Used Available Use% Mounted on\n" +
"xxx:/home/users/s 14428928 12956424 -1 90% /home/users/s";
FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
try {
fsu.freeSpaceUnix("/home/users/s", false, false);
fail();
} catch (IOException ex) {}
try {
fsu.freeSpaceUnix("/home/users/s", true, false);
fail();
} catch (IOException ex) {}
try {
fsu.freeSpaceUnix("/home/users/s", false, true);
fail();
} catch (IOException ex) {}
try {
fsu.freeSpaceUnix("/home/users/s", true, true);
fail();
} catch (IOException ex) {}
}
public void testGetFreeSpaceUnix_String_InvalidResponse4() throws Exception {
String lines =
"Filesystem 1K-blocks Used Available Use% Mounted on\n" +
"xxx-yyyyyyy-zzz:/home/users/s";
FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
try {
fsu.freeSpaceUnix("/home/users/s", false, false);
fail();
} catch (IOException ex) {}
try {
fsu.freeSpaceUnix("/home/users/s", true, false);
fail();
} catch (IOException ex) {}
try {
fsu.freeSpaceUnix("/home/users/s", false, true);
fail();
} catch (IOException ex) {}
try {
fsu.freeSpaceUnix("/home/users/s", true, true);
fail();
} catch (IOException ex) {}
}
//-----------------------------------------------------------------------
static class MockFileSystemUtils extends FileSystemUtils {
private final int exitCode;
private final byte[] bytes;
private final String cmd;
public MockFileSystemUtils(int exitCode, String lines) {
this(exitCode, lines, null);
}
public MockFileSystemUtils(int exitCode, String lines, String cmd) {
this.exitCode = exitCode;
this.bytes = lines.getBytes();
this.cmd = cmd;
}
Process openProcess(String[] params) {
if (cmd != null) {
assertEquals(cmd, params[params.length - 1]);
}
return new Process() {
public InputStream getErrorStream() {
return null;
}
public InputStream getInputStream() {
return new ByteArrayInputStream(bytes);
}
public OutputStream getOutputStream() {
return null;
}
public int waitFor() throws InterruptedException {
return exitCode;
}
public int exitValue() {
return exitCode;
}
public void destroy() {
}
};
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -