/
TestJsonNode.java
163 lines (136 loc) · 5.27 KB
/
TestJsonNode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package com.fasterxml.jackson.databind.node;
import java.util.Comparator;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.io.SerializedString;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.util.RawValue;
/**
* Basic tests for {@link JsonNode} base class and some features
* of implementation classes
*/
public class TestJsonNode extends NodeTestBase
{
private final ObjectMapper MAPPER = objectMapper();
public void testText()
{
assertNull(TextNode.valueOf(null));
TextNode empty = TextNode.valueOf("");
assertStandardEquals(empty);
assertSame(TextNode.EMPTY_STRING_NODE, empty);
// 1.6:
assertNodeNumbers(TextNode.valueOf("-3"), -3, -3.0);
assertNodeNumbers(TextNode.valueOf("17.75"), 17, 17.75);
// [JACKSON-587]
long value = 127353264013893L;
TextNode n = TextNode.valueOf(String.valueOf(value));
assertEquals(value, n.asLong());
// and then with non-numeric input
n = TextNode.valueOf("foobar");
assertNodeNumbersForNonNumeric(n);
assertEquals("foobar", n.asText("barf"));
assertEquals("", empty.asText("xyz"));
assertTrue(TextNode.valueOf("true").asBoolean(true));
assertTrue(TextNode.valueOf("true").asBoolean(false));
assertFalse(TextNode.valueOf("false").asBoolean(true));
assertFalse(TextNode.valueOf("false").asBoolean(false));
}
public void testBoolean()
{
BooleanNode f = BooleanNode.getFalse();
assertNotNull(f);
assertTrue(f.isBoolean());
assertSame(f, BooleanNode.valueOf(false));
assertStandardEquals(f);
assertFalse(f.booleanValue());
assertFalse(f.asBoolean());
assertEquals("false", f.asText());
assertEquals(JsonToken.VALUE_FALSE, f.asToken());
// and ditto for true
BooleanNode t = BooleanNode.getTrue();
assertNotNull(t);
assertTrue(t.isBoolean());
assertSame(t, BooleanNode.valueOf(true));
assertStandardEquals(t);
assertTrue(t.booleanValue());
assertTrue(t.asBoolean());
assertEquals("true", t.asText());
assertEquals(JsonToken.VALUE_TRUE, t.asToken());
// 1.6:
assertNodeNumbers(f, 0, 0.0);
assertNodeNumbers(t, 1, 1.0);
}
public void testBinary() throws Exception
{
assertNull(BinaryNode.valueOf(null));
assertNull(BinaryNode.valueOf(null, 0, 0));
BinaryNode empty = BinaryNode.valueOf(new byte[1], 0, 0);
assertSame(BinaryNode.EMPTY_BINARY_NODE, empty);
assertStandardEquals(empty);
byte[] data = new byte[3];
data[1] = (byte) 3;
BinaryNode n = BinaryNode.valueOf(data, 1, 1);
data[2] = (byte) 3;
BinaryNode n2 = BinaryNode.valueOf(data, 2, 1);
assertTrue(n.equals(n2));
assertEquals("\"Aw==\"", n.toString());
assertEquals("AAMD", new BinaryNode(data).asText());
// 1.6:
assertNodeNumbersForNonNumeric(n);
}
public void testPOJO()
{
POJONode n = new POJONode("x"); // not really a pojo but that's ok
assertStandardEquals(n);
assertEquals(n, new POJONode("x"));
assertEquals("x", n.asText());
// not sure if this is what it'll remain as but:
assertEquals("x", n.toString());
assertEquals(new POJONode(null), new POJONode(null));
// 1.6:
// default; non-numeric
assertNodeNumbersForNonNumeric(n);
// but if wrapping actual number, use it
assertNodeNumbers(new POJONode(Integer.valueOf(123)), 123, 123.0);
}
// [databind#743]
public void testRawValue() throws Exception
{
ObjectNode root = MAPPER.createObjectNode();
root.putRawValue("a", new RawValue(new SerializedString("[1, 2, 3]")));
assertEquals("{\"a\":[1, 2, 3]}", MAPPER.writeValueAsString(root));
}
// [databind#790]
public void testCustomComparators() throws Exception
{
ObjectNode root1 = MAPPER.createObjectNode();
root1.put("value", 5);
ObjectNode root2 = MAPPER.createObjectNode();
root2.put("value", 5.0);
// default equals(): not strictly equal
assertFalse(root1.equals(root2));
assertFalse(root2.equals(root1));
assertTrue(root1.equals(root1));
assertTrue(root2.equals(root2));
// but. Custom comparator can make all the difference
Comparator<JsonNode> cmp = new Comparator<JsonNode>() {
@Override
public int compare(JsonNode o1, JsonNode o2) {
if (o1.equals(o2)) {
return 0;
}
if ((o1 instanceof NumericNode) && (o2 instanceof NumericNode)) {
double d1 = ((NumericNode) o1).asDouble();
double d2 = ((NumericNode) o2).asDouble();
if (d1 == d2) { // strictly equals because it's integral value
return 1;
}
}
return 0;
}
};
assertTrue(root1.equals(cmp, root2));
assertTrue(root2.equals(cmp, root1));
assertTrue(root1.equals(cmp, root1));
assertTrue(root2.equals(cmp, root2));
}
}