Skip to content

Commit d0e2cf4

Browse files
authored
Merge pull request #78 from migueltt/tokener-error-message
Unit tests for constructor JSONObject(JSONTokener)
2 parents 44c3e32 + b90bee0 commit d0e2cf4

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed

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

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,6 +1976,175 @@ public void jsonObjectParsingErrors() {
19761976
} catch (JSONException e) {
19771977
assertTrue("", true);
19781978
}
1979+
try {
1980+
// test exception message when including a duplicate key (level 0)
1981+
String str = "{\n"
1982+
+" \"attr01\":\"value-01\",\n"
1983+
+" \"attr02\":\"value-02\",\n"
1984+
+" \"attr03\":\"value-03\",\n"
1985+
+" \"attr03\":\"value-04\"\n"
1986+
+ "}";
1987+
new JSONObject(str);
1988+
fail("Expected an exception");
1989+
} catch (JSONException e) {
1990+
assertEquals("Expecting an expection message",
1991+
"Duplicate key \"attr03\" at 90 [character 13 line 5]",
1992+
e.getMessage());
1993+
}
1994+
try {
1995+
// test exception message when including a duplicate key (level 0) holding an object
1996+
String str = "{\n"
1997+
+" \"attr01\":\"value-01\",\n"
1998+
+" \"attr02\":\"value-02\",\n"
1999+
+" \"attr03\":\"value-03\",\n"
2000+
+" \"attr03\": {"
2001+
+" \"attr04-01\":\"value-04-01\",n"
2002+
+" \"attr04-02\":\"value-04-02\",n"
2003+
+" \"attr04-03\":\"value-04-03\"n"
2004+
+ " }\n"
2005+
+ "}";
2006+
new JSONObject(str);
2007+
fail("Expected an exception");
2008+
} catch (JSONException e) {
2009+
assertEquals("Expecting an expection message",
2010+
"Duplicate key \"attr03\" at 90 [character 13 line 5]",
2011+
e.getMessage());
2012+
}
2013+
try {
2014+
// test exception message when including a duplicate key (level 0) holding an array
2015+
String str = "{\n"
2016+
+" \"attr01\":\"value-01\",\n"
2017+
+" \"attr02\":\"value-02\",\n"
2018+
+" \"attr03\":\"value-03\",\n"
2019+
+" \"attr03\": [\n"
2020+
+" {"
2021+
+" \"attr04-01\":\"value-04-01\",n"
2022+
+" \"attr04-02\":\"value-04-02\",n"
2023+
+" \"attr04-03\":\"value-04-03\"n"
2024+
+" }\n"
2025+
+ " ]\n"
2026+
+ "}";
2027+
new JSONObject(str);
2028+
fail("Expected an exception");
2029+
} catch (JSONException e) {
2030+
assertEquals("Expecting an expection message",
2031+
"Duplicate key \"attr03\" at 90 [character 13 line 5]",
2032+
e.getMessage());
2033+
}
2034+
try {
2035+
// test exception message when including a duplicate key (level 1)
2036+
String str = "{\n"
2037+
+" \"attr01\":\"value-01\",\n"
2038+
+" \"attr02\":\"value-02\",\n"
2039+
+" \"attr03\":\"value-03\",\n"
2040+
+" \"attr04\": {\n"
2041+
+" \"attr04-01\":\"value04-01\",\n"
2042+
+" \"attr04-02\":\"value04-02\",\n"
2043+
+" \"attr04-03\":\"value04-03\",\n"
2044+
+" \"attr04-03\":\"value04-04\"\n"
2045+
+ " }\n"
2046+
+ "}";
2047+
new JSONObject(str);
2048+
fail("Expected an exception");
2049+
} catch (JSONException e) {
2050+
assertEquals("Expecting an expection message",
2051+
"Duplicate key \"attr04-03\" at 215 [character 20 line 9]",
2052+
e.getMessage());
2053+
}
2054+
try {
2055+
// test exception message when including a duplicate key (level 1) holding an object
2056+
String str = "{\n"
2057+
+" \"attr01\":\"value-01\",\n"
2058+
+" \"attr02\":\"value-02\",\n"
2059+
+" \"attr03\":\"value-03\",\n"
2060+
+" \"attr04\": {\n"
2061+
+" \"attr04-01\":\"value04-01\",\n"
2062+
+" \"attr04-02\":\"value04-02\",\n"
2063+
+" \"attr04-03\":\"value04-03\",\n"
2064+
+" \"attr04-03\": {\n"
2065+
+" \"attr04-04-01\":\"value04-04-01\",\n"
2066+
+" \"attr04-04-02\":\"value04-04-02\",\n"
2067+
+" \"attr04-04-03\":\"value04-04-03\",\n"
2068+
+" }\n"
2069+
+" }\n"
2070+
+ "}";
2071+
new JSONObject(str);
2072+
fail("Expected an exception");
2073+
} catch (JSONException e) {
2074+
assertEquals("Expecting an expection message",
2075+
"Duplicate key \"attr04-03\" at 215 [character 20 line 9]",
2076+
e.getMessage());
2077+
}
2078+
try {
2079+
// test exception message when including a duplicate key (level 1) holding an array
2080+
String str = "{\n"
2081+
+" \"attr01\":\"value-01\",\n"
2082+
+" \"attr02\":\"value-02\",\n"
2083+
+" \"attr03\":\"value-03\",\n"
2084+
+" \"attr04\": {\n"
2085+
+" \"attr04-01\":\"value04-01\",\n"
2086+
+" \"attr04-02\":\"value04-02\",\n"
2087+
+" \"attr04-03\":\"value04-03\",\n"
2088+
+" \"attr04-03\": [\n"
2089+
+" {\n"
2090+
+" \"attr04-04-01\":\"value04-04-01\",\n"
2091+
+" \"attr04-04-02\":\"value04-04-02\",\n"
2092+
+" \"attr04-04-03\":\"value04-04-03\",\n"
2093+
+" }\n"
2094+
+" ]\n"
2095+
+" }\n"
2096+
+ "}";
2097+
new JSONObject(str);
2098+
fail("Expected an exception");
2099+
} catch (JSONException e) {
2100+
assertEquals("Expecting an expection message",
2101+
"Duplicate key \"attr04-03\" at 215 [character 20 line 9]",
2102+
e.getMessage());
2103+
}
2104+
try {
2105+
// test exception message when including a duplicate key in object (level 0) within an array
2106+
String str = "[\n"
2107+
+" {\n"
2108+
+" \"attr01\":\"value-01\",\n"
2109+
+" \"attr02\":\"value-02\"\n"
2110+
+" },\n"
2111+
+" {\n"
2112+
+" \"attr01\":\"value-01\",\n"
2113+
+" \"attr01\":\"value-02\"\n"
2114+
+" }\n"
2115+
+ "]";
2116+
new JSONArray(str);
2117+
fail("Expected an exception");
2118+
} catch (JSONException e) {
2119+
assertEquals("Expecting an expection message",
2120+
"Duplicate key \"attr01\" at 124 [character 17 line 8]",
2121+
e.getMessage());
2122+
}
2123+
try {
2124+
// test exception message when including a duplicate key in object (level 1) within an array
2125+
String str = "[\n"
2126+
+" {\n"
2127+
+" \"attr01\":\"value-01\",\n"
2128+
+" \"attr02\": {\n"
2129+
+" \"attr02-01\":\"value-02-01\",\n"
2130+
+" \"attr02-02\":\"value-02-02\"\n"
2131+
+" }\n"
2132+
+" },\n"
2133+
+" {\n"
2134+
+" \"attr01\":\"value-01\",\n"
2135+
+" \"attr02\": {\n"
2136+
+" \"attr02-01\":\"value-02-01\",\n"
2137+
+" \"attr02-01\":\"value-02-02\"\n"
2138+
+" }\n"
2139+
+" }\n"
2140+
+ "]";
2141+
new JSONArray(str);
2142+
fail("Expected an exception");
2143+
} catch (JSONException e) {
2144+
assertEquals("Expecting an expection message",
2145+
"Duplicate key \"attr02-01\" at 269 [character 24 line 13]",
2146+
e.getMessage());
2147+
}
19792148
}
19802149

19812150
/**

0 commit comments

Comments
 (0)