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