Repository navigation
Expand file tree
/
Copy pathIssue1056CyclicSerializationTest.java
More file actions
59 lines (51 loc) · 1.9 KB
/
Copy pathIssue1056CyclicSerializationTest.java
File metadata and controls
59 lines (51 loc) · 1.9 KB
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
package org.json;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThrows;
import org.junit.Test;
/**
* Tests cyclic-reference detection during serialization. Lives in {@code org.json}
* so tests can assert {@link JSONObject.CyclicReferenceException} by type.
*/
public class Issue1056CyclicSerializationTest {
@Test
public void selfReferentialJSONObject() {
JSONObject jo = new JSONObject();
jo.put("key", "value");
jo.put("self", jo);
JSONException ex = assertThrows(JSONException.class, jo::toString);
assertTrue(ex instanceof JSONObject.CyclicReferenceException);
}
@Test
public void mutualJSONObjectCycle() {
JSONObject a = new JSONObject();
JSONObject b = new JSONObject();
a.put("b", b);
b.put("a", a);
JSONException ex = assertThrows(JSONException.class, a::toString);
assertTrue(ex instanceof JSONObject.CyclicReferenceException);
}
@Test
public void mixedJSONObjectJSONArrayCycle() {
JSONObject jo = new JSONObject();
JSONArray arr = new JSONArray();
jo.put("arr", arr);
arr.put(jo);
JSONException ex = assertThrows(JSONException.class, jo::toString);
assertTrue(ex instanceof JSONObject.CyclicReferenceException);
}
@Test
public void selfReferentialJSONArray() {
JSONArray arr = new JSONArray();
arr.put("x");
arr.put(arr);
JSONException ex = assertThrows(JSONException.class, arr::toString);
assertTrue(ex instanceof JSONObject.CyclicReferenceException);
}
@Test
public void valueToStringOnCyclicJSONObject() {
JSONObject jo = new JSONObject();
jo.put("self", jo);
JSONException ex = assertThrows(JSONException.class, () -> JSONObject.valueToString(jo));
assertTrue(ex instanceof JSONObject.CyclicReferenceException);
}
}