Skip to content

Commit 56dda3c

Browse files
committed
[TEST] Global JSONParserConfiguration Testcases
1 parent 21f7a94 commit 56dda3c

1 file changed

Lines changed: 139 additions & 4 deletions

File tree

src/test/java/org/json/junit/JSONParserConfigurationTest.java

Lines changed: 139 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,156 @@
88
import org.junit.Test;
99

1010
import java.io.IOException;
11+
import java.io.StringReader;
12+
import java.lang.reflect.Field;
1113
import java.nio.file.Files;
1214
import java.nio.file.Paths;
1315
import java.util.Arrays;
16+
import java.util.Collections;
1417
import java.util.List;
18+
import java.util.Map;
1519
import java.util.stream.Collectors;
1620
import java.util.stream.Stream;
1721

18-
import static org.junit.Assert.assertEquals;
19-
import static org.junit.Assert.assertThrows;
20-
import static org.junit.Assert.assertTrue;
21-
import static org.junit.Assert.fail;
22+
import org.junit.After;
23+
import org.junit.Before;
24+
25+
import static org.junit.Assert.*;
2226

2327
public class JSONParserConfigurationTest {
2428
private static final String TEST_SOURCE = "{\"key\": \"value1\", \"key\": \"value2\"}";
2529

30+
@Before
31+
public void resetGlobalConfigurationBeforeTest() {
32+
resetGlobalConfiguration();
33+
}
34+
35+
@After
36+
public void resetGlobalConfigurationAfterTest() {
37+
resetGlobalConfiguration();
38+
}
39+
40+
@Test
41+
public void globalConfigurationShouldRejectNull() {
42+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
43+
() -> JSONParserConfiguration.setGlobalConfiguration(null));
44+
45+
assertEquals("Global JSONParserConfiguration cannot be null.", exception.getMessage());
46+
}
47+
48+
@Test
49+
public void globalConfigurationShouldOnlyBeSetOnce() {
50+
JSONParserConfiguration.setGlobalConfiguration(new JSONParserConfiguration());
51+
52+
IllegalStateException exception = assertThrows(IllegalStateException.class,
53+
() -> JSONParserConfiguration.setGlobalConfiguration(new JSONParserConfiguration().withStrictMode(true)));
54+
55+
assertEquals("Global JSONParserConfiguration has already been set. It cannot be modified.",
56+
exception.getMessage());
57+
}
58+
59+
@Test
60+
public void globalOverwriteDuplicateKeyShouldAffectDefaultJSONObjectStringConstructor() {
61+
JSONParserConfiguration.setGlobalConfiguration(new JSONParserConfiguration().withOverwriteDuplicateKey(true));
62+
63+
JSONObject jsonObject = new JSONObject(TEST_SOURCE);
64+
65+
assertEquals("value2", jsonObject.getString("key"));
66+
}
67+
68+
@Test
69+
public void globalStrictModeShouldAffectDefaultJSONTokenerReaderConstructor() {
70+
JSONParserConfiguration.setGlobalConfiguration(new JSONParserConfiguration().withStrictMode(true));
71+
72+
JSONException exception = assertThrows(JSONException.class,
73+
() -> new JSONObject(new JSONTokener(new StringReader("{\"key\":\"value\"} invalid trailing text"))));
74+
75+
assertEquals("Strict mode error: Unparsed characters found at end of input text at 17 [character 18 line 1]",
76+
exception.getMessage());
77+
}
78+
79+
@Test
80+
public void globalUseNativeNullsShouldAffectDefaultJSONObjectMapConstructor() {
81+
JSONParserConfiguration.setGlobalConfiguration(new JSONParserConfiguration().withUseNativeNulls(true));
82+
Map<String, Object> nullMap = Collections.singletonMap("nullKey", null);
83+
84+
JSONObject jsonObject = new JSONObject(nullMap);
85+
86+
assertTrue(jsonObject.has("nullKey"));
87+
assertEquals(JSONObject.NULL, jsonObject.get("nullKey"));
88+
}
89+
90+
@Test
91+
public void globalUseNativeNullsShouldAffectDefaultJSONArrayArrayWrapping() {
92+
JSONParserConfiguration.setGlobalConfiguration(new JSONParserConfiguration().withUseNativeNulls(true));
93+
Map<String, Object> nullMap = Collections.singletonMap("nullKey", null);
94+
95+
JSONArray jsonArray = new JSONArray(new Object[] { nullMap });
96+
JSONObject nestedObject = jsonArray.getJSONObject(0);
97+
98+
assertTrue(nestedObject.has("nullKey"));
99+
assertEquals(JSONObject.NULL, nestedObject.get("nullKey"));
100+
}
101+
102+
@Test
103+
public void whenNoGlobalConfigurationSet_shouldUseDefaultConfiguration() {
104+
// No global configuration set, should use defaults
105+
// Default: overwriteDuplicateKey=false, so duplicate keys throw exception
106+
JSONException exception = assertThrows(JSONException.class,
107+
() -> new JSONObject(TEST_SOURCE));
108+
109+
assertTrue(exception.getMessage().contains("Duplicate key"));
110+
}
111+
112+
@Test
113+
public void globalStrictModeShouldAffectDefaultJSONArrayStringConstructor() {
114+
JSONParserConfiguration.setGlobalConfiguration(new JSONParserConfiguration().withStrictMode(true));
115+
116+
JSONException exception = assertThrows(JSONException.class,
117+
() -> new JSONArray("[\"value\"] invalid trailing text"));
118+
119+
assertEquals("Strict mode error: Unparsed characters found at end of input text at 11 [character 12 line 1]",
120+
exception.getMessage());
121+
}
122+
123+
@Test
124+
public void globalUseNativeNullsShouldAffectDefaultJSONArrayCollectionConstructor() {
125+
JSONParserConfiguration.setGlobalConfiguration(new JSONParserConfiguration().withUseNativeNulls(true));
126+
Map<String, Object> nullMap = Collections.singletonMap("nullKey", null);
127+
List<Map<String, Object>> collection = Collections.singletonList(nullMap);
128+
129+
JSONArray jsonArray = new JSONArray(collection);
130+
JSONObject nestedObject = jsonArray.getJSONObject(0);
131+
132+
assertTrue(nestedObject.has("nullKey"));
133+
assertEquals(JSONObject.NULL, nestedObject.get("nullKey"));
134+
}
135+
136+
@Test
137+
public void globalMaxNestingDepthShouldAffectDefaultJSONObjectMapConstructor() {
138+
JSONParserConfiguration.setGlobalConfiguration(new JSONParserConfiguration().withMaxNestingDepth(1));
139+
// Create a nested map that exceeds max depth 1 (depth 0 -> depth 1 -> depth 2 would exceed)
140+
Map<String, Object> innerMostMap = Collections.singletonMap("innermost", "value");
141+
Map<String, Object> innerMap = Collections.singletonMap("inner", innerMostMap);
142+
Map<String, Object> outerMap = Collections.singletonMap("outer", innerMap);
143+
144+
JSONException exception = assertThrows(JSONException.class,
145+
() -> new JSONObject(outerMap));
146+
147+
assertTrue(exception.getMessage().contains("depth"));
148+
}
149+
150+
151+
private void resetGlobalConfiguration() {
152+
try {
153+
Field field = JSONParserConfiguration.class.getDeclaredField("globalConfig");
154+
field.setAccessible(true);
155+
field.set(null, null);
156+
} catch (ReflectiveOperationException e) {
157+
throw new AssertionError("Unable to reset global JSONParserConfiguration for test isolation.", e);
158+
}
159+
}
160+
26161
@Test(expected = JSONException.class)
27162
public void testThrowException() {
28163
new JSONObject(TEST_SOURCE);

0 commit comments

Comments
 (0)