1 package org.apache.maven.doxia.sink;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.CharArrayWriter;
23 import java.io.IOException;
24 import java.io.Writer;
25
26 import org.apache.maven.doxia.AbstractModuleTest;
27 import org.apache.maven.doxia.logging.PlexusLoggerWrapper;
28 import org.codehaus.plexus.util.IOUtil;
29 import org.custommonkey.xmlunit.Diff;
30 import org.custommonkey.xmlunit.XMLUnit;
31
32
33
34
35
36
37
38 public abstract class AbstractSinkTest
39 extends AbstractModuleTest
40 {
41 private final CharArrayWriter testWriter = new CharArrayWriter();
42 private Sink sink;
43
44
45
46
47
48
49
50
51 @Override
52 protected void setUp()
53 throws Exception
54 {
55 super.setUp();
56
57 testWriter.reset();
58 sink = createSink( testWriter );
59 sink.enableLogging( new PlexusLoggerWrapper( getContainer().getLogger() ) );
60 }
61
62
63
64
65
66
67
68 protected String wrapXml( String xmlFragment )
69 {
70 return xmlFragment;
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 public final void testTestDocument() throws IOException
89 {
90 Writer writer = ( isXmlSink() ? getXmlTestWriter( "testDocument" ) : getTestWriter( "testDocument" ) );
91 Sink testSink = createSink( writer );
92
93 try
94 {
95 SinkTestDocument.generate( testSink );
96 }
97 finally
98 {
99 testSink.close();
100 IOUtil.close( writer );
101 }
102 }
103
104
105
106
107
108
109 public void testTitle()
110 {
111 String title = "Grodek";
112 sink.title();
113 sink.text( title );
114 sink.title_();
115 sink.flush();
116 sink.close();
117
118 String actual = testWriter.toString();
119 String expected = getTitleBlock( title );
120
121 assertEquals( "Wrong title!", expected, actual );
122 }
123
124
125
126
127
128
129 public void testAuthor()
130 {
131 String author = "Georg_Trakl";
132 sink.author();
133 sink.text( author );
134 sink.author_();
135 sink.flush();
136 sink.close();
137
138 String actual = testWriter.toString();
139 String expected = getAuthorBlock( author );
140
141 assertEquals( "Wrong author!", expected, actual );
142 }
143
144
145
146
147
148
149 public void testDate()
150 {
151 String date = "1914";
152 sink.date();
153 sink.text( date );
154 sink.date_();
155 sink.flush();
156 sink.close();
157
158 String actual = testWriter.toString();
159 String expected = getDateBlock( date );
160
161 assertEquals( "Wrong date!", expected, actual );
162 }
163
164
165
166
167
168
169 public void testHead()
170 {
171 sink.head();
172 sink.head_();
173 sink.flush();
174 sink.close();
175
176 String actual = testWriter.toString();
177 String expected = getHeadBlock();
178
179 assertEquals( "Wrong head!", expected, actual );
180 }
181
182
183
184
185
186
187 public void testBody()
188 {
189 sink.body();
190 sink.body_();
191 sink.flush();
192 sink.close();
193
194 String actual = testWriter.toString();
195 String expected = getBodyBlock();
196
197 assertEquals( "Wrong body!", expected, actual );
198 }
199
200
201
202
203
204
205
206 public void testSectionTitle()
207 {
208 String title = "Title";
209 sink.sectionTitle();
210 sink.text( title );
211 sink.sectionTitle_();
212 sink.flush();
213 sink.close();
214
215 String actual = testWriter.toString();
216 String expected = getSectionTitleBlock( title );
217
218 assertEquals( "Wrong sectionTitle!", expected, actual );
219 }
220
221
222
223
224
225
226
227 public void testSection1()
228 {
229 String title = "Title1";
230 sink.section1();
231 sink.sectionTitle1();
232 sink.text( title );
233 sink.sectionTitle1_();
234 sink.section1_();
235 sink.flush();
236 sink.close();
237
238 String actual = testWriter.toString();
239 String expected = getSection1Block( title );
240
241 assertEquals( "Wrong section1 block!", expected, actual );
242 }
243
244
245
246
247
248
249
250 public void testSection2()
251 {
252 String title = "Title2";
253 sink.section2();
254 sink.sectionTitle2();
255 sink.text( title );
256 sink.sectionTitle2_();
257 sink.section2_();
258 sink.flush();
259 sink.close();
260
261 String actual = testWriter.toString();
262 String expected = getSection2Block( title );
263
264 assertEquals( "Wrong section2 block!", expected, actual );
265 }
266
267
268
269
270
271
272
273 public void testSection3()
274 {
275 String title = "Title3";
276 sink.section3();
277 sink.sectionTitle3();
278 sink.text( title );
279 sink.sectionTitle3_();
280 sink.section3_();
281 sink.flush();
282 sink.close();
283
284 String actual = testWriter.toString();
285 String expected = getSection3Block( title );
286
287 assertEquals( "Wrong section3 block!", expected, actual );
288 }
289
290
291
292
293
294
295
296
297 public void testSection4()
298 {
299 String title = "Title4";
300 sink.section4();
301 sink.sectionTitle4();
302 sink.text( title );
303 sink.sectionTitle4_();
304 sink.section4_();
305 sink.flush();
306 sink.close();
307
308 String actual = testWriter.toString();
309 String expected = getSection4Block( title );
310
311 assertEquals( "Wrong section4 block!", expected, actual );
312 }
313
314
315
316
317
318
319
320 public void testSection5()
321 {
322 String title = "Title5";
323 sink.section5();
324 sink.sectionTitle5();
325 sink.text( title );
326 sink.sectionTitle5_();
327 sink.section5_();
328 sink.flush();
329 sink.close();
330
331 String actual = testWriter.toString();
332 String expected = getSection5Block( title );
333
334 assertEquals( "Wrong section5 block!", expected, actual );
335 }
336
337
338
339
340
341
342
343 public void testList()
344 {
345 String item = "list_item";
346 sink.list();
347 sink.listItem();
348 sink.text( item );
349 sink.listItem_();
350 sink.list_();
351 sink.flush();
352 sink.close();
353
354 String actual = testWriter.toString();
355 String expected = getListBlock( item );
356
357 assertEquals( "Wrong list!", expected, actual );
358 }
359
360
361
362
363
364
365
366
367 public void testNumberedList()
368 {
369 String item = "numbered_list_item";
370 sink.numberedList( Sink.NUMBERING_LOWER_ROMAN );
371 sink.numberedListItem();
372 sink.text( item );
373 sink.numberedListItem_();
374 sink.numberedList_();
375 sink.flush();
376 sink.close();
377
378 String actual = testWriter.toString();
379 String expected = getNumberedListBlock( item );
380
381 assertEquals( "Wrong numbered list!", expected, actual );
382 }
383
384
385
386
387
388
389
390
391
392 public void testDefinitionList()
393 {
394 String definum = "definum";
395 String definition = "definition";
396 sink.definitionList();
397 sink.definitionListItem();
398 sink.definedTerm();
399 sink.text( definum );
400 sink.definedTerm_();
401 sink.definition();
402 sink.text( definition );
403 sink.definition_();
404 sink.definitionListItem_();
405 sink.definitionList_();
406 sink.flush();
407 sink.close();
408
409 String actual = testWriter.toString();
410 String expected = getDefinitionListBlock( definum, definition );
411
412 assertEquals( "Wrong definition list!", expected, actual );
413 }
414
415
416
417
418
419
420
421 public void testFigure() throws Exception
422 {
423 String source = "figure.jpg";
424 String caption = "Figure_caption";
425 sink.figure();
426 sink.figureGraphics( source );
427 sink.figureCaption();
428 sink.text( caption );
429 sink.figureCaption_();
430 sink.figure_();
431 sink.flush();
432 sink.close();
433
434 String actual = testWriter.toString();
435 String expected = getFigureBlock( source, caption );
436
437 if ( isXmlSink() )
438 {
439 Diff diff = XMLUnit.compareXML( wrapXml( expected ), wrapXml( actual ) );
440 assertTrue( "Wrong figure!", diff.identical() );
441 }
442 else
443 {
444 assertEquals( "Wrong figure!", expected, actual );
445 }
446 }
447
448
449 public void testFigureWithoutCaption() throws Exception
450 {
451 String source = "figure.jpg";
452 sink.figure();
453 sink.figureGraphics( source );
454 sink.figure_();
455 sink.flush();
456 sink.close();
457
458 String actual = testWriter.toString();
459 String expected = getFigureBlock( source, null );
460
461 if ( isXmlSink() )
462 {
463 Diff diff = XMLUnit.compareXML( wrapXml( expected ), wrapXml( actual ) );
464 assertTrue( "Wrong figure!", diff.identical() );
465 }
466 else
467 {
468 assertEquals( "Wrong figure!", expected, actual );
469 }
470 }
471
472
473
474
475
476
477
478
479
480 public void testTable() throws Exception
481 {
482 String cell = "cell";
483 String caption = "Table_caption";
484 int[] justify = { Sink.JUSTIFY_CENTER };
485 sink.table();
486 sink.tableRows( justify, false );
487 sink.tableRow();
488 sink.tableCell();
489 sink.text( cell );
490 sink.tableCell_();
491 sink.tableRow_();
492 sink.tableRows_();
493 sink.tableCaption();
494 sink.text( caption );
495 sink.tableCaption_();
496 sink.table_();
497 sink.flush();
498 sink.close();
499
500 String actual = testWriter.toString();
501 String expected = getTableBlock( cell, caption );
502
503 if ( isXmlSink() )
504 {
505 Diff diff = XMLUnit.compareXML( wrapXml( expected ), wrapXml( actual ) );
506 assertTrue( "Wrong table!", diff.identical() );
507 }
508 else
509 {
510 assertEquals( "Wrong table!", expected, actual );
511 }
512 }
513
514
515
516
517
518
519 public void testParagraph()
520 {
521 String text = "Text";
522 sink.paragraph();
523 sink.text( text );
524 sink.paragraph_();
525 sink.flush();
526 sink.close();
527
528 String actual = testWriter.toString();
529 String expected = getParagraphBlock( text );
530
531 assertEquals( "Wrong paragraph!", expected, actual );
532 }
533
534
535
536
537
538
539 public void testVerbatim()
540 {
541 String text = "Text";
542 sink.verbatim( SinkEventAttributeSet.BOXED );
543 sink.text( text );
544 sink.verbatim_();
545 sink.flush();
546 sink.close();
547
548 String actual = testWriter.toString();
549 String expected = getVerbatimBlock( text );
550
551 assertEquals( "Wrong verbatim!", expected, actual );
552 }
553
554
555
556
557
558
559 public void testHorizontalRule()
560 {
561 sink.horizontalRule();
562 sink.flush();
563 sink.close();
564
565 String actual = testWriter.toString();
566 String expected = getHorizontalRuleBlock();
567
568 assertEquals( "Wrong horizontal rule!", expected, actual );
569 }
570
571
572
573
574
575
576 public void testPageBreak()
577 {
578 sink.pageBreak();
579 sink.flush();
580 sink.close();
581
582 String actual = testWriter.toString();
583 String expected = getPageBreakBlock();
584
585 assertEquals( "Wrong pageBreak!", expected, actual );
586 }
587
588
589
590
591
592
593 public void testAnchor()
594 {
595 String anchor = "Anchor";
596 sink.anchor( anchor );
597 sink.text( anchor );
598 sink.anchor_();
599 sink.flush();
600 sink.close();
601
602 String actual = testWriter.toString();
603 String expected = getAnchorBlock( anchor );
604
605 assertEquals( "Wrong anchor!", expected, actual );
606 }
607
608
609
610
611
612
613 public void testLink()
614 {
615 String link = "#Link";
616 String text = "Text";
617 sink.link( link );
618 sink.text( text );
619 sink.link_();
620 sink.flush();
621 sink.close();
622
623 String actual = testWriter.toString();
624 String expected = getLinkBlock( link, text );
625
626 assertEquals( "Wrong link!", expected, actual );
627 }
628
629
630
631
632
633
634 public void testItalic()
635 {
636 String text = "Italic";
637 sink.italic();
638 sink.text( text );
639 sink.italic_();
640 sink.flush();
641 sink.close();
642
643 String actual = testWriter.toString();
644 String expected = getItalicBlock( text );
645
646 assertEquals( "Wrong italic!", expected, actual );
647 }
648
649
650
651
652
653
654 public void testBold()
655 {
656 String text = "Bold";
657 sink.bold();
658 sink.text( text );
659 sink.bold_();
660 sink.flush();
661 sink.close();
662
663 String actual = testWriter.toString();
664 String expected = getBoldBlock( text );
665
666 assertEquals( "Wrong bold!", expected, actual );
667 }
668
669
670
671
672
673
674 public void testMonospaced()
675 {
676 String text = "Monospaced";
677 sink.monospaced();
678 sink.text( text );
679 sink.monospaced_();
680 sink.flush();
681 sink.close();
682
683 String actual = testWriter.toString();
684 String expected = getMonospacedBlock( text );
685
686 assertEquals( "Wrong monospaced!", expected, actual );
687 }
688
689
690
691
692
693
694 public void testLineBreak()
695 {
696 sink.lineBreak();
697 sink.flush();
698 sink.close();
699
700 String actual = testWriter.toString();
701 String expected = getLineBreakBlock();
702
703 assertEquals( "Wrong lineBreak!", expected, actual );
704 }
705
706
707
708
709
710
711 public void testNonBreakingSpace()
712 {
713 sink.nonBreakingSpace();
714 sink.flush();
715 sink.close();
716
717 String actual = testWriter.toString();
718 String expected = getNonBreakingSpaceBlock();
719
720 assertEquals( "Wrong nonBreakingSpace!", expected, actual );
721 }
722
723
724
725
726
727
728 public void testText()
729 {
730 String text = "~,_=,_-,_+,_*,_[,_],_<,_>,_{,_},_\\";
731 sink.text( text );
732 sink.flush();
733 sink.close();
734
735 String actual = testWriter.toString();
736 String expected = getTextBlock( text );
737
738 assertEquals( "Wrong text!", expected, actual );
739 }
740
741
742
743
744
745
746 public void testRawText()
747 {
748 String text = "~,_=,_-,_+,_*,_[,_],_<,_>,_{,_},_\\";
749 sink.rawText( text );
750 sink.flush();
751 sink.close();
752
753 String actual = testWriter.toString();
754 String expected = getRawTextBlock( text );
755
756 assertEquals( "Wrong rawText!", expected, actual );
757 }
758
759
760
761
762
763
764
765 public void testComment()
766 {
767 String comment = "Simple comment with ----";
768 sink.comment( comment );
769 sink.flush();
770 sink.close();
771
772 String actual = testWriter.toString();
773 String expected = getCommentBlock( comment );
774
775 assertEquals( "Wrong comment!", expected, actual );
776 }
777
778
779
780
781
782
783
784
785
786 protected Sink getSink()
787 {
788 return sink;
789 }
790
791
792
793
794
795 protected String getSinkContent()
796 {
797 return testWriter.toString();
798 }
799
800
801
802
803
804 protected String getOutputDir()
805 {
806 return "sink/";
807 }
808
809
810
811
812
813
814
815
816
817
818
819 protected abstract boolean isXmlSink();
820
821
822
823
824
825
826 protected abstract Sink createSink( Writer writer );
827
828
829
830
831
832
833
834 protected abstract String getTitleBlock( String title );
835
836
837
838
839
840
841
842 protected abstract String getAuthorBlock( String author );
843
844
845
846
847
848
849
850 protected abstract String getDateBlock( String date );
851
852
853
854
855
856
857 protected abstract String getHeadBlock();
858
859
860
861
862
863
864 protected abstract String getBodyBlock();
865
866
867
868
869
870
871
872 protected abstract String getSectionTitleBlock( String title );
873
874
875
876
877
878
879
880 protected abstract String getSection1Block( String title );
881
882
883
884
885
886
887
888 protected abstract String getSection2Block( String title );
889
890
891
892
893
894
895
896 protected abstract String getSection3Block( String title );
897
898
899
900
901
902
903
904 protected abstract String getSection4Block( String title );
905
906
907
908
909
910
911
912 protected abstract String getSection5Block( String title );
913
914
915
916
917
918
919
920 protected abstract String getListBlock( String item );
921
922
923
924
925
926
927
928 protected abstract String getNumberedListBlock( String item );
929
930
931
932
933
934
935
936
937 protected abstract String getDefinitionListBlock( String definum,
938 String definition );
939
940
941
942
943
944
945
946
947 protected abstract String getFigureBlock( String source, String caption );
948
949
950
951
952
953
954
955
956 protected abstract String getTableBlock( String cell, String caption );
957
958
959
960
961
962
963
964 protected abstract String getParagraphBlock( String text );
965
966
967
968
969
970
971
972 protected abstract String getVerbatimBlock( String text );
973
974
975
976
977
978
979 protected abstract String getHorizontalRuleBlock();
980
981
982
983
984
985
986 protected abstract String getPageBreakBlock();
987
988
989
990
991
992
993
994 protected abstract String getAnchorBlock( String anchor );
995
996
997
998
999
1000
1001
1002
1003 protected abstract String getLinkBlock( String link, String text );
1004
1005
1006
1007
1008
1009
1010
1011 protected abstract String getItalicBlock( String text );
1012
1013
1014
1015
1016
1017
1018
1019 protected abstract String getBoldBlock( String text );
1020
1021
1022
1023
1024
1025
1026
1027 protected abstract String getMonospacedBlock( String text );
1028
1029
1030
1031
1032
1033
1034 protected abstract String getLineBreakBlock();
1035
1036
1037
1038
1039
1040
1041
1042 protected abstract String getNonBreakingSpaceBlock();
1043
1044
1045
1046
1047
1048
1049
1050 protected abstract String getTextBlock( String text );
1051
1052
1053
1054
1055
1056
1057
1058 protected abstract String getRawTextBlock( String text );
1059
1060
1061
1062
1063
1064
1065
1066
1067 protected abstract String getCommentBlock( String text );
1068
1069 protected final void verifyValignSup( String text )
1070 {
1071 sink.text( "ValignSup", new SinkEventAttributeSet( SinkEventAttributes.VALIGN, "sup" ) );
1072 sink.flush();
1073 sink.close();
1074
1075 String actual = testWriter.toString();
1076
1077 assertEquals( "Wrong valign sup!", text, actual );
1078 }
1079
1080 protected final void verifyValignSub( String text )
1081 {
1082 sink.text( "ValignSub", new SinkEventAttributeSet( SinkEventAttributes.VALIGN, "sub" ) );
1083 sink.flush();
1084 sink.close();
1085
1086 String actual = testWriter.toString();
1087
1088 assertEquals( "Wrong valign sub!", text, actual );
1089 }
1090
1091 protected final void verifyDecorationUnderline( String text )
1092 {
1093 sink.text( "DecorationUnderline", new SinkEventAttributeSet( SinkEventAttributes.DECORATION, "underline" ) );
1094 sink.flush();
1095 sink.close();
1096
1097 String actual = testWriter.toString();
1098
1099 assertEquals( "Wrong decoration underline!", text, actual );
1100 }
1101
1102 protected final void verifyDecorationLineThrough( String text )
1103 {
1104 sink.text( "DecorationLineThrough", new SinkEventAttributeSet( SinkEventAttributes.DECORATION, "line-through" ) );
1105 sink.flush();
1106 sink.close();
1107
1108 String actual = testWriter.toString();
1109
1110 assertEquals( "Wrong decoration line-through!", text, actual );
1111 }
1112
1113
1114 }