package net.exoego.queen.projection;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import net.exoego.function.Func1;
import net.exoego.function.Func2;
import net.exoego.function.helper.OnFile;
import net.exoego.queen.Seq;
import net.exoego.util.Pair;
import org.junit.rules.TemporaryFolder;
import static net.exoego.queen.Sequence.singleton;
import static net.exoego.util.Make.array;
class TraverseTestBase {
static final File temp;
static {
final TemporaryFolder tempDir = createDir();
final File root;
try {
root = tempDir.newFolder("root");
} catch (IOException e) {
throw new RuntimeException(e);
}
createFiles(root, array("usr", "bin", "sys"));
final File usr = root.listFiles(OnFile.filenameMatches("usr"))[0];
createFiles(usr, "Alice", "Bob", "Carol");
final File bin = root.listFiles(OnFile.filenameMatches("bin"))[0];
createFiles(bin, "ruby", "python");
createTextFile(usr.listFiles(OnFile.filenameMatches("Bob"))[0], "Project");
createTextFile(bin.listFiles(OnFile.filenameMatches("ruby"))[0], "Rails");
temp = tempDir.getRoot();
}
private static void createTextFile(final File dir, final String... fileNames) {
for (final String fileName : fileNames) {
try {
final File textFile = File.createTempFile(fileName, ".txt");
final FileWriter writer = new FileWriter(textFile);
writer.append("not empty");
writer.close();
textFile.renameTo(new File(toFolderString(dir) + fileName + ".txt"));
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
}
}
private static String toFolderString(final File dir) {return dir.getPath() + System.getProperty("file.separator");}
private static void createFiles(final File dir, final String... fileNames) {
for (final String filename : fileNames) {
final File f = new File(toFolderString(dir) + filename);
if (!f.mkdir()) {
throw new IllegalStateException("Cant create dir");
}
}
}
private static TemporaryFolder createDir() {
final TemporaryFolder dir = new TemporaryFolder();
try {
dir.create();
} catch (IOException e) {
throw new IllegalStateException(e);
}
return dir;
}
static final Func1<Integer, Seq<Integer>> NULL_MAPPER = null;
static final Func1<Integer, Seq<Integer>> SUM_WITH_DEPTH = arg1 -> singleton(arg1 + arg1);
static final Func1<Integer, Seq<Integer>> NULL_RETURNER = arg1 -> {
if (arg1 % 2 == 0) {
return null;
}
return singleton(arg1 + arg1);
};
static final Func2<Integer, Integer, Pair<Integer, Integer>> TO_DEPTH = ((Func2<Integer, Integer, Pair<Integer, Integer>>) Pair::pair)
.flip();
}