Improve conversion tests
[fanfix.git] / src / be / nikiroo / fanfix / test / ConversionTest.java
index 54d08858fa3b087cdae553f4523518a7fb089592..d932782e29756657ab29117fb0427c693267d145 100644 (file)
@@ -6,7 +6,10 @@ 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.Map;
+import java.util.Map.Entry;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
 
@@ -24,10 +27,20 @@ class ConversionTest extends TestLauncher {
        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 {
@@ -48,11 +61,8 @@ class ConversionTest extends TestLauncher {
                        }
                });
 
-               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));
                }
        }
 
@@ -63,6 +73,15 @@ class ConversionTest extends TestLauncher {
                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
@@ -81,21 +100,17 @@ class ConversionTest extends TestLauncher {
                                // Check conversion:
                                compareFiles(this, expectedDir, resultDir, type, null);
 
-                               // Cross-checks:
-
                                // LATEX not supported as input
-                               if (!BasicOutput.OutputType.LATEX.equals(type)) {
-                                       for (BasicOutput.OutputType crossType : BasicOutput.OutputType
-                                                       .values()) {
-                                               // NOT for special mode SYSOUT
-                                               if (!BasicOutput.OutputType.SYSOUT.equals(crossType)) {
-                                                       File crossDir = tempFiles
-                                                                       .createTempDir("cross-result");
-                                                       generate(this, target, crossDir, crossType);
-                                                       compareFiles(this, crossDir, resultDir, crossType,
-                                                                       crossType);
-                                               }
-                                       }
+                               if (BasicOutput.OutputType.LATEX.equals(type)) {
+                                       return;
+                               }
+
+                               // Cross-checks:
+                               for (BasicOutput.OutputType crossType : realTypes) {
+                                       File crossDir = tempFiles.createTempDir("cross-result");
+                                       generate(this, target, crossDir, crossType);
+                                       compareFiles(this, resultDir, crossDir, crossType,
+                                                       crossType);
                                }
                        }
                };
@@ -213,13 +228,14 @@ class ConversionTest extends TestLauncher {
                                        String resultLine = resultLines.get(j);
 
                                        boolean skip = false;
-                                       for (String skipStart : new String[] { "CREATION_DATE=",
-                                                       "SUBJECT=", "URL=", "UUID=" }) {
-                                               if (name.endsWith(".info")
-                                                               && expectedLine.startsWith(skipStart)
-                                                               && resultLine.startsWith(skipStart)) {
-                                                       // TODO: check the format?
-                                                       skip = true;
+                                       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;
+                                                       }
                                                }
                                        }
 
@@ -235,6 +251,7 @@ class ConversionTest extends TestLauncher {
                }
        }
 
+       // TODO: remove and use IOUtils when updated
        private static void unzip(File zipFile, File targetDirectory)
                        throws IOException {
                if (targetDirectory.exists() && targetDirectory.isFile()) {