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  import javax.swing.text.html.HTML.Tag;
23  
24  import org.apache.maven.doxia.markup.Markup;
25  
26  import junit.framework.TestCase;
27  
28  /**
29   *
30   * @author ltheussl
31   */
32  public class AbstractXmlSinkTest
33          extends TestCase
34  {
35      /**
36       * Test of set/getNameSpace method, of class AbstractXmlSink.
37       */
38      public void testNameSpace()
39      {
40          final Tag t = Tag.A;
41          final String ns = "ns";
42          final XmlTestSink instance = new XmlTestSink();
43  
44          instance.writeStartTag( t );
45          instance.writeEndTag( t );
46          assertEquals( "<a></a>", instance.getText() );
47  
48          instance.writeSimpleTag( t );
49          assertEquals( "<a />", instance.getText() );
50  
51          instance.setNameSpace( ns );
52  
53          instance.writeStartTag( t );
54          instance.writeEndTag( t );
55          assertEquals( "<ns:a></ns:a>", instance.getText() );
56  
57          instance.writeSimpleTag( t );
58          assertEquals( "<ns:a />", instance.getText() );
59  
60          assertEquals( ns, instance.getNameSpace() );
61  
62          try
63          {
64              instance.writeStartTag( null );
65              fail( "null tag should fail!" );
66          }
67          catch ( IllegalArgumentException e )
68          {
69              assertNotNull( e );
70          }
71  
72          try
73          {
74              instance.writeEndTag( null );
75              fail( "null tag should fail!" );
76          }
77          catch ( IllegalArgumentException e )
78          {
79              assertNotNull( e );
80          }
81      }
82  
83      /**
84       * Test of writeStartTag method, of class AbstractXmlSink.
85       */
86      public void testWriteStartTag()
87      {
88          final Tag t = Tag.A;
89          final SinkEventAttributes att = new SinkEventAttributeSet( SinkEventAttributeSet.BOLD );
90          final XmlTestSink instance = new XmlTestSink();
91  
92          instance.writeStartTag( t );
93          assertEquals( "<a>", instance.getText() );
94  
95          instance.writeStartTag( t, att );
96          assertEquals( "<a style=\"bold\">", instance.getText() );
97  
98          instance.writeStartTag( t, att, false );
99          assertEquals( "<a style=\"bold\">", instance.getText() );
100 
101         instance.writeStartTag( t, att, true );
102         assertEquals( "<a style=\"bold\" />", instance.getText() );
103     }
104 
105     /**
106      * Test of writeEOL method, of class AbstractXmlSink.
107      */
108     public void testWriteEOL()
109     {
110         final XmlTestSink instance = new XmlTestSink();
111 
112         instance.writeEOL();
113         assertEquals( Markup.EOL, instance.getText() );
114     }
115 
116     /**
117      * Test of writeSimpleTag method, of class AbstractXmlSink.
118      */
119     public void testWriteSimpleTag()
120     {
121         final Tag t = Tag.A;
122         final SinkEventAttributes att = new SinkEventAttributeSet( SinkEventAttributeSet.BOLD );
123         final XmlTestSink instance = new XmlTestSink();
124 
125         instance.writeSimpleTag( t );
126         assertEquals( "<a />", instance.getText() );
127 
128         instance.writeSimpleTag( t, att );
129         assertEquals( "<a style=\"bold\" />", instance.getText() );
130     }
131 
132     /** Test sink. */
133     private class XmlTestSink
134             extends AbstractXmlSink
135     {
136         private final StringBuilder buffer = new StringBuilder( 0 );
137 
138         public void reset()
139         {
140             buffer.setLength( 0 );
141         }
142 
143         public String getText()
144         {
145             String text = buffer.toString();
146             reset();
147 
148             return text;
149         }
150 
151         protected void write( String text )
152         {
153             buffer.append( text );
154         }
155     }
156 }