}\r
\r
try {\r
- DeleteDir.listup(new File(args[0]));\r
+ DeleteDir.delete(new File(args[0]));\r
}\r
catch(Exception e) {\r
e.printStackTrace();\r
}\r
}\r
\r
- public static void listup(File file) throws IOException {\r
+ public static void delete(File file) throws IOException {\r
if (!file.exists()) {\r
System.out.println("ERROR: ファイルまたはディレクトリが見つかりませんでした。");\r
- return;\r
+ throw new IOException("File not found.");\r
}\r
\r
if (file.isDirectory()) {\r
File files[] = file.listFiles();\r
if (files != null) {\r
for (int i=0; i < files.length; i++) {\r
- listup(files[i]); // 再帰呼び出し\r
+ delete(files[i]); // 再帰呼び出し\r
}\r
}\r
}\r
package hayashi.tools.files;
import java.io.File;
+import java.io.IOException;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.*;
import org.junit.Test;
-import junit.framework.TestCase;
+public class DeleteDirTest {
-public class DeleteDirTest extends TestCase {
-
- @Test(expected = Exception.class)
+ @Test(expected = IOException.class)
public void 指定したファイルがないとき() throws Exception {
- DeleteDir.listup(new File("testspace", "FOLDER"));
+ DeleteDir.delete(new File("testspace", "FOLDER"));
+ fail("例外が発生しない"); // 例外が発生しなければ失敗
+ }
+
+ @Test
+ public void 削除対象がファイルのとき() throws IOException {
+ File newfile = new File("testspace", "FILE");
+ newfile.createNewFile();
+ DeleteDir.delete(new File("testspace", "FILE"));
+ assertThat(newfile.exists(), is(false));
}
}