mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
Unit tests for JSONTokener
This commit is contained in:
parent
44c3e321b5
commit
fefd616d73
2 changed files with 171 additions and 0 deletions
|
@ -22,6 +22,7 @@ dependencies {
|
||||||
testCompile group: 'junit', name: 'junit', version: '4.+'
|
testCompile group: 'junit', name: 'junit', version: '4.+'
|
||||||
testCompile group: 'com.jayway.jsonpath', name: 'json-path', version: '2.1.0'
|
testCompile group: 'com.jayway.jsonpath', name: 'json-path', version: '2.1.0'
|
||||||
testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.5'
|
testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.5'
|
||||||
|
testCompile group: 'slf4j.org', name: 'slf4j', version: '1.6.1'
|
||||||
// Uncomment if you are testing against a JSON-Java release
|
// Uncomment if you are testing against a JSON-Java release
|
||||||
// testCompile 'org.json:json:20160212'
|
// testCompile 'org.json:json:20160212'
|
||||||
// Uncomment if you have copied a local JSON-Java jar file into this project
|
// Uncomment if you have copied a local JSON-Java jar file into this project
|
||||||
|
|
|
@ -1976,6 +1976,176 @@ public class JSONObjectTest {
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
assertTrue("", true);
|
assertTrue("", true);
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
// test exception message when including a duplicate key (level 0)
|
||||||
|
String str = "{\n"
|
||||||
|
+" \"attr01\":\"value-01\",\n"
|
||||||
|
+" \"attr02\":\"value-02\",\n"
|
||||||
|
+" \"attr03\":\"value-03\",\n"
|
||||||
|
+" \"attr03\":\"value-04\"\n"
|
||||||
|
+ "}";
|
||||||
|
new JSONObject(str);
|
||||||
|
fail("Expected an exception");
|
||||||
|
} catch (JSONException e) {
|
||||||
|
assertEquals("Expecting an expection message",
|
||||||
|
"Duplicate key \"attr03\" at 90 [character 13 line 5]",
|
||||||
|
e.getMessage());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
// test exception message when including a duplicate key (level 0) holding an object
|
||||||
|
String str = "{\n"
|
||||||
|
+" \"attr01\":\"value-01\",\n"
|
||||||
|
+" \"attr02\":\"value-02\",\n"
|
||||||
|
+" \"attr03\":\"value-03\",\n"
|
||||||
|
+" \"attr03\": {"
|
||||||
|
+" \"attr04-01\":\"value-04-01\",n"
|
||||||
|
+" \"attr04-02\":\"value-04-02\",n"
|
||||||
|
+" \"attr04-03\":\"value-04-03\"n"
|
||||||
|
+ " }\n"
|
||||||
|
+ "}";
|
||||||
|
new JSONObject(str);
|
||||||
|
fail("Expected an exception");
|
||||||
|
} catch (JSONException e) {
|
||||||
|
assertEquals("Expecting an expection message",
|
||||||
|
"Duplicate key \"attr03\" at 90 [character 13 line 5]",
|
||||||
|
e.getMessage());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
// test exception message when including a duplicate key (level 0) holding an array
|
||||||
|
String str = "{\n"
|
||||||
|
+" \"attr01\":\"value-01\",\n"
|
||||||
|
+" \"attr02\":\"value-02\",\n"
|
||||||
|
+" \"attr03\":\"value-03\",\n"
|
||||||
|
+" \"attr03\": [\n"
|
||||||
|
+" {"
|
||||||
|
+" \"attr04-01\":\"value-04-01\",n"
|
||||||
|
+" \"attr04-02\":\"value-04-02\",n"
|
||||||
|
+" \"attr04-03\":\"value-04-03\"n"
|
||||||
|
+" }\n"
|
||||||
|
+ " ]\n"
|
||||||
|
+ "}";
|
||||||
|
new JSONObject(str);
|
||||||
|
fail("Expected an exception");
|
||||||
|
} catch (JSONException e) {
|
||||||
|
assertEquals("Expecting an expection message",
|
||||||
|
"Duplicate key \"attr03\" at 90 [character 13 line 5]",
|
||||||
|
e.getMessage());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
// test exception message when including a duplicate key (level 1)
|
||||||
|
String str = "{\n"
|
||||||
|
+" \"attr01\":\"value-01\",\n"
|
||||||
|
+" \"attr02\":\"value-02\",\n"
|
||||||
|
+" \"attr03\":\"value-03\",\n"
|
||||||
|
+" \"attr04\": {\n"
|
||||||
|
+" \"attr04-01\":\"value04-01\",\n"
|
||||||
|
+" \"attr04-02\":\"value04-02\",\n"
|
||||||
|
+" \"attr04-03\":\"value04-03\",\n"
|
||||||
|
+" \"attr04-03\":\"value04-04\"\n"
|
||||||
|
+ " }\n"
|
||||||
|
+ "}";
|
||||||
|
new JSONObject(str);
|
||||||
|
fail("Expected an exception");
|
||||||
|
} catch (JSONException e) {
|
||||||
|
assertEquals("Expecting an expection message",
|
||||||
|
"Duplicate key \"attr04-03\" at 215 [character 20 line 9]",
|
||||||
|
e.getMessage());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
// test exception message when including a duplicate key (level 1) holding an object
|
||||||
|
String str = "{\n"
|
||||||
|
+" \"attr01\":\"value-01\",\n"
|
||||||
|
+" \"attr02\":\"value-02\",\n"
|
||||||
|
+" \"attr03\":\"value-03\",\n"
|
||||||
|
+" \"attr04\": {\n"
|
||||||
|
+" \"attr04-01\":\"value04-01\",\n"
|
||||||
|
+" \"attr04-02\":\"value04-02\",\n"
|
||||||
|
+" \"attr04-03\":\"value04-03\",\n"
|
||||||
|
+" \"attr04-03\": {\n"
|
||||||
|
+" \"attr04-04-01\":\"value04-04-01\",\n"
|
||||||
|
+" \"attr04-04-02\":\"value04-04-02\",\n"
|
||||||
|
+" \"attr04-04-03\":\"value04-04-03\",\n"
|
||||||
|
+" }\n"
|
||||||
|
+" }\n"
|
||||||
|
+ "}";
|
||||||
|
new JSONObject(str);
|
||||||
|
fail("Expected an exception");
|
||||||
|
} catch (JSONException e) {
|
||||||
|
assertEquals("Expecting an expection message",
|
||||||
|
"Duplicate key \"attr04-03\" at 215 [character 20 line 9]",
|
||||||
|
e.getMessage());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
// test exception message when including a duplicate key (level 1) holding an array
|
||||||
|
String str = "{\n"
|
||||||
|
+" \"attr01\":\"value-01\",\n"
|
||||||
|
+" \"attr02\":\"value-02\",\n"
|
||||||
|
+" \"attr03\":\"value-03\",\n"
|
||||||
|
+" \"attr04\": {\n"
|
||||||
|
+" \"attr04-01\":\"value04-01\",\n"
|
||||||
|
+" \"attr04-02\":\"value04-02\",\n"
|
||||||
|
+" \"attr04-03\":\"value04-03\",\n"
|
||||||
|
+" \"attr04-03\": [\n"
|
||||||
|
+" {\n"
|
||||||
|
+" \"attr04-04-01\":\"value04-04-01\",\n"
|
||||||
|
+" \"attr04-04-02\":\"value04-04-02\",\n"
|
||||||
|
+" \"attr04-04-03\":\"value04-04-03\",\n"
|
||||||
|
+" }\n"
|
||||||
|
+" ]\n"
|
||||||
|
+" }\n"
|
||||||
|
+ "}";
|
||||||
|
new JSONObject(str);
|
||||||
|
fail("Expected an exception");
|
||||||
|
} catch (JSONException e) {
|
||||||
|
assertEquals("Expecting an expection message",
|
||||||
|
"Duplicate key \"attr04-03\" at 215 [character 20 line 9]",
|
||||||
|
e.getMessage());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
// test exception message when including a duplicate key in object (level 0) within an array
|
||||||
|
String str = "[\n"
|
||||||
|
+" {\n"
|
||||||
|
+" \"attr01\":\"value-01\",\n"
|
||||||
|
+" \"attr02\":\"value-02\"\n"
|
||||||
|
+" },\n"
|
||||||
|
+" {\n"
|
||||||
|
+" \"attr01\":\"value-01\",\n"
|
||||||
|
+" \"attr01\":\"value-02\"\n"
|
||||||
|
+" }\n"
|
||||||
|
+ "]";
|
||||||
|
new JSONArray(str);
|
||||||
|
fail("Expected an exception");
|
||||||
|
} catch (JSONException e) {
|
||||||
|
assertEquals("Expecting an expection message",
|
||||||
|
"Duplicate key \"attr01\" at 124 [character 17 line 8]",
|
||||||
|
e.getMessage());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
// test exception message when including a duplicate key in object (level 1) within an array
|
||||||
|
String str = "[\n"
|
||||||
|
+" {\n"
|
||||||
|
+" \"attr01\":\"value-01\",\n"
|
||||||
|
+" \"attr02\": {\n"
|
||||||
|
+" \"attr02-01\":\"value-02-01\",\n"
|
||||||
|
+" \"attr02-02\":\"value-02-02\"\n"
|
||||||
|
+" }\n"
|
||||||
|
+" },\n"
|
||||||
|
+" {\n"
|
||||||
|
+" \"attr01\":\"value-01\",\n"
|
||||||
|
+" \"attr02\": {\n"
|
||||||
|
+" \"attr02-01\":\"value-02-01\",\n"
|
||||||
|
+" \"attr02-01\":\"value-02-02\"\n"
|
||||||
|
+" }\n"
|
||||||
|
+" }\n"
|
||||||
|
+ "]";
|
||||||
|
System.out.println(str);
|
||||||
|
new JSONArray(str);
|
||||||
|
fail("Expected an exception");
|
||||||
|
} catch (JSONException e) {
|
||||||
|
assertEquals("Expecting an expection message",
|
||||||
|
"Duplicate key \"attr02-01\" at 269 [character 24 line 13]",
|
||||||
|
e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue