tests: fix NPE, add BasicSupportUtilities tests
[nikiroo-utils.git] / src / be / nikiroo / fanfix / test / BasicSupportUtilitiesTest.java
1 package be.nikiroo.fanfix.test;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.net.URL;
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import be.nikiroo.fanfix.Instance;
10 import be.nikiroo.fanfix.bundles.StringId;
11 import be.nikiroo.fanfix.data.Paragraph;
12 import be.nikiroo.fanfix.data.Paragraph.ParagraphType;
13 import be.nikiroo.fanfix.data.Story;
14 import be.nikiroo.fanfix.supported.BasicSupport;
15 import be.nikiroo.fanfix.supported.BasicSupportHelper;
16 import be.nikiroo.fanfix.supported.BasicSupportImages;
17 import be.nikiroo.fanfix.supported.BasicSupportPara;
18 import be.nikiroo.fanfix.supported.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;
23
24 class BasicSupportUtilitiesTest extends TestLauncher {
25 // quote chars
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);
34
35 public BasicSupportUtilitiesTest(String[] args) {
36 super("BasicSupportUtilities", args);
37
38 addSeries(new TestLauncher("General", args) {
39 {
40 addTest(new TestCase("BasicSupport.makeParagraphs()") {
41 @Override
42 public void test() throws Exception {
43 BasicSupportParaPublic bsPara = new BasicSupportParaPublic() {
44 @Override
45 public void fixBlanksBreaks(List<Paragraph> paras) {
46 }
47
48 @Override
49 public List<Paragraph> requotify(Paragraph para, boolean html) {
50 List<Paragraph> paras = new ArrayList<Paragraph>(
51 1);
52 paras.add(para);
53 return paras;
54 }
55 };
56
57 List<Paragraph> paras = null;
58
59 paras = bsPara.makeParagraphs(null, null, "", true, null);
60 assertEquals(
61 "An empty content should not generate paragraphs",
62 0, paras.size());
63
64 paras = bsPara.makeParagraphs(null, null,
65 "Line 1</p><p>Line 2</p><p>Line 3</p>", true, null);
66 assertEquals(5, paras.size());
67 assertEquals("Line 1", paras.get(0).getContent());
68 assertEquals(ParagraphType.BLANK, paras.get(1)
69 .getType());
70 assertEquals("Line 2", paras.get(2).getContent());
71 assertEquals(ParagraphType.BLANK, paras.get(3)
72 .getType());
73 assertEquals("Line 3", paras.get(4).getContent());
74
75 paras = bsPara.makeParagraphs(null, null,
76 "<p>Line1</p><p>Line2</p><p>Line3</p>", true, null);
77 assertEquals(6, paras.size());
78 }
79 });
80
81 addTest(new TestCase("BasicSupport.removeDoubleBlanks()") {
82 @Override
83 public void test() throws Exception {
84 BasicSupportParaPublic support = new BasicSupportParaPublic();
85
86 List<Paragraph> paras = null;
87
88 paras = support
89 .makeParagraphs(
90 null,
91 null,
92 "<p>Line1</p><p>Line2</p><p>Line3<br/><br><p></p>",
93 true,
94 null);
95 assertEquals(5, paras.size());
96
97 paras = support
98 .makeParagraphs(
99 null,
100 null,
101 "<p>Line1</p><p>Line2</p><p>Line3<br/><br><p></p>* * *",
102 true,
103 null);
104 assertEquals(5, paras.size());
105
106 paras = support.makeParagraphs(null, null, "1<p>* * *<p>2",
107 true, null);
108 assertEquals(3, paras.size());
109 assertEquals(ParagraphType.BREAK, paras.get(1)
110 .getType());
111
112 paras = support.makeParagraphs(null, null,
113 "1<p><br/><p>* * *<p>2", true, null);
114 assertEquals(3, paras.size());
115 assertEquals(ParagraphType.BREAK, paras.get(1)
116 .getType());
117
118 paras = support.makeParagraphs(null, null,
119 "1<p>* * *<br/><p><br><p>2", true, null);
120 assertEquals(3, paras.size());
121 assertEquals(ParagraphType.BREAK, paras.get(1)
122 .getType());
123
124 paras = support.makeParagraphs(null, null,
125 "1<p><br/><br>* * *<br/><p><br><p>2", true, null);
126 assertEquals(3, paras.size());
127 assertEquals(ParagraphType.BREAK, paras.get(1)
128 .getType());
129 }
130 });
131
132 addTest(new TestCase("BasicSupport.processPara() quotes") {
133 @Override
134 public void test() throws Exception {
135 BasicSupportParaPublic support = new BasicSupportParaPublic();
136
137 Paragraph para;
138
139 // sanity check
140 para = support.processPara("", true);
141 assertEquals(ParagraphType.BLANK, para.getType());
142 //
143
144 para = support.processPara("\"Yes, my Lord!\"", true);
145 assertEquals(ParagraphType.QUOTE, para.getType());
146 assertEquals(openDoubleQuote + "Yes, my Lord!"
147 + closeDoubleQuote, para.getContent());
148
149 para = support.processPara("«Yes, my Lord!»", true);
150 assertEquals(ParagraphType.QUOTE, para.getType());
151 assertEquals(openDoubleQuote + "Yes, my Lord!"
152 + closeDoubleQuote, para.getContent());
153
154 para = support.processPara("'Yes, my Lord!'", true);
155 assertEquals(ParagraphType.QUOTE, para.getType());
156 assertEquals(openQuote + "Yes, my Lord!" + closeQuote,
157 para.getContent());
158
159 para = support.processPara("‹Yes, my Lord!›", true);
160 assertEquals(ParagraphType.QUOTE, para.getType());
161 assertEquals(openQuote + "Yes, my Lord!" + closeQuote,
162 para.getContent());
163 }
164 });
165
166 addTest(new TestCase(
167 "BasicSupport.processPara() double-simple quotes") {
168 @Override
169 public void setUp() throws Exception {
170 super.setUp();
171
172 }
173
174 @Override
175 public void tearDown() throws Exception {
176
177 super.tearDown();
178 }
179
180 @Override
181 public void test() throws Exception {
182 BasicSupportParaPublic support = new BasicSupportParaPublic();
183
184 Paragraph para;
185
186 para = support.processPara("''Yes, my Lord!''", true);
187 assertEquals(ParagraphType.QUOTE, para.getType());
188 assertEquals(openDoubleQuote + "Yes, my Lord!"
189 + closeDoubleQuote, para.getContent());
190
191 para = support.processPara("‹‹Yes, my Lord!››", true);
192 assertEquals(ParagraphType.QUOTE, para.getType());
193 assertEquals(openDoubleQuote + "Yes, my Lord!"
194 + closeDoubleQuote, para.getContent());
195 }
196 });
197
198 addTest(new TestCase("BasicSupport.processPara() apostrophe") {
199 @Override
200 public void test() throws Exception {
201 BasicSupportParaPublic support = new BasicSupportParaPublic();
202
203 Paragraph para;
204
205 String text = "Nous étions en été, mais cela aurait être l'hiver quand nous n'étions encore qu'à Aubeuge";
206 para = support.processPara(text, true);
207 assertEquals(ParagraphType.NORMAL, para.getType());
208 assertEquals(text, para.getContent());
209 }
210 });
211
212 addTest(new TestCase("BasicSupport.processPara() words count") {
213 @Override
214 public void test() throws Exception {
215 BasicSupportParaPublic support = new BasicSupportParaPublic();
216
217 Paragraph para;
218
219 para = support.processPara("«Yes, my Lord!»", true);
220 assertEquals(3, para.getWords());
221
222 para = support.processPara("One, twee, trois.", true);
223 assertEquals(3, para.getWords());
224 }
225 });
226
227 addTest(new TestCase("BasicSupport.requotify() words count") {
228 @Override
229 public void test() throws Exception {
230 BasicSupportParaPublic support = new BasicSupportParaPublic();
231
232 char openDoubleQuote = Instance.getTrans()
233 .getCharacter(StringId.OPEN_DOUBLE_QUOTE);
234 char closeDoubleQuote = Instance.getTrans()
235 .getCharacter(StringId.CLOSE_DOUBLE_QUOTE);
236
237 String content = null;
238 Paragraph para = null;
239 List<Paragraph> paras = null;
240 long words = 0;
241
242 content = "One, twee, trois.";
243 para = new Paragraph(ParagraphType.NORMAL, content,
244 content.split(" ").length);
245 paras = support.requotify(para, false);
246 words = 0;
247 for (Paragraph p : paras) {
248 words += p.getWords();
249 }
250 assertEquals("Bad words count in a single paragraph",
251 para.getWords(), words);
252
253 content = "Such WoW! So Web2.0! With Colours!";
254 para = new Paragraph(ParagraphType.NORMAL, content,
255 content.split(" ").length);
256 paras = support.requotify(para, false);
257 words = 0;
258 for (Paragraph p : paras) {
259 words += p.getWords();
260 }
261 assertEquals("Bad words count in a single paragraph",
262 para.getWords(), words);
263
264 content = openDoubleQuote + "Such a good idea!"
265 + closeDoubleQuote
266 + ", she said. This ought to be a new para.";
267 para = new Paragraph(ParagraphType.QUOTE, content,
268 content.split(" ").length);
269 paras = support.requotify(para, false);
270 words = 0;
271 for (Paragraph p : paras) {
272 words += p.getWords();
273 }
274 assertEquals(
275 "Bad words count in a requotified paragraph",
276 para.getWords(), words);
277 }
278 });
279 }
280 });
281
282 addSeries(new TestLauncher("Text", args) {
283 {
284 addTest(new TestCase("Chapter detection simple") {
285 private File tmp;
286
287 @Override
288 public void setUp() throws Exception {
289 tmp = File.createTempFile("fanfix-text-file_", ".test");
290 IOUtils.writeSmallFile(tmp.getParentFile(),
291 tmp.getName(), "TITLE"
292 + "\n"//
293 + "By nona"
294 + "\n" //
295 + "\n" //
296 + "Chapter 0: Resumé" + "\n" + "\n"
297 + "'sume." + "\n" + "\n"
298 + "Chapter 1: chap1" + "\n" + "\n"
299 + "Fanfan." + "\n" + "\n"
300 + "Chapter 2: Chap2" + "\n" + "\n" //
301 + "Tulipe." + "\n");
302 }
303
304 @Override
305 public void tearDown() throws Exception {
306 tmp.delete();
307 }
308
309 @Override
310 public void test() throws Exception {
311 BasicSupport support = BasicSupport.getSupport(
312 SupportType.TEXT, tmp.toURI().toURL());
313
314 Story story = support.process(null);
315
316 assertEquals(2, story.getChapters().size());
317 assertEquals(1, story.getChapters().get(1)
318 .getParagraphs().size());
319 assertEquals("Tulipe.", story.getChapters().get(1)
320 .getParagraphs().get(0).getContent());
321 }
322 });
323
324 addTest(new TestCase("Chapter detection with String 'Chapter'") {
325 private File tmp;
326
327 @Override
328 public void setUp() throws Exception {
329 tmp = File.createTempFile("fanfix-text-file_", ".test");
330 IOUtils.writeSmallFile(tmp.getParentFile(),
331 tmp.getName(), "TITLE"
332 + "\n"//
333 + "By nona"
334 + "\n" //
335 + "\n" //
336 + "Chapter 0: Resumé" + "\n" + "\n"
337 + "'sume." + "\n" + "\n"
338 + "Chapter 1: chap1" + "\n" + "\n"
339 + "Chapter fout-la-merde" + "\n"
340 + "\n"//
341 + "Fanfan." + "\n" + "\n"
342 + "Chapter 2: Chap2" + "\n" + "\n" //
343 + "Tulipe." + "\n");
344 }
345
346 @Override
347 public void tearDown() throws Exception {
348 tmp.delete();
349 }
350
351 @Override
352 public void test() throws Exception {
353 BasicSupport support = BasicSupport.getSupport(
354 SupportType.TEXT, tmp.toURI().toURL());
355
356 Story story = support.process(null);
357
358 assertEquals(2, story.getChapters().size());
359 assertEquals(1, story.getChapters().get(1)
360 .getParagraphs().size());
361 assertEquals("Tulipe.", story.getChapters().get(1)
362 .getParagraphs().get(0).getContent());
363 }
364 });
365 }
366 });
367 }
368
369 class BasicSupportParaPublic extends BasicSupportPara {
370 public BasicSupportParaPublic() {
371 super(new BasicSupportHelper(), new BasicSupportImages());
372 }
373
374 @Override
375 // and make it public!
376 public Paragraph makeParagraph(BasicSupport support, URL source,
377 String line, boolean html) {
378 return super.makeParagraph(support, source, line, html);
379 }
380
381 @Override
382 // and make it public!
383 public List<Paragraph> makeParagraphs(BasicSupport support,
384 URL source, String content, boolean html, Progress pg)
385 throws IOException {
386 return super.makeParagraphs(support, source, content, html, pg);
387 }
388
389 @Override
390 // and make it public!
391 public Paragraph processPara(String line, boolean html) {
392 return super.processPara(line, html);
393 }
394
395 @Override
396 // and make it public!
397 public List<Paragraph> requotify(Paragraph para, boolean html) {
398 return super.requotify(para, html);
399 }
400 }
401 }