View Javadoc
1   package org.apache.maven.doxia.sink;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  /**
23   * @author Robert Scholte
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          // here's an example of the strength of randomAccessSink. Summary and detail are built in reverse order
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 }