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
23
24
25 import java.io.ByteArrayOutputStream;
26 import java.io.Writer;
27
28 import junit.framework.TestCase;
29
30 public class RandomAccessSinkTest
31 extends TestCase
32 {
33 private SinkFactory factory = new AbstractXmlSinkFactory()
34 {
35
36 protected Sink createSink( Writer writer, String encoding, String languageId )
37 {
38 return new TextSink( writer );
39 }
40
41 protected Sink createSink( Writer writer, String encoding )
42 {
43 return new TextSink( writer );
44 }
45 };
46
47 private void buildSimple( Sink sink, String text )
48 throws Exception
49 {
50 sink.anchor( "foobar" );
51 sink.text( text );
52 sink.anchor_();
53 }
54
55 public void testSimple()
56 throws Exception
57 {
58 String encoding = "UTF-8";
59 String text = "Hello World";
60 ByteArrayOutputStream outFlatSink = new ByteArrayOutputStream();
61 Sink flatSink = factory.createSink( outFlatSink, encoding );
62 buildSimple( flatSink, text );
63 flatSink.flush();
64 flatSink.close();
65
66 ByteArrayOutputStream outRandomAccessSink = new ByteArrayOutputStream();
67 RandomAccessSink randomAccessSink = new RandomAccessSink( factory, outRandomAccessSink, encoding );
68 buildSimple( randomAccessSink, text );
69 randomAccessSink.flush();
70 randomAccessSink.close();
71
72 assertEquals( outFlatSink.toString( encoding ), outRandomAccessSink.toString( encoding ) );
73 }
74
75 public void testComplex()
76 throws Exception
77 {
78 String encoding = "UTF-8";
79 String summaryText = "Summary text";
80 String detailText = "Detail text";
81 ByteArrayOutputStream outFlatSink = new ByteArrayOutputStream();
82 Sink flatSink = factory.createSink( outFlatSink, encoding );
83 buildSimple( flatSink, summaryText );
84 flatSink.horizontalRule();
85 buildSimple( flatSink, detailText );
86 flatSink.flush();
87 flatSink.close();
88
89 ByteArrayOutputStream outRandomAccessSink = new ByteArrayOutputStream();
90 RandomAccessSink randomAccessSink = new RandomAccessSink( factory, outRandomAccessSink, encoding );
91 Sink summarySink = randomAccessSink.addSinkHook();
92 randomAccessSink.horizontalRule();
93 Sink detailSink = randomAccessSink.addSinkHook();
94
95
96 buildSimple( detailSink, detailText );
97 buildSimple( summarySink, summaryText );
98
99 randomAccessSink.flush();
100 randomAccessSink.close();
101
102 assertEquals( outFlatSink.toString( encoding ), outRandomAccessSink.toString( encoding ) );
103 }
104 }