From 9b75402fd752653f27158f1c1775a062672c7b27 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Tue, 5 May 2020 18:27:33 +0200 Subject: [PATCH] woops, forgot half of them --- Instance.java | 16 +- Main.java | 168 +++++----- bundles/Config.java | 3 - reader/BasicReader.java | 156 +-------- reader/Reader.java | 267 ---------------- test/BasicSupportDeprecatedTest.java | 451 --------------------------- test/BasicSupportUtilitiesTest.java | 397 ----------------------- test/ConversionTest.java | 284 ----------------- test/LibraryTest.java | 258 --------------- test/Test.java | 177 ----------- 10 files changed, 86 insertions(+), 2091 deletions(-) delete mode 100644 reader/Reader.java delete mode 100644 test/BasicSupportDeprecatedTest.java delete mode 100644 test/BasicSupportUtilitiesTest.java delete mode 100644 test/ConversionTest.java delete mode 100644 test/LibraryTest.java delete mode 100644 test/Test.java diff --git a/Instance.java b/Instance.java index 5c00a07..c3c086f 100644 --- a/Instance.java +++ b/Instance.java @@ -284,9 +284,9 @@ public class Instance { } /** - * Get the (unique) {@link LocalLibrary} for the program. + * Get the (unique) {@link BasicLibrary} for the program. * - * @return the {@link LocalLibrary} + * @return the {@link BasicLibrary} */ public BasicLibrary getLibrary() { if (lib == null) { @@ -296,6 +296,18 @@ public class Instance { return lib; } + /** + * Change the default {@link BasicLibrary} for this program. + *

+ * Be careful. + * + * @param lib + * the new {@link BasicLibrary} + */ + public void setLibrary(BasicLibrary lib) { + this.lib = lib; + } + /** * Return the directory where to look for default cover pages. * diff --git a/Main.java b/Main.java index 961816a..8d72803 100644 --- a/Main.java +++ b/Main.java @@ -22,8 +22,7 @@ import be.nikiroo.fanfix.library.RemoteLibraryServer; import be.nikiroo.fanfix.output.BasicOutput; import be.nikiroo.fanfix.output.BasicOutput.OutputType; import be.nikiroo.fanfix.reader.BasicReader; -import be.nikiroo.fanfix.reader.Reader; -import be.nikiroo.fanfix.reader.Reader.ReaderType; +import be.nikiroo.fanfix.reader.CliReader; import be.nikiroo.fanfix.searchable.BasicSearchable; import be.nikiroo.fanfix.supported.BasicSupport; import be.nikiroo.fanfix.supported.SupportType; @@ -38,7 +37,7 @@ import be.nikiroo.utils.serial.server.ServerObject; */ public class Main { private enum MainAction { - IMPORT, EXPORT, CONVERT, READ, READ_URL, LIST, HELP, SET_READER, START, VERSION, SERVER, STOP_SERVER, REMOTE, SET_SOURCE, SET_TITLE, SET_AUTHOR, SEARCH, SEARCH_TAG + IMPORT, EXPORT, CONVERT, READ, READ_URL, LIST, HELP, START, VERSION, SERVER, STOP_SERVER, REMOTE, SET_SOURCE, SET_TITLE, SET_AUTHOR, SEARCH, SEARCH_TAG } /** @@ -76,8 +75,6 @@ public class Main { *

  • --set-source [id] [new source]: change the source of the given story
  • *
  • --set-title [id] [new title]: change the title of the given story
  • *
  • --set-author [id] [new author]: change the author of the given story
  • - *
  • --set-reader [reader type]: set the reader type to CLI, TUI or LOCAL - * for this command
  • *
  • --version: get the version of the program
  • *
  • --server: start the server mode (see config file for parameters)
  • *
  • --stop-server: stop the running server on this port if any
  • @@ -314,10 +311,6 @@ public class Main { case HELP: exitCode = 255; break; - case SET_READER: - exitCode = setReaderType(args[i]); - action = MainAction.START; - break; case START: exitCode = 255; // not supposed to be selected by user break; @@ -339,10 +332,11 @@ public class Main { port = Integer.parseInt(args[i]); BasicLibrary lib = new RemoteLibrary(key, host, port); - lib = new CacheLibrary(Instance.getInstance().getRemoteDir(host), lib, + lib = new CacheLibrary( + Instance.getInstance().getRemoteDir(host), lib, Instance.getInstance().getUiConfig()); - BasicReader.setDefaultLibrary(lib); + Instance.getInstance().setLibrary(lib); action = MainAction.START; } else { @@ -413,11 +407,6 @@ public class Main { updates.ok(); // we consider it read break; case LIST: - if (BasicReader.getReader() == null) { - Instance.getInstance().getTraceHandler().error(new Exception("No reader type has been configured")); - exitCode = 10; - break; - } exitCode = list(sourceString); break; case SET_SOURCE: @@ -445,20 +434,46 @@ public class Main { } break; case READ: - if (BasicReader.getReader() == null) { - Instance.getInstance().getTraceHandler().error(new Exception("No reader type has been configured")); - exitCode = 10; + if (luid == null || luid.isEmpty()) { + syntax(false); + exitCode = 255; break; } - exitCode = read(luid, chapString, true); + + try { + BasicLibrary lib = Instance.getInstance().getLibrary(); + exitCode = read(lib.getStory(luid, null), chapString); + } catch (IOException e) { + Instance.getInstance().getTraceHandler() + .error(new IOException("Failed to read book", e)); + exitCode = 2; + } + break; case READ_URL: - if (BasicReader.getReader() == null) { - Instance.getInstance().getTraceHandler().error(new Exception("No reader type has been configured")); - exitCode = 10; + if (urlString == null || urlString.isEmpty()) { + syntax(false); + exitCode = 255; break; } - exitCode = read(urlString, chapString, false); + + try { + BasicSupport support = BasicSupport + .getSupport(BasicReader.getUrl(urlString)); + if (support == null) { + Instance.getInstance().getTraceHandler() + .error("URL not supported: " + urlString); + exitCode = 2; + break; + } + + exitCode = read(support.process(null), chapString); + } catch (IOException e) { + Instance.getInstance().getTraceHandler() + .error(new IOException("Failed to read book", e)); + exitCode = 2; + } + break; case SEARCH: page = page == null ? 1 : page; @@ -475,19 +490,13 @@ public class Main { break; } - if (BasicReader.getReader() == null) { - Instance.getInstance().getTraceHandler().error(new Exception("No reader type has been configured")); - exitCode = 10; - break; - } - try { if (searchOn == null) { - BasicReader.getReader().search(true); + new CliReader().listSearchables(); } else if (search != null) { - BasicReader.getReader().search(searchOn, search, page, - item, true); + new CliReader().searchBooksByKeyword(searchOn, search, page, + item); } else { exitCode = 255; } @@ -517,15 +526,9 @@ public class Main { break; } - if (BasicReader.getReader() == null) { - Instance.getInstance().getTraceHandler().error(new Exception("No reader type has been configured")); - exitCode = 10; - break; - } - try { - BasicReader.getReader().searchTag(searchOn, page, item, - true, tags.toArray(new Integer[] {})); + new CliReader().searchBooksByTag(searchOn, page, item, + tags.toArray(new Integer[] {})); } catch (IOException e1) { Instance.getInstance().getTraceHandler().error(e1); } @@ -535,9 +538,6 @@ public class Main { syntax(true); exitCode = 0; break; - case SET_READER: - exitCode = 255; - break; case VERSION: System.out .println(String.format("Fanfix version %s" @@ -547,13 +547,8 @@ public class Main { updates.ok(); // we consider it read break; case START: - if (BasicReader.getReader() == null) { - Instance.getInstance().getTraceHandler().error(new Exception("No reader type has been configured")); - exitCode = 10; - break; - } try { - BasicReader.getReader().browse(null); + new CliReader().listBooks(null); } catch (IOException e) { Instance.getInstance().getTraceHandler().error(e); exitCode = 66; @@ -684,9 +679,8 @@ public class Main { * @return the exit return code (0 = success) */ private static int list(String source) { - BasicReader.setDefaultReaderType(ReaderType.CLI); try { - BasicReader.getReader().browse(source); + new CliReader().listBooks(source); } catch (IOException e) { Instance.getInstance().getTraceHandler().error(e); return 66; @@ -699,41 +693,41 @@ public class Main { * Start the current reader for this {@link Story}. * * @param story - * the LUID of the {@link Story} in the {@link LocalLibrary} - * or the {@link Story} {@link URL} + * the story to read * @param chapString * which {@link Chapter} to read (starting at 1), or NULL to get * the {@link Story} description - * @param library - * TRUE if the source is the {@link Story} LUID, FALSE if it is a - * {@link URL} * * @return the exit return code (0 = success) */ - private static int read(String story, String chapString, boolean library) { - try { - Reader reader = BasicReader.getReader(); - if (library) { - reader.setMeta(story); - } else { - reader.setMeta(BasicReader.getUrl(story), null); + private static int read(Story story, String chapString) { + Integer chap = null; + if (chapString != null) { + try { + chap = Integer.parseInt(chapString); + } catch (NumberFormatException e) { + Instance.getInstance().getTraceHandler().error(new IOException( + "Chapter number cannot be parsed: " + chapString, e)); + return 2; } + } - if (chapString != null) { - try { - reader.setChapter(Integer.parseInt(chapString)); - reader.read(true); - } catch (NumberFormatException e) { - Instance.getInstance().getTraceHandler() - .error(new IOException("Chapter number cannot be parsed: " + chapString, e)); - return 2; + if (story != null) { + try { + if (chap == null) { + new CliReader().listChapters(story); + } else { + new CliReader().printChapter(story, chap); } - } else { - reader.read(true); + } catch (IOException e) { + Instance.getInstance().getTraceHandler() + .error(new IOException("Failed to read book", e)); + return 2; } - } catch (IOException e) { - Instance.getInstance().getTraceHandler().error(e); - return 1; + } else { + Instance.getInstance().getTraceHandler() + .error("Cannot find book: " + chapString); + return 2; } return 0; @@ -862,24 +856,4 @@ public class Main { System.err.println(trans(StringId.ERR_SYNTAX)); } } - - /** - * Set the default reader type for this session only (it can be changed in - * the configuration file, too, but this value will override it). - * - * @param readerTypeString - * the type - */ - private static int setReaderType(String readerTypeString) { - try { - ReaderType readerType = ReaderType.valueOf(readerTypeString - .toUpperCase()); - BasicReader.setDefaultReaderType(readerType); - return 0; - } catch (IllegalArgumentException e) { - Instance.getInstance().getTraceHandler() - .error(new IOException("Unknown reader type: " + readerTypeString, e)); - return 1; - } - } } diff --git a/bundles/Config.java b/bundles/Config.java index fd27a83..587851a 100644 --- a/bundles/Config.java +++ b/bundles/Config.java @@ -16,9 +16,6 @@ public enum Config { @Meta(description = "The language to use for in the program (example: en-GB, fr-BE...) or nothing for default system language (can be overwritten with the variable $LANG)",// format = Format.LOCALE, list = { "en-GB", "fr-BE" }) LANG, // - @Meta(description = "The default reader type to use to read stories:\nCLI = simple output to console\nTUI = a Text User Interface with menus and windows, based upon Jexer\nGUI = a GUI with locally stored files, based upon Swing", // - hidden = true, format = Format.FIXED_LIST, list = { "CLI", "GUI", "TUI" }, def = "GUI") - READER_TYPE, // @Meta(description = "File format options",// group = true) diff --git a/reader/BasicReader.java b/reader/BasicReader.java index 7f79da3..9ec0879 100644 --- a/reader/BasicReader.java +++ b/reader/BasicReader.java @@ -11,171 +11,18 @@ import java.util.Map; import java.util.TreeMap; import be.nikiroo.fanfix.Instance; -import be.nikiroo.fanfix.bundles.Config; import be.nikiroo.fanfix.bundles.UiConfig; import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.data.Story; import be.nikiroo.fanfix.library.BasicLibrary; -import be.nikiroo.fanfix.library.LocalLibrary; -import be.nikiroo.fanfix.supported.BasicSupport; -import be.nikiroo.utils.Progress; import be.nikiroo.utils.StringUtils; -import be.nikiroo.utils.serial.SerialUtils; /** * The class that handles the different {@link Story} readers you can use. - *

    - * All the readers should be accessed via {@link BasicReader#getReader()}. * * @author niki */ -public abstract class BasicReader implements Reader { - private static BasicLibrary defaultLibrary = Instance.getInstance().getLibrary(); - private static ReaderType defaultType = ReaderType.GUI; - - private BasicLibrary lib; - private MetaData meta; - private Story story; - private int chapter; - - /** - * Take the default reader type configuration from the config file. - */ - static { - String typeString = Instance.getInstance().getConfig().getString(Config.READER_TYPE); - if (typeString != null && !typeString.isEmpty()) { - try { - ReaderType type = ReaderType.valueOf(typeString.toUpperCase()); - defaultType = type; - } catch (IllegalArgumentException e) { - // Do nothing - } - } - } - - @Override - public synchronized Story getStory(Progress pg) throws IOException { - if (story == null) { - story = getLibrary().getStory(meta.getLuid(), pg); - } - - return story; - } - - @Override - public BasicLibrary getLibrary() { - if (lib == null) { - lib = defaultLibrary; - } - - return lib; - } - - @Override - public void setLibrary(BasicLibrary lib) { - this.lib = lib; - } - - @Override - public synchronized MetaData getMeta() { - return meta; - } - - @Override - public synchronized void setMeta(MetaData meta) throws IOException { - setMeta(meta == null ? null : meta.getLuid()); // must check the library - } - - @Override - public synchronized void setMeta(String luid) throws IOException { - story = null; - meta = getLibrary().getInfo(luid); - - if (meta == null) { - throw new IOException("Cannot retrieve story from library: " + luid); - } - } - - @Override - public synchronized void setMeta(URL url, Progress pg) throws IOException { - BasicSupport support = BasicSupport.getSupport(url); - if (support == null) { - throw new IOException("URL not supported: " + url.toString()); - } - - story = support.process(pg); - if (story == null) { - throw new IOException( - "Cannot retrieve story from external source: " - + url.toString()); - } - - meta = story.getMeta(); - } - - @Override - public int getChapter() { - return chapter; - } - - @Override - public void setChapter(int chapter) { - this.chapter = chapter; - } - - /** - * Return a new {@link BasicReader} ready for use if one is configured. - *

    - * Can return NULL if none are configured. - * - * @return a {@link BasicReader}, or NULL if none configured - */ - public static Reader getReader() { - try { - if (defaultType != null) { - return (Reader) SerialUtils.createObject(defaultType - .getTypeName()); - } - } catch (Exception e) { - Instance.getInstance().getTraceHandler() - .error(new Exception("Cannot create a reader of type: " + defaultType + " (Not compiled in?)", e)); - } - - return null; - } - - /** - * The default {@link Reader.ReaderType} used when calling - * {@link BasicReader#getReader()}. - * - * @return the default type - */ - public static ReaderType getDefaultReaderType() { - return defaultType; - } - - /** - * The default {@link Reader.ReaderType} used when calling - * {@link BasicReader#getReader()}. - * - * @param defaultType - * the new default type - */ - public static void setDefaultReaderType(ReaderType defaultType) { - BasicReader.defaultType = defaultType; - } - - /** - * Change the default {@link LocalLibrary} to open with the - * {@link BasicReader}s. - * - * @param lib - * the new {@link LocalLibrary} - */ - public static void setDefaultLibrary(BasicLibrary lib) { - BasicReader.defaultLibrary = lib; - } - +public abstract class BasicReader { /** * Return an {@link URL} from this {@link String}, be it a file path or an * actual {@link URL}. @@ -263,7 +110,6 @@ public abstract class BasicReader implements Reader { * @throws IOException * in case of I/O error */ - @Override public void openExternal(BasicLibrary lib, String luid, boolean sync) throws IOException { MetaData meta = lib.getInfo(luid); diff --git a/reader/Reader.java b/reader/Reader.java deleted file mode 100644 index 3ecf247..0000000 --- a/reader/Reader.java +++ /dev/null @@ -1,267 +0,0 @@ -package be.nikiroo.fanfix.reader; - -import java.io.IOException; -import java.net.URL; - -import be.nikiroo.fanfix.data.MetaData; -import be.nikiroo.fanfix.data.Story; -import be.nikiroo.fanfix.library.BasicLibrary; -import be.nikiroo.fanfix.supported.SupportType; -import be.nikiroo.utils.Progress; - -/** - * A {@link Reader} is a class that will handle {@link Story} reading and - * browsing. - * - * @author niki - */ -public interface Reader { - /** - * A type of {@link BasicReader}. - * - * @author niki - */ - public enum ReaderType { - /** Simple reader that outputs everything on the console */ - CLI, - /** Reader that starts local programs to handle the stories */ - GUI, - /** A text (UTF-8) reader with menu and text windows */ - TUI, - /** A GUI reader implemented with the Android framework */ - ANDROID, - - ; - - /** - * Return the full class name of a type that implements said - * {@link ReaderType}. - * - * @return the class name - */ - public String getTypeName() { - String pkg = "be.nikiroo.fanfix.reader."; - switch (this) { - case CLI: - return pkg + "cli.CliReader"; - case TUI: - return pkg + "tui.TuiReader"; - case GUI: - return pkg + "ui.GuiReader"; - case ANDROID: - return pkg + "android.AndroidReader"; - } - - return null; - } - } - - /** - * Return the current target {@link MetaData}. - * - * @return the meta - */ - public MetaData getMeta(); - - /** - * Return the current {@link Story} as described by the current - * {@link MetaData}. - * - * @param pg - * the optional progress - * - * @return the {@link Story} - * - * @throws IOException - * in case of I/O error - * - */ - public Story getStory(Progress pg) throws IOException; - - /** - * The {@link BasicLibrary} to load the stories from (by default, takes the - * default {@link BasicLibrary}). - * - * @return the {@link BasicLibrary} - */ - public BasicLibrary getLibrary(); - - /** - * Change the {@link BasicLibrary} that will be managed by this - * {@link BasicReader}. - * - * @param lib - * the new {@link BasicLibrary} - */ - public void setLibrary(BasicLibrary lib); - - /** - * Set a {@link Story} from the current {@link BasicLibrary} into the - * {@link Reader}. - * - * @param luid - * the {@link Story} ID - * - * @throws IOException - * in case of I/O error - */ - public void setMeta(String luid) throws IOException; - - /** - * Set a {@link Story} from the current {@link BasicLibrary} into the - * {@link Reader}. - * - * @param meta - * the meta - * - * @throws IOException - * in case of I/O error - */ - public void setMeta(MetaData meta) throws IOException; - - /** - * Set an external {@link Story} into this {@link Reader}. - * - * @param source - * the {@link Story} {@link URL} - * @param pg - * the optional progress reporter - * - * @throws IOException - * in case of I/O error - */ - public void setMeta(URL source, Progress pg) throws IOException; - - /** - * Start the {@link Story} Reading. - * - * @param sync - * execute the process synchronously (wait until it is terminated - * before returning) - * - * @throws IOException - * in case of I/O error or if the {@link Story} was not - * previously set - */ - public void read(boolean sync) throws IOException; - - /** - * The selected chapter to start reading at (starting at 1, 0 = description, - * -1 = none). - * - * @return the chapter, or -1 for "no chapter" - */ - public int getChapter(); - - /** - * The selected chapter to start reading at (starting at 1, 0 = description, - * -1 = none). - * - * @param chapter - * the chapter, or -1 for "no chapter" - */ - public void setChapter(int chapter); - - /** - * Start the reader in browse mode for the given source (or pass NULL for - * all sources). - *

    - * Note that this must be a synchronous action. - * - * @param source - * the type of {@link Story} to take into account, or NULL for - * all - * - * @throws IOException - * in case of I/O error - */ - public void browse(String source) throws IOException; - - /** - * Display all supports that allow search operations. - * - * @param sync - * execute the process synchronously (wait until it is terminated - * before returning) - * - * @throws IOException - * in case of I/O error - */ - public void search(boolean sync) throws IOException; - - /** - * Search for the given terms and find stories that correspond if possible. - * - * @param searchOn - * the website to search on - * @param keywords - * the words to search for (cannot be NULL) - * @param page - * the page of results to show (0 = request the maximum number of - * pages, pages start at 1) - * @param item - * the item to select (0 = do not select a specific item but show - * all the page, items start at 1) - * @param sync - * execute the process synchronously (wait until it is terminated - * before returning) - * - * @throws IOException - * in case of I/O error - */ - public void search(SupportType searchOn, String keywords, int page, - int item, boolean sync) throws IOException; - - /** - * Search based upon a hierarchy of tags, or search for (sub)tags. - *

    - * We use the tags DisplayName. - *

    - * If no tag is given, the main tags will be shown. - *

    - * If a non-leaf tag is given, the subtags will be shown. - *

    - * If a leaf tag is given (or a full hierarchy ending with a leaf tag), - * stories will be shown. - *

    - * You can select the story you want with the item number. - * - * @param searchOn - * the website to search on - * @param page - * the page of results to show (0 = request the maximum number of - * pages, pages start at 1) - * @param item - * the item to select (0 = do not select a specific item but show - * all the page, items start at 1) - * @param sync - * execute the process synchronously (wait until it is terminated - * before returning) - * @param tags - * the tags indices to search for (this is a tag - * hierarchy, NOT a multiple tags choice) - * - * @throws IOException - * in case of I/O error - */ - public void searchTag(SupportType searchOn, int page, int item, - boolean sync, Integer... tags) throws IOException; - - /** - * Open the {@link Story} with an external reader (the program should be - * passed the main file associated with this {@link Story}). - * - * @param lib - * the {@link BasicLibrary} to select the {@link Story} from - * @param luid - * the {@link Story} LUID - * @param sync - * execute the process synchronously (wait until it is terminated - * before returning) - * - * @throws IOException - * in case of I/O error - */ - public void openExternal(BasicLibrary lib, String luid, boolean sync) - throws IOException; -} diff --git a/test/BasicSupportDeprecatedTest.java b/test/BasicSupportDeprecatedTest.java deleted file mode 100644 index c8c4b40..0000000 --- a/test/BasicSupportDeprecatedTest.java +++ /dev/null @@ -1,451 +0,0 @@ -package be.nikiroo.fanfix.test; - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.ArrayList; -import java.util.List; -import java.util.Map.Entry; - -import be.nikiroo.fanfix.Instance; -import be.nikiroo.fanfix.bundles.StringId; -import be.nikiroo.fanfix.data.MetaData; -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.BasicSupport_Deprecated; -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 BasicSupportDeprecatedTest extends TestLauncher { - // quote chars - private char openQuote = Instance.getInstance().getTrans().getCharacter(StringId.OPEN_SINGLE_QUOTE); - private char closeQuote = Instance.getInstance().getTrans().getCharacter(StringId.CLOSE_SINGLE_QUOTE); - private char openDoubleQuote = Instance.getInstance().getTrans().getCharacter(StringId.OPEN_DOUBLE_QUOTE); - private char closeDoubleQuote = Instance.getInstance().getTrans().getCharacter(StringId.CLOSE_DOUBLE_QUOTE); - - public BasicSupportDeprecatedTest(String[] args) { - super("BasicSupportDeprecated", args); - - addSeries(new TestLauncher("General", args) { - { - addTest(new TestCase("BasicSupport.makeParagraphs()") { - @Override - public void test() throws Exception { - BasicSupportEmpty support = new BasicSupportEmpty() { - @Override - protected boolean isHtml() { - return true; - } - - @Override - public void fixBlanksBreaks(List paras) { - } - - @Override - public List requotify(Paragraph para) { - List paras = new ArrayList( - 1); - paras.add(para); - return paras; - } - }; - - List paras = null; - - paras = support.makeParagraphs(null, "", null); - assertEquals( - "An empty content should not generate paragraphs", - 0, paras.size()); - - paras = support.makeParagraphs(null, - "Line 1

    Line 2

    Line 3

    ", 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 = support.makeParagraphs(null, - "

    Line1

    Line2

    Line3

    ", null); - assertEquals(6, paras.size()); - } - }); - - addTest(new TestCase("BasicSupport.removeDoubleBlanks()") { - @Override - public void test() throws Exception { - BasicSupportEmpty support = new BasicSupportEmpty() { - @Override - protected boolean isHtml() { - return true; - } - }; - - List paras = null; - - paras = support - .makeParagraphs( - null, - "

    Line1

    Line2

    Line3

    ", - null); - assertEquals(5, paras.size()); - - paras = support - .makeParagraphs( - null, - "

    Line1

    Line2

    Line3

    * * *", - null); - assertEquals(5, paras.size()); - - paras = support.makeParagraphs(null, "1

    * * *

    2", - null); - assertEquals(3, paras.size()); - assertEquals(ParagraphType.BREAK, paras.get(1) - .getType()); - - paras = support.makeParagraphs(null, - "1


    * * *

    2", null); - assertEquals(3, paras.size()); - assertEquals(ParagraphType.BREAK, paras.get(1) - .getType()); - - paras = support.makeParagraphs(null, - "1

    * * *


    2", null); - assertEquals(3, paras.size()); - assertEquals(ParagraphType.BREAK, paras.get(1) - .getType()); - - paras = support.makeParagraphs(null, - "1



    * * *


    2", null); - assertEquals(3, paras.size()); - assertEquals(ParagraphType.BREAK, paras.get(1) - .getType()); - } - }); - - addTest(new TestCase("BasicSupport.processPara() quotes") { - @Override - public void test() throws Exception { - BasicSupportEmpty support = new BasicSupportEmpty() { - @Override - protected boolean isHtml() { - return true; - } - }; - - Paragraph para; - - // sanity check - para = support.processPara(""); - assertEquals(ParagraphType.BLANK, para.getType()); - // - - para = support.processPara("\"Yes, my Lord!\""); - assertEquals(ParagraphType.QUOTE, para.getType()); - assertEquals(openDoubleQuote + "Yes, my Lord!" - + closeDoubleQuote, para.getContent()); - - para = support.processPara("«Yes, my Lord!»"); - assertEquals(ParagraphType.QUOTE, para.getType()); - assertEquals(openDoubleQuote + "Yes, my Lord!" - + closeDoubleQuote, para.getContent()); - - para = support.processPara("'Yes, my Lord!'"); - assertEquals(ParagraphType.QUOTE, para.getType()); - assertEquals(openQuote + "Yes, my Lord!" + closeQuote, - para.getContent()); - - para = support.processPara("‹Yes, my Lord!›"); - 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 { - BasicSupportEmpty support = new BasicSupportEmpty() { - @Override - protected boolean isHtml() { - return true; - } - }; - - Paragraph para; - - para = support.processPara("''Yes, my Lord!''"); - assertEquals(ParagraphType.QUOTE, para.getType()); - assertEquals(openDoubleQuote + "Yes, my Lord!" - + closeDoubleQuote, para.getContent()); - - para = support.processPara("‹‹Yes, my Lord!››"); - 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 { - BasicSupportEmpty support = new BasicSupportEmpty() { - @Override - protected boolean isHtml() { - return true; - } - }; - - 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); - assertEquals(ParagraphType.NORMAL, para.getType()); - assertEquals(text, para.getContent()); - } - }); - - addTest(new TestCase("BasicSupport.processPara() words count") { - @Override - public void test() throws Exception { - BasicSupportEmpty support = new BasicSupportEmpty() { - @Override - protected boolean isHtml() { - return true; - } - }; - - Paragraph para; - - para = support.processPara("«Yes, my Lord!»"); - assertEquals(3, para.getWords()); - - para = support.processPara("One, twee, trois."); - assertEquals(3, para.getWords()); - } - }); - - addTest(new TestCase("BasicSupport.requotify() words count") { - @Override - public void test() throws Exception { - BasicSupportEmpty support = new BasicSupportEmpty(); - - char openDoubleQuote = Instance.getInstance().getTrans() - .getCharacter(StringId.OPEN_DOUBLE_QUOTE); - char closeDoubleQuote = Instance.getInstance().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); - 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); - 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); - 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()); - } - }); - } - }); - } - - private class BasicSupportEmpty extends BasicSupport_Deprecated { - @Override - protected boolean supports(URL url) { - return false; - } - - @Override - protected boolean isHtml() { - return false; - } - - @Override - protected MetaData getMeta(URL source, InputStream in) - throws IOException { - return null; - } - - @Override - protected String getDesc(URL source, InputStream in) throws IOException { - return null; - } - - @Override - protected List> getChapters(URL source, - InputStream in, Progress pg) throws IOException { - return null; - } - - @Override - protected String getChapterContent(URL source, InputStream in, - int number, Progress pg) throws IOException { - return null; - } - - @Override - // and make it public! - public List makeParagraphs(URL source, String content, - Progress pg) throws IOException { - return super.makeParagraphs(source, content, pg); - } - - @Override - // and make it public! - public void fixBlanksBreaks(List paras) { - super.fixBlanksBreaks(paras); - } - - @Override - // and make it public! - public Paragraph processPara(String line) { - return super.processPara(line); - } - - @Override - // and make it public! - public List requotify(Paragraph para) { - return super.requotify(para); - } - } -} diff --git a/test/BasicSupportUtilitiesTest.java b/test/BasicSupportUtilitiesTest.java deleted file mode 100644 index 9cec220..0000000 --- a/test/BasicSupportUtilitiesTest.java +++ /dev/null @@ -1,397 +0,0 @@ -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.getInstance().getTrans().getCharacter(StringId.OPEN_SINGLE_QUOTE); - private char closeQuote = Instance.getInstance().getTrans().getCharacter(StringId.CLOSE_SINGLE_QUOTE); - private char openDoubleQuote = Instance.getInstance().getTrans().getCharacter(StringId.OPEN_DOUBLE_QUOTE); - private char closeDoubleQuote = Instance.getInstance().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.getInstance().getTrans() - .getCharacter(StringId.OPEN_DOUBLE_QUOTE); - char closeDoubleQuote = Instance.getInstance().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/test/ConversionTest.java b/test/ConversionTest.java deleted file mode 100644 index d730b3b..0000000 --- a/test/ConversionTest.java +++ /dev/null @@ -1,284 +0,0 @@ -package be.nikiroo.fanfix.test; - -import java.io.File; -import java.io.FilenameFilter; -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 be.nikiroo.fanfix.Instance; -import be.nikiroo.fanfix.Main; -import be.nikiroo.fanfix.output.BasicOutput; -import be.nikiroo.utils.IOUtils; -import be.nikiroo.utils.TraceHandler; -import be.nikiroo.utils.test.TestCase; -import be.nikiroo.utils.test.TestLauncher; - -class ConversionTest extends TestLauncher { - private String testUri; - private String expectedDir; - private String resultDir; - private List realTypes; - private Map> skipCompare; - private Map> skipCompareCross; - - 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(); - for (BasicOutput.OutputType type : BasicOutput.OutputType.values()) { - if (!BasicOutput.OutputType.SYSOUT.equals(type)) { - realTypes.add(type); - } - } - - 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 { - new File(expectedDir).mkdirs(); - new File(resultDir).mkdirs(); - assertEquals("The Expected directory \"" + expectedDir - + "\" cannot be created", true, - new File(expectedDir).exists()); - assertEquals("The Result directory \"" + resultDir - + "\" cannot be created", true, - new File(resultDir).exists()); - } - }); - - for (BasicOutput.OutputType type : realTypes) { - addTest(getTestFor(type)); - } - } - - @Override - protected void start() throws Exception { - skipCompare = new HashMap>(); - skipCompareCross = new HashMap>(); - - skipCompare.put("epb.ncx", Arrays.asList( - " ", - " ")); - 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(" ")); - skipCompareCross.put("index.html", - Arrays.asList("

    ")); - skipCompareCross.put("URL", Arrays.asList("")); - } - - @Override - protected void stop() throws Exception { - } - - private TestCase getTestFor(final BasicOutput.OutputType type) { - return new TestCase(type + " output mode") { - @Override - public void test() throws Exception { - File target = generate(this, testUri, new File(resultDir), type); - target = new File(target.getAbsolutePath() - + type.getDefaultExtension(false)); - - // Check conversion: - compareFiles(this, new File(expectedDir), new File(resultDir), - type, "Generate " + type); - - // LATEX not supported as input - if (BasicOutput.OutputType.LATEX.equals(type)) { - return; - } - - // Cross-checks: - for (BasicOutput.OutputType crossType : realTypes) { - 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, String testUri, File resultDir, - BasicOutput.OutputType type) throws Exception { - final List errors = new ArrayList(); - - TraceHandler previousTraceHandler = Instance.getInstance().getTraceHandler(); - Instance.getInstance().setTraceHandler(new TraceHandler(true, true, 0) { - @Override - public void error(String message) { - errors.add(message); - } - - @Override - public void error(Exception e) { - error(" "); - for (Throwable t = e; t != null; t = t.getCause()) { - error(((t == e) ? "(" : "..caused by: (") - + t.getClass().getSimpleName() + ") " - + t.getMessage()); - for (StackTraceElement s : t.getStackTrace()) { - error("\t" + s.toString()); - } - } - } - }); - - try { - File target = new File(resultDir, type.toString()); - int code = Main.convert(testUri, type.toString(), - target.getAbsolutePath(), false, null); - - String error = ""; - for (String err : errors) { - if (!error.isEmpty()) - error += "\n"; - error += err; - } - testCase.assertEquals("The conversion returned an error message: " - + error, 0, errors.size()); - if (code != 0) { - testCase.fail("The conversion failed with return code: " + code); - } - - return target; - } finally { - Instance.getInstance().setTraceHandler(previousTraceHandler); - } - } - - private void compareFiles(TestCase testCase, File expectedDir, - File resultDir, final BasicOutput.OutputType limitTiFiles, - final String errMess) throws Exception { - - Map> skipCompare = errMess.startsWith("Cross") ? this.skipCompareCross - : this.skipCompare; - - FilenameFilter filter = null; - if (limitTiFiles != null) { - filter = new FilenameFilter() { - @Override - public boolean accept(File dir, String name) { - return name.toLowerCase().startsWith( - limitTiFiles.toString().toLowerCase()); - } - }; - } - - List resultFiles; - List 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); - - for (int i = 0; i < resultFiles.size(); i++) { - File expected = new File(expectedDir, expectedFiles.get(i)); - File result = new File(resultDir, resultFiles.get(i)); - - testCase.assertEquals(errMess + ": type mismatch: expected a " - + (expected.isDirectory() ? "directory" : "file") - + ", received a " - + (result.isDirectory() ? "directory" : "file"), - expected.isDirectory(), result.isDirectory()); - - if (expected.isDirectory()) { - compareFiles(testCase, expected, result, null, errMess); - continue; - } - - if (expected.getName().endsWith(".cbz") - || expected.getName().endsWith(".epub")) { - File tmpExpected = Test.tempFiles.createTempDir(expected - .getName() + "[zip-content]"); - File tmpResult = Test.tempFiles.createTempDir(result.getName() - + "[zip-content]"); - IOUtils.unzip(expected, tmpExpected); - IOUtils.unzip(result, tmpResult); - compareFiles(testCase, tmpExpected, tmpResult, null, errMess); - } else { - List expectedLines = Arrays.asList(IOUtils - .readSmallFile(expected).split("\n")); - List resultLines = Arrays.asList(IOUtils.readSmallFile( - result).split("\n")); - - String name = expected.getAbsolutePath(); - if (name.startsWith(expectedDir.getAbsolutePath())) { - name = expectedDir.getName() - + 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); - - boolean skip = false; - for (Entry> skipThose : skipCompare - .entrySet()) { - for (String skipStart : skipThose.getValue()) { - if (name.endsWith(skipThose.getKey()) - && expectedLine.startsWith(skipStart) - && resultLine.startsWith(skipStart)) { - skip = true; - } - } - } - - if (skip) { - continue; - } - - testCase.assertEquals(errMess + ": line " + (j + 1) - + " is not the same in file " + name, expectedLine, - resultLine); - } - } - } - } -} diff --git a/test/LibraryTest.java b/test/LibraryTest.java deleted file mode 100644 index da44438..0000000 --- a/test/LibraryTest.java +++ /dev/null @@ -1,258 +0,0 @@ -package be.nikiroo.fanfix.test; - -import java.io.File; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import be.nikiroo.fanfix.data.Chapter; -import be.nikiroo.fanfix.data.MetaData; -import be.nikiroo.fanfix.data.Paragraph; -import be.nikiroo.fanfix.data.Story; -import be.nikiroo.fanfix.library.BasicLibrary; -import be.nikiroo.fanfix.library.LocalLibrary; -import be.nikiroo.fanfix.output.BasicOutput.OutputType; -import be.nikiroo.utils.IOUtils; -import be.nikiroo.utils.test.TestCase; -import be.nikiroo.utils.test.TestLauncher; - -class LibraryTest extends TestLauncher { - private BasicLibrary lib; - private File tmp; - - public LibraryTest(String[] args) { - super("Library", args); - - final String luid1 = "001"; // A - final String luid2 = "002"; // B - final String luid3 = "003"; // B then A, then B - final String name1 = "My story 1"; - final String name2 = "My story 2"; - final String name3 = "My story 3"; - final String name3ex = "My story 3 [edited]"; - final String source1 = "Source A"; - final String source2 = "Source B"; - final String author1 = "Unknown author"; - final String author2 = "Another other otter author"; - - final String errMess = "The resulting stories in the list are not what we expected"; - - addSeries(new TestLauncher("Local", args) { - { - addTest(new TestCase("getList") { - @Override - public void test() throws Exception { - List metas = lib.getList().getMetas(); - assertEquals(errMess, Arrays.asList(), - titlesAsList(metas)); - } - }); - - addTest(new TestCase("save") { - @Override - public void test() throws Exception { - lib.save(story(luid1, name1, source1, author1), luid1, - null); - - List metas = lib.getList().getMetas(); - assertEquals(errMess, Arrays.asList(name1), - titlesAsList(metas)); - } - }); - - addTest(new TestCase("save more") { - @Override - public void test() throws Exception { - List metas = null; - - lib.save(story(luid2, name2, source2, author1), luid2, - null); - - metas = lib.getList().getMetas(); - assertEquals(errMess, Arrays.asList(name1, name2), - titlesAsList(metas)); - - lib.save(story(luid3, name3, source2, author1), luid3, - null); - - metas = lib.getList().getMetas(); - assertEquals(errMess, - Arrays.asList(name1, name2, name3), - titlesAsList(metas)); - } - }); - - addTest(new TestCase("save override luid (change author)") { - @Override - public void test() throws Exception { - // same luid as a previous one - lib.save(story(luid3, name3ex, source2, author2), - luid3, null); - - List metas = lib.getList().getMetas(); - assertEquals(errMess, - Arrays.asList(name1, name2, name3ex), - titlesAsList(metas)); - } - }); - - addTest(new TestCase("getList with results") { - @Override - public void test() throws Exception { - List metas = lib.getList().getMetas(); - assertEquals(3, metas.size()); - } - }); - - addTest(new TestCase("getList by source") { - @Override - public void test() throws Exception { - List metas = null; - - metas = lib.getList().filter(source1, null, null); - assertEquals(1, metas.size()); - - metas = lib.getList().filter(source2, null, null); - assertEquals(2, metas.size()); - - metas = lib.getList().filter((String)null, null, null); - assertEquals(3, metas.size()); - } - }); - - addTest(new TestCase("getList by author") { - @Override - public void test() throws Exception { - List metas = null; - - metas = lib.getList().filter(null, author1, null); - assertEquals(2, metas.size()); - - metas = lib.getList().filter(null, author2, null); - assertEquals(1, metas.size()); - - metas = lib.getList().filter((String)null, null, null); - assertEquals(3, metas.size()); - } - }); - - addTest(new TestCase("changeType") { - @Override - public void test() throws Exception { - List metas = null; - - lib.changeSource(luid3, source1, null); - - metas = lib.getList().filter(source1, null, null); - assertEquals(2, metas.size()); - - metas = lib.getList().filter(source2, null, null); - assertEquals(1, metas.size()); - - metas = lib.getList().filter((String)null, null, null); - assertEquals(3, metas.size()); - } - }); - - addTest(new TestCase("save override luid (change source)") { - @Override - public void test() throws Exception { - List metas = null; - - // same luid as a previous one - lib.save(story(luid3, "My story 3", source2, author2), - luid3, null); - - metas = lib.getList().filter(source1, null, null); - assertEquals(1, metas.size()); - - metas = lib.getList().filter(source2, null, null); - assertEquals(2, metas.size()); - - metas = lib.getList().filter((String)null, null, null); - assertEquals(3, metas.size()); - } - }); - } - }); - } - - @Override - protected void start() throws Exception { - tmp = File.createTempFile(".test-fanfix", ".library"); - tmp.delete(); - tmp.mkdir(); - - lib = new LocalLibrary(tmp, OutputType.INFO_TEXT, OutputType.CBZ); - } - - @Override - protected void stop() throws Exception { - IOUtils.deltree(tmp); - } - - /** - * Return the (sorted) list of titles present in this list of - * {@link MetaData}s. - * - * @param metas - * the meta - * - * @return the sorted list - */ - private List titlesAsList(List metas) { - List list = new ArrayList(); - for (MetaData meta : metas) { - list.add(meta.getTitle()); - } - - Collections.sort(list); - return list; - } - - private Story story(String luid, String title, String source, String author) { - Story story = new Story(); - - MetaData meta = new MetaData(); - meta.setLuid(luid); - meta.setTitle(title); - meta.setSource(source); - meta.setAuthor(author); - story.setMeta(meta); - - Chapter resume = chapter(0, "Resume"); - meta.setResume(resume); - - List chapters = new ArrayList(); - chapters.add(chapter(1, "Chap 1")); - chapters.add(chapter(2, "Chap 2")); - story.setChapters(chapters); - - long words = 0; - for (Chapter chap : story.getChapters()) { - words += chap.getWords(); - } - meta.setWords(words); - - return story; - } - - private Chapter chapter(int number, String name) { - Chapter chapter = new Chapter(number, name); - - List paragraphs = new ArrayList(); - paragraphs.add(new Paragraph(Paragraph.ParagraphType.NORMAL, - "some words in this paragraph please thank you", 8)); - - chapter.setParagraphs(paragraphs); - - long words = 0; - for (Paragraph para : chapter.getParagraphs()) { - words += para.getWords(); - } - chapter.setWords(words); - - return chapter; - } -} diff --git a/test/Test.java b/test/Test.java deleted file mode 100644 index 5ec24a4..0000000 --- a/test/Test.java +++ /dev/null @@ -1,177 +0,0 @@ -package be.nikiroo.fanfix.test; - -import java.io.File; -import java.io.IOException; - -import be.nikiroo.fanfix.Instance; -import be.nikiroo.fanfix.bundles.Config; -import be.nikiroo.fanfix.bundles.ConfigBundle; -import be.nikiroo.utils.IOUtils; -import be.nikiroo.utils.TempFiles; -import be.nikiroo.utils.resources.Bundles; -import be.nikiroo.utils.test.TestLauncher; - -/** - * Tests for Fanfix. - * - * @author niki - */ -public class Test extends TestLauncher { - // - // 4 files can control the test: - // - test/VERBOSE: enable verbose mode - // - test/OFFLINE: to forbid any downloading - // - test/URLS: to allow testing URLs - // - test/FORCE_REFRESH: to force a clear of the cache - // - // Note that test/CACHE can be kept, as it will contain all internet related - // files you need (if you allow URLs, run the test once which will populate - // the CACHE then go OFFLINE, it will still work). - // - // 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. - */ - static TempFiles tempFiles; - - /** - * Create the Fanfix {@link TestLauncher}. - * - * @param args - * the arguments to configure the number of columns and the ok/ko - * {@link String}s - * @param urlsAllowed - * allow testing URLs (.url files) - * - * @throws IOException - */ - public Test(String[] args, boolean urlsAllowed) throws IOException { - super("Fanfix", args); - Instance.getInstance().setTraceHandler(null); - addSeries(new BasicSupportUtilitiesTest(args)); - addSeries(new BasicSupportDeprecatedTest(args)); - addSeries(new LibraryTest(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 (urlsAllowed && file.getName().endsWith(".url")) { - uri = IOUtils.readSmallFile(file).trim(); - } else if (file.getName().endsWith(".story")) { - uri = file.getAbsolutePath(); - } else { - continue; - } - - addSeries(new ConversionTest(file.getName(), uri, expectedDir, - resultDir, args)); - } - } - } - - /** - * Main entry point of the program. - * - * @param args - * the arguments passed to the {@link TestLauncher}s. - * @throws IOException - * in case of I/O error - */ - static public void main(String[] args) throws IOException { - Instance.init(); - - // Verbose mode: - boolean verbose = new File("test/VERBOSE").exists(); - - // Can force refresh - boolean forceRefresh = new File("test/FORCE_REFRESH").exists(); - - // Allow URLs: - boolean urlsAllowed = new File("test/URLS").exists(); - - - // Only download files if allowed: - boolean offline = new File("test/OFFLINE").exists(); - Instance.getInstance().getCache().setOffline(offline); - - - - int result = 0; - tempFiles = new TempFiles("fanfix-test"); - try { - File tmpConfig = tempFiles.createTempDir("fanfix-config"); - File localCache = new File("test/CACHE"); - prepareCache(localCache, forceRefresh); - - ConfigBundle config = new ConfigBundle(); - Bundles.setDirectory(tmpConfig.getAbsolutePath()); - config.setString(Config.CACHE_DIR, localCache.getAbsolutePath()); - config.setInteger(Config.CACHE_MAX_TIME_STABLE, -1); - config.setInteger(Config.CACHE_MAX_TIME_CHANGING, -1); - config.updateFile(tmpConfig.getPath()); - System.setProperty("CONFIG_DIR", tmpConfig.getAbsolutePath()); - - Instance.init(true); - Instance.getInstance().getCache().setOffline(offline); - - TestLauncher tests = new Test(args, urlsAllowed); - tests.setDetails(verbose); - - result = tests.launch(); - - IOUtils.deltree(tmpConfig); - prepareCache(localCache, forceRefresh); - } finally { - // Test temp files - tempFiles.close(); - - // This is usually done in Fanfix.Main: - Instance.getInstance().getTempFiles().close(); - } - - System.exit(result); - } - - /** - * Prepare the cache (or clean it up). - *

    - * The cache directory will always exist if this method succeed - * - * @param localCache - * the cache directory - * @param forceRefresh - * TRUE to force acache refresh (delete all files) - * - * @throw IOException if the cache cannot be created - */ - private static void prepareCache(File localCache, boolean forceRefresh) - throws IOException { - // if needed - localCache.mkdirs(); - - if (!localCache.isDirectory()) { - throw new IOException("Cannot get a cache"); - } - - // delete local cached files (_*) or all files if forceRefresh - for (File f : localCache.listFiles()) { - if (forceRefresh || f.getName().startsWith("_")) { - IOUtils.deltree(f); - } - } - } -} -- 2.27.0