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