44
55import com .fasterxml .jackson .databind .BaseMapTest ;
66import com .fasterxml .jackson .databind .JsonNode ;
7+ import com .fasterxml .jackson .databind .ObjectMapper ;
78
89public class TestFindMethods
910 extends BaseMapTest
1011{
12+ private final String JSON_SAMPLE = "{ \" a\" : { \" value\" : 3 },"
13+ +"\" array\" : [ { \" b\" : 3 }, {\" value\" : 42}, { \" other\" : true } ]"
14+ +"}" ;
15+
16+ private final String JSON_4229 = a2q ("{"
17+ + " 'target': 'target1'," // Found in <= 2.15.3 and 2.16.0
18+ + " 'object1': {"
19+ + " 'target': 'target2' " // Found in <= 2.15.3, but not in 2.16.0
20+ + " },"
21+ + " 'object2': {"
22+ + " 'target': { " // Found in <= 2.15.3, but not in 2.16.0
23+ + " 'target': 'ignoredAsParentIsTarget'" // Expect not to be found (as sub-tree search ends when parent is found)
24+ + " }"
25+ + " }"
26+ + "}" );
27+
28+ private final ObjectMapper MAPPER = newJsonMapper ();
29+
1130 public void testNonMatching () throws Exception
1231 {
13- JsonNode root = _buildTree ( );
32+ JsonNode root = MAPPER . readTree ( JSON_SAMPLE );
1433
1534 assertNull (root .findValue ("boogaboo" ));
1635 assertNull (root .findParent ("boogaboo" ));
@@ -24,7 +43,7 @@ public void testNonMatching() throws Exception
2443
2544 public void testMatchingSingle () throws Exception
2645 {
27- JsonNode root = _buildTree ( );
46+ JsonNode root = MAPPER . readTree ( JSON_SAMPLE );
2847
2948 JsonNode node = root .findValue ("b" );
3049 assertNotNull (node );
@@ -38,7 +57,7 @@ public void testMatchingSingle() throws Exception
3857
3958 public void testMatchingMultiple () throws Exception
4059 {
41- JsonNode root = _buildTree ( );
60+ JsonNode root = MAPPER . readTree ( JSON_SAMPLE );
4261
4362 List <JsonNode > nodes = root .findValues ("value" );
4463 assertEquals (2 , nodes .size ());
@@ -61,11 +80,25 @@ public void testMatchingMultiple() throws Exception
6180 assertEquals ("42" , values .get (1 ));
6281 }
6382
64- private JsonNode _buildTree () throws Exception
83+ // [databind#4229]: regression in 2.16.0
84+ public void testFindValues4229 () throws Exception
6585 {
66- final String SAMPLE = "{ \" a\" : { \" value\" : 3 },"
67- +"\" array\" : [ { \" b\" : 3 }, {\" value\" : 42}, { \" other\" : true } ]"
68- +"}" ;
69- return objectMapper ().readTree (SAMPLE );
86+ JsonNode rootNode = MAPPER .readTree (JSON_4229 );
87+ assertEquals (Arrays .asList (
88+ rootNode .at ("/target" ),
89+ rootNode .at ("/object1/target" ),
90+ rootNode .at ("/object2/target" )),
91+ rootNode .findValues ("target" ));
92+ }
93+
94+ // [databind#4229]: regression in 2.16.0
95+ public void testFindParents4229 () throws Exception
96+ {
97+ JsonNode rootNode = MAPPER .readTree (JSON_4229 );
98+ assertEquals (Arrays .asList (
99+ rootNode ,
100+ rootNode .at ("/object1" ),
101+ rootNode .at ("/object2" )),
102+ rootNode .findParents ("target" ));
70103 }
71104}
0 commit comments