From dabc4b957b52302c39cb6d07c6c91a1f91195d5a Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Tue, 17 Sep 2019 18:23:18 +0200 Subject: [PATCH] open conversion test for external input --- .../nikiroo/fanfix/test/ConversionTest.java | 53 ++++++++------ src/be/nikiroo/fanfix/test/Test.java | 69 ++++++++++++++++-- .../{expected => expected_test.story}/cbz.cbz | Bin .../epub.epub | Bin .../html/html.info | 0 .../html/html.txt | 0 .../html/index.html | 0 .../html/style.css | 0 .../info_text | 0 .../info_text.info | 0 .../latex.tex | 0 .../text.txt | 0 12 files changed, 90 insertions(+), 32 deletions(-) rename test/{expected => expected_test.story}/cbz.cbz (100%) rename test/{expected => expected_test.story}/epub.epub (100%) rename test/{expected => expected_test.story}/html/html.info (100%) rename test/{expected => expected_test.story}/html/html.txt (100%) rename test/{expected => expected_test.story}/html/index.html (100%) rename test/{expected => expected_test.story}/html/style.css (100%) rename test/{expected => expected_test.story}/info_text (100%) rename test/{expected => expected_test.story}/info_text.info (100%) rename test/{expected => expected_test.story}/latex.tex (100%) rename test/{expected => expected_test.story}/text.txt (100%) diff --git a/src/be/nikiroo/fanfix/test/ConversionTest.java b/src/be/nikiroo/fanfix/test/ConversionTest.java index 0eb1eb0..371c05a 100644 --- a/src/be/nikiroo/fanfix/test/ConversionTest.java +++ b/src/be/nikiroo/fanfix/test/ConversionTest.java @@ -18,15 +18,20 @@ import be.nikiroo.utils.test.TestCase; import be.nikiroo.utils.test.TestLauncher; class ConversionTest extends TestLauncher { - private File testFile; - private File expectedDir; - private File resultDir; + private String testUri; + private String expectedDir; + private String resultDir; private List realTypes; private Map> skipCompare; - public ConversionTest(String[] args) { + public ConversionTest(final String testUri, final String expectedDir, + final String resultDir, String[] args) { super("Conversion", args); + this.testUri = testUri; + this.expectedDir = expectedDir; + this.resultDir = resultDir; + // Special mode SYSOUT is not a file type (System.out) realTypes = new ArrayList(); for (BasicOutput.OutputType type : BasicOutput.OutputType.values()) { @@ -38,20 +43,23 @@ 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()); + 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()); } }); @@ -62,10 +70,6 @@ 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/"); - skipCompare = new HashMap>(); skipCompare.put("epb.ncx", Arrays.asList(" errors = new ArrayList(); @@ -137,8 +142,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) { diff --git a/src/be/nikiroo/fanfix/test/Test.java b/src/be/nikiroo/fanfix/test/Test.java index 8ca8b7c..7aac0ce 100644 --- a/src/be/nikiroo/fanfix/test/Test.java +++ b/src/be/nikiroo/fanfix/test/Test.java @@ -18,6 +18,17 @@ import be.nikiroo.utils.test.TestLauncher; * @author niki */ public class Test extends TestLauncher { + // + // 3 files can control the test: + // - test/VERBOSE: enable verbose mode + // - test/OFFLINE: to forbid any downloading + // - test/FORCE_REFRESH: to force a clear of the cache + // + // The test files will be: + // - test/*.url: URL to download in text format, content = URL + // - test/*.story: text mode story, content = story + // + /** * The temporary files handler. */ @@ -29,14 +40,40 @@ public class Test extends TestLauncher { * @param args * the arguments to configure the number of columns and the ok/ko * {@link String}s + * + * @throws IOException */ - public Test(String[] args) { + public Test(String[] args) throws IOException { super("Fanfix", args); Instance.setTraceHandler(null); addSeries(new BasicSupportUtilitiesTest(args)); addSeries(new BasicSupportDeprecatedTest(args)); addSeries(new LibraryTest(args)); - addSeries(new ConversionTest(args)); + + File sources = new File("test/"); + if (sources.isDirectory()) { + for (File file : sources.listFiles()) { + if (file.isDirectory()) { + continue; + } + + String expectedDir = new File(file.getParentFile(), "expected_" + + file.getName()).getAbsolutePath(); + String resultDir = new File(file.getParentFile(), "result_" + + file.getName()).getAbsolutePath(); + + String uri; + if (file.getName().endsWith(".url")) { + uri = IOUtils.readSmallFile(file).trim(); + } else if (file.getName().endsWith(".story")) { + uri = file.getAbsolutePath(); + } else { + continue; + } + + addSeries(new ConversionTest(uri, expectedDir, resultDir, args)); + } + } } /** @@ -49,20 +86,33 @@ public class Test extends TestLauncher { */ static public void main(String[] args) throws IOException { Instance.init(); - //Instance.getCache().setOffline(true); - + + // Verbose mode: + boolean verbose = new File("test/VERBOSE").exists(); + + // Can force refresh + boolean forceRefresh = new File("test/FORCE_REFRESH").exists(); + + // Only download files if allowed: + boolean offline = new File("test/OFFLINE").exists(); + Instance.getCache().setOffline(offline); + int result = 0; tempFiles = new TempFiles("fanfix-test"); try { File tmpConfig = tempFiles.createTempDir("fanfix-config"); - File tmpCache = tempFiles.createTempDir("fanfix-cache"); + File localCache = new File("test/CACHE"); + if (forceRefresh) { + IOUtils.deltree(localCache); + } + localCache.mkdirs(); FileOutputStream out = null; try { out = new FileOutputStream(new File(tmpConfig, "config.properties")); Properties props = new Properties(); - props.setProperty("CACHE_DIR", tmpCache.getAbsolutePath()); + props.setProperty("CACHE_DIR", localCache.getAbsolutePath()); props.store(out, null); } finally { if (out != null) { @@ -76,10 +126,13 @@ public class Test extends TestLauncher { System.setProperty("CONFIG_DIR", tmpConfig.getAbsolutePath()); - result = new Test(args).launch(); + TestLauncher tests = new Test(args); + tests.setDetails(verbose); + + result = tests.launch(); IOUtils.deltree(tmpConfig); - IOUtils.deltree(tmpCache); + IOUtils.deltree(localCache); } finally { // Test temp files tempFiles.close(); diff --git a/test/expected/cbz.cbz b/test/expected_test.story/cbz.cbz similarity index 100% rename from test/expected/cbz.cbz rename to test/expected_test.story/cbz.cbz diff --git a/test/expected/epub.epub b/test/expected_test.story/epub.epub similarity index 100% rename from test/expected/epub.epub rename to test/expected_test.story/epub.epub diff --git a/test/expected/html/html.info b/test/expected_test.story/html/html.info similarity index 100% rename from test/expected/html/html.info rename to test/expected_test.story/html/html.info diff --git a/test/expected/html/html.txt b/test/expected_test.story/html/html.txt similarity index 100% rename from test/expected/html/html.txt rename to test/expected_test.story/html/html.txt diff --git a/test/expected/html/index.html b/test/expected_test.story/html/index.html similarity index 100% rename from test/expected/html/index.html rename to test/expected_test.story/html/index.html diff --git a/test/expected/html/style.css b/test/expected_test.story/html/style.css similarity index 100% rename from test/expected/html/style.css rename to test/expected_test.story/html/style.css diff --git a/test/expected/info_text b/test/expected_test.story/info_text similarity index 100% rename from test/expected/info_text rename to test/expected_test.story/info_text diff --git a/test/expected/info_text.info b/test/expected_test.story/info_text.info similarity index 100% rename from test/expected/info_text.info rename to test/expected_test.story/info_text.info diff --git a/test/expected/latex.tex b/test/expected_test.story/latex.tex similarity index 100% rename from test/expected/latex.tex rename to test/expected_test.story/latex.tex diff --git a/test/expected/text.txt b/test/expected_test.story/text.txt similarity index 100% rename from test/expected/text.txt rename to test/expected_test.story/text.txt -- 2.27.0