From 86d49dbc7c3eca665b7823b5de49bb73a31c7722 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Fri, 23 Mar 2018 15:37:58 +0100 Subject: [PATCH] Continue conversion test + HTML.supports() fix --- src/be/nikiroo/fanfix/supported/Html.java | 42 +++++++++++-------- src/be/nikiroo/fanfix/supported/InfoText.java | 24 +++-------- src/be/nikiroo/fanfix/supported/Text.java | 39 ++++++++++++++++- .../nikiroo/fanfix/test/ConversionTest.java | 40 +++++++++--------- 4 files changed, 89 insertions(+), 56 deletions(-) diff --git a/src/be/nikiroo/fanfix/supported/Html.java b/src/be/nikiroo/fanfix/supported/Html.java index 8dec5f7..f66032b 100644 --- a/src/be/nikiroo/fanfix/supported/Html.java +++ b/src/be/nikiroo/fanfix/supported/Html.java @@ -22,14 +22,17 @@ class Html extends InfoText { @Override protected boolean supports(URL url) { - if (url.getPath().toLowerCase() - .endsWith(File.separatorChar + "index.html")) { - try { - File file = new File(url.toURI()).getParentFile(); - return super.supports(file.toURI().toURL()); - } catch (URISyntaxException e) { - } catch (MalformedURLException e) { + try { + File file = new File(url.toURI()); + if (file.getName().equals("index.html")) { + file = file.getParentFile(); } + + file = new File(file, file.getName()); + + return super.supports(file.toURI().toURL()); + } catch (URISyntaxException e) { + } catch (MalformedURLException e) { } return false; @@ -37,17 +40,22 @@ class Html extends InfoText { @Override public URL getCanonicalUrl(URL source) { - if (source.toString().endsWith(File.separator + "index.html")) { - try { - File fakeFile = new File(source.toURI()); // "story/index.html" - fakeFile = new File(fakeFile.getParent()); // "story" - fakeFile = new File(fakeFile, fakeFile.getName()); // "story/story" - return fakeFile.toURI().toURL(); - } catch (Exception e) { - Instance.getTraceHandler().error( - new IOException("Cannot find the right URL for " - + source, e)); + + try { + File fakeFile = new File(source.toURI()); + if (fakeFile.getName().equals("index.html")) { // "story/index.html" + fakeFile = new File(fakeFile.getParent()); // -> "story/" } + + if (fakeFile.isDirectory()) { // "story/" + fakeFile = new File(fakeFile, fakeFile.getName() + ".txt"); // "story/story.txt" + } + + return fakeFile.toURI().toURL(); + } catch (Exception e) { + Instance.getTraceHandler().error( + new IOException("Cannot find the right URL for " + source, + e)); } return source; diff --git a/src/be/nikiroo/fanfix/supported/InfoText.java b/src/be/nikiroo/fanfix/supported/InfoText.java index 9ede844..5488ab0 100644 --- a/src/be/nikiroo/fanfix/supported/InfoText.java +++ b/src/be/nikiroo/fanfix/supported/InfoText.java @@ -6,7 +6,6 @@ import java.io.InputStream; import java.net.URISyntaxException; import java.net.URL; -import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.data.MetaData; /** @@ -27,9 +26,11 @@ class InfoText extends Text { @Override protected MetaData getMeta(URL source, InputStream in) throws IOException { try { - MetaData meta = InfoReader.readMeta( - new File(new File(source.toURI()).getPath() + ".info"), - true); + File sourceFile = new File(source.toURI()); + sourceFile = assureNoTxt(sourceFile); + + MetaData meta = InfoReader.readMeta(new File(sourceFile.getPath() + + ".info"), true); // Some old .info files don't have those now required fields... String test = meta.getTitle() == null ? "" : meta.getTitle(); @@ -61,19 +62,6 @@ class InfoText extends Text { @Override protected boolean supports(URL url) { - if ("file".equals(url.getProtocol())) { - File file; - try { - file = new File(url.toURI()); - file = new File(file.getPath() + ".info"); - } catch (URISyntaxException e) { - Instance.getTraceHandler().error(e); - file = null; - } - - return file != null && file.exists(); - } - - return false; + return supports(url, true); } } diff --git a/src/be/nikiroo/fanfix/supported/Text.java b/src/be/nikiroo/fanfix/supported/Text.java index 25d7899..fb4a0ad 100644 --- a/src/be/nikiroo/fanfix/supported/Text.java +++ b/src/be/nikiroo/fanfix/supported/Text.java @@ -236,20 +236,55 @@ class Text extends BasicSupport_Deprecated { @Override protected boolean supports(URL url) { + return supports(url, false); + } + + /** + * Check if we supports this {@link URL}, that is, if the info file can be + * found OR not found. + * + * @param url + * the {@link URL} to check + * @param info + * TRUE to require the info file, FALSE to forbid the info file + * + * @return TRUE if it is supported + */ + protected boolean supports(URL url, boolean info) { + boolean infoPresent = false; if ("file".equals(url.getProtocol())) { File file; try { file = new File(url.toURI()); + file = assureNoTxt(file); file = new File(file.getPath() + ".info"); } catch (URISyntaxException e) { Instance.getTraceHandler().error(e); file = null; } - return file == null || !file.exists(); + infoPresent = (file != null && file.exists()); } - return false; + return infoPresent == info; + } + + /** + * Remove the ".txt" extension if it is present. + * + * @param file + * the file to process + * + * @return the same file or a copy of it without the ".txt" extension if it + * was present + */ + protected File assureNoTxt(File file) { + if (file.getName().endsWith(".txt")) { + file = new File(file.getPath().substring(0, + file.getPath().length() - 4)); + } + + return file; } /** diff --git a/src/be/nikiroo/fanfix/test/ConversionTest.java b/src/be/nikiroo/fanfix/test/ConversionTest.java index 07ec747..92aca36 100644 --- a/src/be/nikiroo/fanfix/test/ConversionTest.java +++ b/src/be/nikiroo/fanfix/test/ConversionTest.java @@ -24,10 +24,19 @@ class ConversionTest extends TestLauncher { private File testFile; private File expectedDir; private File resultDir; + private List realTypes; public ConversionTest(String[] args) { super("Conversion", args); + // Special mode SYSOUT is not a file type (System.out) + realTypes = new ArrayList(); + 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 +57,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)); } } @@ -81,21 +87,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); } } }; -- 2.27.0