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