Repository navigation
Fix #60: Stax2ReaderAdapter.nextTag() has to be overridden for correct depth and partial chunked reads #61
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
Open
winfriedgerlach
wants to merge
2
commits into
FasterXML:main
Choose a base branch
from
winfriedgerlach:#60-fix-Stax2ReaderAdapter.nextTag
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
109 changes: 109 additions & 0 deletions
109
src/test/java/org/codehaus/stax2/ri/Stax2ReaderAdapterTest.java
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 |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package org.codehaus.stax2.ri; | ||
|
|
||
| import java.io.StringReader; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import javax.xml.stream.XMLInputFactory; | ||
| import javax.xml.stream.XMLStreamConstants; | ||
| import javax.xml.stream.XMLStreamException; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import org.codehaus.stax2.XMLStreamReader2; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| /** | ||
| * Tests for state {@link Stax2ReaderAdapter} keeps on top of the wrapped reader, which has to be updated by every | ||
| * method that advances it. | ||
| *<p> | ||
| * Covers {@code nextTag()}, and leaving a chunked typed array read early, where the wrapped reader is already at | ||
| * END_ELEMENT and the depth must not be decremented a second time. | ||
| */ | ||
| class Stax2ReaderAdapterTest | ||
| { | ||
| // White space, comment and PI between tags, for nextTag() to skip | ||
| private static final String XML = "<root> <a><!-- c --><b/></a>\n<?pi?><c></c> </root>"; | ||
|
|
||
| // "<name>" for START_ELEMENT, "</name>" for END_ELEMENT, each with getDepth() | ||
| private static final List<String> EXPECTED = Arrays.asList( | ||
| "<root> 1", "<a> 2", "<b> 3", "</b> 3", "</a> 2", "<c> 2", "</c> 2", "</root> 1"); | ||
|
|
||
| // Element "a" is only partially read as an int array before moving on | ||
| private static final String XML_ABANDONED = "<root><a>1 2 3 4 5</a> <b/></root>"; | ||
|
|
||
| private static final List<String> EXPECTED_ABANDONED = Arrays.asList( | ||
| "</a> 2", "<b> 2", "</b> 2", "</root> 1"); | ||
|
|
||
| @Test | ||
| void depthWithNext() throws Exception | ||
| { | ||
| assertEquals(EXPECTED, walk(false)); | ||
| } | ||
|
|
||
| @Test | ||
| void depthWithNextTag() throws Exception | ||
| { | ||
| assertEquals(EXPECTED, walk(true)); | ||
| } | ||
|
|
||
| @Test | ||
| void depthAfterAbandonedChunkedReadWithNext() throws Exception | ||
| { | ||
| assertEquals(EXPECTED_ABANDONED, walkAbandoned(false)); | ||
| } | ||
|
|
||
| @Test | ||
| void depthAfterAbandonedChunkedReadWithNextTag() throws Exception | ||
| { | ||
| assertEquals(EXPECTED_ABANDONED, walkAbandoned(true)); | ||
| } | ||
|
|
||
| /* | ||
| /////////////////////////////////////////// | ||
| // Helper methods | ||
| /////////////////////////////////////////// | ||
| */ | ||
|
|
||
| private static List<String> walk(boolean useNextTag) throws XMLStreamException | ||
| { | ||
| // not wrapIfNecessary(): must be the adapter even if a Stax2 impl is on classpath | ||
| XMLStreamReader2 sr = new Stax2ReaderAdapter(XMLInputFactory.newInstance() | ||
| .createXMLStreamReader(new StringReader(XML))); | ||
| List<String> result = new ArrayList<>(); | ||
| for (int i = 0; i < EXPECTED.size(); ) { | ||
| int type = useNextTag ? sr.nextTag() : sr.next(); | ||
| if (type == XMLStreamConstants.START_ELEMENT) { | ||
| result.add("<"+sr.getLocalName()+"> "+sr.getDepth()); | ||
| ++i; | ||
| } else if (type == XMLStreamConstants.END_ELEMENT) { | ||
| result.add("</"+sr.getLocalName()+"> "+sr.getDepth()); | ||
| ++i; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private static List<String> walkAbandoned(boolean useNextTag) throws XMLStreamException | ||
| { | ||
| XMLStreamReader2 sr = new Stax2ReaderAdapter(XMLInputFactory.newInstance() | ||
| .createXMLStreamReader(new StringReader(XML_ABANDONED))); | ||
| sr.nextTag(); | ||
| sr.nextTag(); | ||
| assertEquals(2, sr.readElementAsIntArray(new int[2], 0, 2)); | ||
| List<String> result = new ArrayList<>(); | ||
| for (int i = 0; i < EXPECTED_ABANDONED.size(); ) { | ||
| int type = useNextTag ? sr.nextTag() : sr.next(); | ||
| if (type == XMLStreamConstants.START_ELEMENT) { | ||
| result.add("<"+sr.getLocalName()+"> "+sr.getDepth()); | ||
| ++i; | ||
| } else if (type == XMLStreamConstants.END_ELEMENT) { | ||
| result.add("</"+sr.getLocalName()+"> "+sr.getDepth()); | ||
| ++i; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
very similar to
next()method right above now