1 package org.apache.maven.shared.utils.xml;
2
3 import java.io.IOException;
4 import javax.swing.text.html.HTML;
5 import java.io.StringWriter;
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 import org.apache.maven.shared.utils.Os;
29 import org.apache.maven.shared.utils.StringUtils;
30 import org.junit.After;
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Test;
34
35
36
37
38
39
40
41 public class PrettyPrintXmlWriterTest
42 {
43 StringWriter w;
44
45 PrettyPrintXMLWriter writer;
46
47 @Before
48 public void before()
49 throws Exception
50 {
51 w = new StringWriter();
52 writer = new PrettyPrintXMLWriter( w );
53 }
54
55 @After
56 public void after()
57 throws Exception
58 {
59 writer = null;
60 w = null;
61 }
62
63 @Test
64 public void testDefaultPrettyPrintXMLWriter() throws IOException
65 {
66 writer.startElement( HTML.Tag.HTML.toString() );
67
68 writeXhtmlHead( writer );
69
70 writeXhtmlBody( writer );
71
72 writer.endElement();
73
74 Assert.assertEquals( expectedResult( Os.LINE_SEP ), w.toString() );
75 }
76
77 @Test
78 public void testPrettyPrintXMLWriterWithGivenLineSeparator() throws IOException
79 {
80 writer.setLineSeparator( "\n" );
81
82 writer.startElement( HTML.Tag.HTML.toString() );
83
84 writeXhtmlHead( writer );
85
86 writeXhtmlBody( writer );
87
88 writer.endElement();
89
90 Assert.assertEquals( expectedResult( "\n" ), w.toString() );
91 }
92
93 @Test
94 public void testPrettyPrintXMLWriterWithGivenLineIndenter() throws IOException
95 {
96 writer.setLineIndenter( " " );
97
98 writer.startElement( HTML.Tag.HTML.toString() );
99
100 writeXhtmlHead( writer );
101
102 writeXhtmlBody( writer );
103
104 writer.endElement();
105
106 Assert.assertEquals( expectedResult( " ", Os.LINE_SEP ), w.toString() );
107 }
108
109 @Test
110 public void testEscapeXmlAttributeWindows() throws IOException
111 {
112
113 writer.startElement( HTML.Tag.DIV.toString() );
114 writer.addAttribute( "class", "sect\r\nion" );
115 writer.endElement();
116 Assert.assertEquals( "<div class=\"sect ion\"/>", w.toString() );
117 }
118
119 @Test
120 public void testEscapeXmlAttributeMac() throws IOException
121 {
122
123 writer.startElement( HTML.Tag.DIV.toString() );
124 writer.addAttribute( "class", "sect\rion" );
125 writer.endElement();
126 Assert.assertEquals( "<div class=\"sect ion\"/>", w.toString() );
127 }
128
129 @Test
130 public void testEscapeXmlAttributeTrailingCR() throws IOException
131 {
132
133 writer.startElement( HTML.Tag.DIV.toString() );
134 writer.addAttribute( "class", "section\r" );
135 writer.endElement();
136 Assert.assertEquals( "<div class=\"section \"/>", w.toString() );
137 }
138
139 @Test
140 public void testEscapeXmlAttributeUnix() throws IOException
141 {
142
143 writer.startElement( HTML.Tag.DIV.toString() );
144 writer.addAttribute( "class", "sect\nion" );
145 writer.endElement();
146 Assert.assertEquals( "<div class=\"sect ion\"/>", w.toString() );
147 }
148
149 private void writeXhtmlHead( XMLWriter writer ) throws IOException
150 {
151 writer.startElement( HTML.Tag.HEAD.toString() );
152 writer.startElement( HTML.Tag.TITLE.toString() );
153 writer.writeText( "title" );
154 writer.endElement();
155 writer.startElement( HTML.Tag.META.toString() );
156 writer.addAttribute( "name", "author" );
157 writer.addAttribute( "content", "Author" );
158 writer.endElement();
159 writer.startElement( HTML.Tag.META.toString() );
160 writer.addAttribute( "name", "date" );
161 writer.addAttribute( "content", "Date" );
162 writer.endElement();
163 writer.endElement();
164 }
165
166 private void writeXhtmlBody( XMLWriter writer ) throws IOException
167 {
168 writer.startElement( HTML.Tag.BODY.toString() );
169 writer.startElement( HTML.Tag.P.toString() );
170 writer.writeText( "Paragraph 1, line 1. Paragraph 1, line 2." );
171 writer.endElement();
172 writer.startElement( HTML.Tag.DIV.toString() );
173 writer.addAttribute( "class", "section" );
174 writer.startElement( HTML.Tag.H2.toString() );
175 writer.writeText( "Section title" );
176 writer.endElement();
177 writer.endElement();
178 writer.endElement();
179 }
180
181 private String expectedResult( String lineSeparator )
182 {
183 return expectedResult( " ", lineSeparator );
184 }
185
186 private String expectedResult( String lineIndenter, String lineSeparator )
187 {
188 StringBuilder expected = new StringBuilder();
189
190 expected.append( "<html>" ).append( lineSeparator );
191 expected.append( StringUtils.repeat( lineIndenter, 1 ) ).append( "<head>" ).append( lineSeparator );
192 expected.append( StringUtils.repeat( lineIndenter, 2 ) ).append( "<title>title</title>" )
193 .append( lineSeparator );
194 expected.append( StringUtils.repeat( lineIndenter, 2 ) )
195 .append( "<meta name=\"author\" content=\"Author\"/>" ).append( lineSeparator );
196 expected.append( StringUtils.repeat( lineIndenter, 2 ) ).append( "<meta name=\"date\" content=\"Date\"/>" )
197 .append( lineSeparator );
198 expected.append( StringUtils.repeat( lineIndenter, 1 ) ).append( "</head>" ).append( lineSeparator );
199 expected.append( StringUtils.repeat( lineIndenter, 1 ) ).append( "<body>" ).append( lineSeparator );
200 expected.append( StringUtils.repeat( lineIndenter, 2 ) )
201 .append( "<p>Paragraph 1, line 1. Paragraph 1, line 2.</p>" ).append( lineSeparator );
202 expected.append( StringUtils.repeat( lineIndenter, 2 ) ).append( "<div class=\"section\">" )
203 .append( lineSeparator );
204 expected.append( StringUtils.repeat( lineIndenter, 3 ) ).append( "<h2>Section title</h2>" )
205 .append( lineSeparator );
206 expected.append( StringUtils.repeat( lineIndenter, 2 ) ).append( "</div>" ).append( lineSeparator );
207 expected.append( StringUtils.repeat( lineIndenter, 1 ) ).append( "</body>" ).append( lineSeparator );
208 expected.append( "</html>" );
209
210 return expected.toString();
211 }
212 }