1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
32
33 public class AbstractXmlSinkTest {
34
35
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
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
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
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
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 }