Created
August 21, 2013 01:39
-
-
Save tomoyamkung/6289473 to your computer and use it in GitHub Desktop.
[Java]DirectoryFileFilter のサンプルテストケース
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.DirectoryFileFilter; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class DirectoryFileFilterTest { | |
private File dir; | |
@Before | |
public void setUp() throws Exception { | |
dir = new File("testdata/org_apache_commons_io_filefilter/directory_file_filter_test"); | |
} | |
@Test | |
public void ディレクトリのみを取得する() throws Exception { | |
// Setup | |
// Exercise | |
String[] actual = dir.list(DirectoryFileFilter.DIRECTORY); | |
// Verify | |
assertThat("入れ子のディレクトリはフィルタの対象外である", actual.length, is(2)); | |
assertThat(actual[0], is("dir2")); | |
assertThat(actual[1], is("hoge_dir1")); | |
} | |
@Test | |
public void 継承したクラスでの動作確認() throws Exception { | |
// Setup | |
// Exercise | |
String[] actual = dir.list(new CustomDirectoryFileFilter()); | |
// Verify | |
assertThat("hoge から始まるディレクトリは1つである", actual.length, is(1)); | |
assertThat(actual[0], is("hoge_dir1")); | |
} | |
/** | |
* DirectoryFileFilter を継承したクラス。 | |
* | |
* ディレクトリ名が "hoge" から始まるものをピックアップするクラス。 | |
* | |
* @author tomoyamkung | |
* | |
*/ | |
class CustomDirectoryFileFilter extends DirectoryFileFilter { | |
private static final long serialVersionUID = -8061218320524169863L; | |
/* (non-Javadoc) | |
* @see org.apache.commons.io.filefilter.DirectoryFileFilter#accept(java.io.File) | |
*/ | |
@Override | |
public boolean accept(File file) { | |
return file.getName().startsWith("hoge"); | |
// "hoge" から始まるディレクトリをピックアップ対象とする | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
テストデータとして、次のディレクトリとファイルを用意して検証。
# ls -R .: dir2 fileX.txt hoge_dir1 ./dir2: sub_dir2_1 ./dir2/sub_dir2_1: file2_1.txt ./hoge_dir1: file1.txt