|
16 | 16 | * limitations under the License.
|
17 | 17 | */
|
18 | 18 |
|
| 19 | +import java.io.File; |
| 20 | +import java.io.FileOutputStream; |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.OutputStreamWriter; |
19 | 23 | import java.io.StringWriter;
|
| 24 | +import java.util.NoSuchElementException; |
20 | 25 |
|
21 | 26 | import javax.swing.text.html.HTML.Tag;
|
22 | 27 |
|
|
28 | 33 | * Test of {@link PrettyPrintXMLWriter}
|
29 | 34 | *
|
30 | 35 | * @author <a href="mailto:[email protected]">Vincent Siveton</a>
|
| 36 | + * @author <a href="mailto:[email protected]">Gabriel Belingueres</a> |
31 | 37 | * @version $Id$
|
32 | 38 | */
|
33 | 39 | public class PrettyPrintXMLWriterTest
|
@@ -128,6 +134,75 @@ public void testEscapeXmlAttribute()
|
128 | 134 | assertEquals( "<div class=\"sect ion\"/>", w.toString() );
|
129 | 135 | }
|
130 | 136 |
|
| 137 | + public void testendElementAlreadyClosed() |
| 138 | + { |
| 139 | + try |
| 140 | + { |
| 141 | + writer.startElement( Tag.DIV.toString() ); |
| 142 | + writer.addAttribute( "class", "someattribute" ); |
| 143 | + writer.endElement(); // Tag.DIV closed |
| 144 | + writer.endElement(); // Tag.DIV already closed, and there is no other outer tag! |
| 145 | + fail( "Should throw a NoSuchElementException" ); |
| 146 | + } |
| 147 | + catch ( NoSuchElementException e ) |
| 148 | + { |
| 149 | + assert ( true ); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Issue #51: https://github.com/codehaus-plexus/plexus-utils/issues/51 |
| 155 | + * |
| 156 | + * Purpose: test if concatenation string optimization bug is present. |
| 157 | + * |
| 158 | + * Target environment: Java 7 (u79 and u80 verified) running on Windows. |
| 159 | + * |
| 160 | + * Detection strategy: Tries to build a big XML file (~750MB size) and with |
| 161 | + * many nested tags to force the JVM to trigger the concatenation string |
| 162 | + * optimization bug that throws a NoSuchElementException when calling |
| 163 | + * endElement() method. |
| 164 | + * |
| 165 | + * @throws IOException if an I/O error occurs |
| 166 | + */ |
| 167 | + public void testIssue51DetectJava7ConcatenationBug() |
| 168 | + throws IOException |
| 169 | + { |
| 170 | + File dir = new File( "target/test-xml" ); |
| 171 | + if ( !dir.exists() ) |
| 172 | + { |
| 173 | + assertTrue( "cannot create directory test-xml", dir.mkdir() ); |
| 174 | + } |
| 175 | + File xmlFile = new File( dir, "test-issue-51.xml" ); |
| 176 | + OutputStreamWriter osw = new OutputStreamWriter( new FileOutputStream( xmlFile ), "UTF-8" ); |
| 177 | + writer = new PrettyPrintXMLWriter( osw ); |
| 178 | + |
| 179 | + int iterations = 20000; |
| 180 | + |
| 181 | + try |
| 182 | + { |
| 183 | + for ( int i = 0; i < iterations; ++i ) |
| 184 | + { |
| 185 | + writer.startElement( Tag.DIV.toString() + i ); |
| 186 | + writer.addAttribute( "class", "someattribute" ); |
| 187 | + } |
| 188 | + for ( int i = 0; i < iterations; ++i ) |
| 189 | + { |
| 190 | + writer.endElement(); // closes Tag.DIV + i |
| 191 | + } |
| 192 | + } |
| 193 | + catch ( NoSuchElementException e ) |
| 194 | + { |
| 195 | + fail( "Should not throw a NoSuchElementException" ); |
| 196 | + } |
| 197 | + finally |
| 198 | + { |
| 199 | + if ( osw != null ) |
| 200 | + { |
| 201 | + osw.close(); |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + |
131 | 206 | private void writeXhtmlHead( XMLWriter writer )
|
132 | 207 | {
|
133 | 208 | writer.startElement( Tag.HEAD.toString() );
|
|
0 commit comments