📄 fileutilstestcase.java
字号:
}
Collection files = FileUtils.listFiles(subDir,
new WildcardFileFilter("*.*"),
new WildcardFileFilter("*"));
int count = files.size();
Object[] fileObjs = files.toArray();
assertEquals(files.size(), fileNames.length);
Map foundFileNames = new HashMap();
for (int i = 0; i < count; ++i) {
boolean found = false;
for(int j = 0; (( !found ) && (j < fileNames.length)); ++j) {
if ( fileNames[j].equals(((File) fileObjs[i]).getName())) {
foundFileNames.put(fileNames[j], fileNames[j]);
found = true;
}
}
}
assertEquals(foundFileNames.size(), fileNames.length);
subDir.delete();
}
public void testIterateFiles() throws Exception {
File srcDir = getTestDirectory();
File subDir = new File(srcDir, "list_test" );
subDir.mkdir();
String[] fileNames = {"a.txt", "b.txt", "c.txt", "d.txt", "e.txt", "f.txt"};
int[] fileSizes = {123, 234, 345, 456, 678, 789};
for (int i = 0; i < fileNames.length; ++i) {
File theFile = new File(subDir, fileNames[i]);
createFile(theFile, fileSizes[i]);
}
Iterator files = FileUtils.iterateFiles(subDir,
new WildcardFileFilter("*.*"),
new WildcardFileFilter("*"));
Map foundFileNames = new HashMap();
while (files.hasNext()) {
boolean found = false;
String fileName = ((File) files.next()).getName();
for (int j = 0; (( !found ) && (j < fileNames.length)); ++j) {
if ( fileNames[j].equals(fileName)) {
foundFileNames.put(fileNames[j], fileNames[j]);
found = true;
}
}
}
assertEquals(foundFileNames.size(), fileNames.length);
subDir.delete();
}
public void testReadFileToString() throws Exception {
File file = new File(getTestDirectory(), "read.obj");
FileOutputStream out = new FileOutputStream(file);
byte[] text = "Hello /u1234".getBytes("UTF8");
out.write(text);
out.close();
String data = FileUtils.readFileToString(file, "UTF8");
assertEquals("Hello /u1234", data);
}
public void testReadFileToByteArray() throws Exception {
File file = new File(getTestDirectory(), "read.txt");
FileOutputStream out = new FileOutputStream(file);
out.write(11);
out.write(21);
out.write(31);
out.close();
byte[] data = FileUtils.readFileToByteArray(file);
assertEquals(3, data.length);
assertEquals(11, data[0]);
assertEquals(21, data[1]);
assertEquals(31, data[2]);
}
public void testReadLines() throws Exception {
File file = newFile("lines.txt");
try {
String[] data = new String[] {"hello", "/u1234", "", "this is", "some text"};
createLineBasedFile(file, data);
List lines = FileUtils.readLines(file, "UTF-8");
assertEquals(Arrays.asList(data), lines);
} finally {
deleteFile(file);
}
}
public void testWriteStringToFile1() throws Exception {
File file = new File(getTestDirectory(), "write.txt");
FileUtils.writeStringToFile(file, "Hello /u1234", "UTF8");
byte[] text = "Hello /u1234".getBytes("UTF8");
assertEqualContent(text, file);
}
public void testWriteStringToFile2() throws Exception {
File file = new File(getTestDirectory(), "write.txt");
FileUtils.writeStringToFile(file, "Hello /u1234", null);
byte[] text = "Hello /u1234".getBytes();
assertEqualContent(text, file);
}
public void testWriteByteArrayToFile() throws Exception {
File file = new File(getTestDirectory(), "write.obj");
byte[] data = new byte[] {11, 21, 31};
FileUtils.writeByteArrayToFile(file, data);
assertEqualContent(data, file);
}
public void testWriteLines_4arg() throws Exception {
Object[] data = new Object[] {
"hello", new StringBuffer("world"), "", "this is", null, "some text"};
List list = Arrays.asList(data);
File file = newFile("lines.txt");
FileUtils.writeLines(file, "US-ASCII", list, "*");
String expected = "hello*world**this is**some text*";
String actual = FileUtils.readFileToString(file, "US-ASCII");
assertEquals(expected, actual);
}
public void testWriteLines_4arg_Writer_nullData() throws Exception {
File file = newFile("lines.txt");
FileUtils.writeLines(file, "US-ASCII", (List) null, "*");
assertEquals("Sizes differ", 0, file.length());
}
public void testWriteLines_4arg_nullSeparator() throws Exception {
Object[] data = new Object[] {
"hello", new StringBuffer("world"), "", "this is", null, "some text"};
List list = Arrays.asList(data);
File file = newFile("lines.txt");
FileUtils.writeLines(file, "US-ASCII", list, null);
String expected = "hello" + IOUtils.LINE_SEPARATOR + "world" + IOUtils.LINE_SEPARATOR +
IOUtils.LINE_SEPARATOR + "this is" + IOUtils.LINE_SEPARATOR +
IOUtils.LINE_SEPARATOR + "some text" + IOUtils.LINE_SEPARATOR;
String actual = FileUtils.readFileToString(file, "US-ASCII");
assertEquals(expected, actual);
}
public void testWriteLines_3arg_nullSeparator() throws Exception {
Object[] data = new Object[] {
"hello", new StringBuffer("world"), "", "this is", null, "some text"};
List list = Arrays.asList(data);
File file = newFile("lines.txt");
FileUtils.writeLines(file, "US-ASCII", list);
String expected = "hello" + IOUtils.LINE_SEPARATOR + "world" + IOUtils.LINE_SEPARATOR +
IOUtils.LINE_SEPARATOR + "this is" + IOUtils.LINE_SEPARATOR +
IOUtils.LINE_SEPARATOR + "some text" + IOUtils.LINE_SEPARATOR;
String actual = FileUtils.readFileToString(file, "US-ASCII");
assertEquals(expected, actual);
}
//-----------------------------------------------------------------------
public void testChecksumCRC32() throws Exception {
// create a test file
String text = "Imagination is more important than knowledge - Einstein";
File file = new File(getTestDirectory(), "checksum-test.txt");
FileUtils.writeStringToFile(file, text, "US-ASCII");
// compute the expected checksum
Checksum expectedChecksum = new CRC32();
expectedChecksum.update(text.getBytes("US-ASCII"), 0, text.length());
long expectedValue = expectedChecksum.getValue();
// compute the checksum of the file
long resultValue = FileUtils.checksumCRC32(file);
assertEquals(expectedValue, resultValue);
}
public void testChecksum() throws Exception {
// create a test file
String text = "Imagination is more important than knowledge - Einstein";
File file = new File(getTestDirectory(), "checksum-test.txt");
FileUtils.writeStringToFile(file, text, "US-ASCII");
// compute the expected checksum
Checksum expectedChecksum = new CRC32();
expectedChecksum.update(text.getBytes("US-ASCII"), 0, text.length());
long expectedValue = expectedChecksum.getValue();
// compute the checksum of the file
Checksum testChecksum = new CRC32();
Checksum resultChecksum = FileUtils.checksum(file, testChecksum);
long resultValue = resultChecksum.getValue();
assertSame(testChecksum, resultChecksum);
assertEquals(expectedValue, resultValue);
}
public void testChecksumOnNullFile() throws Exception {
try {
FileUtils.checksum((File) null, new CRC32());
fail();
} catch (NullPointerException ex) {
// expected
}
}
public void testChecksumOnNullChecksum() throws Exception {
// create a test file
String text = "Imagination is more important than knowledge - Einstein";
File file = new File(getTestDirectory(), "checksum-test.txt");
FileUtils.writeStringToFile(file, text, "US-ASCII");
try {
FileUtils.checksum(file, (Checksum) null);
fail();
} catch (NullPointerException ex) {
// expected
}
}
public void testChecksumOnDirectory() throws Exception {
try {
FileUtils.checksum(new File("."), new CRC32());
fail();
} catch (IllegalArgumentException ex) {
// expected
}
}
public void testChecksumDouble() throws Exception {
// create a test file
String text1 = "Imagination is more important than knowledge - Einstein";
File file1 = new File(getTestDirectory(), "checksum-test.txt");
FileUtils.writeStringToFile(file1, text1, "US-ASCII");
// create a second test file
String text2 = "To be or not to be - Shakespeare";
File file2 = new File(getTestDirectory(), "checksum-test2.txt");
FileUtils.writeStringToFile(file2, text2, "US-ASCII");
// compute the expected checksum
Checksum expectedChecksum = new CRC32();
expectedChecksum.update(text1.getBytes("US-ASCII"), 0, text1.length());
expectedChecksum.update(text2.getBytes("US-ASCII"), 0, text2.length());
long expectedValue = expectedChecksum.getValue();
// compute the checksum of the file
Checksum testChecksum = new CRC32();
FileUtils.checksum(file1, testChecksum);
FileUtils.checksum(file2, testChecksum);
long resultValue = testChecksum.getValue();
assertEquals(expectedValue, resultValue);
}
public void testDeleteQuietlyForNull() {
try {
FileUtils.deleteQuietly(null);
} catch (Exception ex) {
fail(ex.getMessage());
}
}
public void testDeleteQuietlyDir() throws IOException {
File testDirectory = new File(getTestDirectory(), "testDeleteQuietlyDir");
File testFile= new File(testDirectory, "testDeleteQuietlyFile");
testDirectory.mkdirs();
createFile(testFile, 0);
assertTrue(testDirectory.exists());
assertTrue(testFile.exists());
FileUtils.deleteQuietly(testDirectory);
assertFalse("Check No Exist", testDirectory.exists());
assertFalse("Check No Exist", testFile.exists());
}
public void testDeleteQuietlyFile() throws IOException {
File testFile= new File(getTestDirectory(), "testDeleteQuietlyFile");
createFile(testFile, 0);
assertTrue(testFile.exists());
FileUtils.deleteQuietly(testFile);
assertFalse("Check No Exist", testFile.exists());
}
public void testDeleteQuietlyNonExistent() {
File testFile = new File("testDeleteQuietlyNonExistent");
assertFalse(testFile.exists());
try {
FileUtils.deleteQuietly(testFile);
} catch (Exception ex) {
fail(ex.getMessage());
}
}
public void testMoveFile_Rename() throws Exception {
File destination = new File( getTestDirectory(), "move1.txt" );
FileUtils.moveFile( testFile1, destination );
assertTrue( "Check Exist", destination.exists() );
assertTrue( "Original deleted", ! testFile1.exists() );
}
public void testMoveFile_CopyDelete() throws Exception {
File destination = new File( getTestDirectory(), "move2.txt" );
File src = new File( testFile1.getAbsolutePath() ) {
// Force renameTo to fail, as if destination is on another
// filesystem
public boolean renameTo( File f ) {
return false;
}
};
FileUtils.moveFile( src, destination );
assertTrue( "Check Exist", destination.exists() );
assertTrue( "Original deleted", ! src.exists() );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -