Instance: use getInstance()
[nikiroo-utils.git] / src / be / nikiroo / fanfix / test / ConversionTest.java
index 4fe1653d4a81f08965dc8e5f4b54b5b0fb7c4e83..d730b3b5efecc85f8a1b581c0ae16dc719e66187 100644 (file)
@@ -1,37 +1,37 @@
 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.Map;
 import java.util.Map.Entry;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipInputStream;
 
 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 String testUri;
+       private String expectedDir;
+       private String resultDir;
        private List<BasicOutput.OutputType> realTypes;
        private Map<String, List<String>> skipCompare;
+       private Map<String, List<String>> skipCompareCross;
 
-       public ConversionTest(String[] args) {
-               super("Conversion", args);
+       public ConversionTest(String testName, final String testUri,
+                       final String expectedDir, final String resultDir, String[] args) {
+               super("Conversion - " + testName, args);
+
+               this.testUri = testUri;
+               this.expectedDir = expectedDir;
+               this.resultDir = resultDir;
 
                // Special mode SYSOUT is not a file type (System.out)
                realTypes = new ArrayList<BasicOutput.OutputType>();
@@ -41,23 +41,28 @@ class ConversionTest extends TestLauncher {
                        }
                }
 
-               addTest(new TestCase("Read the test file") {
-                       @Override
-                       public void test() throws Exception {
-                               assertEquals("The test file \"" + testFile
-                                               + "\" cannot be found", true, testFile.exists());
-                       }
-               });
+               if (!testUri.startsWith("http://") && !testUri.startsWith("https://")) {
+                       addTest(new TestCase("Read the test file") {
+                               @Override
+                               public void test() throws Exception {
+                                       assertEquals("The test file \"" + testUri
+                                                       + "\" cannot be found", true,
+                                                       new File(testUri).exists());
+                               }
+                       });
+               }
 
                addTest(new TestCase("Assure directories exist") {
                        @Override
                        public void test() throws Exception {
-                               expectedDir.mkdirs();
-                               resultDir.mkdirs();
+                               new File(expectedDir).mkdirs();
+                               new File(resultDir).mkdirs();
                                assertEquals("The Expected directory \"" + expectedDir
-                                               + "\" cannot be created", true, expectedDir.exists());
+                                               + "\" cannot be created", true,
+                                               new File(expectedDir).exists());
                                assertEquals("The Result directory \"" + resultDir
-                                               + "\" cannot be created", true, resultDir.exists());
+                                               + "\" cannot be created", true,
+                                               new File(resultDir).exists());
                        }
                });
 
@@ -68,38 +73,46 @@ class ConversionTest extends TestLauncher {
 
        @Override
        protected void start() throws Exception {
-               testFile = new File("test/test.story");
-               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="));
+               skipCompareCross = new HashMap<String, List<String>>();
+
+               skipCompare.put("epb.ncx", Arrays.asList(
+                               "               <meta name=\"dtb:uid\" content=",
+                               "               <meta name=\"epub-creator\" 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(".info", Arrays.asList("CREATION_DATE=",
+                               "URL=\"file:/", "UUID=EPUBCREATOR=\"", ""));
                skipCompare.put("URL", Arrays.asList("file:/"));
+
+               for (String key : skipCompare.keySet()) {
+                       skipCompareCross.put(key, skipCompare.get(key));
+               }
+
+               skipCompareCross.put(".info", Arrays.asList(""));
+               skipCompareCross.put("epb.opf", Arrays.asList("      <dc:"));
+               skipCompareCross.put("title.xhtml",
+                               Arrays.asList("                 <div class=\"type\">"));
+               skipCompareCross.put("index.html",
+                               Arrays.asList("                 <div class=\"type\">"));
+               skipCompareCross.put("URL", Arrays.asList(""));
        }
 
        @Override
        protected void stop() throws Exception {
-               tempFiles.close();
        }
 
        private TestCase getTestFor(final BasicOutput.OutputType type) {
                return new TestCase(type + " output mode") {
                        @Override
                        public void test() throws Exception {
-                               File target = generate(this, testFile, resultDir, type);
+                               File target = generate(this, testUri, new File(resultDir), type);
                                target = new File(target.getAbsolutePath()
                                                + type.getDefaultExtension(false));
 
                                // Check conversion:
-                               compareFiles(this, expectedDir, resultDir, type, "Generate "
-                                               + type);
+                               compareFiles(this, new File(expectedDir), new File(resultDir),
+                                               type, "Generate " + type);
 
                                // LATEX not supported as input
                                if (BasicOutput.OutputType.LATEX.equals(type)) {
@@ -108,22 +121,25 @@ class ConversionTest extends TestLauncher {
 
                                // Cross-checks:
                                for (BasicOutput.OutputType crossType : realTypes) {
-                                       File crossDir = tempFiles.createTempDir("cross-result");
-                                       generate(this, target, crossDir, crossType);
-                                       compareFiles(this, resultDir, crossDir, crossType,
-                                                       "Cross compare " + crossType + " generated from "
-                                                                       + type);
+                                       File crossDir = Test.tempFiles
+                                                       .createTempDir("cross-result");
+
+                                       generate(this, target.getAbsolutePath(), crossDir,
+                                                       crossType);
+                                       compareFiles(this, new File(resultDir), crossDir,
+                                                       crossType, "Cross compare " + crossType
+                                                                       + " generated from " + type);
                                }
                        }
                };
        }
 
-       private File generate(TestCase testCase, File testFile, File resultDir,
+       private File generate(TestCase testCase, String testUri, File resultDir,
                        BasicOutput.OutputType type) throws Exception {
                final List<String> errors = new ArrayList<String>();
 
-               TraceHandler previousTraceHandler = Instance.getTraceHandler();
-               Instance.setTraceHandler(new TraceHandler(true, true, 0) {
+               TraceHandler previousTraceHandler = Instance.getInstance().getTraceHandler();
+               Instance.getInstance().setTraceHandler(new TraceHandler(true, true, 0) {
                        @Override
                        public void error(String message) {
                                errors.add(message);
@@ -145,8 +161,8 @@ class ConversionTest extends TestLauncher {
 
                try {
                        File target = new File(resultDir, type.toString());
-                       int code = Main.convert(testFile.getAbsolutePath(),
-                                       type.toString(), target.getAbsolutePath(), false, null);
+                       int code = Main.convert(testUri, type.toString(),
+                                       target.getAbsolutePath(), false, null);
 
                        String error = "";
                        for (String err : errors) {
@@ -162,13 +178,17 @@ class ConversionTest extends TestLauncher {
 
                        return target;
                } finally {
-                       Instance.setTraceHandler(previousTraceHandler);
+                       Instance.getInstance().setTraceHandler(previousTraceHandler);
                }
        }
 
        private void compareFiles(TestCase testCase, File expectedDir,
                        File resultDir, final BasicOutput.OutputType limitTiFiles,
                        final String errMess) throws Exception {
+
+               Map<String, List<String>> skipCompare = errMess.startsWith("Cross") ? this.skipCompareCross
+                               : this.skipCompare;
+
                FilenameFilter filter = null;
                if (limitTiFiles != null) {
                        filter = new FilenameFilter() {
@@ -180,10 +200,16 @@ class ConversionTest extends TestLauncher {
                        };
                }
 
-               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(errMess, expectedFiles, resultFiles);
 
@@ -204,12 +230,12 @@ class ConversionTest extends TestLauncher {
 
                        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);
+                               IOUtils.unzip(expected, tmpExpected);
+                               IOUtils.unzip(result, tmpResult);
                                compareFiles(testCase, tmpExpected, tmpResult, null, errMess);
                        } else {
                                List<String> expectedLines = Arrays.asList(IOUtils
@@ -223,6 +249,11 @@ class ConversionTest extends TestLauncher {
                                                        + 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);
@@ -250,39 +281,4 @@ 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()) {
-                       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();
-               }
-       }
 }