-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
36cbb80
commit 86f8560
Showing
3 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
232 changes: 232 additions & 0 deletions
232
src/test/java/com/fasterxml/jackson/failing/ParsingContext2525Test.java
This file contains 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,232 @@ | ||
package com.fasterxml.jackson.failing; | ||
|
||
import java.io.IOException; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonPointer; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
|
||
import com.fasterxml.jackson.databind.*; | ||
import com.fasterxml.jackson.databind.util.TokenBuffer; | ||
|
||
public class ParsingContext2525Test extends BaseMapTest | ||
{ | ||
private final ObjectMapper MAPPER = sharedMapper(); | ||
|
||
private final String MINIMAL_ARRAY_DOC = "[ 42 ]"; | ||
|
||
private final String MINIMAL_OBJECT_DOC = "{\"answer\" : 42 }"; | ||
|
||
private final String FULL_DOC = aposToQuotes("{'a':123,'array':[1,2,[3],5,{'obInArray':4}]," | ||
+"'ob':{'first':[false,true],'second':{'sub':37}},'b':true}"); | ||
|
||
/* | ||
/********************************************************************** | ||
/* Baseline sanity check first | ||
/********************************************************************** | ||
*/ | ||
|
||
public void testAllWithRegularParser() throws Exception | ||
{ | ||
try (JsonParser p = MAPPER.getFactory().createParser(MINIMAL_ARRAY_DOC)) { | ||
_testSimpleArrayUsingPathAsPointer(p); | ||
} | ||
try (JsonParser p = MAPPER.getFactory().createParser(MINIMAL_OBJECT_DOC)) { | ||
_testSimpleObjectUsingPathAsPointer(p); | ||
} | ||
try (JsonParser p = MAPPER.getFactory().createParser(FULL_DOC)) { | ||
_testFullDocUsingPathAsPointer(p); | ||
} | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Then TokenBuffer-backed tests | ||
/********************************************************************** | ||
*/ | ||
|
||
// 25-Oct-2019, tatu: These appear more problematic than I thought, wrt | ||
// parent contexts... | ||
|
||
public void testSimpleArrayWithBuffer() throws Exception | ||
{ | ||
try (TokenBuffer buf = _readAsTokenBuffer(MINIMAL_ARRAY_DOC)) { | ||
_testSimpleArrayUsingPathAsPointer(buf.asParser()); | ||
} | ||
} | ||
|
||
public void testSimpleObjectWithBuffer() throws Exception | ||
{ | ||
try (TokenBuffer buf = _readAsTokenBuffer(MINIMAL_OBJECT_DOC)) { | ||
_testSimpleObjectUsingPathAsPointer(buf.asParser()); | ||
} | ||
} | ||
|
||
public void testFullDocWithBuffer() throws Exception | ||
{ | ||
try (TokenBuffer buf = _readAsTokenBuffer(FULL_DOC)) { | ||
_testFullDocUsingPathAsPointer(buf.asParser()); | ||
} | ||
} | ||
|
||
private TokenBuffer _readAsTokenBuffer(String doc) throws IOException | ||
{ | ||
try (JsonParser p = MAPPER.getFactory().createParser(doc)) { | ||
p.nextToken(); | ||
return TokenBuffer.asCopyOfValue(p) | ||
.overrideParentContext(null); | ||
} | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* And Tree-backed tests | ||
/********************************************************************** | ||
*/ | ||
|
||
public void testSimpleArrayWithTree() throws Exception | ||
{ | ||
JsonNode root = MAPPER.readTree(MINIMAL_ARRAY_DOC); | ||
try (JsonParser p = root.traverse(null)) { | ||
_testSimpleArrayUsingPathAsPointer(p); | ||
} | ||
} | ||
|
||
public void testSimpleObjectWithTree() throws Exception | ||
{ | ||
JsonNode root = MAPPER.readTree(MINIMAL_OBJECT_DOC); | ||
try (JsonParser p = root.traverse(null)) { | ||
_testSimpleObjectUsingPathAsPointer(p); | ||
} | ||
} | ||
|
||
public void testFullDocWithTree() throws Exception | ||
{ | ||
JsonNode root = MAPPER.readTree(FULL_DOC); | ||
try (JsonParser p = root.traverse(null)) { | ||
_testFullDocUsingPathAsPointer(p); | ||
} | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Shared helper methods | ||
/********************************************************************** | ||
*/ | ||
|
||
private void _testSimpleArrayUsingPathAsPointer(JsonParser p) throws Exception | ||
{ | ||
assertSame(JsonPointer.empty(), p.getParsingContext().pathAsPointer()); | ||
|
||
assertToken(JsonToken.START_ARRAY, p.nextToken()); | ||
assertSame(JsonPointer.empty(), p.getParsingContext().pathAsPointer()); | ||
|
||
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); | ||
assertEquals("/0", p.getParsingContext().pathAsPointer().toString()); | ||
|
||
assertToken(JsonToken.END_ARRAY, p.nextToken()); | ||
assertSame(JsonPointer.empty(), p.getParsingContext().pathAsPointer()); | ||
|
||
assertNull(p.nextToken()); | ||
} | ||
|
||
private void _testSimpleObjectUsingPathAsPointer(JsonParser p) throws Exception | ||
{ | ||
assertSame(JsonPointer.empty(), p.getParsingContext().pathAsPointer()); | ||
|
||
assertToken(JsonToken.START_OBJECT, p.nextToken()); | ||
assertSame(JsonPointer.empty(), p.getParsingContext().pathAsPointer()); | ||
|
||
assertToken(JsonToken.FIELD_NAME, p.nextToken()); | ||
assertEquals("/answer", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); | ||
assertEquals(42, p.getIntValue()); | ||
assertEquals("/answer", p.getParsingContext().pathAsPointer().toString()); | ||
|
||
assertToken(JsonToken.END_OBJECT, p.nextToken()); | ||
assertSame(JsonPointer.empty(), p.getParsingContext().pathAsPointer()); | ||
|
||
assertNull(p.nextToken()); | ||
} | ||
|
||
private void _testFullDocUsingPathAsPointer(JsonParser p) throws Exception | ||
{ | ||
// by default should just get "empty" | ||
assertSame(JsonPointer.empty(), p.getParsingContext().pathAsPointer()); | ||
|
||
// let's just traverse, then: | ||
assertToken(JsonToken.START_OBJECT, p.nextToken()); | ||
assertSame(JsonPointer.empty(), p.getParsingContext().pathAsPointer()); | ||
|
||
assertToken(JsonToken.FIELD_NAME, p.nextToken()); // a | ||
assertEquals("/a", p.getParsingContext().pathAsPointer().toString()); | ||
|
||
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); | ||
assertEquals("/a", p.getParsingContext().pathAsPointer().toString()); | ||
|
||
assertToken(JsonToken.FIELD_NAME, p.nextToken()); // array | ||
assertEquals("/array", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.START_ARRAY, p.nextToken()); | ||
assertEquals("/array", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); // 1 | ||
assertEquals("/array/0", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); // 2 | ||
assertEquals("/array/1", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.START_ARRAY, p.nextToken()); | ||
assertEquals("/array/2", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); // 3 | ||
assertEquals("/array/2/0", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.END_ARRAY, p.nextToken()); | ||
assertEquals("/array/2", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); // 5 | ||
assertEquals("/array/3", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.START_OBJECT, p.nextToken()); | ||
assertEquals("/array/4", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.FIELD_NAME, p.nextToken()); // obInArray | ||
assertEquals("/array/4/obInArray", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); // 4 | ||
assertEquals("/array/4/obInArray", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.END_OBJECT, p.nextToken()); | ||
assertEquals("/array/4", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.END_ARRAY, p.nextToken()); // /array | ||
assertEquals("/array", p.getParsingContext().pathAsPointer().toString()); | ||
|
||
assertToken(JsonToken.FIELD_NAME, p.nextToken()); // ob | ||
assertEquals("/ob", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.START_OBJECT, p.nextToken()); | ||
assertEquals("/ob", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.FIELD_NAME, p.nextToken()); // first | ||
assertEquals("/ob/first", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.START_ARRAY, p.nextToken()); | ||
assertEquals("/ob/first", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.VALUE_FALSE, p.nextToken()); | ||
assertEquals("/ob/first/0", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.VALUE_TRUE, p.nextToken()); | ||
assertEquals("/ob/first/1", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.END_ARRAY, p.nextToken()); | ||
assertEquals("/ob/first", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.FIELD_NAME, p.nextToken()); // second | ||
assertEquals("/ob/second", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.START_OBJECT, p.nextToken()); | ||
assertEquals("/ob/second", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.FIELD_NAME, p.nextToken()); // sub | ||
assertEquals("/ob/second/sub", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); // 37 | ||
assertEquals("/ob/second/sub", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.END_OBJECT, p.nextToken()); | ||
assertEquals("/ob/second", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.END_OBJECT, p.nextToken()); // /ob | ||
assertEquals("/ob", p.getParsingContext().pathAsPointer().toString()); | ||
|
||
assertToken(JsonToken.FIELD_NAME, p.nextToken()); // b | ||
assertEquals("/b", p.getParsingContext().pathAsPointer().toString()); | ||
assertToken(JsonToken.VALUE_TRUE, p.nextToken()); | ||
assertEquals("/b", p.getParsingContext().pathAsPointer().toString()); | ||
|
||
assertToken(JsonToken.END_OBJECT, p.nextToken()); | ||
assertSame(JsonPointer.empty(), p.getParsingContext().pathAsPointer()); | ||
|
||
assertNull(p.nextToken()); | ||
} | ||
|
||
} |
This file contains 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