View Javadoc
1   package org.apache.maven.doxia.sink.impl;
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 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         // here's an example of the strength of randomAccessSink. Summary and detail are built in reverse order
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 }