forked from codehaus-plexus/plexus-utils
/
PrettyPrintXMLWriterTest.java
269 lines (227 loc) · 8.86 KB
/
PrettyPrintXMLWriterTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package org.codehaus.plexus.util.xml;
/*
* Copyright The Codehaus Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.NoSuchElementException;
import javax.swing.text.html.HTML.Tag;
import org.codehaus.plexus.util.StringUtils;
import junit.framework.TestCase;
/**
* Test of {@link PrettyPrintXMLWriter}
*
* @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
* @author <a href="mailto:belingueres@gmail.com">Gabriel Belingueres</a>
* @version $Id$
*/
public class PrettyPrintXMLWriterTest
extends TestCase
{
StringWriter w;
PrettyPrintXMLWriter writer;
/** {@inheritDoc} */
protected void setUp()
throws Exception
{
super.setUp();
initWriter();
}
/** {@inheritDoc} */
protected void tearDown()
throws Exception
{
super.tearDown();
writer = null;
w = null;
}
private void initWriter()
{
w = new StringWriter();
writer = new PrettyPrintXMLWriter( w );
}
public void testDefaultPrettyPrintXMLWriter()
{
writer.startElement( Tag.HTML.toString() );
writeXhtmlHead( writer );
writeXhtmlBody( writer );
writer.endElement(); // Tag.HTML
assertEquals( expectedResult( PrettyPrintXMLWriter.LS ), w.toString() );
}
public void testPrettyPrintXMLWriterWithGivenLineSeparator()
{
writer.setLineSeparator( "\n" );
writer.startElement( Tag.HTML.toString() );
writeXhtmlHead( writer );
writeXhtmlBody( writer );
writer.endElement(); // Tag.HTML
assertEquals( expectedResult( "\n" ), w.toString() );
}
public void testPrettyPrintXMLWriterWithGivenLineIndenter()
{
writer.setLineIndenter( " " );
writer.startElement( Tag.HTML.toString() );
writeXhtmlHead( writer );
writeXhtmlBody( writer );
writer.endElement(); // Tag.HTML
assertEquals( expectedResult( " ", PrettyPrintXMLWriter.LS ), w.toString() );
}
public void testEscapeXmlAttribute()
{
// Windows
writer.startElement( Tag.DIV.toString() );
writer.addAttribute( "class", "sect\r\nion" );
writer.endElement(); // Tag.DIV
assertEquals( "<div class=\"sect ion\"/>", w.toString() );
// Mac
initWriter();
writer.startElement( Tag.DIV.toString() );
writer.addAttribute( "class", "sect\rion" );
writer.endElement(); // Tag.DIV
assertEquals( "<div class=\"sect ion\"/>", w.toString() );
// Unix
initWriter();
writer.startElement( Tag.DIV.toString() );
writer.addAttribute( "class", "sect\nion" );
writer.endElement(); // Tag.DIV
assertEquals( "<div class=\"sect ion\"/>", w.toString() );
}
public void testendElementAlreadyClosed()
{
try
{
writer.startElement( Tag.DIV.toString() );
writer.addAttribute( "class", "someattribute" );
writer.endElement(); // Tag.DIV closed
writer.endElement(); // Tag.DIV already closed, and there is no other outer tag!
fail( "Should throw a NoSuchElementException" );
}
catch ( NoSuchElementException e )
{
assert ( true );
}
}
/**
* Issue #51: https://github.com/codehaus-plexus/plexus-utils/issues/51
*
* Purpose: test if concatenation string optimization bug is present.
*
* Target environment: Java 7 (u79 and u80 verified) running on Windows.
*
* Detection strategy: Tries to build a big XML file (~750MB size) and with
* many nested tags to force the JVM to trigger the concatenation string
* optimization bug that throws a NoSuchElementException when calling
* endElement() method.
*
* @throws IOException if an I/O error occurs
*/
public void testIssue51DetectJava7ConcatenationBug()
throws IOException
{
File dir = new File( "target/test-xml" );
if ( !dir.exists() )
{
assertTrue( "cannot create directory test-xml", dir.mkdir() );
}
File xmlFile = new File( dir, "test-issue-51.xml" );
OutputStreamWriter osw = new OutputStreamWriter( new FileOutputStream( xmlFile ), "UTF-8" );
writer = new PrettyPrintXMLWriter( osw );
int iterations = 20000;
try
{
for ( int i = 0; i < iterations; ++i )
{
writer.startElement( Tag.DIV.toString() + i );
writer.addAttribute( "class", "someattribute" );
}
for ( int i = 0; i < iterations; ++i )
{
writer.endElement(); // closes Tag.DIV + i
}
}
catch ( NoSuchElementException e )
{
fail( "Should not throw a NoSuchElementException" );
}
finally
{
if ( osw != null )
{
osw.close();
}
}
}
private void writeXhtmlHead( XMLWriter writer )
{
writer.startElement( Tag.HEAD.toString() );
writer.startElement( Tag.TITLE.toString() );
writer.writeText( "title" );
writer.endElement(); // Tag.TITLE
writer.startElement( Tag.META.toString() );
writer.addAttribute( "name", "author" );
writer.addAttribute( "content", "Author" );
writer.endElement(); // Tag.META
writer.startElement( Tag.META.toString() );
writer.addAttribute( "name", "date" );
writer.addAttribute( "content", "Date" );
writer.endElement(); // Tag.META
writer.endElement(); // Tag.HEAD
}
private void writeXhtmlBody( XMLWriter writer )
{
writer.startElement( Tag.BODY.toString() );
writer.startElement( Tag.P.toString() );
writer.writeText( "Paragraph 1, line 1. Paragraph 1, line 2." );
writer.endElement(); // Tag.P
writer.startElement( Tag.DIV.toString() );
writer.addAttribute( "class", "section" );
writer.startElement( Tag.H2.toString() );
writer.writeText( "Section title" );
writer.endElement(); // Tag.H2
writer.endElement(); // Tag.DIV
writer.endElement(); // Tag.BODY
}
private String expectedResult( String lineSeparator )
{
return expectedResult( " ", lineSeparator );
}
private String expectedResult( String lineIndenter, String lineSeparator )
{
StringBuilder expected = new StringBuilder();
expected.append( "<html>" ).append( lineSeparator );
expected.append( StringUtils.repeat( lineIndenter, 1 ) ).append( "<head>" ).append( lineSeparator );
expected.append( StringUtils.repeat( lineIndenter,
2 ) ).append( "<title>title</title>" ).append( lineSeparator );
expected.append( StringUtils.repeat( lineIndenter,
2 ) ).append( "<meta name=\"author\" content=\"Author\"/>" ).append( lineSeparator );
expected.append( StringUtils.repeat( lineIndenter,
2 ) ).append( "<meta name=\"date\" content=\"Date\"/>" ).append( lineSeparator );
expected.append( StringUtils.repeat( lineIndenter, 1 ) ).append( "</head>" ).append( lineSeparator );
expected.append( StringUtils.repeat( lineIndenter, 1 ) ).append( "<body>" ).append( lineSeparator );
expected.append( StringUtils.repeat( lineIndenter,
2 ) ).append( "<p>Paragraph 1, line 1. Paragraph 1, line 2.</p>" ).append( lineSeparator );
expected.append( StringUtils.repeat( lineIndenter,
2 ) ).append( "<div class=\"section\">" ).append( lineSeparator );
expected.append( StringUtils.repeat( lineIndenter,
3 ) ).append( "<h2>Section title</h2>" ).append( lineSeparator );
expected.append( StringUtils.repeat( lineIndenter, 2 ) ).append( "</div>" ).append( lineSeparator );
expected.append( StringUtils.repeat( lineIndenter, 1 ) ).append( "</body>" ).append( lineSeparator );
expected.append( "</html>" );
return expected.toString();
}
}