/
JSONArrayTest.java
66 lines (56 loc) · 1.96 KB
/
JSONArrayTest.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
package org.codehaus.jettison.json;
import junit.framework.TestCase;
public class JSONArrayTest extends TestCase {
public void testInvalidArraySequence() throws Exception {
try {
new JSONArray("[32,");
fail("Exception expected");
} catch (JSONException ex) {
assertTrue(ex.getMessage().startsWith("JSONArray text has a trailing ','"));
}
}
public void testInvalidArraySequence2() throws Exception {
try {
new JSONArray("[32,34");
fail("Exception expected");
} catch (JSONException ex) {
assertTrue(ex.getMessage().startsWith("Expected a ',' or ']'"));
}
}
public void testEscapingInArrayIsOnByDefault() {
JSONArray array = new JSONArray();
array.put("a string with / character");
String expectedValue = "[\"a string with \\/ character\"]";
assertEquals(expectedValue, array.toString());
}
public void testEscapingInArrayIsTrunedOff() throws JSONException {
JSONObject obj = new JSONObject();
obj.put("key", "http://example.com/foo");
obj.setEscapeForwardSlashAlways(false);
JSONArray array = new JSONArray();
array.put("a string with / character");
array.put(obj);
array.setEscapeForwardSlashAlways(false);
System.out.println(array.toString());
String expectedValue = "[\"a string with / character\",{\"key\":\"http://example.com/foo\"}]";
assertEquals(expectedValue, array.toString());
}
public void testInfiniteLoop() {
String str = "[*/*A25] **";
try {
new JSONArray(str);
fail("Failure expected on malformed JSON");
} catch (JSONException ex) {
// expected
}
}
public void testInfiniteLoop2() {
String str = "[/";
try {
new JSONArray(str);
fail("Failure expected on malformed JSON");
} catch (JSONException ex) {
// expected
}
}
}