JsonIO: fix missing fields from MetaData
[fanfix.git] / test / BasicSupportDeprecatedTest.java
1 package be.nikiroo.fanfix.test;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.net.URL;
7 import java.util.ArrayList;
8 import java.util.List;
9 import java.util.Map.Entry;
10
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;
24
25 class BasicSupportDeprecatedTest extends TestLauncher {
26 // quote chars
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);
31
32 public BasicSupportDeprecatedTest(String[] args) {
33 super("BasicSupportDeprecated", args);
34
35 addSeries(new TestLauncher("General", args) {
36 {
37 addTest(new TestCase("BasicSupport.makeParagraphs()") {
38 @Override
39 public void test() throws Exception {
40 BasicSupportEmpty support = new BasicSupportEmpty() {
41 @Override
42 protected boolean isHtml() {
43 return true;
44 }
45
46 @Override
47 public void fixBlanksBreaks(List<Paragraph> paras) {
48 }
49
50 @Override
51 public List<Paragraph> requotify(Paragraph para) {
52 List<Paragraph> paras = new ArrayList<Paragraph>(
53 1);
54 paras.add(para);
55 return paras;
56 }
57 };
58
59 List<Paragraph> paras = null;
60
61 paras = support.makeParagraphs(null, "", null);
62 assertEquals(
63 "An empty content should not generate paragraphs",
64 0, paras.size());
65
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)
71 .getType());
72 assertEquals("Line 2", paras.get(2).getContent());
73 assertEquals(ParagraphType.BLANK, paras.get(3)
74 .getType());
75 assertEquals("Line 3", paras.get(4).getContent());
76
77 paras = support.makeParagraphs(null,
78 "<p>Line1</p><p>Line2</p><p>Line3</p>", null);
79 assertEquals(6, paras.size());
80 }
81 });
82
83 addTest(new TestCase("BasicSupport.removeDoubleBlanks()") {
84 @Override
85 public void test() throws Exception {
86 BasicSupportEmpty support = new BasicSupportEmpty() {
87 @Override
88 protected boolean isHtml() {
89 return true;
90 }
91 };
92
93 List<Paragraph> paras = null;
94
95 paras = support
96 .makeParagraphs(
97 null,
98 "<p>Line1</p><p>Line2</p><p>Line3<br/><br><p></p>",
99 null);
100 assertEquals(5, paras.size());
101
102 paras = support
103 .makeParagraphs(
104 null,
105 "<p>Line1</p><p>Line2</p><p>Line3<br/><br><p></p>* * *",
106 null);
107 assertEquals(5, paras.size());
108
109 paras = support.makeParagraphs(null, "1<p>* * *<p>2",
110 null);
111 assertEquals(3, paras.size());
112 assertEquals(ParagraphType.BREAK, paras.get(1)
113 .getType());
114
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)
119 .getType());
120
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)
125 .getType());
126
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)
131 .getType());
132 }
133 });
134
135 addTest(new TestCase("BasicSupport.processPara() quotes") {
136 @Override
137 public void test() throws Exception {
138 BasicSupportEmpty support = new BasicSupportEmpty() {
139 @Override
140 protected boolean isHtml() {
141 return true;
142 }
143 };
144
145 Paragraph para;
146
147 // sanity check
148 para = support.processPara("");
149 assertEquals(ParagraphType.BLANK, para.getType());
150 //
151
152 para = support.processPara("\"Yes, my Lord!\"");
153 assertEquals(ParagraphType.QUOTE, para.getType());
154 assertEquals(openDoubleQuote + "Yes, my Lord!"
155 + closeDoubleQuote, para.getContent());
156
157 para = support.processPara("«Yes, my Lord!»");
158 assertEquals(ParagraphType.QUOTE, para.getType());
159 assertEquals(openDoubleQuote + "Yes, my Lord!"
160 + closeDoubleQuote, para.getContent());
161
162 para = support.processPara("'Yes, my Lord!'");
163 assertEquals(ParagraphType.QUOTE, para.getType());
164 assertEquals(openQuote + "Yes, my Lord!" + closeQuote,
165 para.getContent());
166
167 para = support.processPara("‹Yes, my Lord!›");
168 assertEquals(ParagraphType.QUOTE, para.getType());
169 assertEquals(openQuote + "Yes, my Lord!" + closeQuote,
170 para.getContent());
171 }
172 });
173
174 addTest(new TestCase(
175 "BasicSupport.processPara() double-simple quotes") {
176 @Override
177 public void setUp() throws Exception {
178 super.setUp();
179
180 }
181
182 @Override
183 public void tearDown() throws Exception {
184
185 super.tearDown();
186 }
187
188 @Override
189 public void test() throws Exception {
190 BasicSupportEmpty support = new BasicSupportEmpty() {
191 @Override
192 protected boolean isHtml() {
193 return true;
194 }
195 };
196
197 Paragraph para;
198
199 para = support.processPara("''Yes, my Lord!''");
200 assertEquals(ParagraphType.QUOTE, para.getType());
201 assertEquals(openDoubleQuote + "Yes, my Lord!"
202 + closeDoubleQuote, para.getContent());
203
204 para = support.processPara("‹‹Yes, my Lord!››");
205 assertEquals(ParagraphType.QUOTE, para.getType());
206 assertEquals(openDoubleQuote + "Yes, my Lord!"
207 + closeDoubleQuote, para.getContent());
208 }
209 });
210
211 addTest(new TestCase("BasicSupport.processPara() apostrophe") {
212 @Override
213 public void test() throws Exception {
214 BasicSupportEmpty support = new BasicSupportEmpty() {
215 @Override
216 protected boolean isHtml() {
217 return true;
218 }
219 };
220
221 Paragraph para;
222
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());
227 }
228 });
229
230 addTest(new TestCase("BasicSupport.processPara() words count") {
231 @Override
232 public void test() throws Exception {
233 BasicSupportEmpty support = new BasicSupportEmpty() {
234 @Override
235 protected boolean isHtml() {
236 return true;
237 }
238 };
239
240 Paragraph para;
241
242 para = support.processPara("«Yes, my Lord!»");
243 assertEquals(3, para.getWords());
244
245 para = support.processPara("One, twee, trois.");
246 assertEquals(3, para.getWords());
247 }
248 });
249
250 addTest(new TestCase("BasicSupport.requotify() words count") {
251 @Override
252 public void test() throws Exception {
253 BasicSupportEmpty support = new BasicSupportEmpty();
254
255 char openDoubleQuote = Instance.getInstance().getTrans()
256 .getCharacter(StringId.OPEN_DOUBLE_QUOTE);
257 char closeDoubleQuote = Instance.getInstance().getTrans()
258 .getCharacter(StringId.CLOSE_DOUBLE_QUOTE);
259
260 String content = null;
261 Paragraph para = null;
262 List<Paragraph> paras = null;
263 long words = 0;
264
265 content = "One, twee, trois.";
266 para = new Paragraph(ParagraphType.NORMAL, content,
267 content.split(" ").length);
268 paras = support.requotify(para);
269 words = 0;
270 for (Paragraph p : paras) {
271 words += p.getWords();
272 }
273 assertEquals("Bad words count in a single paragraph",
274 para.getWords(), words);
275
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);
280 words = 0;
281 for (Paragraph p : paras) {
282 words += p.getWords();
283 }
284 assertEquals("Bad words count in a single paragraph",
285 para.getWords(), words);
286
287 content = openDoubleQuote + "Such a good idea!"
288 + closeDoubleQuote
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);
293 words = 0;
294 for (Paragraph p : paras) {
295 words += p.getWords();
296 }
297 assertEquals(
298 "Bad words count in a requotified paragraph",
299 para.getWords(), words);
300 }
301 });
302 }
303 });
304
305 addSeries(new TestLauncher("Text", args) {
306 {
307 addTest(new TestCase("Chapter detection simple") {
308 private File tmp;
309
310 @Override
311 public void setUp() throws Exception {
312 tmp = File.createTempFile("fanfix-text-file_", ".test");
313 IOUtils.writeSmallFile(tmp.getParentFile(),
314 tmp.getName(), "TITLE"
315 + "\n"//
316 + "By nona"
317 + "\n" //
318 + "\n" //
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" //
324 + "Tulipe." + "\n");
325 }
326
327 @Override
328 public void tearDown() throws Exception {
329 tmp.delete();
330 }
331
332 @Override
333 public void test() throws Exception {
334 BasicSupport support = BasicSupport.getSupport(
335 SupportType.TEXT, tmp.toURI().toURL());
336
337 Story story = support.process(null);
338
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());
344 }
345 });
346
347 addTest(new TestCase("Chapter detection with String 'Chapter'") {
348 private File tmp;
349
350 @Override
351 public void setUp() throws Exception {
352 tmp = File.createTempFile("fanfix-text-file_", ".test");
353 IOUtils.writeSmallFile(tmp.getParentFile(),
354 tmp.getName(), "TITLE"
355 + "\n"//
356 + "By nona"
357 + "\n" //
358 + "\n" //
359 + "Chapter 0: Resumé" + "\n" + "\n"
360 + "'sume." + "\n" + "\n"
361 + "Chapter 1: chap1" + "\n" + "\n"
362 + "Chapter fout-la-merde" + "\n"
363 + "\n"//
364 + "Fanfan." + "\n" + "\n"
365 + "Chapter 2: Chap2" + "\n" + "\n" //
366 + "Tulipe." + "\n");
367 }
368
369 @Override
370 public void tearDown() throws Exception {
371 tmp.delete();
372 }
373
374 @Override
375 public void test() throws Exception {
376 BasicSupport support = BasicSupport.getSupport(
377 SupportType.TEXT, tmp.toURI().toURL());
378
379 Story story = support.process(null);
380
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());
386 }
387 });
388 }
389 });
390 }
391
392 private class BasicSupportEmpty extends BasicSupport_Deprecated {
393 @Override
394 protected boolean supports(URL url) {
395 return false;
396 }
397
398 @Override
399 protected boolean isHtml() {
400 return false;
401 }
402
403 @Override
404 protected MetaData getMeta(URL source, InputStream in)
405 throws IOException {
406 return null;
407 }
408
409 @Override
410 protected String getDesc(URL source, InputStream in) throws IOException {
411 return null;
412 }
413
414 @Override
415 protected List<Entry<String, URL>> getChapters(URL source,
416 InputStream in, Progress pg) throws IOException {
417 return null;
418 }
419
420 @Override
421 protected String getChapterContent(URL source, InputStream in,
422 int number, Progress pg) throws IOException {
423 return null;
424 }
425
426 @Override
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);
431 }
432
433 @Override
434 // and make it public!
435 public void fixBlanksBreaks(List<Paragraph> paras) {
436 super.fixBlanksBreaks(paras);
437 }
438
439 @Override
440 // and make it public!
441 public Paragraph processPara(String line) {
442 return super.processPara(line);
443 }
444
445 @Override
446 // and make it public!
447 public List<Paragraph> requotify(Paragraph para) {
448 return super.requotify(para);
449 }
450 }
451 }