-
Notifications
You must be signed in to change notification settings - Fork 28
/
JSONObjectTest.java
151 lines (132 loc) · 4.83 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
package org.codehaus.jettison.json;
import junit.framework.TestCase;
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
}
}
}