OSDN Git Service

testcase: DeleteDirTest
authorhayashi <hayashi.yuu@gmail.com>
Thu, 18 May 2017 10:56:38 +0000 (19:56 +0900)
committerhayashi <hayashi.yuu@gmail.com>
Thu, 18 May 2017 10:56:38 +0000 (19:56 +0900)
src/hayashi/tools/files/DeleteDir.java
test/hayashi/tools/files/DeleteDirTest.java

index 7ad56b3..29525c4 100644 (file)
@@ -22,7 +22,7 @@ public class DeleteDir
         }\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
@@ -30,17 +30,17 @@ public class DeleteDir
                }\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
index d9e188a..e63abb0 100644 (file)
@@ -1,15 +1,25 @@
 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));
        }
 }