package be.nikiroo.fanfix.test;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FilenameFilter;
-import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.HashMap;
import java.util.List;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipInputStream;
+import java.util.Map;
+import java.util.Map.Entry;
import be.nikiroo.fanfix.Instance;
import be.nikiroo.fanfix.Main;
import be.nikiroo.fanfix.output.BasicOutput;
import be.nikiroo.utils.IOUtils;
-import be.nikiroo.utils.TempFiles;
import be.nikiroo.utils.TraceHandler;
import be.nikiroo.utils.test.TestCase;
import be.nikiroo.utils.test.TestLauncher;
class ConversionTest extends TestLauncher {
- private TempFiles tempFiles;
private File testFile;
private File expectedDir;
private File resultDir;
+ private List<BasicOutput.OutputType> realTypes;
+ private Map<String, List<String>> skipCompare;
public ConversionTest(String[] args) {
super("Conversion", args);
+ // Special mode SYSOUT is not a file type (System.out)
+ realTypes = new ArrayList<BasicOutput.OutputType>();
+ for (BasicOutput.OutputType type : BasicOutput.OutputType.values()) {
+ if (!BasicOutput.OutputType.SYSOUT.equals(type)) {
+ realTypes.add(type);
+ }
+ }
+
addTest(new TestCase("Read the test file") {
@Override
public void test() throws Exception {
}
});
- for (BasicOutput.OutputType type : BasicOutput.OutputType.values()) {
- // NOT for special mode SYSOUT
- if (!BasicOutput.OutputType.SYSOUT.equals(type)) {
- addTest(getTestFor(type));
- }
+ for (BasicOutput.OutputType type : realTypes) {
+ addTest(getTestFor(type));
}
}
expectedDir = new File("test/expected/");
resultDir = new File("test/result/");
- tempFiles = new TempFiles("Fanfix-ConversionTest");
+ skipCompare = new HashMap<String, List<String>>();
+ skipCompare.put("epb.ncx",
+ Arrays.asList(" <meta name=\"dtb:uid\" content="));
+ skipCompare.put("epb.opf", Arrays.asList(" <dc:subject>",
+ " <dc:identifier id=\"BookId\" opf:scheme=\"URI\">"));
+ skipCompare.put(".info",
+ Arrays.asList("CREATION_DATE=", "SUBJECT=", "URL=", "UUID="));
+ skipCompare.put("URL", Arrays.asList("file:/"));
}
@Override
protected void stop() throws Exception {
- tempFiles.close();
}
private TestCase getTestFor(final BasicOutput.OutputType type) {
+ type.getDefaultExtension(false));
// Check conversion:
- compareFiles(this, expectedDir, resultDir, type);
+ compareFiles(this, expectedDir, resultDir, type, "Generate "
+ + type);
+
+ // LATEX not supported as input
+ if (BasicOutput.OutputType.LATEX.equals(type)) {
+ return;
+ }
// Cross-checks:
- for (BasicOutput.OutputType type : BasicOutput.OutputType
- .values()) {
- // NOT for special mode SYSOUT
- if (!BasicOutput.OutputType.SYSOUT.equals(type)) {
- File crossDir = tempFiles.createTempDir("cross-result");
- generate(this, target, crossDir, type);
- compareFiles(this, crossDir, resultDir, type);
- }
+ for (BasicOutput.OutputType crossType : realTypes) {
+ File crossDir = Test.tempFiles
+ .createTempDir("cross-result");
+ generate(this, target, crossDir, crossType);
+ compareFiles(this, resultDir, crossDir, crossType,
+ "Cross compare " + crossType + " generated from "
+ + type);
}
}
};
}
private void compareFiles(TestCase testCase, File expectedDir,
- File resultDir, final BasicOutput.OutputType typeToCompare)
- throws Exception {
-
+ File resultDir, final BasicOutput.OutputType limitTiFiles,
+ final String errMess) throws Exception {
FilenameFilter filter = null;
- if (typeToCompare != null) {
+ if (limitTiFiles != null) {
filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
- return name.startsWith(typeToCompare.toString());
+ return name.toLowerCase().startsWith(
+ limitTiFiles.toString().toLowerCase());
}
};
}
- List<String> resultFiles = Arrays.asList(resultDir.list(filter));
- resultFiles.sort(null);
- List<String> expectedFiles = Arrays.asList(expectedDir.list(filter));
- expectedFiles.sort(null);
+ List<String> resultFiles;
+ List<String> expectedFiles;
+ {
+ String[] resultArr = resultDir.list(filter);
+ Arrays.sort(resultArr);
+ resultFiles = Arrays.asList(resultArr);
+ String[] expectedArr = expectedDir.list(filter);
+ Arrays.sort(expectedArr);
+ expectedFiles = Arrays.asList(expectedArr);
+ }
- testCase.assertEquals("The resulting file names are not expected",
- expectedFiles, resultFiles);
+ testCase.assertEquals(errMess, expectedFiles, resultFiles);
for (int i = 0; i < resultFiles.size(); i++) {
File expected = new File(expectedDir, expectedFiles.get(i));
File result = new File(resultDir, resultFiles.get(i));
- testCase.assertEquals(
- "Type mismatch: expected a "
- + (expected.isDirectory() ? "directory" : "file")
- + ", received a "
- + (result.isDirectory() ? "directory" : "file"),
+ testCase.assertEquals(errMess + ": type mismatch: expected a "
+ + (expected.isDirectory() ? "directory" : "file")
+ + ", received a "
+ + (result.isDirectory() ? "directory" : "file"),
expected.isDirectory(), result.isDirectory());
if (expected.isDirectory()) {
- compareFiles(testCase, expected, result, null);
+ compareFiles(testCase, expected, result, null, errMess);
continue;
}
if (expected.getName().endsWith(".cbz")
|| expected.getName().endsWith(".epub")) {
- File tmpExpected = tempFiles.createTempDir(expected.getName()
- + "[zip-content]");
- File tmpResult = tempFiles.createTempDir(result.getName()
+ File tmpExpected = Test.tempFiles.createTempDir(expected
+ .getName() + "[zip-content]");
+ File tmpResult = Test.tempFiles.createTempDir(result.getName()
+ "[zip-content]");
- unzip(expected, tmpExpected);
- unzip(result, tmpResult);
- compareFiles(testCase, tmpExpected, tmpResult, null);
+ IOUtils.unzip(expected, tmpExpected);
+ IOUtils.unzip(result, tmpResult);
+ compareFiles(testCase, tmpExpected, tmpResult, null, errMess);
} else {
List<String> expectedLines = Arrays.asList(IOUtils
.readSmallFile(expected).split("\n"));
+ name.substring(expectedDir.getAbsolutePath()
.length());
}
+
+ testCase.assertEquals(errMess + ": " + name
+ + ": the number of lines is not the same",
+ expectedLines.size(), resultLines.size());
+
for (int j = 0; j < expectedLines.size(); j++) {
String expectedLine = expectedLines.get(j);
String resultLine = resultLines.get(j);
- if (name.endsWith(".info")
- && expectedLine.startsWith("CREATION_DATE=")
- && resultLine.startsWith("CREATION_DATE=")) {
- // TODO: check the format?
+
+ boolean skip = false;
+ for (Entry<String, List<String>> skipThose : skipCompare
+ .entrySet()) {
+ for (String skipStart : skipThose.getValue()) {
+ if (name.endsWith(skipThose.getKey())
+ && expectedLine.startsWith(skipStart)
+ && resultLine.startsWith(skipStart)) {
+ skip = true;
+ }
+ }
+ }
+
+ if (skip) {
continue;
}
- testCase.assertEquals("Line " + (j + 1)
+
+ testCase.assertEquals(errMess + ": line " + (j + 1)
+ " is not the same in file " + name, expectedLine,
resultLine);
}
}
}
}
-
- private static void unzip(File zipFile, File targetDirectory)
- throws IOException {
- if (targetDirectory.exists() && targetDirectory.isFile()) {
- throw new IOException("Cannot unzip " + zipFile + " into "
- + targetDirectory + ": it is not a directory");
- }
-
- targetDirectory.mkdir();
- if (!targetDirectory.exists()) {
- throw new IOException("Cannot create target directory "
- + targetDirectory);
- }
-
- FileInputStream in = new FileInputStream(zipFile);
- try {
- ZipInputStream zipStream = new ZipInputStream(in);
- try {
- for (ZipEntry entry = zipStream.getNextEntry(); entry != null; entry = zipStream
- .getNextEntry()) {
- File file = new File(targetDirectory, entry.getName());
- if (entry.isDirectory()) {
- file.mkdirs();
- } else {
- IOUtils.write(zipStream, file);
- }
- }
- } finally {
- zipStream.close();
- }
- } finally {
- in.close();
- }
- }
}