package net.exoego.function;
import java.io.File;
import java.io.IOException;
import java.util.regex.Pattern;
import net.exoego.function.helper.OnFile;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
/**
* Created with IntelliJ IDEA.
*/
public class OnFileTest {
private File root;
private File relativeNonExist;
@Before
public void createFiles() throws IOException {
final TemporaryFolder temp = new TemporaryFolder();
temp.create();
root = temp.getRoot();
temp.newFile("hoge.txt");
temp.newFile("photo1.jpg");
temp.newFile("photo2.jpg");
final File sub = temp.newFolder("sub");
final File file1 = temp.newFile("photo3.jpg");
final File file2 = temp.newFile("photo4.jpg");
file1.renameTo(new File(sub.getAbsolutePath() + File.separator + file1.getName()));
file2.renameTo(new File(sub.getAbsolutePath() + File.separator + file2.getName()));
relativeNonExist = new File("./relative" + System.nanoTime() + ".txt");
}
@After
public void removeFiles() {
root.delete();
relativeNonExist.delete();
}
@Test
public void Filename_matches_to_pattern() throws Exception {
final FilePredicate filePredicate = OnFile.filenameMatches(Pattern.compile("jpg"));
assertThat(root.listFiles(filePredicate).length, is(2));
}
@Test(expected = NullPointerException.class)
public void Filename_matches_to_pattern_fail_fast_if_null() throws Exception {
final Pattern jpg = null;
OnFile.filenameMatches(jpg);
}
@Test
public void Filename_matches_to_string() throws Exception {
final FilePredicate filePredicate = OnFile.filenameMatches("jpg");
assertThat(root.listFiles(filePredicate).length, is(2));
}
@Test(expected = NullPointerException.class)
public void Filename_matches_to_string_fail_fast_if_null() throws Exception {
final String jpg = null;
OnFile.filenameMatches(jpg);
}
@Test
public void ListFile() {
assertThat(OnFile.listFiles().apply(root).size(), is(4));
assertThat(OnFile.listFiles().apply(root.listFiles()[0]).size(), is(0));
assertThat(OnFile.listFiles().apply(relativeNonExist).size(), is(0));
}
}