Created
August 21, 2013 03:31
-
-
Save tomoyamkung/6290060 to your computer and use it in GitHub Desktop.
[Java]SizeFileFilter のサンプルテストケース。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.tomoyamkung.org.apache.commons.io.filefilter; | |
import static org.hamcrest.CoreMatchers.*; | |
import static org.junit.Assert.*; | |
import java.io.File; | |
import org.apache.commons.io.filefilter.SizeFileFilter; | |
import org.junit.Before; | |
import org.junit.Test; | |
/** | |
* SizeFileFilter のサンプルテストケース。 | |
* | |
* 次のパターンを確認したテストケース。 | |
* | |
* <ul> | |
* <li>フィルタした結果に閾値と同じサイズのファイルが含まれるのか</li> | |
* <li>閾値未満のファイルをフィルタできるのか</li> | |
* <li>サブディレクトリがあった場合の動作確認</li> | |
* </ul> | |
* | |
* @author tomoyamkung | |
* | |
*/ | |
public class SizeFileFilterTest { | |
/** フィルタリングするファイルのサイズ(単位:バイト) */ | |
private int size; | |
@Before | |
public void setUp() throws Exception { | |
size = 1024; | |
} | |
/** | |
* 閾値と同じサイズのファイルが実行結果に含まれるかを確認する。 | |
* | |
* ディレクトリに次のファイルがあった場合、、、 | |
* | |
* <ul> | |
* <li>subdir_size1023.txt → ファイルサイズが 1023 バイトのファイル</li> | |
* <li>subdir_size1024.txt → ファイルサイズが 1024 バイトのファイル</li> | |
* <li>subdir_size1025.txt → ファイルサイズが 1025 バイトのファイル</li> | |
* </ul> | |
* | |
* "subdir_size1024.txt" も実行結果に含まれるのかを確認する。 | |
* | |
* @throws Exception | |
*/ | |
@Test | |
public void 閾値の動作確認() throws Exception { | |
// Setup | |
File subdir = new File("testdata/org_apache_commons_io_filefilter/size_file_filter_test/subdir"); | |
// Exercise | |
String[] actual = subdir.list(new SizeFileFilter(size)); | |
// Verify | |
assertThat(String.format("%d Byte 以上のファイルは2つである", size), actual.length, is(2)); | |
// → size1023.txt は結果に含まれていない | |
// → size1024.txt が結果に含まれている | |
assertThat(actual[0], is("subdir_size1024.txt")); | |
assertThat(actual[1], is("subdir_size1025.txt")); | |
} | |
/** | |
* 閾値未満のファイルもピックアップできることを確認する。 | |
* | |
* ディレクトリに次のファイルがあった場合、、、 | |
* | |
* <ul> | |
* <li>subdir_size1023.txt → ファイルサイズが 1023 バイトのファイル</li> | |
* <li>subdir_size1024.txt → ファイルサイズが 1024 バイトのファイル</li> | |
* <li>subdir_size1025.txt → ファイルサイズが 1025 バイトのファイル</li> | |
* </ul> | |
* | |
* "subdir_size1024.txt" が実行結果に含まれないのかを確認する。 | |
* | |
* @throws Exception | |
*/ | |
@Test | |
public void 閾値未満のファイルをピックアップする() throws Exception { | |
// Setup | |
File subdir = new File("testdata/org_apache_commons_io_filefilter/size_file_filter_test/subdir"); | |
// Exercise | |
String[] actual = subdir.list(new SizeFileFilter(size, false)); | |
// SizeFileFilter の第2引数に false を指定すると、第1引数に指定したサイズ未満のファイルをピックアップする | |
// Verify | |
assertThat(String.format("%d Byte のファイルは含まれていないこと", size), actual.length, is(1)); | |
assertThat(actual[0], is("subdir_size1023.txt")); | |
// → size1024.txt が結果に含まれていない | |
} | |
/** | |
* サブディレクトリがある場合の実行結果がどのようになるのかを確認する。 | |
* | |
* カレントディレクトリが次のようになっていた場合の動作確認。 | |
* | |
* <ul> | |
* <li>size1023.txt</li> | |
* <li>size1024.txt</li> | |
* <li>size1025.txt</li> | |
* <li>subdir</li> | |
* <ul> | |
* <li>subdir_size1023.txt</li> | |
* <li>subdir_size1024.txt</li> | |
* <li>subdir_size1025.txt</li> | |
* </ul> | |
* </ul> | |
* | |
* @throws Exception | |
*/ | |
@Test | |
public void サブディレクトリのファイルも検査するのか確認する() throws Exception { | |
// Setup | |
File dir = new File("testdata/org_apache_commons_io_filefilter/size_file_filter_test"); | |
// Exercise | |
String[] actual = dir.list(new SizeFileFilter(size)); | |
// Verify | |
assertThat(String.format("サブディレクトリ自体もピックアップされること"), actual.length, is(3)); | |
assertThat(actual[0], is("size1024.txt")); | |
assertThat(actual[1], is("size1025.txt")); | |
assertThat(actual[2], is("subdir")); | |
// サブディレクトリにある個々のファイルをピックアップするのではなく、ディレクトリ自体がピックアップされる | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
テストデータは次のものを用意。
# ll -R .: 合計 16 -rw-r--r-- 1 tomoyamkung 1023 yyyy/MM/dd hh:mm:ss size1023.txt -rw-r--r-- 1 tomoyamkung 1024 yyyy/MM/dd hh:mm:ss size1024.txt -rw-r--r-- 1 tomoyamkung 1025 yyyy/MM/dd hh:mm:ss size1025.txt drwxr-xr-x+ 1 tomoyamkung 0 yyyy/MM/dd hh:mm:ss subdir ./subdir: 合計 12 -rw-r--r-- 1 tomoyamkung 1023 yyyy/MM/dd hh:mm:ss subdir_size1023.txt -rw-r--r-- 1 tomoyamkung 1024 yyyy/MM/dd hh:mm:ss subdir_size1024.txt -rw-r--r-- 1 tomoyamkung 1025 yyyy/MM/dd hh:mm:ss subdir_size1025.txt