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 BasicSupportDeprecatedTest
extends TestLauncher
{
27 private char openQuote
= Instance
.getInstance().getTrans().getCharacter(StringId
.OPEN_SINGLE_QUOTE
);
28 private char closeQuote
= Instance
.getInstance().getTrans().getCharacter(StringId
.CLOSE_SINGLE_QUOTE
);
29 private char openDoubleQuote
= Instance
.getInstance().getTrans().getCharacter(StringId
.OPEN_DOUBLE_QUOTE
);
30 private char closeDoubleQuote
= Instance
.getInstance().getTrans().getCharacter(StringId
.CLOSE_DOUBLE_QUOTE
);
32 public BasicSupportDeprecatedTest(String
[] args
) {
33 super("BasicSupportDeprecated", args
);
35 addSeries(new TestLauncher("General", args
) {
37 addTest(new TestCase("BasicSupport.makeParagraphs()") {
39 public void test() throws Exception
{
40 BasicSupportEmpty support
= new BasicSupportEmpty() {
42 protected boolean isHtml() {
47 public void fixBlanksBreaks(List
<Paragraph
> paras
) {
51 public List
<Paragraph
> requotify(Paragraph para
) {
52 List
<Paragraph
> paras
= new ArrayList
<Paragraph
>(
59 List
<Paragraph
> paras
= null;
61 paras
= support
.makeParagraphs(null, "", null);
63 "An empty content should not generate paragraphs",
66 paras
= support
.makeParagraphs(null,
67 "Line 1</p><p>Line 2</p><p>Line 3</p>", null);
68 assertEquals(5, paras
.size());
69 assertEquals("Line 1", paras
.get(0).getContent());
70 assertEquals(ParagraphType
.BLANK
, paras
.get(1)
72 assertEquals("Line 2", paras
.get(2).getContent());
73 assertEquals(ParagraphType
.BLANK
, paras
.get(3)
75 assertEquals("Line 3", paras
.get(4).getContent());
77 paras
= support
.makeParagraphs(null,
78 "<p>Line1</p><p>Line2</p><p>Line3</p>", null);
79 assertEquals(6, paras
.size());
83 addTest(new TestCase("BasicSupport.removeDoubleBlanks()") {
85 public void test() throws Exception
{
86 BasicSupportEmpty support
= new BasicSupportEmpty() {
88 protected boolean isHtml() {
93 List
<Paragraph
> paras
= null;
98 "<p>Line1</p><p>Line2</p><p>Line3<br/><br><p></p>",
100 assertEquals(5, paras
.size());
105 "<p>Line1</p><p>Line2</p><p>Line3<br/><br><p></p>* * *",
107 assertEquals(5, paras
.size());
109 paras
= support
.makeParagraphs(null, "1<p>* * *<p>2",
111 assertEquals(3, paras
.size());
112 assertEquals(ParagraphType
.BREAK
, paras
.get(1)
115 paras
= support
.makeParagraphs(null,
116 "1<p><br/><p>* * *<p>2", null);
117 assertEquals(3, paras
.size());
118 assertEquals(ParagraphType
.BREAK
, paras
.get(1)
121 paras
= support
.makeParagraphs(null,
122 "1<p>* * *<br/><p><br><p>2", null);
123 assertEquals(3, paras
.size());
124 assertEquals(ParagraphType
.BREAK
, paras
.get(1)
127 paras
= support
.makeParagraphs(null,
128 "1<p><br/><br>* * *<br/><p><br><p>2", null);
129 assertEquals(3, paras
.size());
130 assertEquals(ParagraphType
.BREAK
, paras
.get(1)
135 addTest(new TestCase("BasicSupport.processPara() quotes") {
137 public void test() throws Exception
{
138 BasicSupportEmpty support
= new BasicSupportEmpty() {
140 protected boolean isHtml() {
148 para
= support
.processPara("");
149 assertEquals(ParagraphType
.BLANK
, para
.getType());
152 para
= support
.processPara("\"Yes, my Lord!\"");
153 assertEquals(ParagraphType
.QUOTE
, para
.getType());
154 assertEquals(openDoubleQuote
+ "Yes, my Lord!"
155 + closeDoubleQuote
, para
.getContent());
157 para
= support
.processPara("«Yes, my Lord!»");
158 assertEquals(ParagraphType
.QUOTE
, para
.getType());
159 assertEquals(openDoubleQuote
+ "Yes, my Lord!"
160 + closeDoubleQuote
, para
.getContent());
162 para
= support
.processPara("'Yes, my Lord!'");
163 assertEquals(ParagraphType
.QUOTE
, para
.getType());
164 assertEquals(openQuote
+ "Yes, my Lord!" + closeQuote
,
167 para
= support
.processPara("‹Yes, my Lord!›");
168 assertEquals(ParagraphType
.QUOTE
, para
.getType());
169 assertEquals(openQuote
+ "Yes, my Lord!" + closeQuote
,
174 addTest(new TestCase(
175 "BasicSupport.processPara() double-simple quotes") {
177 public void setUp() throws Exception
{
183 public void tearDown() throws Exception
{
189 public void test() throws Exception
{
190 BasicSupportEmpty support
= new BasicSupportEmpty() {
192 protected boolean isHtml() {
199 para
= support
.processPara("''Yes, my Lord!''");
200 assertEquals(ParagraphType
.QUOTE
, para
.getType());
201 assertEquals(openDoubleQuote
+ "Yes, my Lord!"
202 + closeDoubleQuote
, para
.getContent());
204 para
= support
.processPara("‹‹Yes, my Lord!››");
205 assertEquals(ParagraphType
.QUOTE
, para
.getType());
206 assertEquals(openDoubleQuote
+ "Yes, my Lord!"
207 + closeDoubleQuote
, para
.getContent());
211 addTest(new TestCase("BasicSupport.processPara() apostrophe") {
213 public void test() throws Exception
{
214 BasicSupportEmpty support
= new BasicSupportEmpty() {
216 protected boolean isHtml() {
223 String text
= "Nous étions en été, mais cela aurait être l'hiver quand nous n'étions encore qu'à Aubeuge";
224 para
= support
.processPara(text
);
225 assertEquals(ParagraphType
.NORMAL
, para
.getType());
226 assertEquals(text
, para
.getContent());
230 addTest(new TestCase("BasicSupport.processPara() words count") {
232 public void test() throws Exception
{
233 BasicSupportEmpty support
= new BasicSupportEmpty() {
235 protected boolean isHtml() {
242 para
= support
.processPara("«Yes, my Lord!»");
243 assertEquals(3, para
.getWords());
245 para
= support
.processPara("One, twee, trois.");
246 assertEquals(3, para
.getWords());
250 addTest(new TestCase("BasicSupport.requotify() words count") {
252 public void test() throws Exception
{
253 BasicSupportEmpty support
= new BasicSupportEmpty();
255 char openDoubleQuote
= Instance
.getInstance().getTrans()
256 .getCharacter(StringId
.OPEN_DOUBLE_QUOTE
);
257 char closeDoubleQuote
= Instance
.getInstance().getTrans()
258 .getCharacter(StringId
.CLOSE_DOUBLE_QUOTE
);
260 String content
= null;
261 Paragraph para
= null;
262 List
<Paragraph
> paras
= null;
265 content
= "One, twee, trois.";
266 para
= new Paragraph(ParagraphType
.NORMAL
, content
,
267 content
.split(" ").length
);
268 paras
= support
.requotify(para
);
270 for (Paragraph p
: paras
) {
271 words
+= p
.getWords();
273 assertEquals("Bad words count in a single paragraph",
274 para
.getWords(), words
);
276 content
= "Such WoW! So Web2.0! With Colours!";
277 para
= new Paragraph(ParagraphType
.NORMAL
, content
,
278 content
.split(" ").length
);
279 paras
= support
.requotify(para
);
281 for (Paragraph p
: paras
) {
282 words
+= p
.getWords();
284 assertEquals("Bad words count in a single paragraph",
285 para
.getWords(), words
);
287 content
= openDoubleQuote
+ "Such a good idea!"
289 + ", she said. This ought to be a new para.";
290 para
= new Paragraph(ParagraphType
.QUOTE
, content
,
291 content
.split(" ").length
);
292 paras
= support
.requotify(para
);
294 for (Paragraph p
: paras
) {
295 words
+= p
.getWords();
298 "Bad words count in a requotified paragraph",
299 para
.getWords(), words
);
305 addSeries(new TestLauncher("Text", args
) {
307 addTest(new TestCase("Chapter detection simple") {
311 public void setUp() throws Exception
{
312 tmp
= File
.createTempFile("fanfix-text-file_", ".test");
313 IOUtils
.writeSmallFile(tmp
.getParentFile(),
314 tmp
.getName(), "TITLE"
319 + "Chapter 0: Resumé" + "\n" + "\n"
320 + "'sume." + "\n" + "\n"
321 + "Chapter 1: chap1" + "\n" + "\n"
322 + "Fanfan." + "\n" + "\n"
323 + "Chapter 2: Chap2" + "\n" + "\n" //
328 public void tearDown() throws Exception
{
333 public void test() throws Exception
{
334 BasicSupport support
= BasicSupport
.getSupport(
335 SupportType
.TEXT
, tmp
.toURI().toURL());
337 Story story
= support
.process(null);
339 assertEquals(2, story
.getChapters().size());
340 assertEquals(1, story
.getChapters().get(1)
341 .getParagraphs().size());
342 assertEquals("Tulipe.", story
.getChapters().get(1)
343 .getParagraphs().get(0).getContent());
347 addTest(new TestCase("Chapter detection with String 'Chapter'") {
351 public void setUp() throws Exception
{
352 tmp
= File
.createTempFile("fanfix-text-file_", ".test");
353 IOUtils
.writeSmallFile(tmp
.getParentFile(),
354 tmp
.getName(), "TITLE"
359 + "Chapter 0: Resumé" + "\n" + "\n"
360 + "'sume." + "\n" + "\n"
361 + "Chapter 1: chap1" + "\n" + "\n"
362 + "Chapter fout-la-merde" + "\n"
364 + "Fanfan." + "\n" + "\n"
365 + "Chapter 2: Chap2" + "\n" + "\n" //
370 public void tearDown() throws Exception
{
375 public void test() throws Exception
{
376 BasicSupport support
= BasicSupport
.getSupport(
377 SupportType
.TEXT
, tmp
.toURI().toURL());
379 Story story
= support
.process(null);
381 assertEquals(2, story
.getChapters().size());
382 assertEquals(1, story
.getChapters().get(1)
383 .getParagraphs().size());
384 assertEquals("Tulipe.", story
.getChapters().get(1)
385 .getParagraphs().get(0).getContent());
392 private class BasicSupportEmpty
extends BasicSupport_Deprecated
{
394 protected boolean supports(URL url
) {
399 protected boolean isHtml() {
404 protected MetaData
getMeta(URL source
, InputStream in
)
410 protected String
getDesc(URL source
, InputStream in
) throws IOException
{
415 protected List
<Entry
<String
, URL
>> getChapters(URL source
,
416 InputStream in
, Progress pg
) throws IOException
{
421 protected String
getChapterContent(URL source
, InputStream in
,
422 int number
, Progress pg
) throws IOException
{
427 // and make it public!
428 public List
<Paragraph
> makeParagraphs(URL source
, String content
,
429 Progress pg
) throws IOException
{
430 return super.makeParagraphs(source
, content
, pg
);
434 // and make it public!
435 public void fixBlanksBreaks(List
<Paragraph
> paras
) {
436 super.fixBlanksBreaks(paras
);
440 // and make it public!
441 public Paragraph
processPara(String line
) {
442 return super.processPara(line
);
446 // and make it public!
447 public List
<Paragraph
> requotify(Paragraph para
) {
448 return super.requotify(para
);