1 package org.apache.maven.doxia;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.ByteArrayOutputStream;
23 import java.io.File;
24 import java.io.FileOutputStream;
25 import java.io.FileReader;
26 import java.io.FileWriter;
27 import java.io.OutputStream;
28 import java.io.StringWriter;
29
30 import org.apache.maven.doxia.wrapper.InputFileWrapper;
31 import org.apache.maven.doxia.wrapper.InputReaderWrapper;
32 import org.apache.maven.doxia.wrapper.OutputFileWrapper;
33 import org.apache.maven.doxia.wrapper.OutputStreamWrapper;
34 import org.codehaus.plexus.PlexusTestCase;
35 import org.codehaus.plexus.util.FileUtils;
36 import org.codehaus.plexus.util.IOUtil;
37 import org.codehaus.plexus.util.ReaderFactory;
38 import org.codehaus.plexus.util.WriterFactory;
39
40
41
42
43
44
45 public class ConverterTest
46 extends PlexusTestCase
47 {
48 private Converter converter;
49
50 private boolean formatOutput;
51
52
53 @Override
54 protected void setUp()
55 throws Exception
56 {
57 super.setUp();
58
59 converter = new DefaultConverter();
60
61 formatOutput = Boolean.parseBoolean( System.getProperty( "format", "false" ) );
62 }
63
64
65 @Override
66 protected void tearDown()
67 throws Exception
68 {
69 super.tearDown();
70
71 converter = null;
72 }
73
74
75
76
77 public void testGetInputFormats()
78 {
79 assertNotNull( converter.getInputFormats() );
80 }
81
82
83
84
85 public void testGetOutputFormats()
86 {
87 assertNotNull( converter.getOutputFormats() );
88 }
89
90
91
92
93
94
95
96 public void testFileConverterWithInputFileOutputDir()
97 throws Exception
98 {
99 String in = getBasedir() + "/src/test/resources/unit/Doxia.htm";
100 String from = "xhtml";
101 String out = getBasedir() + "/target/unit/";
102 String to = "apt";
103
104 InputFileWrapper input =
105 InputFileWrapper.valueOf( in, from, ReaderFactory.UTF_8, converter.getInputFormats() );
106 OutputFileWrapper output =
107 OutputFileWrapper.valueOf( out, to, WriterFactory.UTF_8, converter.getOutputFormats() );
108
109 converter.setFormatOutput( formatOutput );
110 converter.convert( input, output );
111 assertTrue( new File( out, "Doxia.htm.apt" ).exists() );
112 assertTrue( new File( out, "Doxia.htm.apt" ).length() != 0 );
113
114 FileUtils.deleteDirectory( new File( getBasedir() + "/target/unit/" ) );
115 }
116
117
118
119
120
121
122
123 public void testFileConverterWithInputFileOutputFile()
124 throws Exception
125 {
126 String in = getBasedir() + "/src/test/resources/unit/Doxia.htm";
127 String from = "xhtml";
128 String out = getBasedir() + "/target/unit/Doxia.apt";
129 String to = "apt";
130
131 InputFileWrapper input =
132 InputFileWrapper.valueOf( in, from, ReaderFactory.UTF_8, converter.getInputFormats() );
133 OutputFileWrapper output =
134 OutputFileWrapper.valueOf( out, to, WriterFactory.UTF_8, converter.getOutputFormats() );
135
136 converter.setFormatOutput( formatOutput );
137 converter.convert( input, output );
138 assertTrue( new File( out ).exists() );
139 assertTrue( new File( out ).length() != 0 );
140
141 FileUtils.deleteDirectory( new File( getBasedir() + "/target/unit/" ) );
142 }
143
144
145
146
147
148
149
150 public void testAptFileConverter()
151 throws Exception
152 {
153 String in = getBasedir() + "/src/test/resources/unit/apt/test.apt";
154 String from = "apt";
155 String out = getBasedir() + "/target/unit/file/apt/test.apt.xhtml";
156 String to = "xhtml";
157
158 InputFileWrapper input =
159 InputFileWrapper.valueOf( in, from, ReaderFactory.UTF_8, converter.getInputFormats() );
160 OutputFileWrapper output =
161 OutputFileWrapper.valueOf( out, to, WriterFactory.UTF_8, converter.getOutputFormats() );
162
163 converter.setFormatOutput( formatOutput );
164 converter.convert( input, output );
165 assertTrue( new File( out ).exists() );
166 assertTrue( new File( out ).length() != 0 );
167
168 in = getBasedir() + "/target/unit/file/apt/test.apt.xhtml";
169 from = "xhtml";
170 out = getBasedir() + "/target/unit/file/apt/test.apt";
171 to = "apt";
172
173 input = InputFileWrapper.valueOf( in, from, ReaderFactory.UTF_8, converter.getInputFormats() );
174 output = OutputFileWrapper.valueOf( out, to, WriterFactory.UTF_8, converter.getOutputFormats() );
175
176 converter.setFormatOutput( formatOutput );
177 converter.convert( input, output );
178 assertTrue( new File( out ).exists() );
179 assertTrue( new File( out ).length() != 0 );
180 }
181
182
183
184
185
186
187
188 public void testConfluenceFileConverter()
189 throws Exception
190 {
191 String in = getBasedir() + "/src/test/resources/unit/confluence/test.confluence";
192 String from = "confluence";
193 String out = getBasedir() + "/target/unit/file/confluence/test.confluence.xhtml";
194 String to = "xhtml";
195
196 InputFileWrapper input =
197 InputFileWrapper.valueOf( in, from, ReaderFactory.UTF_8, converter.getInputFormats() );
198 OutputFileWrapper output =
199 OutputFileWrapper.valueOf( out, to, WriterFactory.UTF_8, converter.getOutputFormats() );
200
201 converter.setFormatOutput( formatOutput );
202 converter.convert( input, output );
203 assertTrue( new File( out ).exists() );
204 assertTrue( new File( out ).length() != 0 );
205
206 in = getBasedir() + "/target/unit/file/confluence/test.confluence.xhtml";
207 from = "xhtml";
208 out = getBasedir() + "/target/unit/file/confluence/test.confluence";
209 to = "confluence";
210
211 input = InputFileWrapper.valueOf( in, from, ReaderFactory.UTF_8, converter.getInputFormats() );
212 output = OutputFileWrapper.valueOf( out, to, WriterFactory.UTF_8, converter.getOutputFormats() );
213
214 converter.setFormatOutput( formatOutput );
215 converter.convert( input, output );
216 }
217
218
219
220
221
222
223
224 public void testDocbookFileConverter()
225 throws Exception
226 {
227 String in = getBasedir() + "/src/test/resources/unit/docbook/test.xml";
228 String from = "docbook";
229 String out = getBasedir() + "/target/unit/file/docbook/test.docbook.xhtml";
230 String to = "xhtml";
231
232 InputFileWrapper input =
233 InputFileWrapper.valueOf( in, from, ReaderFactory.UTF_8, converter.getInputFormats() );
234 OutputFileWrapper output =
235 OutputFileWrapper.valueOf( out, to, WriterFactory.UTF_8, converter.getOutputFormats() );
236
237 converter.setFormatOutput( formatOutput );
238 converter.convert( input, output );
239 assertTrue( new File( out ).exists() );
240 assertTrue( new File( out ).length() != 0 );
241
242 in = getBasedir() + "/target/unit/file/docbook/test.docbook.xhtml";
243 from = "xhtml";
244 out = getBasedir() + "/target/unit/file/docbook/test.docbook";
245 to = "docbook";
246
247 input = InputFileWrapper.valueOf( in, from, converter.getInputFormats() );
248 output = OutputFileWrapper.valueOf( out, to, converter.getOutputFormats() );
249
250 converter.setFormatOutput( formatOutput );
251 converter.convert( input, output );
252 assertTrue( new File( out ).exists() );
253 }
254
255
256
257
258
259
260
261 public void testFmlFileConverter()
262 throws Exception
263 {
264 String in = getBasedir() + "/src/test/resources/unit/fml/test.fml";
265 String from = "fml";
266 String out = getBasedir() + "/target/unit/file/fml/test.fml.xhtml";
267 String to = "xhtml";
268
269 InputFileWrapper input =
270 InputFileWrapper.valueOf( in, from, ReaderFactory.UTF_8, converter.getInputFormats() );
271 OutputFileWrapper output =
272 OutputFileWrapper.valueOf( out, to, WriterFactory.UTF_8, converter.getOutputFormats() );
273
274 converter.setFormatOutput( formatOutput );
275 converter.convert( input, output );
276 assertTrue( new File( out ).exists() );
277 assertTrue( new File( out ).length() != 0 );
278
279 in = getBasedir() + "/target/unit/file/fml/test.fml.xhtml";
280 from = "xhtml";
281 out = getBasedir() + "/target/unit/file/fml/test.fml";
282 to = "fml";
283
284 try
285 {
286 input = InputFileWrapper.valueOf( in, from, ReaderFactory.UTF_8, converter.getInputFormats() );
287 output = OutputFileWrapper.valueOf( out, to, WriterFactory.UTF_8, converter.getOutputFormats() );
288
289 converter.setFormatOutput( formatOutput );
290 converter.convert( input, output );
291
292 fail();
293 }
294 catch ( UnsupportedFormatException e )
295 {
296 assertTrue( true );
297 }
298 }
299
300
301
302
303
304
305
306 public void testTwikiFileConverter()
307 throws Exception
308 {
309 String in = getBasedir() + "/src/test/resources/unit/twiki/test.twiki";
310 String from = "twiki";
311 String out = getBasedir() + "/target/unit/file/twiki/test.twiki.xhtml";
312 String to = "xhtml";
313
314 InputFileWrapper input =
315 InputFileWrapper.valueOf( in, from, ReaderFactory.UTF_8, converter.getInputFormats() );
316 OutputFileWrapper output =
317 OutputFileWrapper.valueOf( out, to, WriterFactory.UTF_8, converter.getOutputFormats() );
318
319 converter.setFormatOutput( formatOutput );
320 converter.convert( input, output );
321 assertTrue( new File( out ).exists() );
322 assertTrue( new File( out ).length() != 0 );
323
324 in = getBasedir() + "/target/unit/file/twiki/test.twiki.xhtml";
325 from = "xhtml";
326 out = getBasedir() + "/target/unit/file/twiki/test.twiki";
327 to = "twiki";
328
329 input = InputFileWrapper.valueOf( in, from, ReaderFactory.UTF_8, converter.getInputFormats() );
330 output = OutputFileWrapper.valueOf( out, to, WriterFactory.UTF_8, converter.getOutputFormats() );
331
332 converter.setFormatOutput( formatOutput );
333 try
334 {
335 converter.convert( input, output );
336 }
337 catch ( ConverterException e )
338 {
339
340 if ( !e.getMessage().contains( "Error validating the model" ) )
341 {
342 throw e;
343 }
344 }
345 }
346
347
348
349
350
351
352
353 public void testXdocFileConverter()
354 throws Exception
355 {
356 String in = getBasedir() + "/src/test/resources/unit/xdoc/test.xml";
357 String from = "xdoc";
358 String out = getBasedir() + "/target/unit/file/xdoc/test.xdoc.xhtml";
359 String to = "xhtml";
360
361 InputFileWrapper input =
362 InputFileWrapper.valueOf( in, from, ReaderFactory.UTF_8, converter.getInputFormats() );
363 OutputFileWrapper output =
364 OutputFileWrapper.valueOf( out, to, WriterFactory.UTF_8, converter.getOutputFormats() );
365
366 converter.setFormatOutput( formatOutput );
367 converter.convert( input, output );
368 assertTrue( new File( out ).exists() );
369 assertTrue( new File( out ).length() != 0 );
370
371 in = getBasedir() + "/target/unit/file/xdoc/test.xdoc.xhtml";
372 from = "xhtml";
373 out = getBasedir() + "/target/unit/file/xdoc/test.xdoc";
374 to = "xdoc";
375
376 input = InputFileWrapper.valueOf( in, from, ReaderFactory.UTF_8, converter.getInputFormats() );
377 output = OutputFileWrapper.valueOf( out, to, WriterFactory.UTF_8, converter.getOutputFormats() );
378
379 converter.setFormatOutput( formatOutput );
380 converter.convert( input, output );
381 assertTrue( new File( out ).exists() );
382 assertTrue( new File( out ).length() != 0 );
383 }
384
385
386
387
388
389
390
391 public void testXhtmlFileConverter()
392 throws Exception
393 {
394 String in = getBasedir() + "/src/test/resources/unit/xhtml/test.xhtml";
395 String from = "xhtml";
396 String out = getBasedir() + "/target/unit/file/xhtml/test.xhtml.xhtml";
397 String to = "xhtml";
398
399 InputFileWrapper input =
400 InputFileWrapper.valueOf( in, from, ReaderFactory.UTF_8, converter.getInputFormats() );
401 OutputFileWrapper output =
402 OutputFileWrapper.valueOf( out, to, WriterFactory.UTF_8, converter.getOutputFormats() );
403
404 converter.setFormatOutput( formatOutput );
405 converter.convert( input, output );
406 assertTrue( new File( out ).exists() );
407 assertTrue( new File( out ).length() != 0 );
408
409 in = getBasedir() + "/target/unit/file/xhtml/test.xhtml.xhtml";
410 from = "xhtml";
411 out = getBasedir() + "/target/unit/file/xhtml/test.xhtml";
412 to = "xhtml";
413
414 input = InputFileWrapper.valueOf( in, from, ReaderFactory.UTF_8, converter.getInputFormats() );
415 output = OutputFileWrapper.valueOf( out, to, WriterFactory.UTF_8, converter.getOutputFormats() );
416
417 converter.setFormatOutput( formatOutput );
418 converter.convert( input, output );
419 assertTrue( new File( out ).exists() );
420 assertTrue( new File( out ).length() != 0 );
421 }
422
423
424
425
426
427
428 public void testXhtml5FileConverter()
429 throws Exception
430 {
431 String in = getBasedir() + "/src/test/resources/unit/xhtml/test.xhtml5";
432 String from = "xhtml5";
433 String out = getBasedir() + "/target/unit/file/xhtml/test.xhtml.xhtml5";
434 String to = "xhtml5";
435
436 InputFileWrapper input =
437 InputFileWrapper.valueOf( in, from, ReaderFactory.UTF_8, converter.getInputFormats() );
438 OutputFileWrapper output =
439 OutputFileWrapper.valueOf( out, to, WriterFactory.UTF_8, converter.getOutputFormats() );
440
441 converter.setFormatOutput( formatOutput );
442 converter.convert( input, output );
443 assertTrue( new File( out ).exists() );
444 assertTrue( new File( out ).length() != 0 );
445
446 in = getBasedir() + "/target/unit/file/xhtml/test.xhtml.xhtml5";
447 from = "xhtml5";
448 out = getBasedir() + "/target/unit/file/xhtml/test.xhtml5";
449 to = "xhtml5";
450
451 input = InputFileWrapper.valueOf( in, from, ReaderFactory.UTF_8, converter.getInputFormats() );
452 output = OutputFileWrapper.valueOf( out, to, WriterFactory.UTF_8, converter.getOutputFormats() );
453
454 converter.setFormatOutput( formatOutput );
455 converter.convert( input, output );
456 assertTrue( new File( out ).exists() );
457 assertTrue( new File( out ).length() != 0 );
458 }
459
460
461
462
463
464
465
466 public void testAptWriterConverter()
467 throws Exception
468 {
469 String in = getBasedir() + "/src/test/resources/unit/apt/test.apt";
470 String from = "apt";
471 String out = getBasedir() + "/target/unit/writer/apt/test.apt.xhtml";
472 String to = "xhtml";
473
474 File inFile = new File( in );
475 File outFile = new File( out );
476 outFile.getParentFile().mkdirs();
477
478 try ( OutputStream fo = new FileOutputStream( outFile ) )
479 {
480 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
481
482 InputReaderWrapper input = InputReaderWrapper.valueOf( new FileReader( inFile ), from,
483 converter.getInputFormats() );
484 OutputStreamWrapper output = OutputStreamWrapper.valueOf( outputStream, to, "UTF-8",
485 converter.getOutputFormats() );
486
487 converter.setFormatOutput( formatOutput );
488 converter.convert( input, output );
489
490 IOUtil.copy( outputStream.toByteArray(), fo );
491 }
492
493 assertTrue( outFile.exists() );
494 assertTrue( outFile.length() != 0 );
495 }
496
497
498
499
500
501
502
503 public void testConfluenceWriterConverter()
504 throws Exception
505 {
506 String in = getBasedir() + "/src/test/resources/unit/confluence/test.confluence";
507 String from = "confluence";
508 String out = getBasedir() + "/target/unit/writer/confluence/test.confluence.xhtml";
509 String to = "xhtml";
510
511 File inFile = new File( in );
512 File outFile = new File( out );
513 outFile.getParentFile().mkdirs();
514
515 try ( OutputStream fo = new FileOutputStream( outFile ) )
516 {
517 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
518
519 InputReaderWrapper input = InputReaderWrapper.valueOf( new FileReader( inFile ), from,
520 converter.getInputFormats() );
521 OutputStreamWrapper output = OutputStreamWrapper.valueOf( outputStream, to, "UTF-8",
522 converter.getOutputFormats() );
523
524 converter.setFormatOutput( formatOutput );
525 converter.convert( input, output );
526
527 IOUtil.copy( outputStream.toByteArray(), fo );
528 }
529
530 assertTrue( outFile.exists() );
531 assertTrue( outFile.length() != 0 );
532 }
533
534
535
536
537
538
539
540 public void testAutoDetectConverter()
541 throws Exception
542 {
543 String in = getBasedir() + "/src/test/resources/unit/xdoc/test.xml";
544 String out = getBasedir() + "/target/unit/writer/apt/test.xdoc.apt";
545 String to = "xhtml";
546
547 File inFile = new File( in );
548 File outFile = new File( out );
549 outFile.getParentFile().mkdirs();
550
551 try (FileWriter fw = new FileWriter( outFile ))
552 {
553 StringWriter writer = new StringWriter();
554
555 InputFileWrapper input =
556 InputFileWrapper.valueOf( inFile.getAbsolutePath(), null, converter.getInputFormats() );
557 OutputFileWrapper output =
558 OutputFileWrapper.valueOf( outFile.getAbsolutePath(), to, converter.getOutputFormats() );
559
560 converter.setFormatOutput( formatOutput );
561 converter.convert( input, output );
562
563 IOUtil.copy( writer.toString(), fw );
564
565 assertTrue( outFile.exists() );
566 assertTrue( outFile.length() != 0 );
567 }
568
569 in = getBasedir() + "/src/test/resources/unit/apt/test.apt";
570 out = getBasedir() + "/target/unit/writer/apt/test.apt.xhtml";
571 to = "xhtml";
572
573 inFile = new File( in );
574 outFile = new File( out );
575 outFile.getParentFile().mkdirs();
576
577 try (FileWriter fw = new FileWriter( outFile ))
578 {
579 StringWriter writer = new StringWriter();
580
581 InputFileWrapper input =
582 InputFileWrapper.valueOf( inFile.getAbsolutePath(), null, converter.getInputFormats() );
583 OutputFileWrapper output =
584 OutputFileWrapper.valueOf( outFile.getAbsolutePath(), to, converter.getOutputFormats() );
585
586 converter.setFormatOutput( formatOutput );
587 converter.convert( input, output );
588
589 IOUtil.copy( writer.toString(), fw );
590
591 assertTrue( outFile.exists() );
592 assertTrue( outFile.length() != 0 );
593 }
594
595 in = getBasedir() + "/src/test/resources/unit/apt/test.unknown";
596 out = getBasedir() + "/target/unit/writer/apt/test.apt.xhtml";
597 to = "xhtml";
598
599 inFile = new File( in );
600 outFile = new File( out );
601 outFile.getParentFile().mkdirs();
602
603 try (FileWriter fw = new FileWriter( outFile ))
604 {
605 InputFileWrapper input =
606 InputFileWrapper.valueOf( inFile.getAbsolutePath(), null, converter.getInputFormats() );
607 OutputFileWrapper output =
608 OutputFileWrapper.valueOf( outFile.getAbsolutePath(), to, converter.getOutputFormats() );
609
610 converter.setFormatOutput( formatOutput );
611 converter.convert( input, output );
612
613 fail();
614 }
615 catch ( UnsupportedOperationException e )
616 {
617 assertTrue( true );
618 }
619
620 in = getBasedir() + "/src/test/resources/unit/xhtml/test.xhtml5";
621 out = getBasedir() + "/target/unit/writer/xhtml/test.html5.xhtml5";
622 to = "xhtml5";
623
624 inFile = new File( in );
625 outFile = new File( out );
626 outFile.getParentFile().mkdirs();
627
628 try (FileWriter fw = new FileWriter( outFile ))
629 {
630 StringWriter writer = new StringWriter();
631
632 InputFileWrapper input =
633 InputFileWrapper.valueOf( inFile.getAbsolutePath(), null, converter.getInputFormats() );
634 OutputFileWrapper output =
635 OutputFileWrapper.valueOf( outFile.getAbsolutePath(), to, converter.getOutputFormats() );
636
637 converter.setFormatOutput( formatOutput );
638 converter.convert( input, output );
639
640 IOUtil.copy( writer.toString(), fw );
641
642 assertTrue( outFile.exists() );
643 assertTrue( outFile.length() != 0 );
644 }
645
646
647 }
648
649 private String autoDetectEncoding( File f )
650 {
651 return DefaultConverter.autoDetectEncoding( f );
652 }
653
654 private String autoDetectEncoding( String filename )
655 {
656 return autoDetectEncoding( new File( getBasedir() + "/src/test/resources/unit/" + filename ) );
657 }
658
659
660
661
662 public void testAutodetectEncoding()
663 {
664 assertEquals( "ISO-8859-1", autoDetectEncoding( "apt/test.apt" ) );
665 assertEquals( "ISO-8859-1", autoDetectEncoding( "confluence/test.confluence" ) );
666 assertEquals( "UTF-8", autoDetectEncoding( "docbook/test.xml" ) );
667 assertEquals( "UTF-8", autoDetectEncoding( "fml/test.fml" ) );
668 assertEquals( "ISO-8859-1", autoDetectEncoding( "twiki/test.twiki" ) );
669 assertEquals( "UTF-8", autoDetectEncoding( "xhtml/test.xhtml" ) );
670 }
671
672 private String autoDetectFormat( File f, String encoding )
673 {
674 return DefaultConverter.autoDetectFormat( f, encoding );
675 }
676
677 private String autoDetectFormat( String filename, String encoding )
678 {
679 return autoDetectFormat( new File( getBasedir() + "/src/test/resources/unit/" + filename ), encoding );
680 }
681
682
683
684
685
686 public void testAutodetectFormat()
687 {
688 assertEquals( autoDetectFormat( "apt/test.apt", "UTF-8" ), "apt" );
689
690 try
691 {
692 autoDetectFormat( "apt/test.unknown", "UTF-8" );
693 fail();
694 }
695 catch ( UnsupportedOperationException e )
696 {
697 assertTrue( true );
698 }
699
700 assertEquals( autoDetectFormat( "confluence/test.confluence", "UTF-8" ), "confluence" );
701 assertEquals( autoDetectFormat( "docbook/test.xml", "UTF-8" ), "docbook" );
702 assertEquals( autoDetectFormat( "fml/test.fml", "UTF-8" ), "fml" );
703 assertEquals( autoDetectFormat( "twiki/test.twiki", "UTF-8" ), "twiki" );
704 assertEquals( autoDetectFormat( "xhtml/test.xhtml", "UTF-8" ), "xhtml" );
705 }
706 }