1 package be
.nikiroo
.fanfix
.test
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
7 import java
.util
.ArrayList
;
9 import java
.util
.Map
.Entry
;
11 import be
.nikiroo
.fanfix
.Instance
;
12 import be
.nikiroo
.fanfix
.bundles
.StringId
;
13 import be
.nikiroo
.fanfix
.data
.MetaData
;
14 import be
.nikiroo
.fanfix
.data
.Paragraph
;
15 import be
.nikiroo
.fanfix
.data
.Paragraph
.ParagraphType
;
16 import be
.nikiroo
.fanfix
.data
.Story
;
17 import be
.nikiroo
.fanfix
.supported
.BasicSupport
;
18 import be
.nikiroo
.fanfix
.supported
.BasicSupport_Deprecated
;
19 import be
.nikiroo
.fanfix
.supported
.SupportType
;
20 import be
.nikiroo
.utils
.IOUtils
;
21 import be
.nikiroo
.utils
.Progress
;
22 import be
.nikiroo
.utils
.test
.TestCase
;
23 import be
.nikiroo
.utils
.test
.TestLauncher
;
25 class BasicSupportTest
extends TestLauncher
{
27 private char openQuote
= Instance
.getTrans().getCharacter(
28 StringId
.OPEN_SINGLE_QUOTE
);
29 private char closeQuote
= Instance
.getTrans().getCharacter(
30 StringId
.CLOSE_SINGLE_QUOTE
);
31 private char openDoubleQuote
= Instance
.getTrans().getCharacter(
32 StringId
.OPEN_DOUBLE_QUOTE
);
33 private char closeDoubleQuote
= Instance
.getTrans().getCharacter(
34 StringId
.CLOSE_DOUBLE_QUOTE
);
36 public BasicSupportTest(String
[] args
) {
37 super("BasicSupport", args
);
39 addSeries(new TestLauncher("General", args
) {
41 addTest(new TestCase("BasicSupport.makeParagraphs()") {
43 public void test() throws Exception
{
44 BasicSupportEmpty support
= new BasicSupportEmpty() {
46 protected boolean isHtml() {
51 public void fixBlanksBreaks(List
<Paragraph
> paras
) {
55 public List
<Paragraph
> requotify(Paragraph para
) {
56 List
<Paragraph
> paras
= new ArrayList
<Paragraph
>(
63 List
<Paragraph
> paras
= null;
65 paras
= support
.makeParagraphs(null, "", null);
67 "An empty content should not generate paragraphs",
70 paras
= support
.makeParagraphs(null,
71 "Line 1</p><p>Line 2</p><p>Line 3</p>", null);
72 assertEquals(5, paras
.size());
73 assertEquals("Line 1", paras
.get(0).getContent());
74 assertEquals(ParagraphType
.BLANK
, paras
.get(1)
76 assertEquals("Line 2", paras
.get(2).getContent());
77 assertEquals(ParagraphType
.BLANK
, paras
.get(3)
79 assertEquals("Line 3", paras
.get(4).getContent());
81 paras
= support
.makeParagraphs(null,
82 "<p>Line1</p><p>Line2</p><p>Line3</p>", null);
83 assertEquals(6, paras
.size());
87 addTest(new TestCase("BasicSupport.removeDoubleBlanks()") {
89 public void test() throws Exception
{
90 BasicSupportEmpty support
= new BasicSupportEmpty() {
92 protected boolean isHtml() {
97 List
<Paragraph
> paras
= null;
102 "<p>Line1</p><p>Line2</p><p>Line3<br/><br><p></p>",
104 assertEquals(5, paras
.size());
109 "<p>Line1</p><p>Line2</p><p>Line3<br/><br><p></p>* * *",
111 assertEquals(5, paras
.size());
113 paras
= support
.makeParagraphs(null, "1<p>* * *<p>2",
115 assertEquals(3, paras
.size());
116 assertEquals(ParagraphType
.BREAK
, paras
.get(1)
119 paras
= support
.makeParagraphs(null,
120 "1<p><br/><p>* * *<p>2", null);
121 assertEquals(3, paras
.size());
122 assertEquals(ParagraphType
.BREAK
, paras
.get(1)
125 paras
= support
.makeParagraphs(null,
126 "1<p>* * *<br/><p><br><p>2", null);
127 assertEquals(3, paras
.size());
128 assertEquals(ParagraphType
.BREAK
, paras
.get(1)
131 paras
= support
.makeParagraphs(null,
132 "1<p><br/><br>* * *<br/><p><br><p>2", null);
133 assertEquals(3, paras
.size());
134 assertEquals(ParagraphType
.BREAK
, paras
.get(1)
139 addTest(new TestCase("BasicSupport.processPara() quotes") {
141 public void test() throws Exception
{
142 BasicSupportEmpty support
= new BasicSupportEmpty() {
144 protected boolean isHtml() {
152 para
= support
.processPara("");
153 assertEquals(ParagraphType
.BLANK
, para
.getType());
156 para
= support
.processPara("\"Yes, my Lord!\"");
157 assertEquals(ParagraphType
.QUOTE
, para
.getType());
158 assertEquals(openDoubleQuote
+ "Yes, my Lord!"
159 + closeDoubleQuote
, para
.getContent());
161 para
= support
.processPara("«Yes, my Lord!»");
162 assertEquals(ParagraphType
.QUOTE
, para
.getType());
163 assertEquals(openDoubleQuote
+ "Yes, my Lord!"
164 + closeDoubleQuote
, para
.getContent());
166 para
= support
.processPara("'Yes, my Lord!'");
167 assertEquals(ParagraphType
.QUOTE
, para
.getType());
168 assertEquals(openQuote
+ "Yes, my Lord!" + closeQuote
,
171 para
= support
.processPara("‹Yes, my Lord!›");
172 assertEquals(ParagraphType
.QUOTE
, para
.getType());
173 assertEquals(openQuote
+ "Yes, my Lord!" + closeQuote
,
178 addTest(new TestCase(
179 "BasicSupport.processPara() double-simple quotes") {
181 public void setUp() throws Exception
{
187 public void tearDown() throws Exception
{
193 public void test() throws Exception
{
194 BasicSupportEmpty support
= new BasicSupportEmpty() {
196 protected boolean isHtml() {
203 para
= support
.processPara("''Yes, my Lord!''");
204 assertEquals(ParagraphType
.QUOTE
, para
.getType());
205 assertEquals(openDoubleQuote
+ "Yes, my Lord!"
206 + closeDoubleQuote
, para
.getContent());
208 para
= support
.processPara("‹‹Yes, my Lord!››");
209 assertEquals(ParagraphType
.QUOTE
, para
.getType());
210 assertEquals(openDoubleQuote
+ "Yes, my Lord!"
211 + closeDoubleQuote
, para
.getContent());
215 addTest(new TestCase("BasicSupport.processPara() apostrophe") {
217 public void test() throws Exception
{
218 BasicSupportEmpty support
= new BasicSupportEmpty() {
220 protected boolean isHtml() {
227 String text
= "Nous étions en été, mais cela aurait être l'hiver quand nous n'étions encore qu'à Aubeuge";
228 para
= support
.processPara(text
);
229 assertEquals(ParagraphType
.NORMAL
, para
.getType());
230 assertEquals(text
, para
.getContent());
234 addTest(new TestCase("BasicSupport.processPara() words count") {
236 public void test() throws Exception
{
237 BasicSupportEmpty support
= new BasicSupportEmpty() {
239 protected boolean isHtml() {
246 para
= support
.processPara("«Yes, my Lord!»");
247 assertEquals(3, para
.getWords());
249 para
= support
.processPara("One, twee, trois.");
250 assertEquals(3, para
.getWords());
254 addTest(new TestCase("BasicSupport.requotify() words count") {
256 public void test() throws Exception
{
257 BasicSupportEmpty support
= new BasicSupportEmpty();
259 char openDoubleQuote
= Instance
.getTrans()
260 .getCharacter(StringId
.OPEN_DOUBLE_QUOTE
);
261 char closeDoubleQuote
= Instance
.getTrans()
262 .getCharacter(StringId
.CLOSE_DOUBLE_QUOTE
);
264 String content
= null;
265 Paragraph para
= null;
266 List
<Paragraph
> paras
= null;
269 content
= "One, twee, trois.";
270 para
= new Paragraph(ParagraphType
.NORMAL
, content
,
271 content
.split(" ").length
);
272 paras
= support
.requotify(para
);
274 for (Paragraph p
: paras
) {
275 words
+= p
.getWords();
277 assertEquals("Bad words count in a single paragraph",
278 para
.getWords(), words
);
280 content
= "Such WoW! So Web2.0! With Colours!";
281 para
= new Paragraph(ParagraphType
.NORMAL
, content
,
282 content
.split(" ").length
);
283 paras
= support
.requotify(para
);
285 for (Paragraph p
: paras
) {
286 words
+= p
.getWords();
288 assertEquals("Bad words count in a single paragraph",
289 para
.getWords(), words
);
291 content
= openDoubleQuote
+ "Such a good idea!"
293 + ", she said. This ought to be a new para.";
294 para
= new Paragraph(ParagraphType
.QUOTE
, content
,
295 content
.split(" ").length
);
296 paras
= support
.requotify(para
);
298 for (Paragraph p
: paras
) {
299 words
+= p
.getWords();
302 "Bad words count in a requotified paragraph",
303 para
.getWords(), words
);
309 addSeries(new TestLauncher("Text", args
) {
311 addTest(new TestCase("Chapter detection simple") {
315 public void setUp() throws Exception
{
316 tmp
= File
.createTempFile("fanfix-text-file_", ".test");
317 IOUtils
.writeSmallFile(tmp
.getParentFile(),
318 tmp
.getName(), "TITLE"
323 + "Chapter 0: Resumé" + "\n" + "\n"
324 + "'sume." + "\n" + "\n"
325 + "Chapter 1: chap1" + "\n" + "\n"
326 + "Fanfan." + "\n" + "\n"
327 + "Chapter 2: Chap2" + "\n" + "\n" //
332 public void tearDown() throws Exception
{
337 public void test() throws Exception
{
338 BasicSupport support
= BasicSupport
.getSupport(
339 SupportType
.TEXT
, tmp
.toURI().toURL());
341 Story story
= support
.process(null);
343 assertEquals(2, story
.getChapters().size());
344 assertEquals(1, story
.getChapters().get(1)
345 .getParagraphs().size());
346 assertEquals("Tulipe.", story
.getChapters().get(1)
347 .getParagraphs().get(0).getContent());
351 addTest(new TestCase("Chapter detection with String 'Chapter'") {
355 public void setUp() throws Exception
{
356 tmp
= File
.createTempFile("fanfix-text-file_", ".test");
357 IOUtils
.writeSmallFile(tmp
.getParentFile(),
358 tmp
.getName(), "TITLE"
363 + "Chapter 0: Resumé" + "\n" + "\n"
364 + "'sume." + "\n" + "\n"
365 + "Chapter 1: chap1" + "\n" + "\n"
366 + "Chapter fout-la-merde" + "\n"
368 + "Fanfan." + "\n" + "\n"
369 + "Chapter 2: Chap2" + "\n" + "\n" //
374 public void tearDown() throws Exception
{
379 public void test() throws Exception
{
380 BasicSupport support
= BasicSupport
.getSupport(
381 SupportType
.TEXT
, tmp
.toURI().toURL());
383 Story story
= support
.process(null);
385 assertEquals(2, story
.getChapters().size());
386 assertEquals(1, story
.getChapters().get(1)
387 .getParagraphs().size());
388 assertEquals("Tulipe.", story
.getChapters().get(1)
389 .getParagraphs().get(0).getContent());
396 private class BasicSupportEmpty
extends BasicSupport_Deprecated
{
398 protected boolean supports(URL url
) {
403 protected boolean isHtml() {
408 protected MetaData
getMeta(URL source
, InputStream in
)
414 protected String
getDesc(URL source
, InputStream in
) throws IOException
{
419 protected List
<Entry
<String
, URL
>> getChapters(URL source
,
420 InputStream in
, Progress pg
) throws IOException
{
425 protected String
getChapterContent(URL source
, InputStream in
,
426 int number
, Progress pg
) throws IOException
{
431 // and make it public!
432 public List
<Paragraph
> makeParagraphs(URL source
, String content
,
433 Progress pg
) throws IOException
{
434 return super.makeParagraphs(source
, content
, pg
);
438 // and make it public!
439 public void fixBlanksBreaks(List
<Paragraph
> paras
) {
440 super.fixBlanksBreaks(paras
);
444 // and make it public!
445 public Paragraph
processPara(String line
) {
446 return super.processPara(line
);
450 // and make it public!
451 public List
<Paragraph
> requotify(Paragraph para
) {
452 return super.requotify(para
);