hayashi lib.
java
Revisión | dbf0e4051300274061c60e73fefe1e2f1120e317 (tree) |
---|---|
Tiempo | 2017-05-18 19:56:38 |
Autor | ![]() |
Commiter | hayashi |
testcase: DeleteDirTest
@@ -22,7 +22,7 @@ public class DeleteDir | ||
22 | 22 | } |
23 | 23 | |
24 | 24 | try { |
25 | - DeleteDir.listup(new File(args[0])); | |
25 | + DeleteDir.delete(new File(args[0])); | |
26 | 26 | } |
27 | 27 | catch(Exception e) { |
28 | 28 | e.printStackTrace(); |
@@ -30,17 +30,17 @@ public class DeleteDir | ||
30 | 30 | } |
31 | 31 | } |
32 | 32 | |
33 | - public static void listup(File file) throws IOException { | |
33 | + public static void delete(File file) throws IOException { | |
34 | 34 | if (!file.exists()) { |
35 | 35 | System.out.println("ERROR: ファイルまたはディレクトリが見つかりませんでした。"); |
36 | - return; | |
36 | + throw new IOException("File not found."); | |
37 | 37 | } |
38 | 38 | |
39 | 39 | if (file.isDirectory()) { |
40 | 40 | File files[] = file.listFiles(); |
41 | 41 | if (files != null) { |
42 | 42 | for (int i=0; i < files.length; i++) { |
43 | - listup(files[i]); // 再帰呼び出し | |
43 | + delete(files[i]); // 再帰呼び出し | |
44 | 44 | } |
45 | 45 | } |
46 | 46 | } |
@@ -1,15 +1,25 @@ | ||
1 | 1 | package hayashi.tools.files; |
2 | 2 | |
3 | 3 | import java.io.File; |
4 | +import java.io.IOException; | |
4 | 5 | |
6 | +import static org.hamcrest.CoreMatchers.is; | |
7 | +import static org.junit.Assert.*; | |
5 | 8 | import org.junit.Test; |
6 | 9 | |
7 | -import junit.framework.TestCase; | |
10 | +public class DeleteDirTest { | |
8 | 11 | |
9 | -public class DeleteDirTest extends TestCase { | |
10 | - | |
11 | - @Test(expected = Exception.class) | |
12 | + @Test(expected = IOException.class) | |
12 | 13 | public void 指定したファイルがないとき() throws Exception { |
13 | - DeleteDir.listup(new File("testspace", "FOLDER")); | |
14 | + DeleteDir.delete(new File("testspace", "FOLDER")); | |
15 | + fail("例外が発生しない"); // 例外が発生しなければ失敗 | |
16 | + } | |
17 | + | |
18 | + @Test | |
19 | + public void 削除対象がファイルのとき() throws IOException { | |
20 | + File newfile = new File("testspace", "FILE"); | |
21 | + newfile.createNewFile(); | |
22 | + DeleteDir.delete(new File("testspace", "FILE")); | |
23 | + assertThat(newfile.exists(), is(false)); | |
14 | 24 | } |
15 | 25 | } |