Skip to content

Instantly share code, notes, and snippets.

@tomoyamkung
Last active December 21, 2015 00:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomoyamkung/6217432 to your computer and use it in GitHub Desktop.
Save tomoyamkung/6217432 to your computer and use it in GitHub Desktop.
[Java]SuffixFileFilter を使ったファイルフィルタのスニペット。
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.SuffixFileFilter;
import org.junit.Before;
import org.junit.Test;
public class SuffixFileFilterTest {
private File dir;
@Before
public void setUp() throws Exception {
dir = new File("testdata/org_apache_commons_io_filefilter/suffix_file_filter_test");
}
@Test
public void 拡張子がtxtのファイル名を取得する() throws Exception {
// Setup
String suffix = ".txt";
// Exercise
String[] list = dir.list(new SuffixFileFilter(suffix));
// Verify
assertThat(list.length, is(2));
assertThat(list[0], is("hoge1.txt"));
assertThat(list[1], is("hoge2.txt"));
}
@Test
public void 拡張子がjpgとcsvのファイル名を取得する() throws Exception {
// Setup
String[] suffix = {".jpg", ".csv"};
// Exercise
String[] list = dir.list(new SuffixFileFilter(suffix));
// Verify
assertThat(list.length, is(2));
assertThat(list[0], is("piyo.jpg"));
assertThat(list[1], is("var.csv"));
}
}
@tomoyamkung
Copy link
Author

テストデータとして、"testdata/org_apache_commons_io_filefilter/suffix_file_filter_test" ディレクトリを作成し、そこに次のファイルを用意する。

  • hoge1.txt
  • hoge2.txt
  • piyo.jpg
  • var.csv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment