|
1 | 1 | package com.fasterxml.jackson.databind.node;
|
2 | 2 |
|
3 |
| - |
| 3 | +import static com.fasterxml.jackson.databind.BaseMapTest.jsonMapperBuilder; |
| 4 | +import static com.fasterxml.jackson.databind.BaseTest.a2q; |
4 | 5 | import com.fasterxml.jackson.core.JsonParser;
|
5 |
| -import com.fasterxml.jackson.core.JsonProcessingException; |
6 | 6 | import com.fasterxml.jackson.databind.JsonNode;
|
7 | 7 | import com.fasterxml.jackson.databind.ObjectMapper;
|
8 | 8 | import java.util.ArrayList;
|
9 | 9 | import java.util.List;
|
10 |
| -import java.util.stream.Collectors; |
11 | 10 | import org.junit.jupiter.api.Assertions;
|
12 |
| -import org.junit.jupiter.api.BeforeEach; |
13 | 11 | import org.junit.jupiter.api.Test;
|
14 | 12 |
|
15 |
| -public class MissingValues4229Test { |
16 |
| - private static final String jsonString = |
17 |
| - "{\n" |
18 |
| - + " \"target\": \"target1\"," // Found in <= 2.15.3 and 2.16.0\n |
19 |
| - + " \"object1\": {\n" |
20 |
| - + " \"target\": \"target2\"" // Found in <= 2.15.3, but not in 2.16.0\n |
21 |
| - + " },\n" |
22 |
| - + " \"object2\": {\n" |
23 |
| - + " \"target\": {" // Found in <= 2.15.3, but not in 2.16.0 |
24 |
| - + " \"target\": \"ignoredAsParentIsTarget\"" |
25 |
| - // Expect not to be found (as sub-tree search ends when parent is found)\n |
26 |
| - + " }\n" |
27 |
| - + " }\n" |
28 |
| - + " }"; |
29 |
| - |
30 |
| - private JsonNode rootNode; |
31 |
| - |
32 |
| - @BeforeEach |
33 |
| - public void init() throws JsonProcessingException { |
34 |
| - ObjectMapper objectMapper = |
35 |
| - new ObjectMapper().configure(JsonParser.Feature.ALLOW_COMMENTS, true); |
36 |
| - rootNode = objectMapper.readTree(jsonString); |
37 |
| - } |
| 13 | +// [databind#4229] JsonNode findValues and findParents missing expected values |
| 14 | +public class MissingValues4229Test |
| 15 | +{ |
| 16 | + |
| 17 | + private final String JSON = a2q("{" |
| 18 | + + " 'target': 'target1'," // Found in <= 2.15.3 and 2.16.0 |
| 19 | + + " 'object1': {" |
| 20 | + + " 'target': 'target2' " // Found in <= 2.15.3, but not in 2.16.0 |
| 21 | + + " }," |
| 22 | + + " 'object2': {" |
| 23 | + + " 'target': { " // Found in <= 2.15.3, but not in 2.16.0 |
| 24 | + + " 'target': 'ignoredAsParentIsTarget'" // Expect not to be found (as sub-tree search ends when parent is found) |
| 25 | + + " }" |
| 26 | + + " }" |
| 27 | + + "}"); |
| 28 | + |
| 29 | + private final ObjectMapper objectMapper = jsonMapperBuilder() |
| 30 | + .configure(JsonParser.Feature.ALLOW_COMMENTS, true) |
| 31 | + .build(); |
38 | 32 |
|
39 | 33 | @Test
|
40 |
| - public void testFindValues() { |
41 |
| - List<JsonNode> foundNodes = rootNode.findValues("target"); |
| 34 | + public void testFindValues() throws Exception |
| 35 | + { |
| 36 | + JsonNode rootNode = objectMapper.readTree(JSON); |
42 | 37 |
|
43 |
| - List<String> expectedNodePaths = new ArrayList<>(); |
44 |
| - expectedNodePaths.add("/target"); |
45 |
| - expectedNodePaths.add("/object1/target"); |
46 |
| - expectedNodePaths.add("/object2/target"); |
| 38 | + List<JsonNode> expectedNodes = new ArrayList<>(); |
| 39 | + expectedNodes.add(rootNode.at("/target")); |
| 40 | + expectedNodes.add(rootNode.at("/object1/target")); |
| 41 | + expectedNodes.add(rootNode.at("/object2/target")); |
47 | 42 |
|
48 |
| - List<JsonNode> expectedNodes = expectedNodePaths.stream().map(rootNode::at).collect(Collectors.toList()); |
| 43 | + List<JsonNode> actualNodes = rootNode.findValues("target"); |
49 | 44 |
|
50 |
| - Assertions.assertEquals(expectedNodes, foundNodes); |
| 45 | + Assertions.assertEquals(expectedNodes, actualNodes); |
51 | 46 | }
|
52 | 47 |
|
53 | 48 | @Test
|
54 |
| - public void testFindParents() { |
55 |
| - List<JsonNode> foundNodes = rootNode.findParents("target"); |
| 49 | + public void testFindParents() throws Exception |
| 50 | + { |
| 51 | + JsonNode rootNode = objectMapper.readTree(JSON); |
56 | 52 |
|
57 | 53 | List<JsonNode> expectedNodes = new ArrayList<>();
|
58 | 54 | expectedNodes.add(rootNode.at(""));
|
59 | 55 | expectedNodes.add(rootNode.at("/object1"));
|
60 | 56 | expectedNodes.add(rootNode.at("/object2"));
|
61 | 57 |
|
| 58 | + List<JsonNode> foundNodes = rootNode.findParents("target"); |
| 59 | + |
62 | 60 | Assertions.assertEquals(expectedNodes, foundNodes);
|
63 | 61 | }
|
64 | 62 | }
|
0 commit comments