From: Niki Roo
Date: Tue, 17 Sep 2019 06:18:20 +0000 (+0200)
Subject: tests: fix NPE, add BasicSupportUtilities tests
X-Git-Tag: fanfix-3.0.0~6^2~7
X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=8d59ce0748baeeea0458bab49716ab4543aae439;p=fanfix.git
tests: fix NPE, add BasicSupportUtilities tests
---
diff --git a/src/be/nikiroo/fanfix/DataLoader.java b/src/be/nikiroo/fanfix/DataLoader.java
index 0abb323..d342580 100644
--- a/src/be/nikiroo/fanfix/DataLoader.java
+++ b/src/be/nikiroo/fanfix/DataLoader.java
@@ -95,7 +95,7 @@ public class DataLoader {
* @param url
* the resource to open
* @param support
- * the support to use to download the resource
+ * the support to use to download the resource (can be NULL)
* @param stable
* TRUE for more stable resources, FALSE when they often change
*
@@ -149,7 +149,7 @@ public class DataLoader {
* is also used for the cache ID if needed (so we can retrieve
* the content with this URL if needed)
* @param support
- * the support to use to download the resource
+ * the support to use to download the resource (can be NULL)
* @param stable
* TRUE for more stable resources, FALSE when they often change
* @param postParams
@@ -229,7 +229,7 @@ public class DataLoader {
* @param url
* the resource to open
* @param support
- * the support to use to download the resource
+ * the support to use to download the resource (can be NULL)
* @param stable
* TRUE for more stable resources, FALSE when they often change
*
diff --git a/src/be/nikiroo/fanfix/supported/BasicSupport.java b/src/be/nikiroo/fanfix/supported/BasicSupport.java
index 092f89e..d3c0ebb 100644
--- a/src/be/nikiroo/fanfix/supported/BasicSupport.java
+++ b/src/be/nikiroo/fanfix/supported/BasicSupport.java
@@ -37,6 +37,10 @@ public abstract class BasicSupport {
private URL source;
private SupportType type;
private URL currentReferer; // with only one 'r', as in 'HTTP'...
+
+ static protected BasicSupportHelper bsHelper = new BasicSupportHelper();
+ static protected BasicSupportImages bsImages = new BasicSupportImages();
+ static protected BasicSupportPara bsPara = new BasicSupportPara(new BasicSupportHelper(), new BasicSupportImages());
/**
* Check if the given resource is supported by this {@link BasicSupport}.
@@ -274,7 +278,7 @@ public abstract class BasicSupport {
pg.setProgress(50);
if (meta.getCover() == null) {
- meta.setCover(BasicSupportHelper.getDefaultCover(meta.getSubject()));
+ meta.setCover(bsHelper.getDefaultCover(meta.getSubject()));
}
pg.setProgress(60);
@@ -283,7 +287,7 @@ public abstract class BasicSupport {
String descChapterName = Instance.getTrans().getString(
StringId.DESCRIPTION);
story.getMeta().setResume(
- BasicSupportPara.makeChapter(this, source, 0,
+ bsPara.makeChapter(this, source, 0,
descChapterName, //
getDesc(), isHtml(), null));
}
@@ -375,7 +379,7 @@ public abstract class BasicSupport {
String content = getChapterContent(chapUrl, i,
pgGetChapterContent);
pgGetChapterContent.done();
- Chapter cc = BasicSupportPara.makeChapter(this, chapUrl, i,
+ Chapter cc = bsPara.makeChapter(this, chapUrl, i,
chapName, content, isHtml(), pgMakeChapter);
pgMakeChapter.done();
@@ -414,7 +418,7 @@ public abstract class BasicSupport {
*/
public Chapter makeChapter(URL source, int number, String name,
String content) throws IOException {
- return BasicSupportPara.makeChapter(this, source, number, name,
+ return bsPara.makeChapter(this, source, number, name,
content, isHtml(), null);
}
diff --git a/src/be/nikiroo/fanfix/supported/BasicSupportHelper.java b/src/be/nikiroo/fanfix/supported/BasicSupportHelper.java
index c9c9f08..62006f4 100644
--- a/src/be/nikiroo/fanfix/supported/BasicSupportHelper.java
+++ b/src/be/nikiroo/fanfix/supported/BasicSupportHelper.java
@@ -16,7 +16,7 @@ import be.nikiroo.utils.Image;
*
* @author niki
*/
-class BasicSupportHelper {
+public class BasicSupportHelper {
/**
* Get the default cover related to this subject (see .info files).
*
@@ -25,7 +25,7 @@ class BasicSupportHelper {
*
* @return the cover if any, or NULL
*/
- public static Image getDefaultCover(String subject) {
+ public Image getDefaultCover(String subject) {
if (subject != null && !subject.isEmpty()
&& Instance.getCoverDir() != null) {
try {
@@ -48,7 +48,7 @@ class BasicSupportHelper {
*
* @return the extensions
*/
- public static String[] getImageExt(boolean emptyAllowed) {
+ public String[] getImageExt(boolean emptyAllowed) {
if (emptyAllowed) {
return new String[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
}
@@ -61,7 +61,7 @@ class BasicSupportHelper {
* refresh the cache with it if it is.
*
* @param support
- * the linked {@link BasicSupport}
+ * the linked {@link BasicSupport} (can be NULL)
* @param source
* the story source
* @param line
@@ -70,7 +70,7 @@ class BasicSupportHelper {
* @return the image if found, or NULL
*
*/
- public static Image getImage(BasicSupport support, URL source, String line) {
+ public Image getImage(BasicSupport support, URL source, String line) {
URL url = getImageUrl(support, source, line);
if (url != null) {
if ("file".equals(url.getProtocol())) {
@@ -101,7 +101,7 @@ class BasicSupportHelper {
* refresh the cache with it if it is.
*
* @param support
- * the linked {@link BasicSupport}
+ * the linked {@link BasicSupport} (can be NULL)
* @param source
* the story source
* @param line
@@ -110,7 +110,7 @@ class BasicSupportHelper {
* @return the image URL if found, or NULL
*
*/
- public static URL getImageUrl(BasicSupport support, URL source, String line) {
+ public URL getImageUrl(BasicSupport support, URL source, String line) {
URL url = null;
if (line != null) {
@@ -201,7 +201,7 @@ class BasicSupportHelper {
*
* @return the author without prefixes
*/
- public static String fixAuthor(String author) {
+ public String fixAuthor(String author) {
if (author != null) {
for (String suffix : new String[] { " ", ":" }) {
for (String byString : Instance.getConfig().getList(Config.CONF_BYS)) {
diff --git a/src/be/nikiroo/fanfix/supported/BasicSupportImages.java b/src/be/nikiroo/fanfix/supported/BasicSupportImages.java
index 85b79c7..69a7c86 100644
--- a/src/be/nikiroo/fanfix/supported/BasicSupportImages.java
+++ b/src/be/nikiroo/fanfix/supported/BasicSupportImages.java
@@ -9,6 +9,12 @@ import java.net.URL;
import be.nikiroo.fanfix.Instance;
import be.nikiroo.utils.Image;
+/**
+ * Helper class for {@link BasicSupport}, mostly dedicated to images for
+ * the classes that implement {@link BasicSupport}.
+ *
+ * @author niki
+ */
public class BasicSupportImages {
/**
* Check if the given resource can be a local image or a remote image, then
@@ -22,7 +28,7 @@ public class BasicSupportImages {
* @return the image if found, or NULL
*
*/
- static Image getImage(BasicSupport support, File dir, String line) {
+ public Image getImage(BasicSupport support, File dir, String line) {
URL url = getImageUrl(support, dir, line);
if (url != null) {
if ("file".equals(url.getProtocol())) {
@@ -60,7 +66,7 @@ public class BasicSupportImages {
* @return the image URL if found, or NULL
*
*/
- static URL getImageUrl(BasicSupport support, File dir, String line) {
+ public URL getImageUrl(BasicSupport support, File dir, String line) {
URL url = null;
if (line != null) {
@@ -151,7 +157,7 @@ public class BasicSupportImages {
*
* @return the extensions
*/
- static String[] getImageExt(boolean emptyAllowed) {
+ public String[] getImageExt(boolean emptyAllowed) {
if (emptyAllowed) {
return new String[] { "", ".png", ".jpg", ".jpeg", ".gif", ".bmp" };
}
diff --git a/src/be/nikiroo/fanfix/supported/BasicSupportPara.java b/src/be/nikiroo/fanfix/supported/BasicSupportPara.java
index b960348..2f65723 100644
--- a/src/be/nikiroo/fanfix/supported/BasicSupportPara.java
+++ b/src/be/nikiroo/fanfix/supported/BasicSupportPara.java
@@ -20,12 +20,11 @@ import be.nikiroo.utils.StringUtils;
/**
* Helper class for {@link BasicSupport}, mostly dedicated to {@link Paragraph}
- * and text formating for the {@link BasicSupport} class itself (not its
- * children).
+ * and text formating for the {@link BasicSupport} class.
*
* @author niki
*/
-class BasicSupportPara {
+public class BasicSupportPara {
// quote chars
private static char openQuote = Instance.getTrans().getCharacter(
StringId.OPEN_SINGLE_QUOTE);
@@ -36,6 +35,15 @@ class BasicSupportPara {
private static char closeDoubleQuote = Instance.getTrans().getCharacter(
StringId.CLOSE_DOUBLE_QUOTE);
+ // used by this class:
+ BasicSupportHelper bsHelper;
+ BasicSupportImages bsImages;
+
+ public BasicSupportPara(BasicSupportHelper bsHelper, BasicSupportImages bsImages) {
+ this.bsHelper = bsHelper;
+ this.bsImages = bsImages;
+ }
+
/**
* Create a {@link Chapter} object from the given information, formatting
* the content as it should be.
@@ -60,13 +68,13 @@ class BasicSupportPara {
* @throws IOException
* in case of I/O error
*/
- public static Chapter makeChapter(BasicSupport support, URL source,
+ public Chapter makeChapter(BasicSupport support, URL source,
int number, String name, String content, boolean html, Progress pg)
throws IOException {
// Chapter name: process it correctly, then remove the possible
// redundant "Chapter x: " in front of it, or "-" (as in
// "Chapter 5: - Fun!" after the ": " was automatically added)
- String chapterName = BasicSupportPara.processPara(name, false)
+ String chapterName = processPara(name, false)
.getContent().trim();
for (String lang : Instance.getConfig().getList(Config.CONF_CHAPTER)) {
String chapterWord = Instance.getConfig().getStringX(
@@ -116,7 +124,7 @@ class BasicSupportPara {
*
* @return the correctly (or so we hope) quotified paragraphs
*/
- private static List requotify(Paragraph para, boolean html) {
+ protected List requotify(Paragraph para, boolean html) {
List newParas = new ArrayList();
if (para.getType() == ParagraphType.QUOTE
@@ -200,7 +208,7 @@ class BasicSupportPara {
*
* @return the processed {@link Paragraph}
*/
- private static Paragraph processPara(String line, boolean html) {
+ protected Paragraph processPara(String line, boolean html) {
if (html) {
line = StringUtils.unhtml(line).trim();
}
@@ -407,7 +415,8 @@ class BasicSupportPara {
* Convert the given content into {@link Paragraph}s.
*
* @param support
- * the linked {@link BasicSupport}
+ * the linked {@link BasicSupport} (can be NULL),
+ * used to download optional image content in []
* @param source
* the source URL of the story
* @param content
@@ -422,7 +431,7 @@ class BasicSupportPara {
* @throws IOException
* in case of I/O error
*/
- private static List makeParagraphs(BasicSupport support,
+ protected List makeParagraphs(BasicSupport support,
URL source, String content, boolean html, Progress pg)
throws IOException {
if (pg == null) {
@@ -483,7 +492,7 @@ class BasicSupportPara {
// Check quotes for "bad" format
List newParas = new ArrayList();
for (Paragraph para : paras) {
- newParas.addAll(BasicSupportPara.requotify(para, html));
+ newParas.addAll(requotify(para, html));
}
paras = newParas;
@@ -498,7 +507,8 @@ class BasicSupportPara {
* Convert the given line into a single {@link Paragraph}.
*
* @param support
- * the linked {@link BasicSupport}
+ * the linked {@link BasicSupport} (can be NULL),
+ * used to download optional image content in []
* @param source
* the source URL of the story
* @param line
@@ -508,11 +518,11 @@ class BasicSupportPara {
*
* @return the {@link Paragraph}
*/
- private static Paragraph makeParagraph(BasicSupport support, URL source,
+ protected Paragraph makeParagraph(BasicSupport support, URL source,
String line, boolean html) {
Image image = null;
if (line.startsWith("[") && line.endsWith("]")) {
- image = BasicSupportHelper.getImage(support, source, line
+ image = bsHelper.getImage(support, source, line
.substring(1, line.length() - 1).trim());
}
@@ -520,7 +530,7 @@ class BasicSupportPara {
return new Paragraph(image);
}
- return BasicSupportPara.processPara(line, html);
+ return processPara(line, html);
}
/**
@@ -533,7 +543,7 @@ class BasicSupportPara {
* @param paras
* the list of {@link Paragraph}s to fix
*/
- private static void fixBlanksBreaks(List paras) {
+ protected void fixBlanksBreaks(List paras) {
boolean space = false;
boolean brk = true;
for (int i = 0; i < paras.size(); i++) {
diff --git a/src/be/nikiroo/fanfix/supported/Cbz.java b/src/be/nikiroo/fanfix/supported/Cbz.java
index 3682afe..c7475fa 100644
--- a/src/be/nikiroo/fanfix/supported/Cbz.java
+++ b/src/be/nikiroo/fanfix/supported/Cbz.java
@@ -85,7 +85,7 @@ class Cbz extends Epub {
&& entry.getName().startsWith(getDataPrefix())) {
String entryLName = entry.getName().toLowerCase();
boolean imageEntry = false;
- for (String ext : BasicSupportImages.getImageExt(false)) {
+ for (String ext : bsImages.getImageExt(false)) {
if (entryLName.endsWith(ext)) {
imageEntry = true;
}
diff --git a/src/be/nikiroo/fanfix/supported/Epub.java b/src/be/nikiroo/fanfix/supported/Epub.java
index ae26574..82af118 100644
--- a/src/be/nikiroo/fanfix/supported/Epub.java
+++ b/src/be/nikiroo/fanfix/supported/Epub.java
@@ -1,7 +1,6 @@
package be.nikiroo.fanfix.supported;
import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
@@ -17,8 +16,8 @@ import be.nikiroo.fanfix.Instance;
import be.nikiroo.fanfix.data.MetaData;
import be.nikiroo.utils.IOUtils;
import be.nikiroo.utils.Image;
-import be.nikiroo.utils.streams.MarkableFileInputStream;
import be.nikiroo.utils.StringUtils;
+import be.nikiroo.utils.streams.MarkableFileInputStream;
/**
* Support class for EPUB files created with this program (as we need some
@@ -112,7 +111,7 @@ class Epub extends InfoText {
String entryLName = entry.getName().toLowerCase();
boolean imageEntry = false;
- for (String ext : BasicSupportImages.getImageExt(false)) {
+ for (String ext : bsImages.getImageExt(false)) {
if (entryLName.endsWith(ext)) {
imageEntry = true;
}
diff --git a/src/be/nikiroo/fanfix/supported/Fanfiction.java b/src/be/nikiroo/fanfix/supported/Fanfiction.java
index 33c1721..0dcd790 100644
--- a/src/be/nikiroo/fanfix/supported/Fanfiction.java
+++ b/src/be/nikiroo/fanfix/supported/Fanfiction.java
@@ -138,7 +138,7 @@ class Fanfiction extends BasicSupport_Deprecated {
}
}
- return BasicSupportHelper.fixAuthor(author);
+ return bsHelper.fixAuthor(author);
}
private String getDate(InputStream in) {
diff --git a/src/be/nikiroo/fanfix/supported/InfoReader.java b/src/be/nikiroo/fanfix/supported/InfoReader.java
index 3d09a55..c22dbd7 100644
--- a/src/be/nikiroo/fanfix/supported/InfoReader.java
+++ b/src/be/nikiroo/fanfix/supported/InfoReader.java
@@ -1,7 +1,6 @@
package be.nikiroo.fanfix.supported;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
@@ -18,6 +17,10 @@ import be.nikiroo.utils.streams.MarkableFileInputStream;
// not complete: no "description" tag
public class InfoReader {
+ static protected BasicSupportHelper bsHelper = new BasicSupportHelper();
+ // static protected BasicSupportImages bsImages = new BasicSupportImages();
+ // static protected BasicSupportPara bsPara = new BasicSupportPara(new BasicSupportHelper(), new BasicSupportImages());
+
public static MetaData readMeta(File infoFile, boolean withCover)
throws IOException {
if (infoFile == null) {
@@ -58,7 +61,7 @@ public class InfoReader {
if (withCover) {
String infoTag = getInfoTag(in, "COVER");
if (infoTag != null && !infoTag.trim().isEmpty()) {
- meta.setCover(BasicSupportHelper.getImage(null, sourceInfoFile,
+ meta.setCover(bsHelper.getImage(null, sourceInfoFile,
infoTag));
}
if (meta.getCover() == null) {
@@ -75,7 +78,7 @@ public class InfoReader {
meta.setFakeCover(Boolean.parseBoolean(getInfoTag(in, "FAKE_COVER")));
if (withCover && meta.getCover() == null) {
- meta.setCover(BasicSupportHelper.getDefaultCover(meta.getSubject()));
+ meta.setCover(bsHelper.getDefaultCover(meta.getSubject()));
}
return meta;
@@ -99,7 +102,7 @@ public class InfoReader {
.toLowerCase();
// Without removing ext
- cover = BasicSupportHelper.getImage(null, sourceInfoFile,
+ cover = bsHelper.getImage(null, sourceInfoFile,
basefile.getAbsolutePath() + ext);
// Try without ext
@@ -109,7 +112,7 @@ public class InfoReader {
name = name.substring(0, pos);
basefile = new File(basefile.getParent(), name);
- cover = BasicSupportHelper.getImage(null, sourceInfoFile,
+ cover = bsHelper.getImage(null, sourceInfoFile,
basefile.getAbsolutePath() + ext);
}
diff --git a/src/be/nikiroo/fanfix/supported/Text.java b/src/be/nikiroo/fanfix/supported/Text.java
index 1e5977a..2f614fc 100644
--- a/src/be/nikiroo/fanfix/supported/Text.java
+++ b/src/be/nikiroo/fanfix/supported/Text.java
@@ -1,7 +1,6 @@
package be.nikiroo.fanfix.supported;
import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
@@ -19,8 +18,8 @@ import be.nikiroo.fanfix.bundles.Config;
import be.nikiroo.fanfix.data.MetaData;
import be.nikiroo.utils.Image;
import be.nikiroo.utils.ImageUtils;
-import be.nikiroo.utils.streams.MarkableFileInputStream;
import be.nikiroo.utils.Progress;
+import be.nikiroo.utils.streams.MarkableFileInputStream;
/**
* Support class for local stories encoded in textual format, with a few rules:
@@ -147,7 +146,7 @@ class Text extends BasicSupport {
author = authorDate.substring(0, pos);
}
- return BasicSupportHelper.fixAuthor(author);
+ return bsHelper.fixAuthor(author);
}
private String getDate() {
@@ -184,7 +183,7 @@ class Text extends BasicSupport {
}
}
- Image cover = BasicSupportImages.getImage(this,
+ Image cover = bsImages.getImage(this,
sourceFile.getParentFile(), path);
if (cover != null) {
try {
diff --git a/src/be/nikiroo/fanfix/test/BasicSupportTest.java b/src/be/nikiroo/fanfix/test/BasicSupportDeprecatedTest.java
similarity index 98%
rename from src/be/nikiroo/fanfix/test/BasicSupportTest.java
rename to src/be/nikiroo/fanfix/test/BasicSupportDeprecatedTest.java
index b731c44..9f40a80 100644
--- a/src/be/nikiroo/fanfix/test/BasicSupportTest.java
+++ b/src/be/nikiroo/fanfix/test/BasicSupportDeprecatedTest.java
@@ -22,7 +22,7 @@ import be.nikiroo.utils.Progress;
import be.nikiroo.utils.test.TestCase;
import be.nikiroo.utils.test.TestLauncher;
-class BasicSupportTest extends TestLauncher {
+class BasicSupportDeprecatedTest extends TestLauncher {
// quote chars
private char openQuote = Instance.getTrans().getCharacter(
StringId.OPEN_SINGLE_QUOTE);
@@ -33,8 +33,8 @@ class BasicSupportTest extends TestLauncher {
private char closeDoubleQuote = Instance.getTrans().getCharacter(
StringId.CLOSE_DOUBLE_QUOTE);
- public BasicSupportTest(String[] args) {
- super("BasicSupport", args);
+ public BasicSupportDeprecatedTest(String[] args) {
+ super("BasicSupportDeprecated", args);
addSeries(new TestLauncher("General", args) {
{
diff --git a/src/be/nikiroo/fanfix/test/BasicSupportUtilitiesTest.java b/src/be/nikiroo/fanfix/test/BasicSupportUtilitiesTest.java
new file mode 100644
index 0000000..4e34891
--- /dev/null
+++ b/src/be/nikiroo/fanfix/test/BasicSupportUtilitiesTest.java
@@ -0,0 +1,401 @@
+package be.nikiroo.fanfix.test;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+import be.nikiroo.fanfix.Instance;
+import be.nikiroo.fanfix.bundles.StringId;
+import be.nikiroo.fanfix.data.Paragraph;
+import be.nikiroo.fanfix.data.Paragraph.ParagraphType;
+import be.nikiroo.fanfix.data.Story;
+import be.nikiroo.fanfix.supported.BasicSupport;
+import be.nikiroo.fanfix.supported.BasicSupportHelper;
+import be.nikiroo.fanfix.supported.BasicSupportImages;
+import be.nikiroo.fanfix.supported.BasicSupportPara;
+import be.nikiroo.fanfix.supported.SupportType;
+import be.nikiroo.utils.IOUtils;
+import be.nikiroo.utils.Progress;
+import be.nikiroo.utils.test.TestCase;
+import be.nikiroo.utils.test.TestLauncher;
+
+class BasicSupportUtilitiesTest extends TestLauncher {
+ // quote chars
+ private char openQuote = Instance.getTrans().getCharacter(
+ StringId.OPEN_SINGLE_QUOTE);
+ private char closeQuote = Instance.getTrans().getCharacter(
+ StringId.CLOSE_SINGLE_QUOTE);
+ private char openDoubleQuote = Instance.getTrans().getCharacter(
+ StringId.OPEN_DOUBLE_QUOTE);
+ private char closeDoubleQuote = Instance.getTrans().getCharacter(
+ StringId.CLOSE_DOUBLE_QUOTE);
+
+ public BasicSupportUtilitiesTest(String[] args) {
+ super("BasicSupportUtilities", args);
+
+ addSeries(new TestLauncher("General", args) {
+ {
+ addTest(new TestCase("BasicSupport.makeParagraphs()") {
+ @Override
+ public void test() throws Exception {
+ BasicSupportParaPublic bsPara = new BasicSupportParaPublic() {
+ @Override
+ public void fixBlanksBreaks(List paras) {
+ }
+
+ @Override
+ public List requotify(Paragraph para, boolean html) {
+ List paras = new ArrayList(
+ 1);
+ paras.add(para);
+ return paras;
+ }
+ };
+
+ List paras = null;
+
+ paras = bsPara.makeParagraphs(null, null, "", true, null);
+ assertEquals(
+ "An empty content should not generate paragraphs",
+ 0, paras.size());
+
+ paras = bsPara.makeParagraphs(null, null,
+ "Line 1
Line 2
Line 3
", true, null);
+ assertEquals(5, paras.size());
+ assertEquals("Line 1", paras.get(0).getContent());
+ assertEquals(ParagraphType.BLANK, paras.get(1)
+ .getType());
+ assertEquals("Line 2", paras.get(2).getContent());
+ assertEquals(ParagraphType.BLANK, paras.get(3)
+ .getType());
+ assertEquals("Line 3", paras.get(4).getContent());
+
+ paras = bsPara.makeParagraphs(null, null,
+ "Line1
Line2
Line3
", true, null);
+ assertEquals(6, paras.size());
+ }
+ });
+
+ addTest(new TestCase("BasicSupport.removeDoubleBlanks()") {
+ @Override
+ public void test() throws Exception {
+ BasicSupportParaPublic support = new BasicSupportParaPublic();
+
+ List paras = null;
+
+ paras = support
+ .makeParagraphs(
+ null,
+ null,
+ "Line1
Line2
Line3
",
+ true,
+ null);
+ assertEquals(5, paras.size());
+
+ paras = support
+ .makeParagraphs(
+ null,
+ null,
+ "Line1
Line2
Line3
* * *",
+ true,
+ null);
+ assertEquals(5, paras.size());
+
+ paras = support.makeParagraphs(null, null, "1* * *
2",
+ true, null);
+ assertEquals(3, paras.size());
+ assertEquals(ParagraphType.BREAK, paras.get(1)
+ .getType());
+
+ paras = support.makeParagraphs(null, null,
+ "1
* * *
2", true, null);
+ assertEquals(3, paras.size());
+ assertEquals(ParagraphType.BREAK, paras.get(1)
+ .getType());
+
+ paras = support.makeParagraphs(null, null,
+ "1
* * *
2", true, null);
+ assertEquals(3, paras.size());
+ assertEquals(ParagraphType.BREAK, paras.get(1)
+ .getType());
+
+ paras = support.makeParagraphs(null, null,
+ "1
* * *
2", true, null);
+ assertEquals(3, paras.size());
+ assertEquals(ParagraphType.BREAK, paras.get(1)
+ .getType());
+ }
+ });
+
+ addTest(new TestCase("BasicSupport.processPara() quotes") {
+ @Override
+ public void test() throws Exception {
+ BasicSupportParaPublic support = new BasicSupportParaPublic();
+
+ Paragraph para;
+
+ // sanity check
+ para = support.processPara("", true);
+ assertEquals(ParagraphType.BLANK, para.getType());
+ //
+
+ para = support.processPara("\"Yes, my Lord!\"", true);
+ assertEquals(ParagraphType.QUOTE, para.getType());
+ assertEquals(openDoubleQuote + "Yes, my Lord!"
+ + closeDoubleQuote, para.getContent());
+
+ para = support.processPara("«Yes, my Lord!»", true);
+ assertEquals(ParagraphType.QUOTE, para.getType());
+ assertEquals(openDoubleQuote + "Yes, my Lord!"
+ + closeDoubleQuote, para.getContent());
+
+ para = support.processPara("'Yes, my Lord!'", true);
+ assertEquals(ParagraphType.QUOTE, para.getType());
+ assertEquals(openQuote + "Yes, my Lord!" + closeQuote,
+ para.getContent());
+
+ para = support.processPara("â¹Yes, my Lord!âº", true);
+ assertEquals(ParagraphType.QUOTE, para.getType());
+ assertEquals(openQuote + "Yes, my Lord!" + closeQuote,
+ para.getContent());
+ }
+ });
+
+ addTest(new TestCase(
+ "BasicSupport.processPara() double-simple quotes") {
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+
+ super.tearDown();
+ }
+
+ @Override
+ public void test() throws Exception {
+ BasicSupportParaPublic support = new BasicSupportParaPublic();
+
+ Paragraph para;
+
+ para = support.processPara("''Yes, my Lord!''", true);
+ assertEquals(ParagraphType.QUOTE, para.getType());
+ assertEquals(openDoubleQuote + "Yes, my Lord!"
+ + closeDoubleQuote, para.getContent());
+
+ para = support.processPara("â¹â¹Yes, my Lord!âºâº", true);
+ assertEquals(ParagraphType.QUOTE, para.getType());
+ assertEquals(openDoubleQuote + "Yes, my Lord!"
+ + closeDoubleQuote, para.getContent());
+ }
+ });
+
+ addTest(new TestCase("BasicSupport.processPara() apostrophe") {
+ @Override
+ public void test() throws Exception {
+ BasicSupportParaPublic support = new BasicSupportParaPublic();
+
+ Paragraph para;
+
+ String text = "Nous étions en été, mais cela aurait être l'hiver quand nous n'étions encore qu'à Aubeuge";
+ para = support.processPara(text, true);
+ assertEquals(ParagraphType.NORMAL, para.getType());
+ assertEquals(text, para.getContent());
+ }
+ });
+
+ addTest(new TestCase("BasicSupport.processPara() words count") {
+ @Override
+ public void test() throws Exception {
+ BasicSupportParaPublic support = new BasicSupportParaPublic();
+
+ Paragraph para;
+
+ para = support.processPara("«Yes, my Lord!»", true);
+ assertEquals(3, para.getWords());
+
+ para = support.processPara("One, twee, trois.", true);
+ assertEquals(3, para.getWords());
+ }
+ });
+
+ addTest(new TestCase("BasicSupport.requotify() words count") {
+ @Override
+ public void test() throws Exception {
+ BasicSupportParaPublic support = new BasicSupportParaPublic();
+
+ char openDoubleQuote = Instance.getTrans()
+ .getCharacter(StringId.OPEN_DOUBLE_QUOTE);
+ char closeDoubleQuote = Instance.getTrans()
+ .getCharacter(StringId.CLOSE_DOUBLE_QUOTE);
+
+ String content = null;
+ Paragraph para = null;
+ List paras = null;
+ long words = 0;
+
+ content = "One, twee, trois.";
+ para = new Paragraph(ParagraphType.NORMAL, content,
+ content.split(" ").length);
+ paras = support.requotify(para, false);
+ words = 0;
+ for (Paragraph p : paras) {
+ words += p.getWords();
+ }
+ assertEquals("Bad words count in a single paragraph",
+ para.getWords(), words);
+
+ content = "Such WoW! So Web2.0! With Colours!";
+ para = new Paragraph(ParagraphType.NORMAL, content,
+ content.split(" ").length);
+ paras = support.requotify(para, false);
+ words = 0;
+ for (Paragraph p : paras) {
+ words += p.getWords();
+ }
+ assertEquals("Bad words count in a single paragraph",
+ para.getWords(), words);
+
+ content = openDoubleQuote + "Such a good idea!"
+ + closeDoubleQuote
+ + ", she said. This ought to be a new para.";
+ para = new Paragraph(ParagraphType.QUOTE, content,
+ content.split(" ").length);
+ paras = support.requotify(para, false);
+ words = 0;
+ for (Paragraph p : paras) {
+ words += p.getWords();
+ }
+ assertEquals(
+ "Bad words count in a requotified paragraph",
+ para.getWords(), words);
+ }
+ });
+ }
+ });
+
+ addSeries(new TestLauncher("Text", args) {
+ {
+ addTest(new TestCase("Chapter detection simple") {
+ private File tmp;
+
+ @Override
+ public void setUp() throws Exception {
+ tmp = File.createTempFile("fanfix-text-file_", ".test");
+ IOUtils.writeSmallFile(tmp.getParentFile(),
+ tmp.getName(), "TITLE"
+ + "\n"//
+ + "By nona"
+ + "\n" //
+ + "\n" //
+ + "Chapter 0: Resumé" + "\n" + "\n"
+ + "'sume." + "\n" + "\n"
+ + "Chapter 1: chap1" + "\n" + "\n"
+ + "Fanfan." + "\n" + "\n"
+ + "Chapter 2: Chap2" + "\n" + "\n" //
+ + "Tulipe." + "\n");
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ tmp.delete();
+ }
+
+ @Override
+ public void test() throws Exception {
+ BasicSupport support = BasicSupport.getSupport(
+ SupportType.TEXT, tmp.toURI().toURL());
+
+ Story story = support.process(null);
+
+ assertEquals(2, story.getChapters().size());
+ assertEquals(1, story.getChapters().get(1)
+ .getParagraphs().size());
+ assertEquals("Tulipe.", story.getChapters().get(1)
+ .getParagraphs().get(0).getContent());
+ }
+ });
+
+ addTest(new TestCase("Chapter detection with String 'Chapter'") {
+ private File tmp;
+
+ @Override
+ public void setUp() throws Exception {
+ tmp = File.createTempFile("fanfix-text-file_", ".test");
+ IOUtils.writeSmallFile(tmp.getParentFile(),
+ tmp.getName(), "TITLE"
+ + "\n"//
+ + "By nona"
+ + "\n" //
+ + "\n" //
+ + "Chapter 0: Resumé" + "\n" + "\n"
+ + "'sume." + "\n" + "\n"
+ + "Chapter 1: chap1" + "\n" + "\n"
+ + "Chapter fout-la-merde" + "\n"
+ + "\n"//
+ + "Fanfan." + "\n" + "\n"
+ + "Chapter 2: Chap2" + "\n" + "\n" //
+ + "Tulipe." + "\n");
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ tmp.delete();
+ }
+
+ @Override
+ public void test() throws Exception {
+ BasicSupport support = BasicSupport.getSupport(
+ SupportType.TEXT, tmp.toURI().toURL());
+
+ Story story = support.process(null);
+
+ assertEquals(2, story.getChapters().size());
+ assertEquals(1, story.getChapters().get(1)
+ .getParagraphs().size());
+ assertEquals("Tulipe.", story.getChapters().get(1)
+ .getParagraphs().get(0).getContent());
+ }
+ });
+ }
+ });
+ }
+
+ class BasicSupportParaPublic extends BasicSupportPara {
+ public BasicSupportParaPublic() {
+ super(new BasicSupportHelper(), new BasicSupportImages());
+ }
+
+ @Override
+ // and make it public!
+ public Paragraph makeParagraph(BasicSupport support, URL source,
+ String line, boolean html) {
+ return super.makeParagraph(support, source, line, html);
+ }
+
+ @Override
+ // and make it public!
+ public List makeParagraphs(BasicSupport support,
+ URL source, String content, boolean html, Progress pg)
+ throws IOException {
+ return super.makeParagraphs(support, source, content, html, pg);
+ }
+
+ @Override
+ // and make it public!
+ public Paragraph processPara(String line, boolean html) {
+ return super.processPara(line, html);
+ }
+
+ @Override
+ // and make it public!
+ public List requotify(Paragraph para, boolean html) {
+ return super.requotify(para, html);
+ }
+ }
+}
diff --git a/src/be/nikiroo/fanfix/test/Test.java b/src/be/nikiroo/fanfix/test/Test.java
index 614cec1..05edcb0 100644
--- a/src/be/nikiroo/fanfix/test/Test.java
+++ b/src/be/nikiroo/fanfix/test/Test.java
@@ -33,7 +33,8 @@ public class Test extends TestLauncher {
public Test(String[] args) {
super("Fanfix", args);
Instance.setTraceHandler(null);
- addSeries(new BasicSupportTest(args));
+ addSeries(new BasicSupportUtilitiesTest(args));
+ addSeries(new BasicSupportDeprecatedTest(args));
addSeries(new LibraryTest(args));
addSeries(new ConversionTest(args));
}
@@ -47,6 +48,8 @@ public class Test extends TestLauncher {
* in case of I/O error
*/
static public void main(String[] args) throws IOException {
+ Instance.init();
+
int result = 0;
tempFiles = new TempFiles("fanfix-test");
try {