1 package com.fasterxml.jackson.databind.node;
2 
3 import java.io.StringReader;
4 import java.util.Iterator;
5 
6 import com.fasterxml.jackson.core.JsonToken;
7 import com.fasterxml.jackson.databind.JsonNode;
8 
9 public class TestMissingNode extends NodeTestBase
10 {
testMissing()11     public void testMissing()
12     {
13         MissingNode n = MissingNode.getInstance();
14         assertTrue(n.isMissingNode());
15         assertEquals(JsonToken.NOT_AVAILABLE, n.asToken());
16         assertEquals("", n.asText());
17         assertStandardEquals(n);
18         // 10-Dec-2018, tatu: With 2.10, should serialize same as via ObjectMapper/ObjectWriter
19         // 10-Dec-2019, tatu: Surprise! No, this is not how it worked in 2.9, nor does it make
20         //    sense... see [databind#2566] for details
21         assertEquals("", n.toString());
22 
23         assertNodeNumbersForNonNumeric(n);
24 
25         assertTrue(n.asBoolean(true));
26         assertEquals(4, n.asInt(4));
27         assertEquals(5L, n.asLong(5));
28         assertEquals(0.25, n.asDouble(0.25));
29 
30         assertEquals("foo", n.asText("foo"));
31     }
32 
33     /**
34      * Let's also verify behavior of "MissingNode" -- one needs to be able
35      * to traverse such bogus nodes with appropriate methods.
36      */
37     @SuppressWarnings("unused")
testMissingViaMapper()38     public void testMissingViaMapper() throws Exception
39     {
40         String JSON = "[ { }, [ ] ]";
41         JsonNode result = objectMapper().readTree(new StringReader(JSON));
42 
43         assertTrue(result.isContainerNode());
44         assertTrue(result.isArray());
45         assertEquals(2, result.size());
46 
47         int count = 0;
48         for (JsonNode node : result) {
49             ++count;
50         }
51         assertEquals(2, count);
52 
53         Iterator<JsonNode> it = result.iterator();
54 
55         JsonNode onode = it.next();
56         assertTrue(onode.isContainerNode());
57         assertTrue(onode.isObject());
58         assertEquals(0, onode.size());
59         assertFalse(onode.isMissingNode()); // real node
60         assertNull(onode.textValue());
61 
62         // how about dereferencing?
63         assertNull(onode.get(0));
64         JsonNode dummyNode = onode.path(0);
65         assertNotNull(dummyNode);
66         assertTrue(dummyNode.isMissingNode());
67         assertNull(dummyNode.get(3));
68         assertNull(dummyNode.get("whatever"));
69         JsonNode dummyNode2 = dummyNode.path(98);
70         assertNotNull(dummyNode2);
71         assertTrue(dummyNode2.isMissingNode());
72         JsonNode dummyNode3 = dummyNode.path("field");
73         assertNotNull(dummyNode3);
74         assertTrue(dummyNode3.isMissingNode());
75 
76         // and same for the array node
77 
78         JsonNode anode = it.next();
79         assertTrue(anode.isContainerNode());
80         assertTrue(anode.isArray());
81         assertFalse(anode.isMissingNode()); // real node
82         assertEquals(0, anode.size());
83 
84         assertNull(anode.get(0));
85         dummyNode = anode.path(0);
86         assertNotNull(dummyNode);
87         assertTrue(dummyNode.isMissingNode());
88         assertNull(dummyNode.get(0));
89         assertNull(dummyNode.get("myfield"));
90         dummyNode2 = dummyNode.path(98);
91         assertNotNull(dummyNode2);
92         assertTrue(dummyNode2.isMissingNode());
93         dummyNode3 = dummyNode.path("f");
94         assertNotNull(dummyNode3);
95         assertTrue(dummyNode3.isMissingNode());
96     }
97 }
98