📄 filesystemutilstestcase.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.io;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import junit.framework.Test;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import org.apache.commons.io.testtools.FileBasedTestCase;
/**
* This is used to test FileSystemUtils.
*
* @version $Id: FileSystemUtilsTestCase.java 606380 2007-12-22 01:58:46Z ggregory $
*/
public class FileSystemUtilsTestCase extends FileBasedTestCase {
public static void main(String[] args) {
TestRunner.run(suite());
// try {
// System.out.println(FileSystemUtils.freeSpace("C:\\"));
// } catch (IOException ex) {
// ex.printStackTrace();
// }
}
public static Test suite() {
return new TestSuite(FileSystemUtilsTestCase.class);
}
public FileSystemUtilsTestCase(String name) throws IOException {
super(name);
}
protected void setUp() throws Exception {
}
protected void tearDown() throws Exception {
}
//-----------------------------------------------------------------------
public void testGetFreeSpace_String() throws Exception {
// test coverage, as we can't check value
if (File.separatorChar == '/') {
// have to figure out unix block size
String[] cmd = null;
String osName = System.getProperty("os.name");
if (osName.indexOf("hp-ux") >= 0 || osName.indexOf("aix") >= 0) {
cmd = new String[] {"df", "-P", "/"};
} else {
cmd = new String[] {"df", "/"};
}
Process proc = Runtime.getRuntime().exec(cmd);
boolean kilobyteBlock = true;
BufferedReader r = null;
try {
r = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = r.readLine();
if (line.toLowerCase().indexOf("512") >= 0) {
kilobyteBlock = false;
}
} finally {
IOUtils.closeQuietly(r);
}
// now perform the test
long free = FileSystemUtils.freeSpace("/");
long kb = FileSystemUtils.freeSpaceKb("/");
if (kilobyteBlock) {
assertEquals(free, kb, 256d);
} else {
assertEquals(free / 2d, kb, 256d);
}
} else {
long bytes = FileSystemUtils.freeSpace("");
long kb = FileSystemUtils.freeSpaceKb("");
assertEquals((double) bytes / 1024, kb, 256d);
}
}
//-----------------------------------------------------------------------
public void testGetFreeSpaceOS_String_NullPath() throws Exception {
FileSystemUtils fsu = new FileSystemUtils();
try {
fsu.freeSpaceOS(null, 1, false);
fail();
} catch (IllegalArgumentException ex) {}
try {
fsu.freeSpaceOS(null, 1, true);
fail();
} catch (IllegalArgumentException ex) {}
}
public void testGetFreeSpaceOS_String_InitError() throws Exception {
FileSystemUtils fsu = new FileSystemUtils();
try {
fsu.freeSpaceOS("", -1, false);
fail();
} catch (IllegalStateException ex) {}
try {
fsu.freeSpaceOS("", -1, true);
fail();
} catch (IllegalStateException ex) {}
}
public void testGetFreeSpaceOS_String_Other() throws Exception {
FileSystemUtils fsu = new FileSystemUtils();
try {
fsu.freeSpaceOS("", 0, false);
fail();
} catch (IllegalStateException ex) {}
try {
fsu.freeSpaceOS("", 0, true);
fail();
} catch (IllegalStateException ex) {}
}
public void testGetFreeSpaceOS_String_Windows() throws Exception {
FileSystemUtils fsu = new FileSystemUtils() {
protected long freeSpaceWindows(String path) throws IOException {
return 12345L;
}
};
assertEquals(12345L, fsu.freeSpaceOS("", 1, false));
assertEquals(12345L / 1024, fsu.freeSpaceOS("", 1, true));
}
public void testGetFreeSpaceOS_String_Unix() throws Exception {
FileSystemUtils fsu = new FileSystemUtils() {
protected long freeSpaceUnix(String path, boolean kb, boolean posix) throws IOException {
return (kb ? 12345L : 54321);
}
};
assertEquals(54321L, fsu.freeSpaceOS("", 2, false));
assertEquals(12345L, fsu.freeSpaceOS("", 2, true));
}
//-----------------------------------------------------------------------
public void testGetFreeSpaceWindows_String_ParseCommaFormatBytes() throws Exception {
// this is the format of response when calling dir /c
// we have now switched to dir /-c, so we should never get this
String lines =
" Volume in drive C is HDD\n" +
" Volume Serial Number is XXXX-YYYY\n" +
"\n" +
" Directory of C:\\Documents and Settings\\Xxxx\n" +
"\n" +
"19/08/2005 22:43 <DIR> .\n" +
"19/08/2005 22:43 <DIR> ..\n" +
"11/08/2005 01:07 81 build.properties\n" +
"17/08/2005 21:44 <DIR> Desktop\n" +
" 7 File(s) 180,260 bytes\n" +
" 10 Dir(s) 41,411,551,232 bytes free";
FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
assertEquals(41411551232L, fsu.freeSpaceWindows(""));
}
//-----------------------------------------------------------------------
public void testGetFreeSpaceWindows_String_EmptyPath() throws Exception {
String lines =
" Volume in drive C is HDD\n" +
" Volume Serial Number is XXXX-YYYY\n" +
"\n" +
" Directory of C:\\Documents and Settings\\Xxxx\n" +
"\n" +
"19/08/2005 22:43 <DIR> .\n" +
"19/08/2005 22:43 <DIR> ..\n" +
"11/08/2005 01:07 81 build.properties\n" +
"17/08/2005 21:44 <DIR> Desktop\n" +
" 7 File(s) 180260 bytes\n" +
" 10 Dir(s) 41411551232 bytes free";
FileSystemUtils fsu = new MockFileSystemUtils(0, lines, "dir /-c ");
assertEquals(41411551232L, fsu.freeSpaceWindows(""));
}
public void testGetFreeSpaceWindows_String_NormalResponse() throws Exception {
String lines =
" Volume in drive C is HDD\n" +
" Volume Serial Number is XXXX-YYYY\n" +
"\n" +
" Directory of C:\\Documents and Settings\\Xxxx\n" +
"\n" +
"19/08/2005 22:43 <DIR> .\n" +
"19/08/2005 22:43 <DIR> ..\n" +
"11/08/2005 01:07 81 build.properties\n" +
"17/08/2005 21:44 <DIR> Desktop\n" +
" 7 File(s) 180260 bytes\n" +
" 10 Dir(s) 41411551232 bytes free";
FileSystemUtils fsu = new MockFileSystemUtils(0, lines, "dir /-c C:");
assertEquals(41411551232L, fsu.freeSpaceWindows("C:"));
}
public void testGetFreeSpaceWindows_String_StripDrive() throws Exception {
String lines =
" Volume in drive C is HDD\n" +
" Volume Serial Number is XXXX-YYYY\n" +
"\n" +
" Directory of C:\\Documents and Settings\\Xxxx\n" +
"\n" +
"19/08/2005 22:43 <DIR> .\n" +
"19/08/2005 22:43 <DIR> ..\n" +
"11/08/2005 01:07 81 build.properties\n" +
"17/08/2005 21:44 <DIR> Desktop\n" +
" 7 File(s) 180260 bytes\n" +
" 10 Dir(s) 41411551232 bytes free";
FileSystemUtils fsu = new MockFileSystemUtils(0, lines, "dir /-c C:");
assertEquals(41411551232L, fsu.freeSpaceWindows("C:\\somedir"));
}
public void testGetFreeSpaceWindows_String_EmptyResponse() throws Exception {
String lines = "";
FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
try {
fsu.freeSpaceWindows("C:");
fail();
} catch (IOException ex) {}
}
public void testGetFreeSpaceWindows_String_EmptyMultiLineResponse() throws Exception {
String lines = "\n\n";
FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
try {
fsu.freeSpaceWindows("C:");
fail();
} catch (IOException ex) {}
}
public void testGetFreeSpaceWindows_String_InvalidTextResponse() throws Exception {
String lines = "BlueScreenOfDeath";
FileSystemUtils fsu = new MockFileSystemUtils(0, lines);
try {
fsu.freeSpaceWindows("C:");
fail();
} catch (IOException ex) {}
}
public void testGetFreeSpaceWindows_String_NoSuchDirectoryResponse() throws Exception {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -