-
Notifications
You must be signed in to change notification settings - Fork 42
#51: PrettyPrintXMLWriter fails with a java.util.NoSuchElementException (Java 1.7 only) #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
1e26c07
#51: PrettyPrintXMLWriter fails with a java.util.NoSuchElementException
belingueres 87498cc
Changed fix to a series of write() calls, as suggested by Robert
belingueres d430a57
Correction of comment explaining the nature of the proposed change.
belingueres File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,12 @@ | |
* 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; | ||
|
||
|
@@ -28,6 +33,7 @@ | |
* Test of {@link PrettyPrintXMLWriter} | ||
* | ||
* @author <a href="mailto:[email protected]">Vincent Siveton</a> | ||
* @author <a href="mailto:[email protected]">Gabriel Belingueres</a> | ||
* @version $Id$ | ||
*/ | ||
public class PrettyPrintXMLWriterTest | ||
|
@@ -128,6 +134,75 @@ public void testEscapeXmlAttribute() | |
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() ); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice unittest! This also confirms that the following works too:
I prefer this, since it removes the variable creation and assignment explicitly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment is now out of sync, i.e.
:)