测试删除指定目录下的文件和文件夹

来源:互联网 发布:91邀请码淘宝怎么搜 编辑:程序博客网 时间:2024/05/27 20:44

1.被测试类

package com.ebuair.junit;import java.io.File;/** * 删除指定目录下的所有文件 * @author Ebuair * */public class DeleteAllFile {public void deleteAllFile(File file){DeleteAllFile deleteAllFile = new DeleteAllFile();if(file.isFile() && file.exists()){file.delete();}if(file.isDirectory()){File[] childrenFiles = file.listFiles();for(File file2 : childrenFiles){deleteAllFile.deleteAllFile(file2);file2.delete();}}}}
2.测试类

package com.ebuair.junit;import java.io.File;import java.io.IOException;import junit.framework.Assert;import junit.framework.TestCase;/** * 对指定目录下的文件或目录进行删除测试 */public class TestDeleteAllFile extends TestCase{private DeleteAllFile deleteAllFile = null;@Overridepublic void setUp() throws Exception {deleteAllFile = new DeleteAllFile();}public void testDeleteAllFile(){File rootDirectory = new File("rootDrirectory");try {rootDirectory.mkdir();File file1 = new File( rootDirectory,"file1.txt");File dirrectory = new File(rootDirectory,"subdirectory");File subFile2 = new File(dirrectory,"file2.txt");file1.createNewFile();dirrectory.mkdir();subFile2.createNewFile();deleteAllFile.deleteAllFile(rootDirectory);} catch (IOException e) {Assert.fail();}Assert.assertNotNull(rootDirectory);Assert.assertEquals(0, rootDirectory.length());String[] nameStrings = rootDirectory.list();Assert.assertEquals(0, nameStrings.length);rootDirectory.delete();}/** * 对指定目录下的文件进行删除,该目录下只有一个文件的情况的测试 */public void testDeleteAllFile1(){File file = null;try {file = new File("test.tx");deleteAllFile.deleteAllFile(file);} catch (Exception e) {Assert.fail();}boolean isExist = file.exists();Assert.assertEquals(false, isExist);Assert.assertFalse(isExist);}}



0 0
原创粉丝点击