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
.SupportType
;
19 import be
.nikiroo
.utils
.IOUtils
;
20 import be
.nikiroo
.utils
.test
.TestCase
;
21 import be
.nikiroo
.utils
.test
.TestLauncher
;
23 public class BasicSupportTest
extends TestLauncher
{
25 private char openQuote
= Instance
.getTrans().getChar(
26 StringId
.OPEN_SINGLE_QUOTE
);
27 private char closeQuote
= Instance
.getTrans().getChar(
28 StringId
.CLOSE_SINGLE_QUOTE
);
29 private char openDoubleQuote
= Instance
.getTrans().getChar(
30 StringId
.OPEN_DOUBLE_QUOTE
);
31 private char closeDoubleQuote
= Instance
.getTrans().getChar(
32 StringId
.CLOSE_DOUBLE_QUOTE
);
34 public BasicSupportTest(String
[] args
) {
35 super("BasicSupport", args
);
37 addSeries(new TestLauncher("General", args
) {
39 addTest(new TestCase("BasicSupport.makeParagraphs()") {
41 public void test() throws Exception
{
42 BasicSupportEmpty support
= new BasicSupportEmpty() {
44 protected boolean isHtml() {
49 public void fixBlanksBreaks(List
<Paragraph
> paras
) {
53 public List
<Paragraph
> requotify(Paragraph para
) {
54 List
<Paragraph
> paras
= new ArrayList
<Paragraph
>(
61 List
<Paragraph
> paras
= null;
63 paras
= support
.makeParagraphs(null, "");
65 "An empty content should not generate paragraphs",
68 paras
= support
.makeParagraphs(null,
69 "Line 1</p><p>Line 2</p><p>Line 3</p>");
70 assertEquals(5, paras
.size());
71 assertEquals("Line 1", paras
.get(0).getContent());
72 assertEquals(ParagraphType
.BLANK
, paras
.get(1)
74 assertEquals("Line 2", paras
.get(2).getContent());
75 assertEquals(ParagraphType
.BLANK
, paras
.get(3)
77 assertEquals("Line 3", paras
.get(4).getContent());
79 paras
= support
.makeParagraphs(null,
80 "<p>Line1</p><p>Line2</p><p>Line3</p>");
81 assertEquals(6, paras
.size());
85 addTest(new TestCase("BasicSupport.removeDoubleBlanks()") {
87 public void test() throws Exception
{
88 BasicSupportEmpty support
= new BasicSupportEmpty() {
90 protected boolean isHtml() {
95 List
<Paragraph
> paras
= null;
99 "<p>Line1</p><p>Line2</p><p>Line3<br/><br><p></p>");
100 assertEquals(5, paras
.size());
103 .makeParagraphs(null,
104 "<p>Line1</p><p>Line2</p><p>Line3<br/><br><p></p>* * *");
105 assertEquals(5, paras
.size());
107 paras
= support
.makeParagraphs(null, "1<p>* * *<p>2");
108 assertEquals(3, paras
.size());
109 assertEquals(ParagraphType
.BREAK
, paras
.get(1)
112 paras
= support
.makeParagraphs(null,
113 "1<p><br/><p>* * *<p>2");
114 assertEquals(3, paras
.size());
115 assertEquals(ParagraphType
.BREAK
, paras
.get(1)
118 paras
= support
.makeParagraphs(null,
119 "1<p>* * *<br/><p><br><p>2");
120 assertEquals(3, paras
.size());
121 assertEquals(ParagraphType
.BREAK
, paras
.get(1)
124 paras
= support
.makeParagraphs(null,
125 "1<p><br/><br>* * *<br/><p><br><p>2");
126 assertEquals(3, paras
.size());
127 assertEquals(ParagraphType
.BREAK
, paras
.get(1)
132 addTest(new TestCase("BasicSupport.processPara() quotes") {
134 public void test() throws Exception
{
135 BasicSupportEmpty support
= new BasicSupportEmpty() {
137 protected boolean isHtml() {
145 para
= support
.processPara("");
146 assertEquals(ParagraphType
.BLANK
, para
.getType());
149 para
= support
.processPara("\"Yes, my Lord!\"");
150 assertEquals(ParagraphType
.QUOTE
, para
.getType());
151 assertEquals(openDoubleQuote
+ "Yes, my Lord!"
152 + closeDoubleQuote
, para
.getContent());
154 para
= support
.processPara("«Yes, my Lord!»");
155 assertEquals(ParagraphType
.QUOTE
, para
.getType());
156 assertEquals(openDoubleQuote
+ "Yes, my Lord!"
157 + closeDoubleQuote
, para
.getContent());
159 para
= support
.processPara("'Yes, my Lord!'");
160 assertEquals(ParagraphType
.QUOTE
, para
.getType());
161 assertEquals(openQuote
+ "Yes, my Lord!" + closeQuote
,
164 para
= support
.processPara("‹Yes, my Lord!›");
165 assertEquals(ParagraphType
.QUOTE
, para
.getType());
166 assertEquals(openQuote
+ "Yes, my Lord!" + closeQuote
,
171 addTest(new TestCase(
172 "BasicSupport.processPara() double-simple quotes") {
174 public void setUp() throws Exception
{
180 public void tearDown() throws Exception
{
186 public void test() throws Exception
{
187 BasicSupportEmpty support
= new BasicSupportEmpty() {
189 protected boolean isHtml() {
196 para
= support
.processPara("''Yes, my Lord!''");
197 assertEquals(ParagraphType
.QUOTE
, para
.getType());
198 assertEquals(openDoubleQuote
+ "Yes, my Lord!"
199 + closeDoubleQuote
, para
.getContent());
201 para
= support
.processPara("‹‹Yes, my Lord!››");
202 assertEquals(ParagraphType
.QUOTE
, para
.getType());
203 assertEquals(openDoubleQuote
+ "Yes, my Lord!"
204 + closeDoubleQuote
, para
.getContent());
208 addTest(new TestCase("BasicSupport.processPara() apostrophe") {
210 public void test() throws Exception
{
211 BasicSupportEmpty support
= new BasicSupportEmpty() {
213 protected boolean isHtml() {
220 String text
= "Nous étions en été, mais cela aurait être l'hiver quand nous n'étions encore qu'à Aubeuge";
221 para
= support
.processPara(text
);
222 assertEquals(ParagraphType
.NORMAL
, para
.getType());
223 assertEquals(text
, para
.getContent());
227 addTest(new TestCase("BasicSupport.processPara() words count") {
229 public void test() throws Exception
{
230 BasicSupportEmpty support
= new BasicSupportEmpty() {
232 protected boolean isHtml() {
239 para
= support
.processPara("«Yes, my Lord!»");
240 assertEquals(3, para
.getWords());
242 para
= support
.processPara("One, twee, trois.");
243 assertEquals(3, para
.getWords());
247 addTest(new TestCase("BasicSupport.requotify() words count") {
249 public void test() throws Exception
{
250 BasicSupportEmpty support
= new BasicSupportEmpty();
252 char openDoubleQuote
= Instance
.getTrans().getChar(
253 StringId
.OPEN_DOUBLE_QUOTE
);
254 char closeDoubleQuote
= Instance
.getTrans().getChar(
255 StringId
.CLOSE_DOUBLE_QUOTE
);
257 String content
= null;
258 Paragraph para
= null;
259 List
<Paragraph
> paras
= null;
262 content
= "One, twee, trois.";
263 para
= new Paragraph(ParagraphType
.NORMAL
, content
,
264 content
.split(" ").length
);
265 paras
= support
.requotify(para
);
267 for (Paragraph p
: paras
) {
268 words
+= p
.getWords();
270 assertEquals("Bad words count in a single paragraph",
271 para
.getWords(), words
);
273 content
= "Such WoW! So Web2.0! With Colours!";
274 para
= new Paragraph(ParagraphType
.NORMAL
, content
,
275 content
.split(" ").length
);
276 paras
= support
.requotify(para
);
278 for (Paragraph p
: paras
) {
279 words
+= p
.getWords();
281 assertEquals("Bad words count in a single paragraph",
282 para
.getWords(), words
);
284 content
= openDoubleQuote
+ "Such a good idea!"
286 + ", she said. This ought to be a new para.";
287 para
= new Paragraph(ParagraphType
.QUOTE
, content
,
288 content
.split(" ").length
);
289 paras
= support
.requotify(para
);
291 for (Paragraph p
: paras
) {
292 words
+= p
.getWords();
295 "Bad words count in a requotified paragraph",
296 para
.getWords(), words
);
302 addSeries(new TestLauncher("Text", args
) {
304 addTest(new TestCase("Chapter detection simple") {
308 public void setUp() throws Exception
{
309 tmp
= File
.createTempFile("fanfix-text-file_", ".test");
310 IOUtils
.writeSmallFile(tmp
.getParentFile(),
311 tmp
.getName(), "TITLE"
316 + "Chapter 0: Resumé" + "\n" + "\n"
317 + "'sume." + "\n" + "\n"
318 + "Chapter 1: chap1" + "\n" + "\n"
319 + "Fanfan." + "\n" + "\n"
320 + "Chapter 2: Chap2" + "\n" + "\n" //
325 public void tearDown() throws Exception
{
330 public void test() throws Exception
{
331 BasicSupport support
= BasicSupport
332 .getSupport(SupportType
.TEXT
);
334 Story story
= support
335 .process(tmp
.toURI().toURL(), null);
337 assertEquals(2, story
.getChapters().size());
338 assertEquals(1, story
.getChapters().get(1)
339 .getParagraphs().size());
340 assertEquals("Tulipe.", story
.getChapters().get(1)
341 .getParagraphs().get(0).getContent());
345 addTest(new TestCase("Chapter detection with String 'Chapter'") {
349 public void setUp() throws Exception
{
350 tmp
= File
.createTempFile("fanfix-text-file_", ".test");
351 IOUtils
.writeSmallFile(tmp
.getParentFile(),
352 tmp
.getName(), "TITLE"
357 + "Chapter 0: Resumé" + "\n" + "\n"
358 + "'sume." + "\n" + "\n"
359 + "Chapter 1: chap1" + "\n" + "\n"
360 + "Chapter fout-la-merde" + "\n"
362 + "Fanfan." + "\n" + "\n"
363 + "Chapter 2: Chap2" + "\n" + "\n" //
368 public void tearDown() throws Exception
{
373 public void test() throws Exception
{
374 BasicSupport support
= BasicSupport
375 .getSupport(SupportType
.TEXT
);
377 Story story
= support
378 .process(tmp
.toURI().toURL(), null);
380 assertEquals(2, story
.getChapters().size());
381 assertEquals(1, story
.getChapters().get(1)
382 .getParagraphs().size());
383 assertEquals("Tulipe.", story
.getChapters().get(1)
384 .getParagraphs().get(0).getContent());
391 private class BasicSupportEmpty
extends BasicSupport
{
393 protected String
getSourceName() {
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
) throws IOException
{
425 protected String
getChapterContent(URL source
, InputStream in
,
426 int number
) throws IOException
{
431 // and make it public!
432 public List
<Paragraph
> makeParagraphs(URL source
, String content
)
434 return super.makeParagraphs(source
, content
);
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
);