View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.doxia.sink.impl;
20  
21  import javax.swing.text.html.HTML.Tag;
22  
23  import org.apache.maven.doxia.markup.Markup;
24  import org.apache.maven.doxia.sink.SinkEventAttributes;
25  import org.junit.jupiter.api.Test;
26  
27  import static org.junit.jupiter.api.Assertions.*;
28  
29  /**
30   *
31   * @author ltheussl
32   */
33  public class AbstractXmlSinkTest {
34      /**
35       * Test of set/getNameSpace method, of class AbstractXmlSink.
36       */
37      @Test
38      public void testNameSpace() {
39          final Tag t = Tag.A;
40          final String ns = "ns";
41          final XmlTestSink instance = new XmlTestSink();
42  
43          instance.writeStartTag(t);
44          instance.writeEndTag(t);
45          assertEquals("<a></a>", instance.getText());
46  
47          instance.writeSimpleTag(t);
48          assertEquals("<a />", instance.getText());
49  
50          instance.setNameSpace(ns);
51  
52          instance.writeStartTag(t);
53          instance.writeEndTag(t);
54          assertEquals("<ns:a></ns:a>", instance.getText());
55  
56          instance.writeSimpleTag(t);
57          assertEquals("<ns:a />", instance.getText());
58  
59          assertEquals(ns, instance.getNameSpace());
60  
61          try {
62              instance.writeStartTag(null);
63              fail("null tag should fail!");
64          } catch (NullPointerException e) {
65              assertNotNull(e);
66          }
67  
68          try {
69              instance.writeEndTag(null);
70              fail("null tag should fail!");
71          } catch (NullPointerException e) {
72              assertNotNull(e);
73          }
74      }
75  
76      /**
77       * Test of writeStartTag method, of class AbstractXmlSink.
78       */
79      @Test
80      public void testWriteStartTag() {
81          final Tag t = Tag.A;
82          final SinkEventAttributes att = new SinkEventAttributeSet(SinkEventAttributeSet.BOLD);
83          final XmlTestSink instance = new XmlTestSink();
84  
85          instance.writeStartTag(t);
86          assertEquals("<a>", instance.getText());
87  
88          instance.writeStartTag(t, att);
89          assertEquals("<a style=\"bold\">", instance.getText());
90  
91          instance.writeStartTag(t, att, false);
92          assertEquals("<a style=\"bold\">", instance.getText());
93  
94          instance.writeStartTag(t, att, true);
95          assertEquals("<a style=\"bold\" />", instance.getText());
96      }
97  
98      /**
99       * Test of writeEOL method, of class AbstractXmlSink.
100      */
101     @Test
102     public void testWriteEOL() {
103         final XmlTestSink instance = new XmlTestSink();
104 
105         instance.writeEOL();
106         assertEquals(Markup.EOL, instance.getText());
107     }
108 
109     /**
110      * Test of writeSimpleTag method, of class AbstractXmlSink.
111      */
112     @Test
113     public void testWriteSimpleTag() {
114         final Tag t = Tag.A;
115         final SinkEventAttributes att = new SinkEventAttributeSet(SinkEventAttributeSet.BOLD);
116         final XmlTestSink instance = new XmlTestSink();
117 
118         instance.writeSimpleTag(t);
119         assertEquals("<a />", instance.getText());
120 
121         instance.writeSimpleTag(t, att);
122         assertEquals("<a style=\"bold\" />", instance.getText());
123     }
124 
125     /** Test sink. */
126     private static class XmlTestSink extends AbstractXmlSink {
127         private final StringBuilder buffer = new StringBuilder(0);
128 
129         public void reset() {
130             buffer.setLength(0);
131         }
132 
133         public String getText() {
134             String text = buffer.toString();
135             reset();
136 
137             return text;
138         }
139 
140         protected void write(String text) {
141             buffer.append(text);
142         }
143     }
144 }