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