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
.Progress
;
21 import be
.nikiroo
.utils
.test
.TestCase
;
22 import be
.nikiroo
.utils
.test
.TestLauncher
;
24 public class BasicSupportTest
extends TestLauncher
{
26 private char openQuote
= Instance
.getTrans().getCharacter(
27 StringId
.OPEN_SINGLE_QUOTE
);
28 private char closeQuote
= Instance
.getTrans().getCharacter(
29 StringId
.CLOSE_SINGLE_QUOTE
);
30 private char openDoubleQuote
= Instance
.getTrans().getCharacter(
31 StringId
.OPEN_DOUBLE_QUOTE
);
32 private char closeDoubleQuote
= Instance
.getTrans().getCharacter(
33 StringId
.CLOSE_DOUBLE_QUOTE
);
35 public BasicSupportTest(String
[] args
) {
36 super("BasicSupport", args
);
38 addSeries(new TestLauncher("General", args
) {
40 addTest(new TestCase("BasicSupport.makeParagraphs()") {
42 public void test() throws Exception
{
43 BasicSupportEmpty support
= new BasicSupportEmpty() {
45 protected boolean isHtml() {
50 public void fixBlanksBreaks(List
<Paragraph
> paras
) {
54 public List
<Paragraph
> requotify(Paragraph para
) {
55 List
<Paragraph
> paras
= new ArrayList
<Paragraph
>(
62 List
<Paragraph
> paras
= null;
64 paras
= support
.makeParagraphs(null, "", null);
66 "An empty content should not generate paragraphs",
69 paras
= support
.makeParagraphs(null,
70 "Line 1</p><p>Line 2</p><p>Line 3</p>", null);
71 assertEquals(5, paras
.size());
72 assertEquals("Line 1", paras
.get(0).getContent());
73 assertEquals(ParagraphType
.BLANK
, paras
.get(1)
75 assertEquals("Line 2", paras
.get(2).getContent());
76 assertEquals(ParagraphType
.BLANK
, paras
.get(3)
78 assertEquals("Line 3", paras
.get(4).getContent());
80 paras
= support
.makeParagraphs(null,
81 "<p>Line1</p><p>Line2</p><p>Line3</p>", null);
82 assertEquals(6, paras
.size());
86 addTest(new TestCase("BasicSupport.removeDoubleBlanks()") {
88 public void test() throws Exception
{
89 BasicSupportEmpty support
= new BasicSupportEmpty() {
91 protected boolean isHtml() {
96 List
<Paragraph
> paras
= null;
101 "<p>Line1</p><p>Line2</p><p>Line3<br/><br><p></p>",
103 assertEquals(5, paras
.size());
108 "<p>Line1</p><p>Line2</p><p>Line3<br/><br><p></p>* * *",
110 assertEquals(5, paras
.size());
112 paras
= support
.makeParagraphs(null, "1<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>* * *<p>2", null);
120 assertEquals(3, paras
.size());
121 assertEquals(ParagraphType
.BREAK
, paras
.get(1)
124 paras
= support
.makeParagraphs(null,
125 "1<p>* * *<br/><p><br><p>2", null);
126 assertEquals(3, paras
.size());
127 assertEquals(ParagraphType
.BREAK
, paras
.get(1)
130 paras
= support
.makeParagraphs(null,
131 "1<p><br/><br>* * *<br/><p><br><p>2", null);
132 assertEquals(3, paras
.size());
133 assertEquals(ParagraphType
.BREAK
, paras
.get(1)
138 addTest(new TestCase("BasicSupport.processPara() quotes") {
140 public void test() throws Exception
{
141 BasicSupportEmpty support
= new BasicSupportEmpty() {
143 protected boolean isHtml() {
151 para
= support
.processPara("");
152 assertEquals(ParagraphType
.BLANK
, para
.getType());
155 para
= support
.processPara("\"Yes, my Lord!\"");
156 assertEquals(ParagraphType
.QUOTE
, para
.getType());
157 assertEquals(openDoubleQuote
+ "Yes, my Lord!"
158 + closeDoubleQuote
, para
.getContent());
160 para
= support
.processPara("«Yes, my Lord!»");
161 assertEquals(ParagraphType
.QUOTE
, para
.getType());
162 assertEquals(openDoubleQuote
+ "Yes, my Lord!"
163 + closeDoubleQuote
, para
.getContent());
165 para
= support
.processPara("'Yes, my Lord!'");
166 assertEquals(ParagraphType
.QUOTE
, para
.getType());
167 assertEquals(openQuote
+ "Yes, my Lord!" + closeQuote
,
170 para
= support
.processPara("‹Yes, my Lord!›");
171 assertEquals(ParagraphType
.QUOTE
, para
.getType());
172 assertEquals(openQuote
+ "Yes, my Lord!" + closeQuote
,
177 addTest(new TestCase(
178 "BasicSupport.processPara() double-simple quotes") {
180 public void setUp() throws Exception
{
186 public void tearDown() throws Exception
{
192 public void test() throws Exception
{
193 BasicSupportEmpty support
= new BasicSupportEmpty() {
195 protected boolean isHtml() {
202 para
= support
.processPara("''Yes, my Lord!''");
203 assertEquals(ParagraphType
.QUOTE
, para
.getType());
204 assertEquals(openDoubleQuote
+ "Yes, my Lord!"
205 + closeDoubleQuote
, para
.getContent());
207 para
= support
.processPara("‹‹Yes, my Lord!››");
208 assertEquals(ParagraphType
.QUOTE
, para
.getType());
209 assertEquals(openDoubleQuote
+ "Yes, my Lord!"
210 + closeDoubleQuote
, para
.getContent());
214 addTest(new TestCase("BasicSupport.processPara() apostrophe") {
216 public void test() throws Exception
{
217 BasicSupportEmpty support
= new BasicSupportEmpty() {
219 protected boolean isHtml() {
226 String text
= "Nous étions en été, mais cela aurait être l'hiver quand nous n'étions encore qu'à Aubeuge";
227 para
= support
.processPara(text
);
228 assertEquals(ParagraphType
.NORMAL
, para
.getType());
229 assertEquals(text
, para
.getContent());
233 addTest(new TestCase("BasicSupport.processPara() words count") {
235 public void test() throws Exception
{
236 BasicSupportEmpty support
= new BasicSupportEmpty() {
238 protected boolean isHtml() {
245 para
= support
.processPara("«Yes, my Lord!»");
246 assertEquals(3, para
.getWords());
248 para
= support
.processPara("One, twee, trois.");
249 assertEquals(3, para
.getWords());
253 addTest(new TestCase("BasicSupport.requotify() words count") {
255 public void test() throws Exception
{
256 BasicSupportEmpty support
= new BasicSupportEmpty();
258 char openDoubleQuote
= Instance
.getTrans()
259 .getCharacter(StringId
.OPEN_DOUBLE_QUOTE
);
260 char closeDoubleQuote
= Instance
.getTrans()
261 .getCharacter(StringId
.CLOSE_DOUBLE_QUOTE
);
263 String content
= null;
264 Paragraph para
= null;
265 List
<Paragraph
> paras
= null;
268 content
= "One, twee, trois.";
269 para
= new Paragraph(ParagraphType
.NORMAL
, content
,
270 content
.split(" ").length
);
271 paras
= support
.requotify(para
);
273 for (Paragraph p
: paras
) {
274 words
+= p
.getWords();
276 assertEquals("Bad words count in a single paragraph",
277 para
.getWords(), words
);
279 content
= "Such WoW! So Web2.0! With Colours!";
280 para
= new Paragraph(ParagraphType
.NORMAL
, content
,
281 content
.split(" ").length
);
282 paras
= support
.requotify(para
);
284 for (Paragraph p
: paras
) {
285 words
+= p
.getWords();
287 assertEquals("Bad words count in a single paragraph",
288 para
.getWords(), words
);
290 content
= openDoubleQuote
+ "Such a good idea!"
292 + ", she said. This ought to be a new para.";
293 para
= new Paragraph(ParagraphType
.QUOTE
, content
,
294 content
.split(" ").length
);
295 paras
= support
.requotify(para
);
297 for (Paragraph p
: paras
) {
298 words
+= p
.getWords();
301 "Bad words count in a requotified paragraph",
302 para
.getWords(), words
);
308 addSeries(new TestLauncher("Text", args
) {
310 addTest(new TestCase("Chapter detection simple") {
314 public void setUp() throws Exception
{
315 tmp
= File
.createTempFile("fanfix-text-file_", ".test");
316 IOUtils
.writeSmallFile(tmp
.getParentFile(),
317 tmp
.getName(), "TITLE"
322 + "Chapter 0: Resumé" + "\n" + "\n"
323 + "'sume." + "\n" + "\n"
324 + "Chapter 1: chap1" + "\n" + "\n"
325 + "Fanfan." + "\n" + "\n"
326 + "Chapter 2: Chap2" + "\n" + "\n" //
331 public void tearDown() throws Exception
{
336 public void test() throws Exception
{
337 BasicSupport support
= BasicSupport
338 .getSupport(SupportType
.TEXT
);
340 Story story
= support
341 .process(tmp
.toURI().toURL(), 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
381 .getSupport(SupportType
.TEXT
);
383 Story story
= support
384 .process(tmp
.toURI().toURL(), null);
386 assertEquals(2, story
.getChapters().size());
387 assertEquals(1, story
.getChapters().get(1)
388 .getParagraphs().size());
389 assertEquals("Tulipe.", story
.getChapters().get(1)
390 .getParagraphs().get(0).getContent());
397 private class BasicSupportEmpty
extends BasicSupport
{
399 protected String
getSourceName() {
404 protected boolean supports(URL url
) {
409 protected boolean isHtml() {
414 protected MetaData
getMeta(URL source
, InputStream in
)
420 protected String
getDesc(URL source
, InputStream in
) throws IOException
{
425 protected List
<Entry
<String
, URL
>> getChapters(URL source
,
426 InputStream in
, Progress pg
) throws IOException
{
431 protected String
getChapterContent(URL source
, InputStream in
,
432 int number
, Progress pg
) throws IOException
{
437 // and make it public!
438 public List
<Paragraph
> makeParagraphs(URL source
, String content
,
439 Progress pg
) throws IOException
{
440 return super.makeParagraphs(source
, content
, pg
);
444 // and make it public!
445 public void fixBlanksBreaks(List
<Paragraph
> paras
) {
446 super.fixBlanksBreaks(paras
);
450 // and make it public!
451 public Paragraph
processPara(String line
) {
452 return super.processPara(line
);
456 // and make it public!
457 public List
<Paragraph
> requotify(Paragraph para
) {
458 return super.requotify(para
);