1 package com.fasterxml.jackson.databind.misc;
2 
3 import java.io.IOException;
4 
5 import com.fasterxml.jackson.core.JsonParser;
6 import com.fasterxml.jackson.core.JsonPointer;
7 import com.fasterxml.jackson.core.JsonToken;
8 
9 import com.fasterxml.jackson.databind.*;
10 import com.fasterxml.jackson.databind.util.TokenBuffer;
11 
12 public class ParsingContext2525Test extends BaseMapTest
13 {
14     private final ObjectMapper MAPPER = sharedMapper();
15 
16     private final String MINIMAL_ARRAY_DOC = "[ 42 ]";
17 
18     private final String MINIMAL_OBJECT_DOC = "{\"answer\" : 42 }";
19 
20     private final String FULL_DOC = aposToQuotes("{'a':123,'array':[1,2,[3],5,{'obInArray':4}],"
21             +"'ob':{'first':[false,true],'second':{'sub':37}},'b':true}");
22 
23     /*
24     /**********************************************************************
25     /* Baseline sanity check first
26     /**********************************************************************
27      */
28 
testAllWithRegularParser()29     public void testAllWithRegularParser() throws Exception
30     {
31         try (JsonParser p = MAPPER.createParser(MINIMAL_ARRAY_DOC)) {
32             _testSimpleArrayUsingPathAsPointer(p);
33         }
34         try (JsonParser p = MAPPER.createParser(MINIMAL_OBJECT_DOC)) {
35             _testSimpleObjectUsingPathAsPointer(p);
36         }
37         try (JsonParser p = MAPPER.createParser(FULL_DOC)) {
38             _testFullDocUsingPathAsPointer(p);
39         }
40     }
41 
42     /*
43     /**********************************************************************
44     /* Then TokenBuffer-backed tests
45     /**********************************************************************
46      */
47 
testSimpleArrayWithBuffer()48     public void testSimpleArrayWithBuffer() throws Exception
49     {
50         try (TokenBuffer buf = _readAsTokenBuffer(MINIMAL_ARRAY_DOC)) {
51             _testSimpleArrayUsingPathAsPointer(buf.asParser());
52         }
53     }
54 
testSimpleObjectWithBuffer()55     public void testSimpleObjectWithBuffer() throws Exception
56     {
57         try (TokenBuffer buf = _readAsTokenBuffer(MINIMAL_OBJECT_DOC)) {
58             _testSimpleObjectUsingPathAsPointer(buf.asParser());
59         }
60     }
61 
testFullDocWithBuffer()62     public void testFullDocWithBuffer() throws Exception
63     {
64         try (TokenBuffer buf = _readAsTokenBuffer(FULL_DOC)) {
65             _testFullDocUsingPathAsPointer(buf.asParser());
66         }
67     }
68 
_readAsTokenBuffer(String doc)69     private TokenBuffer _readAsTokenBuffer(String doc) throws IOException
70     {
71         try (JsonParser p = MAPPER.createParser(doc)) {
72             p.nextToken();
73             return TokenBuffer.asCopyOfValue(p)
74                     .overrideParentContext(null);
75         }
76     }
77 
78     /*
79     /**********************************************************************
80     /* And Tree-backed tests
81     /**********************************************************************
82      */
83 
testSimpleArrayWithTree()84     public void testSimpleArrayWithTree() throws Exception
85     {
86         JsonNode root = MAPPER.readTree(MINIMAL_ARRAY_DOC);
87         try (JsonParser p = root.traverse(null)) {
88             _testSimpleArrayUsingPathAsPointer(p);
89         }
90     }
91 
testSimpleObjectWithTree()92     public void testSimpleObjectWithTree() throws Exception
93     {
94         JsonNode root = MAPPER.readTree(MINIMAL_OBJECT_DOC);
95         try (JsonParser p = root.traverse(null)) {
96             _testSimpleObjectUsingPathAsPointer(p);
97         }
98     }
99 
testFullDocWithTree()100     public void testFullDocWithTree() throws Exception
101     {
102         JsonNode root = MAPPER.readTree(FULL_DOC);
103         try (JsonParser p = root.traverse(null)) {
104             _testFullDocUsingPathAsPointer(p);
105         }
106     }
107 
108     /*
109     /**********************************************************************
110     /* Shared helper methods
111     /**********************************************************************
112      */
113 
_testSimpleArrayUsingPathAsPointer(JsonParser p)114     private void _testSimpleArrayUsingPathAsPointer(JsonParser p) throws Exception
115     {
116         assertSame(JsonPointer.empty(), p.getParsingContext().pathAsPointer());
117         assertTrue(p.getParsingContext().inRoot());
118 
119         assertToken(JsonToken.START_ARRAY, p.nextToken());
120         assertSame(JsonPointer.empty(), p.getParsingContext().pathAsPointer());
121         assertTrue(p.getParsingContext().inArray());
122 
123         assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
124         assertEquals("/0", p.getParsingContext().pathAsPointer().toString());
125 
126         assertToken(JsonToken.END_ARRAY, p.nextToken());
127         assertSame(JsonPointer.empty(), p.getParsingContext().pathAsPointer());
128         assertTrue(p.getParsingContext().inRoot());
129 
130         assertNull(p.nextToken());
131     }
132 
_testSimpleObjectUsingPathAsPointer(JsonParser p)133     private void _testSimpleObjectUsingPathAsPointer(JsonParser p) throws Exception
134     {
135         assertSame(JsonPointer.empty(), p.getParsingContext().pathAsPointer());
136         assertTrue(p.getParsingContext().inRoot());
137 
138         assertToken(JsonToken.START_OBJECT, p.nextToken());
139         assertSame(JsonPointer.empty(), p.getParsingContext().pathAsPointer());
140         assertTrue(p.getParsingContext().inObject());
141 
142         assertToken(JsonToken.FIELD_NAME, p.nextToken());
143         assertEquals("/answer", p.getParsingContext().pathAsPointer().toString());
144         assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
145         assertEquals(42, p.getIntValue());
146         assertEquals("/answer", p.getParsingContext().pathAsPointer().toString());
147 
148         assertToken(JsonToken.END_OBJECT, p.nextToken());
149         assertSame(JsonPointer.empty(), p.getParsingContext().pathAsPointer());
150         assertTrue(p.getParsingContext().inRoot());
151 
152         assertNull(p.nextToken());
153     }
154 
_testFullDocUsingPathAsPointer(JsonParser p)155     private void _testFullDocUsingPathAsPointer(JsonParser p) throws Exception
156     {
157         // by default should just get "empty"
158         assertSame(JsonPointer.empty(), p.getParsingContext().pathAsPointer());
159         assertTrue(p.getParsingContext().inRoot());
160 
161         // let's just traverse, then:
162         assertToken(JsonToken.START_OBJECT, p.nextToken());
163         assertSame(JsonPointer.empty(), p.getParsingContext().pathAsPointer());
164         assertTrue(p.getParsingContext().inObject());
165 
166         assertToken(JsonToken.FIELD_NAME, p.nextToken()); // a
167         assertEquals("/a", p.getParsingContext().pathAsPointer().toString());
168 
169         assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
170         assertEquals("/a", p.getParsingContext().pathAsPointer().toString());
171 
172         assertToken(JsonToken.FIELD_NAME, p.nextToken()); // array
173         assertEquals("/array", p.getParsingContext().pathAsPointer().toString());
174         assertToken(JsonToken.START_ARRAY, p.nextToken());
175         assertEquals("/array", p.getParsingContext().pathAsPointer().toString());
176         assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); // 1
177         assertEquals("/array/0", p.getParsingContext().pathAsPointer().toString());
178         assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); // 2
179         assertEquals("/array/1", p.getParsingContext().pathAsPointer().toString());
180         assertToken(JsonToken.START_ARRAY, p.nextToken());
181         assertEquals("/array/2", p.getParsingContext().pathAsPointer().toString());
182         assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); // 3
183         assertEquals("/array/2/0", p.getParsingContext().pathAsPointer().toString());
184         assertToken(JsonToken.END_ARRAY, p.nextToken());
185         assertEquals("/array/2", p.getParsingContext().pathAsPointer().toString());
186         assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); // 5
187         assertEquals("/array/3", p.getParsingContext().pathAsPointer().toString());
188         assertToken(JsonToken.START_OBJECT, p.nextToken());
189         assertEquals("/array/4", p.getParsingContext().pathAsPointer().toString());
190         assertToken(JsonToken.FIELD_NAME, p.nextToken()); // obInArray
191         assertEquals("/array/4/obInArray", p.getParsingContext().pathAsPointer().toString());
192         assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); // 4
193         assertEquals("/array/4/obInArray", p.getParsingContext().pathAsPointer().toString());
194         assertToken(JsonToken.END_OBJECT, p.nextToken());
195         assertEquals("/array/4", p.getParsingContext().pathAsPointer().toString());
196         assertToken(JsonToken.END_ARRAY, p.nextToken()); // /array
197         assertEquals("/array", p.getParsingContext().pathAsPointer().toString());
198 
199         assertToken(JsonToken.FIELD_NAME, p.nextToken()); // ob
200         assertEquals("/ob", p.getParsingContext().pathAsPointer().toString());
201         assertToken(JsonToken.START_OBJECT, p.nextToken());
202         assertEquals("/ob", p.getParsingContext().pathAsPointer().toString());
203         assertToken(JsonToken.FIELD_NAME, p.nextToken()); // first
204         assertEquals("/ob/first", p.getParsingContext().pathAsPointer().toString());
205         assertToken(JsonToken.START_ARRAY, p.nextToken());
206         assertEquals("/ob/first", p.getParsingContext().pathAsPointer().toString());
207         assertToken(JsonToken.VALUE_FALSE, p.nextToken());
208         assertEquals("/ob/first/0", p.getParsingContext().pathAsPointer().toString());
209         assertToken(JsonToken.VALUE_TRUE, p.nextToken());
210         assertEquals("/ob/first/1", p.getParsingContext().pathAsPointer().toString());
211         assertToken(JsonToken.END_ARRAY, p.nextToken());
212         assertEquals("/ob/first", p.getParsingContext().pathAsPointer().toString());
213         assertToken(JsonToken.FIELD_NAME, p.nextToken()); // second
214         assertEquals("/ob/second", p.getParsingContext().pathAsPointer().toString());
215         assertToken(JsonToken.START_OBJECT, p.nextToken());
216         assertEquals("/ob/second", p.getParsingContext().pathAsPointer().toString());
217         assertToken(JsonToken.FIELD_NAME, p.nextToken()); // sub
218         assertEquals("/ob/second/sub", p.getParsingContext().pathAsPointer().toString());
219         assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken()); // 37
220         assertEquals("/ob/second/sub", p.getParsingContext().pathAsPointer().toString());
221         assertToken(JsonToken.END_OBJECT, p.nextToken());
222         assertEquals("/ob/second", p.getParsingContext().pathAsPointer().toString());
223         assertToken(JsonToken.END_OBJECT, p.nextToken()); // /ob
224         assertEquals("/ob", p.getParsingContext().pathAsPointer().toString());
225 
226         assertToken(JsonToken.FIELD_NAME, p.nextToken()); // b
227         assertEquals("/b", p.getParsingContext().pathAsPointer().toString());
228         assertToken(JsonToken.VALUE_TRUE, p.nextToken());
229         assertEquals("/b", p.getParsingContext().pathAsPointer().toString());
230 
231         assertToken(JsonToken.END_OBJECT, p.nextToken());
232         assertSame(JsonPointer.empty(), p.getParsingContext().pathAsPointer());
233         assertTrue(p.getParsingContext().inRoot());
234 
235         assertNull(p.nextToken());
236     }
237 }
238