-
Notifications
You must be signed in to change notification settings - Fork 27
/
JSONObjectTest.java
176 lines (155 loc) · 5.64 KB
/
JSONObjectTest.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
164
165
166
167
168
169
170
171
172
173
174
175
176
package org.codehaus.jettison.json;
import junit.framework.TestCase;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JSONObjectTest extends TestCase {
public void testEquals() throws Exception {
JSONObject aJsonObj = new JSONObject("{\"x\":\"y\"}");
JSONObject bJsonObj = new JSONObject("{\"x\":\"y\"}");
assertEquals(aJsonObj,bJsonObj);
}
public void testToLong() throws Exception {
String json = "{\"key\":\"10001325703114005\"}";
JSONObject jsonObject = new JSONObject(json);
long actual = jsonObject.getLong("key");
long expected = 10001325703114005L;
assertTrue(expected < Long.MAX_VALUE);
assertEquals(expected, actual);
}
public void testNotEquals() throws Exception {
JSONObject aJsonObj = new JSONObject("{\"x\":\"y\"}");
JSONObject bJsonObj = new JSONObject("{\"x\":\"b\"}");
assertTrue(!aJsonObj.equals(bJsonObj));
}
public void testAppend() throws Exception {
JSONObject obj = new JSONObject();
obj.append("arr", "val1");
obj.append("arr", "val2");
obj.append("arr", "val3");
assertEquals("{\"arr\":[\"val1\",\"val2\",\"val3\"]}", obj.toString());
}
public void testInvalidArraySequence() throws Exception {
try {
new JSONObject("{\"a\":[");
fail("Exception expected");
} catch (JSONException ex) {
assertTrue(ex.getMessage().startsWith("JSONArray text must end with ']'"));
}
}
public void testInvalidArraySequence2() throws Exception {
try {
new JSONObject("{\"a\":[,");
fail("Exception expected");
} catch (JSONException ex) {
assertTrue(ex.getMessage().startsWith("JSONArray text has a trailing ','"));
}
}
public void testInvalidArraySequence3() throws Exception {
String corruptedJSON = "{\"a\":[[\"b\",{\"revision\": 760839}],";
try {
new JSONObject(corruptedJSON);
fail("Exception expected");
} catch (JSONException ex) {
assertTrue(ex.getMessage().startsWith("JSONArray text has a trailing ','"));
}
}
public void testNullInQuotesGetString() throws Exception {
JSONObject obj = new JSONObject("{\"a\":\"null\"}");
assertEquals("null", obj.getString("a"));
}
public void testExplicitNullGetString() throws Exception {
JSONObject obj = new JSONObject("{\"a\":null}");
assertNull(obj.getString("a"));
}
public void testExplicitNullIsNull() throws Exception {
JSONObject obj = new JSONObject("{\"a\":null}");
assertTrue(obj.isNull("a"));
}
public void testMissingIsNull() throws Exception {
JSONObject obj = new JSONObject("{\"a\":null}");
assertTrue(obj.isNull("b"));
}
public void testSlashEscapingTurnedOnByDefault() throws Exception {
JSONObject obj = new JSONObject();
obj.put("key", "http://example.com/foo");
assertEquals(obj.toString(), "{\"key\":\"http:\\/\\/example.com\\/foo\"}");
}
public void testForwardSlashEscapingModifiedfBySetter() throws Exception {
JSONObject obj = new JSONObject();
obj.put("key", "http://example.com/foo");
assertEquals(obj.toString(), "{\"key\":\"http:\\/\\/example.com\\/foo\"}");
obj.setEscapeForwardSlashAlways(false);
assertEquals(obj.toString(), "{\"key\":\"http://example.com/foo\"}");
obj.setEscapeForwardSlashAlways(true);
assertEquals(obj.toString(), "{\"key\":\"http:\\/\\/example.com\\/foo\"}");
}
public void testMalformedObject() throws Exception {
try {
new JSONObject("{/");
fail("Failure expected on malformed JSON");
} catch (JSONException ex) {
// expected
}
}
public void testMalformedObject2() throws Exception {
try {
new JSONObject("{x");
fail("Failure expected on malformed JSON");
} catch (JSONException ex) {
// expected
}
}
public void testMalformedObject3() throws Exception {
try {
new JSONObject("{/x");
fail("Failure expected on malformed JSON");
} catch (JSONException ex) {
// expected
}
}
public void testMalformedObject4() throws Exception {
try {
new JSONObject("{/*");
fail("Failure expected on malformed JSON");
} catch (JSONException ex) {
// expected
}
}
public void testMalformedObject5() throws Exception {
try {
new JSONObject("{//");
fail("Failure expected on malformed JSON");
} catch (JSONException ex) {
// expected
}
}
public void testMalformedArray() throws Exception {
try {
new JSONObject("{[/");
fail("Failure expected on malformed JSON");
} catch (JSONException ex) {
// expected
}
}
// https://github.com/jettison-json/jettison/issues/52
public void testIssue52() throws Exception {
Map<String,Object> map = new HashMap<>();
map.put("t",map);
new JSONObject(map);
}
// https://github.com/jettison-json/jettison/issues/52
public void testIssue52Recursive() throws Exception {
try {
Map<String, Object> map = new HashMap<>();
Map<String, Object> map2 = new HashMap<>();
map.put("t", map2);
map2.put("t", map);
new JSONObject(map);
} catch (JSONException e) {
assertTrue(e.getMessage().contains("JSONObject has reached recursion depth limit"));
// expected
}
}
}