mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
commit
0e0f3f2167
12 changed files with 641 additions and 275 deletions
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
/bin/
|
||||||
|
build
|
||||||
|
.classpath
|
||||||
|
.project
|
||||||
|
.settings/
|
62
README.md
62
README.md
|
@ -4,13 +4,10 @@ Unit tests to validate the JSON-Java GitHub project code<br>
|
||||||
|
|
||||||
https://github.com/douglascrockford/JSON-java<br>
|
https://github.com/douglascrockford/JSON-java<br>
|
||||||
|
|
||||||
*These tests are a work in progress. Help from interested developers is welcome.*<br>
|
Gradle and Eclipse is the recommended build tool and IDE.<br>
|
||||||
More coverage is needed, but more importantly, improvements to test quality is needed.<br>
|
|
||||||
|
|
||||||
Eclipse is the recommended development environment.<br>
|
|
||||||
Run individual tests or <b>JunitTestSuite</b> using <b>EclEmma Coverage</b>, or execute the **TestRunner** application directly.<br>
|
Run individual tests or <b>JunitTestSuite</b> using <b>EclEmma Coverage</b>, or execute the **TestRunner** application directly.<br>
|
||||||
|
|
||||||
**You will need the following libraries for testing:**<br>
|
**The following libraries are required:**<br>
|
||||||
* asm-1.0.2.jar<br>
|
* asm-1.0.2.jar<br>
|
||||||
* commons-io-2.1.jar<br>
|
* commons-io-2.1.jar<br>
|
||||||
* commons-lang-2.6.jar<br>
|
* commons-lang-2.6.jar<br>
|
||||||
|
@ -23,40 +20,33 @@ Run individual tests or <b>JunitTestSuite</b> using <b>EclEmma Coverage</b>, or
|
||||||
* slf-simple-1.7.12.jar<br>
|
* slf-simple-1.7.12.jar<br>
|
||||||
* JSON-java.jar<br>
|
* JSON-java.jar<br>
|
||||||
|
|
||||||
**To build from the command line using gradle:**
|
**To build from the command line using gradle:**<br>
|
||||||
|
Until the unit tests are merged into the JSON-Java project, the code has to be wired by hand. <br>
|
||||||
|
\# In an empty directory of your choice, clone JSON-Java-unit-test:<br>
|
||||||
````
|
````
|
||||||
build.gradle
|
git clone https://github.com/stleary/JSON-Java-unit-test.git .
|
||||||
# In this example, both the JSON-java jar and the test code is created
|
|
||||||
# from the same build file, in the test code directory. 3rd party jars are
|
|
||||||
# obtained from the maven repository.
|
|
||||||
apply plugin: 'java'
|
|
||||||
jar.baseName = 'JSON-java'
|
|
||||||
|
|
||||||
sourceSets {
|
|
||||||
main {
|
|
||||||
java {
|
|
||||||
srcDir '../JSON-java/src/org/json'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
test {
|
|
||||||
java {
|
|
||||||
srcDir 'src/org/json/junit'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
repositories {
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
testCompile group: 'junit', name: 'junit', version: '4.+'
|
|
||||||
testCompile group: 'com.jayway.jsonpath', name: 'json-path', version: '2.1.0'
|
|
||||||
testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.5'
|
|
||||||
}
|
|
||||||
````
|
````
|
||||||
|
\# Create a directory structure for the JSON-Java code
|
||||||
|
````
|
||||||
|
# Windows version
|
||||||
|
md /s src\org\json
|
||||||
|
````
|
||||||
|
\# clone JSON-Java
|
||||||
|
````
|
||||||
|
git clone https://github.com/stleary/JSON-Java.git src\org\json
|
||||||
|
````
|
||||||
|
\# Build, then execute the unit tests and code coverage
|
||||||
|
````
|
||||||
|
gradle clean build test jacocoTestReport
|
||||||
|
````
|
||||||
|
Unit test results will be in build\reports\tests\index.html<br>
|
||||||
|
Code coverage will be in build\reports\jacoco\html\index.html
|
||||||
|
|
||||||
To measure coverage: http://www.eclemma.org/ (just install the latest in Eclipse)<br>
|
To create an Eclipse project, you will need the Eclipse Gradle plug-in, available from the Eclipse Marketplace. I am currently using Gradle IDE 3.6.4.201503050952-RELEASE<br>
|
||||||
|
Select File > Import > Gradle > Gradle project <br>
|
||||||
|
Browse to the directory where you cloned JSON-Java-unit-test<br>
|
||||||
|
Select Build model<br>
|
||||||
|
Select built project<br>
|
||||||
|
|
||||||
<b>Conventions</b><br>
|
<b>Conventions</b><br>
|
||||||
Test filenames should consist of the name of the module being tested, with the suffix "Test".
|
Test filenames should consist of the name of the module being tested, with the suffix "Test".
|
||||||
|
|
50
build.gradle
50
build.gradle
|
@ -1,26 +1,48 @@
|
||||||
apply plugin: 'java'
|
apply plugin: 'java'
|
||||||
apply plugin: 'eclipse'
|
apply plugin: 'eclipse'
|
||||||
|
apply plugin: 'jacoco'
|
||||||
jar.baseName = 'JSON-java'
|
|
||||||
|
|
||||||
sourceSets {
|
sourceSets {
|
||||||
test {
|
// Uncomment main if you have merged JSON-Java and JSON-Java-unit-test code
|
||||||
java {
|
main {
|
||||||
srcDir 'src/test'
|
java {
|
||||||
|
srcDir 'src'
|
||||||
|
exclude 'test/'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
test {
|
||||||
|
java {
|
||||||
|
srcDir 'src/test'
|
||||||
|
exclude 'resources/'
|
||||||
|
}
|
||||||
|
resources {
|
||||||
|
srcDir 'resources'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
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'
|
||||||
// Use this line if you are testing a JSON-Java release.
|
// Uncomment if you are testing against a JSON-Java release
|
||||||
// Otherwise add an external jar from your local repository in Eclipse
|
// testCompile 'org.json:json:20160212'
|
||||||
// (The gradle build won't work unless you add a main sourceSets entry and a jar.baseName entry
|
// Uncomment if you have copied a local JSON-Java jar file into this project
|
||||||
// testCompile group: 'org.json', name: 'json', version: '20151123'
|
// testCompile files('./JSON-Java.jar')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test { finalizedBy jacocoTestReport }
|
||||||
|
jacocoTestReport{
|
||||||
|
additionalSourceDirs = files(sourceSets.main.allJava.srcDirs)
|
||||||
|
reports {
|
||||||
|
xml.enabled false
|
||||||
|
csv.enabled false
|
||||||
|
html.destination "${buildDir}/reports/jacoco/html"
|
||||||
|
}
|
||||||
|
executionData = files('build/jacoco/test.exec')
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -68,7 +68,7 @@ public class CDLTest {
|
||||||
equals(e.getMessage()));
|
equals(e.getMessage()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempts to create a JSONArray from a string with unbalanced quotes
|
* Attempts to create a JSONArray from a string with unbalanced quotes
|
||||||
* in value line. Expects a JSONException.
|
* in value line. Expects a JSONException.
|
||||||
|
@ -104,7 +104,62 @@ public class CDLTest {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempt to create a JSONArray with unbalanced quotes and a properly escaped doubled quote.
|
||||||
|
* Expects a JSONException.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void unbalancedEscapedQuote(){
|
||||||
|
String badLine = "Col1, Col2\n\"Val1, \"\"Val2\"\"";
|
||||||
|
try {
|
||||||
|
CDL.toJSONArray(badLine);
|
||||||
|
assertTrue("Expecting an exception", false);
|
||||||
|
} catch (JSONException e) {
|
||||||
|
assertTrue("Expecting an exception message",
|
||||||
|
"Missing close quote '\"'. at 27 [character 16 line 3]".
|
||||||
|
equals(e.getMessage()));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert that there is no error for a single escaped quote within a properly embedded quote.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void singleEscapedQuote(){
|
||||||
|
String singleEscape = "Col1, Col2\nVal1, \"\"\"Val2\"";
|
||||||
|
JSONArray jsonArray = CDL.toJSONArray(singleEscape);
|
||||||
|
|
||||||
|
String cdlStr = CDL.toString(jsonArray);
|
||||||
|
assertTrue(cdlStr.contains("Col1"));
|
||||||
|
assertTrue(cdlStr.contains("Col2"));
|
||||||
|
assertTrue(cdlStr.contains("Val1"));
|
||||||
|
assertTrue(cdlStr.contains("\"Val2"));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempt to create a JSONArray with an escape quote and no enclosing quotes.
|
||||||
|
* Expects a JSONException.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void badEscapedQuote(){
|
||||||
|
String badLine = "Col1, Col2\nVal1, \"\"Val2";
|
||||||
|
|
||||||
|
try {
|
||||||
|
CDL.toJSONArray(badLine);
|
||||||
|
assertTrue("Expecting an exception", false);
|
||||||
|
} catch (JSONException e) {
|
||||||
|
System.out.println("Message" + e.getMessage());
|
||||||
|
assertTrue("Expecting an exception message",
|
||||||
|
"Bad character 'V' (86). at 20 [character 9 line 3]".
|
||||||
|
equals(e.getMessage()));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* call toString with a null array
|
* call toString with a null array
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -78,7 +78,7 @@ public class CookieListTest {
|
||||||
// validate JSON content
|
// validate JSON content
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("Expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
|
assertTrue("Expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
|
||||||
assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(JsonPath.read(doc, "$.SID")));
|
assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(jsonObject.query("/SID")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -91,7 +91,7 @@ public class CookieListTest {
|
||||||
// validate JSON content
|
// validate JSON content
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("Expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
|
assertTrue("Expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
|
||||||
assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(JsonPath.read(doc, "$.SID")));
|
assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(jsonObject.query("/SID")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -111,12 +111,12 @@ public class CookieListTest {
|
||||||
// validate JSON content
|
// validate JSON content
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("Expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
|
assertTrue("Expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
|
||||||
assertTrue("expected myCookieValue1", "myCookieValue1".equals(JsonPath.read(doc, "$.name1")));
|
assertTrue("expected myCookieValue1", "myCookieValue1".equals(jsonObject.query("/name1")));
|
||||||
assertTrue("expected myCookieValue2", "myCookieValue2".equals(JsonPath.read(doc, "$.name2")));
|
assertTrue("expected myCookieValue2", "myCookieValue2".equals(jsonObject.query("/name2")));
|
||||||
assertTrue("expected myCookieValue3", "myCookieValue3".equals(JsonPath.read(doc, "$.name3")));
|
assertTrue("expected myCookieValue3", "myCookieValue3".equals(jsonObject.query("/name3")));
|
||||||
assertTrue("expected myCookieValue4", "myCookieValue4".equals(JsonPath.read(doc, "$.name4")));
|
assertTrue("expected myCookieValue4", "myCookieValue4".equals(jsonObject.query("/name4")));
|
||||||
assertTrue("expected myCookieValue5", "myCookieValue5".equals(JsonPath.read(doc, "$.name5")));
|
assertTrue("expected myCookieValue5", "myCookieValue5".equals(jsonObject.query("/name5")));
|
||||||
assertTrue("expected myCookieValue6", "myCookieValue6".equals(JsonPath.read(doc, "$.name6")));
|
assertTrue("expected myCookieValue6", "myCookieValue6".equals(jsonObject.query("/name6")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -151,12 +151,12 @@ public class CookieListTest {
|
||||||
// validate JSON content
|
// validate JSON content
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("Expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
|
assertTrue("Expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
|
||||||
assertTrue("expected myCookieValue1", "myCookieValue1".equals(JsonPath.read(doc, "$.name1")));
|
assertTrue("expected myCookieValue1", "myCookieValue1".equals(jsonObject.query("/name1")));
|
||||||
assertTrue("expected myCookieValue2", "myCookieValue2".equals(JsonPath.read(doc, "$.name2")));
|
assertTrue("expected myCookieValue2", "myCookieValue2".equals(jsonObject.query("/name2")));
|
||||||
assertTrue("expected myCookieValue3", "myCookieValue3".equals(JsonPath.read(doc, "$.name3")));
|
assertTrue("expected myCookieValue3", "myCookieValue3".equals(jsonObject.query("/name3")));
|
||||||
assertTrue("expected myCookieValue4", "myCookieValue4".equals(JsonPath.read(doc, "$.name4")));
|
assertTrue("expected myCookieValue4", "myCookieValue4".equals(jsonObject.query("/name4")));
|
||||||
assertTrue("expected myCookieValue5", "myCookieValue5".equals(JsonPath.read(doc, "$.name5")));
|
assertTrue("expected myCookieValue5", "myCookieValue5".equals(jsonObject.query("/name5")));
|
||||||
assertTrue("expected myCookieValue6", "myCookieValue6".equals(JsonPath.read(doc, "$.name6")));
|
assertTrue("expected myCookieValue6", "myCookieValue6".equals(jsonObject.query("/name6")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -176,11 +176,11 @@ public class CookieListTest {
|
||||||
// validate JSON content
|
// validate JSON content
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("Expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
|
assertTrue("Expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
|
||||||
assertTrue("expected myCookieValue1", "myCookieValue1".equals(JsonPath.read(doc, "$.name1")));
|
assertTrue("expected myCookieValue1", "myCookieValue1".equals(jsonObject.query("/name1")));
|
||||||
assertTrue("expected my Cookie Value 2", "my Cookie Value 2".equals(JsonPath.read(doc, "$.name2")));
|
assertTrue("expected my Cookie Value 2", "my Cookie Value 2".equals(jsonObject.query("/name2")));
|
||||||
assertTrue("expected my+Cookie&Value;3=", "my+Cookie&Value;3=".equals(JsonPath.read(doc, "$.name3")));
|
assertTrue("expected my+Cookie&Value;3=", "my+Cookie&Value;3=".equals(jsonObject.query("/name3")));
|
||||||
assertTrue("expected my%CookieValue4", "my%CookieValue4".equals(JsonPath.read(doc, "$.name4")));
|
assertTrue("expected my%CookieValue4", "my%CookieValue4".equals(jsonObject.query("/name4")));
|
||||||
assertTrue("expected my%CookieValue5", "myCookieValue5".equals(JsonPath.read(doc, "$.name5")));
|
assertTrue("expected my%CookieValue5", "myCookieValue5".equals(jsonObject.query("/name5")));
|
||||||
assertTrue("expected myCookieValue6", "myCookieValue6".equals(JsonPath.read(doc, "$.name6")));
|
assertTrue("expected myCookieValue6", "myCookieValue6".equals(jsonObject.query("/name6")));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,8 +36,8 @@ public class EnumTest {
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider()
|
Object doc = Configuration.defaultConfiguration().jsonProvider()
|
||||||
.parse(jsonObject.toString());
|
.parse(jsonObject.toString());
|
||||||
assertTrue("expecting 2 items in top level object", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
assertTrue("expecting 2 items in top level object", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
assertTrue("expecting val 2", "val 2".equals(JsonPath.read(doc, "$.value")));
|
assertTrue("expecting val 2", "val 2".equals(jsonObject.query("/value")));
|
||||||
assertTrue("expecting 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.intVal")));
|
assertTrue("expecting 2", Integer.valueOf(2).equals(jsonObject.query("/intVal")));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* class which contains enum instances. Each enum should be stored
|
* class which contains enum instances. Each enum should be stored
|
||||||
|
@ -53,8 +53,8 @@ public class EnumTest {
|
||||||
assertTrue("expecting 2 items in top level object", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
assertTrue("expecting 2 items in top level object", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
assertTrue("expecting 2 items in myEnumField object", ((Map<?,?>)(JsonPath.read(doc, "$.myEnumField"))).size() == 2);
|
assertTrue("expecting 2 items in myEnumField object", ((Map<?,?>)(JsonPath.read(doc, "$.myEnumField"))).size() == 2);
|
||||||
assertTrue("expecting 0 items in myEnum object", ((Map<?,?>)(JsonPath.read(doc, "$.myEnum"))).size() == 0);
|
assertTrue("expecting 0 items in myEnum object", ((Map<?,?>)(JsonPath.read(doc, "$.myEnum"))).size() == 0);
|
||||||
assertTrue("expecting 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$.myEnumField.intVal")));
|
assertTrue("expecting 3", Integer.valueOf(3).equals(jsonObject.query("/myEnumField/intVal")));
|
||||||
assertTrue("expecting val 3", "val 3".equals(JsonPath.read(doc, "$.myEnumField.value")));
|
assertTrue("expecting val 3", "val 3".equals(jsonObject.query("/myEnumField/value")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -74,9 +74,9 @@ public class EnumTest {
|
||||||
// validate JSON object
|
// validate JSON object
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("expected 3 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 3);
|
assertTrue("expected 3 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 3);
|
||||||
assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.VAL1")));
|
assertTrue("expected VAL1", MyEnum.VAL1.equals(jsonObject.query("/VAL1")));
|
||||||
assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.VAL2")));
|
assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonObject.query("/VAL2")));
|
||||||
assertTrue("expected VAL3", "VAL3".equals(JsonPath.read(doc, "$.VAL3")));
|
assertTrue("expected VAL3", MyEnum.VAL3.equals(jsonObject.query("/VAL3")));
|
||||||
|
|
||||||
MyEnumField myEnumField = MyEnumField.VAL3;
|
MyEnumField myEnumField = MyEnumField.VAL3;
|
||||||
names = JSONObject.getNames(myEnumField);
|
names = JSONObject.getNames(myEnumField);
|
||||||
|
@ -84,9 +84,9 @@ public class EnumTest {
|
||||||
jsonObject = new JSONObject(myEnumField, names);
|
jsonObject = new JSONObject(myEnumField, names);
|
||||||
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("expected 3 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 3);
|
assertTrue("expected 3 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 3);
|
||||||
assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.VAL1")));
|
assertTrue("expected VAL1", MyEnumField.VAL1.equals(jsonObject.query("/VAL1")));
|
||||||
assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.VAL2")));
|
assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonObject.query("/VAL2")));
|
||||||
assertTrue("expected VAL3", "VAL3".equals(JsonPath.read(doc, "$.VAL3")));
|
assertTrue("expected VAL3", myEnumField.VAL3.equals(jsonObject.query("/VAL3")));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,8 +105,8 @@ public class EnumTest {
|
||||||
// validate JSON content
|
// validate JSON content
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("expected 2 top level objects", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
assertTrue("expected 2 top level objects", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.myEnum")));
|
assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonObject.query("/myEnum")));
|
||||||
assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.myEnumField")));
|
assertTrue("expected VAL1", MyEnumField.VAL1.equals(jsonObject.query("/myEnumField")));
|
||||||
|
|
||||||
JSONArray jsonArray = new JSONArray();
|
JSONArray jsonArray = new JSONArray();
|
||||||
jsonArray.put(myEnum);
|
jsonArray.put(myEnum);
|
||||||
|
@ -115,8 +115,8 @@ public class EnumTest {
|
||||||
// validate JSON content
|
// validate JSON content
|
||||||
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
||||||
assertTrue("expected 2 top level objects", ((List<?>)(JsonPath.read(doc, "$"))).size() == 2);
|
assertTrue("expected 2 top level objects", ((List<?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$[0]")));
|
assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonArray.query("/0")));
|
||||||
assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$[1]")));
|
assertTrue("expected VAL1", MyEnumField.VAL1.equals(jsonArray.query("/1")));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Leaving these tests because they exercise get, opt, and remove
|
* Leaving these tests because they exercise get, opt, and remove
|
||||||
|
@ -174,8 +174,8 @@ public class EnumTest {
|
||||||
// validate JSON content
|
// validate JSON content
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
assertTrue("expected val 2", "val 2".equals(JsonPath.read(doc, "$.value")));
|
assertTrue("expected val 2", "val 2".equals(jsonObject.query("/value")));
|
||||||
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.intVal")));
|
assertTrue("expected 2", Integer.valueOf(2).equals(jsonObject.query("/intVal")));
|
||||||
|
|
||||||
MyEnumClass myEnumClass = new MyEnumClass();
|
MyEnumClass myEnumClass = new MyEnumClass();
|
||||||
myEnumClass.setMyEnum(MyEnum.VAL1);
|
myEnumClass.setMyEnum(MyEnum.VAL1);
|
||||||
|
@ -187,8 +187,8 @@ public class EnumTest {
|
||||||
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
assertTrue("expected 2 myEnumField items", ((Map<?,?>)(JsonPath.read(doc, "$.myEnumField"))).size() == 2);
|
assertTrue("expected 2 myEnumField items", ((Map<?,?>)(JsonPath.read(doc, "$.myEnumField"))).size() == 2);
|
||||||
assertTrue("expected 0 myEnum items", ((Map<?,?>)(JsonPath.read(doc, "$.myEnum"))).size() == 0);
|
assertTrue("expected 0 myEnum items", ((Map<?,?>)(JsonPath.read(doc, "$.myEnum"))).size() == 0);
|
||||||
assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$.myEnumField.intVal")));
|
assertTrue("expected 3", Integer.valueOf(3).equals(jsonObject.query("/myEnumField/intVal")));
|
||||||
assertTrue("expected val 3", "val 3".equals(JsonPath.read(doc, "$.myEnumField.value")));
|
assertTrue("expected val 3", "val 3".equals(jsonObject.query("/myEnumField/value")));
|
||||||
|
|
||||||
String [] names = JSONObject.getNames(myEnum);
|
String [] names = JSONObject.getNames(myEnum);
|
||||||
jsonObject = new JSONObject(myEnum, names);
|
jsonObject = new JSONObject(myEnum, names);
|
||||||
|
@ -196,9 +196,9 @@ public class EnumTest {
|
||||||
// validate JSON content
|
// validate JSON content
|
||||||
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("expected 3 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 3);
|
assertTrue("expected 3 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 3);
|
||||||
assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.VAL1")));
|
assertTrue("expected VAL1", MyEnum.VAL1.equals(jsonObject.query("/VAL1")));
|
||||||
assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.VAL2")));
|
assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonObject.query("/VAL2")));
|
||||||
assertTrue("expected VAL3", "VAL3".equals(JsonPath.read(doc, "$.VAL3")));
|
assertTrue("expected VAL3", MyEnum.VAL3.equals(jsonObject.query("/VAL3")));
|
||||||
|
|
||||||
names = JSONObject.getNames(myEnumField);
|
names = JSONObject.getNames(myEnumField);
|
||||||
jsonObject = new JSONObject(myEnumField, names);
|
jsonObject = new JSONObject(myEnumField, names);
|
||||||
|
@ -206,9 +206,9 @@ public class EnumTest {
|
||||||
// validate JSON content
|
// validate JSON content
|
||||||
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("expected 3 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 3);
|
assertTrue("expected 3 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 3);
|
||||||
assertTrue("expected VAL1", "VAL1".equals(JsonPath.read(doc, "$.VAL1")));
|
assertTrue("expected VAL1", MyEnumField.VAL1.equals(jsonObject.query("/VAL1")));
|
||||||
assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.VAL2")));
|
assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonObject.query("/VAL2")));
|
||||||
assertTrue("expected VAL3", "VAL3".equals(JsonPath.read(doc, "$.VAL3")));
|
assertTrue("expected VAL3", MyEnumField.VAL3.equals(jsonObject.query("/VAL3")));
|
||||||
|
|
||||||
expectedStr = "{\"myEnum\":\"VAL2\", \"myEnumField\":\"VAL2\"}";
|
expectedStr = "{\"myEnum\":\"VAL2\", \"myEnumField\":\"VAL2\"}";
|
||||||
jsonObject = new JSONObject();
|
jsonObject = new JSONObject();
|
||||||
|
@ -218,8 +218,8 @@ public class EnumTest {
|
||||||
// validate JSON content
|
// validate JSON content
|
||||||
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.myEnum")));
|
assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonObject.query("/myEnum")));
|
||||||
assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$.myEnumField")));
|
assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonObject.query("/myEnumField")));
|
||||||
|
|
||||||
JSONArray jsonArray = new JSONArray();
|
JSONArray jsonArray = new JSONArray();
|
||||||
jsonArray.put(myEnum);
|
jsonArray.put(myEnum);
|
||||||
|
@ -228,8 +228,8 @@ public class EnumTest {
|
||||||
// validate JSON content
|
// validate JSON content
|
||||||
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
||||||
assertTrue("expected 2 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 2);
|
assertTrue("expected 2 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$[0]")));
|
assertTrue("expected VAL2", MyEnum.VAL2.equals(jsonArray.query("/0")));
|
||||||
assertTrue("expected VAL2", "VAL2".equals(JsonPath.read(doc, "$[1]")));
|
assertTrue("expected VAL2", MyEnumField.VAL2.equals(jsonArray.query("/1")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -248,8 +248,8 @@ public class EnumTest {
|
||||||
// validate JSON content
|
// validate JSON content
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
assertTrue("expected val 2", "val 2".equals(JsonPath.read(doc, "$.value")));
|
assertTrue("expected val 2", "val 2".equals(jsonObject.query("/value")));
|
||||||
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.intVal")));
|
assertTrue("expected 2", Integer.valueOf(2).equals(jsonObject.query("/intVal")));
|
||||||
|
|
||||||
MyEnumClass myEnumClass = new MyEnumClass();
|
MyEnumClass myEnumClass = new MyEnumClass();
|
||||||
myEnumClass.setMyEnum(MyEnum.VAL1);
|
myEnumClass.setMyEnum(MyEnum.VAL1);
|
||||||
|
@ -261,8 +261,8 @@ public class EnumTest {
|
||||||
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
assertTrue("expected 2 myEnumField items", ((Map<?,?>)(JsonPath.read(doc, "$.myEnumField"))).size() == 2);
|
assertTrue("expected 2 myEnumField items", ((Map<?,?>)(JsonPath.read(doc, "$.myEnumField"))).size() == 2);
|
||||||
assertTrue("expected 0 myEnum items", ((Map<?,?>)(JsonPath.read(doc, "$.myEnum"))).size() == 0);
|
assertTrue("expected 0 myEnum items", ((Map<?,?>)(JsonPath.read(doc, "$.myEnum"))).size() == 0);
|
||||||
assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$.myEnumField.intVal")));
|
assertTrue("expected 3", Integer.valueOf(3).equals(jsonObject.query("/myEnumField/intVal")));
|
||||||
assertTrue("expected val 3", "val 3".equals(JsonPath.read(doc, "$.myEnumField.value")));
|
assertTrue("expected val 3", "val 3".equals(jsonObject.query("/myEnumField/value")));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,24 @@
|
||||||
package org.json.junit;
|
package org.json.junit;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
import org.json.JSONPointerException;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.jayway.jsonpath.*;
|
import com.jayway.jsonpath.Configuration;
|
||||||
|
import com.jayway.jsonpath.JsonPath;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -316,24 +325,24 @@ public class JSONArrayTest {
|
||||||
*/
|
*/
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse("["+joinStr+"]");
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse("["+joinStr+"]");
|
||||||
assertTrue("expected 13 items in top level object", ((List<?>)(JsonPath.read(doc, "$"))).size() == 13);
|
assertTrue("expected 13 items in top level object", ((List<?>)(JsonPath.read(doc, "$"))).size() == 13);
|
||||||
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]")));
|
assertTrue("expected true", Boolean.TRUE.equals(jsonArray.query("/0")));
|
||||||
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]")));
|
assertTrue("expected false", Boolean.FALSE.equals(jsonArray.query("/1")));
|
||||||
assertTrue("expected \"true\"", "true".equals(JsonPath.read(doc, "$[2]")));
|
assertTrue("expected \"true\"", "true".equals(jsonArray.query("/2")));
|
||||||
assertTrue("expected \"false\"", "false".equals(JsonPath.read(doc, "$[3]")));
|
assertTrue("expected \"false\"", "false".equals(jsonArray.query("/3")));
|
||||||
assertTrue("expected hello", "hello".equals(JsonPath.read(doc, "$[4]")));
|
assertTrue("expected hello", "hello".equals(jsonArray.query("/4")));
|
||||||
assertTrue("expected 0.002345", Double.valueOf(0.002345).equals(JsonPath.read(doc, "$[5]")));
|
assertTrue("expected 0.002345", Double.valueOf(0.002345).equals(jsonArray.query("/5")));
|
||||||
assertTrue("expected \"23.45\"", "23.45".equals(JsonPath.read(doc, "$[6]")));
|
assertTrue("expected \"23.45\"", "23.45".equals(jsonArray.query("/6")));
|
||||||
assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$[7]")));
|
assertTrue("expected 42", Integer.valueOf(42).equals(jsonArray.query("/7")));
|
||||||
assertTrue("expected \"43\"", "43".equals(JsonPath.read(doc, "$[8]")));
|
assertTrue("expected \"43\"", "43".equals(jsonArray.query("/8")));
|
||||||
assertTrue("expected 1 item in [9]", ((List<?>)(JsonPath.read(doc, "$[9]"))).size() == 1);
|
assertTrue("expected 1 item in [9]", ((List<?>)(JsonPath.read(doc, "$[9]"))).size() == 1);
|
||||||
assertTrue("expected world", "world".equals(JsonPath.read(doc, "$[9][0]")));
|
assertTrue("expected world", "world".equals(jsonArray.query("/9/0")));
|
||||||
assertTrue("expected 4 items in [10]", ((Map<?,?>)(JsonPath.read(doc, "$[10]"))).size() == 4);
|
assertTrue("expected 4 items in [10]", ((Map<?,?>)(JsonPath.read(doc, "$[10]"))).size() == 4);
|
||||||
assertTrue("expected value1", "value1".equals(JsonPath.read(doc, "$[10].key1")));
|
assertTrue("expected value1", "value1".equals(jsonArray.query("/10/key1")));
|
||||||
assertTrue("expected value2", "value2".equals(JsonPath.read(doc, "$[10].key2")));
|
assertTrue("expected value2", "value2".equals(jsonArray.query("/10/key2")));
|
||||||
assertTrue("expected value3", "value3".equals(JsonPath.read(doc, "$[10].key3")));
|
assertTrue("expected value3", "value3".equals(jsonArray.query("/10/key3")));
|
||||||
assertTrue("expected value4", "value4".equals(JsonPath.read(doc, "$[10].key4")));
|
assertTrue("expected value4", "value4".equals(jsonArray.query("/10/key4")));
|
||||||
assertTrue("expected 0", Integer.valueOf(0).equals(JsonPath.read(doc, "$[11]")));
|
assertTrue("expected 0", Integer.valueOf(0).equals(jsonArray.query("/11")));
|
||||||
assertTrue("expected \"-1\"", "-1".equals(JsonPath.read(doc, "$[12]")));
|
assertTrue("expected \"-1\"", "-1".equals(jsonArray.query("/12")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -461,24 +470,24 @@ public class JSONArrayTest {
|
||||||
// validate JSON
|
// validate JSON
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
||||||
assertTrue("expected 10 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 10);
|
assertTrue("expected 10 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 10);
|
||||||
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]")));
|
assertTrue("expected true", Boolean.TRUE.equals(jsonArray.query("/0")));
|
||||||
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]")));
|
assertTrue("expected false", Boolean.FALSE.equals(jsonArray.query("/1")));
|
||||||
assertTrue("expected 2 items in [2]", ((List<?>)(JsonPath.read(doc, "$[2]"))).size() == 2);
|
assertTrue("expected 2 items in [2]", ((List<?>)(JsonPath.read(doc, "$[2]"))).size() == 2);
|
||||||
assertTrue("expected hello", "hello".equals(JsonPath.read(doc, "$[2][0]")));
|
assertTrue("expected hello", "hello".equals(jsonArray.query("/2/0")));
|
||||||
assertTrue("expected world", "world".equals(JsonPath.read(doc, "$[2][1]")));
|
assertTrue("expected world", "world".equals(jsonArray.query("/2/1")));
|
||||||
assertTrue("expected 2.5", Double.valueOf(2.5).equals(JsonPath.read(doc, "$[3]")));
|
assertTrue("expected 2.5", Double.valueOf(2.5).equals(jsonArray.query("/3")));
|
||||||
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[4]")));
|
assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/4")));
|
||||||
assertTrue("expected 45", Integer.valueOf(45).equals(JsonPath.read(doc, "$[5]")));
|
assertTrue("expected 45", Long.valueOf(45).equals(jsonArray.query("/5")));
|
||||||
assertTrue("expected objectPut", "objectPut".equals(JsonPath.read(doc, "$[6]")));
|
assertTrue("expected objectPut", "objectPut".equals(jsonArray.query("/6")));
|
||||||
assertTrue("expected 3 items in [7]", ((Map<?,?>)(JsonPath.read(doc, "$[7]"))).size() == 3);
|
assertTrue("expected 3 items in [7]", ((Map<?,?>)(JsonPath.read(doc, "$[7]"))).size() == 3);
|
||||||
assertTrue("expected val10", "val10".equals(JsonPath.read(doc, "$[7].key10")));
|
assertTrue("expected val10", "val10".equals(jsonArray.query("/7/key10")));
|
||||||
assertTrue("expected val20", "val20".equals(JsonPath.read(doc, "$[7].key20")));
|
assertTrue("expected val20", "val20".equals(jsonArray.query("/7/key20")));
|
||||||
assertTrue("expected val30", "val30".equals(JsonPath.read(doc, "$[7].key30")));
|
assertTrue("expected val30", "val30".equals(jsonArray.query("/7/key30")));
|
||||||
assertTrue("expected 1 item in [8]", ((Map<?,?>)(JsonPath.read(doc, "$[8]"))).size() == 1);
|
assertTrue("expected 1 item in [8]", ((Map<?,?>)(JsonPath.read(doc, "$[8]"))).size() == 1);
|
||||||
assertTrue("expected v1", "v1".equals(JsonPath.read(doc, "$[8].k1")));
|
assertTrue("expected v1", "v1".equals(jsonArray.query("/8/k1")));
|
||||||
assertTrue("expected 2 items in [9]", ((List<?>)(JsonPath.read(doc, "$[9]"))).size() == 2);
|
assertTrue("expected 2 items in [9]", ((List<?>)(JsonPath.read(doc, "$[9]"))).size() == 2);
|
||||||
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[9][0]")));
|
assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/9/0")));
|
||||||
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[9][1]")));
|
assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/9/1")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -538,25 +547,25 @@ public class JSONArrayTest {
|
||||||
// validate JSON
|
// validate JSON
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
||||||
assertTrue("expected 11 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 11);
|
assertTrue("expected 11 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 11);
|
||||||
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]")));
|
assertTrue("expected true", Boolean.TRUE.equals(jsonArray.query("/0")));
|
||||||
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]")));
|
assertTrue("expected false", Boolean.FALSE.equals(jsonArray.query("/1")));
|
||||||
assertTrue("expected 2 items in [2]", ((List<?>)(JsonPath.read(doc, "$[2]"))).size() == 2);
|
assertTrue("expected 2 items in [2]", ((List<?>)(JsonPath.read(doc, "$[2]"))).size() == 2);
|
||||||
assertTrue("expected hello", "hello".equals(JsonPath.read(doc, "$[2][0]")));
|
assertTrue("expected hello", "hello".equals(jsonArray.query("/2/0")));
|
||||||
assertTrue("expected world", "world".equals(JsonPath.read(doc, "$[2][1]")));
|
assertTrue("expected world", "world".equals(jsonArray.query("/2/1")));
|
||||||
assertTrue("expected 2.5", Double.valueOf(2.5).equals(JsonPath.read(doc, "$[3]")));
|
assertTrue("expected 2.5", Double.valueOf(2.5).equals(jsonArray.query("/3")));
|
||||||
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[4]")));
|
assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/4")));
|
||||||
assertTrue("expected 45", Integer.valueOf(45).equals(JsonPath.read(doc, "$[5]")));
|
assertTrue("expected 45", Long.valueOf(45).equals(jsonArray.query("/5")));
|
||||||
assertTrue("expected objectPut", "objectPut".equals(JsonPath.read(doc, "$[6]")));
|
assertTrue("expected objectPut", "objectPut".equals(jsonArray.query("/6")));
|
||||||
assertTrue("expected null", null == JsonPath.read(doc, "$[7]"));
|
assertTrue("expected null", JSONObject.NULL.equals(jsonArray.query("/7")));
|
||||||
assertTrue("expected 3 items in [8]", ((Map<?,?>)(JsonPath.read(doc, "$[8]"))).size() == 3);
|
assertTrue("expected 3 items in [8]", ((Map<?,?>)(JsonPath.read(doc, "$[8]"))).size() == 3);
|
||||||
assertTrue("expected val10", "val10".equals(JsonPath.read(doc, "$[8].key10")));
|
assertTrue("expected val10", "val10".equals(jsonArray.query("/8/key10")));
|
||||||
assertTrue("expected val20", "val20".equals(JsonPath.read(doc, "$[8].key20")));
|
assertTrue("expected val20", "val20".equals(jsonArray.query("/8/key20")));
|
||||||
assertTrue("expected val30", "val30".equals(JsonPath.read(doc, "$[8].key30")));
|
assertTrue("expected val30", "val30".equals(jsonArray.query("/8/key30")));
|
||||||
assertTrue("expected 2 items in [9]", ((List<?>)(JsonPath.read(doc, "$[9]"))).size() == 2);
|
assertTrue("expected 2 items in [9]", ((List<?>)(JsonPath.read(doc, "$[9]"))).size() == 2);
|
||||||
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[9][0]")));
|
assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/9/0")));
|
||||||
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[9][1]")));
|
assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/9/1")));
|
||||||
assertTrue("expected 1 item in [10]", ((Map<?,?>)(JsonPath.read(doc, "$[10]"))).size() == 1);
|
assertTrue("expected 1 item in [10]", ((Map<?,?>)(JsonPath.read(doc, "$[10]"))).size() == 1);
|
||||||
assertTrue("expected v1", "v1".equals(JsonPath.read(doc, "$[10].k1")));
|
assertTrue("expected v1", "v1".equals(jsonArray.query("/10/k1")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -637,13 +646,13 @@ public class JSONArrayTest {
|
||||||
// validate JSON
|
// validate JSON
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
||||||
assertTrue("expected 7 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 7);
|
assertTrue("expected 7 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 7);
|
||||||
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]")));
|
assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/0")));
|
||||||
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]")));
|
assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/1")));
|
||||||
assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]")));
|
assertTrue("expected 3", Integer.valueOf(3).equals(jsonArray.query("/2")));
|
||||||
assertTrue("expected 4", Integer.valueOf(4).equals(JsonPath.read(doc, "$[3]")));
|
assertTrue("expected 4", Integer.valueOf(4).equals(jsonArray.query("/3")));
|
||||||
assertTrue("expected 5", Integer.valueOf(5).equals(JsonPath.read(doc, "$[4]")));
|
assertTrue("expected 5", Integer.valueOf(5).equals(jsonArray.query("/4")));
|
||||||
assertTrue("expected 6", Integer.valueOf(6).equals(JsonPath.read(doc, "$[5]")));
|
assertTrue("expected 6", Integer.valueOf(6).equals(jsonArray.query("/5")));
|
||||||
assertTrue("expected 7", Integer.valueOf(7).equals(JsonPath.read(doc, "$[6]")));
|
assertTrue("expected 7", Integer.valueOf(7).equals(jsonArray.query("/6")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -686,4 +695,19 @@ public class JSONArrayTest {
|
||||||
new Long(-1).equals(Long.parseLong((String) it.next())));
|
new Long(-1).equals(Long.parseLong((String) it.next())));
|
||||||
assertTrue("should be at end of array", !it.hasNext());
|
assertTrue("should be at end of array", !it.hasNext());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expected = JSONPointerException.class)
|
||||||
|
public void queryWithNoResult() {
|
||||||
|
new JSONArray().query("/a/b");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void optQueryWithNoResult() {
|
||||||
|
assertNull(new JSONArray().optQuery("/a/b"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void optQueryWithSyntaxError() {
|
||||||
|
new JSONArray().optQuery("invalid");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package org.json.junit;
|
package org.json.junit;
|
||||||
|
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
@ -10,16 +11,25 @@ import java.io.StringWriter;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.util.*;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.json.CDL;
|
import org.json.CDL;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
import org.json.JSONPointerException;
|
||||||
import org.json.XML;
|
import org.json.XML;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.jayway.jsonpath.*;
|
import com.jayway.jsonpath.Configuration;
|
||||||
|
import com.jayway.jsonpath.JsonPath;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JSONObject, along with JSONArray, are the central classes of the reference app.
|
* JSONObject, along with JSONArray, are the central classes of the reference app.
|
||||||
|
@ -72,10 +82,10 @@ public class JSONObjectTest {
|
||||||
JSONObject jsonObjectByName = new JSONObject(jsonObject, keys);
|
JSONObject jsonObjectByName = new JSONObject(jsonObject, keys);
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObjectByName.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObjectByName.toString());
|
||||||
assertTrue("expected 4 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 4);
|
assertTrue("expected 4 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 4);
|
||||||
assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey")));
|
assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(jsonObjectByName.query("/falseKey")));
|
||||||
assertTrue("expected \"nullKey\":null", null == JsonPath.read(doc, "$.nullKey"));
|
assertTrue("expected \"nullKey\":null", JSONObject.NULL.equals(jsonObjectByName.query("/nullKey")));
|
||||||
assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(JsonPath.read(doc, "$.stringKey")));
|
assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(jsonObjectByName.query("/stringKey")));
|
||||||
assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(JsonPath.read(doc, "$.doubleKey")));
|
assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(jsonObjectByName.query("/doubleKey")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -109,13 +119,13 @@ public class JSONObjectTest {
|
||||||
// validate JSON
|
// validate JSON
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
|
assertTrue("expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
|
||||||
assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey")));
|
assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(jsonObject.query("/trueKey")));
|
||||||
assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey")));
|
assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(jsonObject.query("/falseKey")));
|
||||||
assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(JsonPath.read(doc, "$.stringKey")));
|
assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(jsonObject.query("/stringKey")));
|
||||||
assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.escapeStringKey")));
|
assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/escapeStringKey")));
|
||||||
assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(JsonPath.read(doc, "$.doubleKey")));
|
assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(jsonObject.query("/doubleKey")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Verifies that the constructor has backwards compatability with RAW types pre-java5.
|
* Verifies that the constructor has backwards compatability with RAW types pre-java5.
|
||||||
*/
|
*/
|
||||||
|
@ -249,8 +259,8 @@ public class JSONObjectTest {
|
||||||
// validate JSON
|
// validate JSON
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
assertTrue("expected \"key2\":java.lang.Exception","java.lang.Exception".equals(JsonPath.read(doc, "$.key2")));
|
|
||||||
assertTrue("expected 0 key1 items", ((Map<?,?>)(JsonPath.read(doc, "$.key1"))).size() == 0);
|
assertTrue("expected 0 key1 items", ((Map<?,?>)(JsonPath.read(doc, "$.key1"))).size() == 0);
|
||||||
|
assertTrue("expected \"key2\":java.lang.Exception","java.lang.Exception".equals(jsonObject.query("/key2")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -272,12 +282,12 @@ public class JSONObjectTest {
|
||||||
// validate JSON
|
// validate JSON
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
|
assertTrue("expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
|
||||||
assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey")));
|
assertTrue("expected \"trueKey\":true", Boolean.TRUE.equals(jsonObject.query("/trueKey")));
|
||||||
assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey")));
|
assertTrue("expected \"falseKey\":false", Boolean.FALSE.equals(jsonObject.query("/falseKey")));
|
||||||
assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(JsonPath.read(doc, "$.stringKey")));
|
assertTrue("expected \"stringKey\":\"hello world!\"", "hello world!".equals(jsonObject.query("/stringKey")));
|
||||||
assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.escapeStringKey")));
|
assertTrue("expected \"escapeStringKey\":\"h\be\tllo w\u1234orld!\"", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/escapeStringKey")));
|
||||||
assertTrue("expected \"intKey\":42", Integer.valueOf("42").equals(JsonPath.read(doc, "$.intKey")));
|
assertTrue("expected \"intKey\":42", Long.valueOf("42").equals(jsonObject.query("/intKey")));
|
||||||
assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(JsonPath.read(doc, "$.doubleKey")));
|
assertTrue("expected \"doubleKey\":-23.45e67", Double.valueOf("-23.45e67").equals(jsonObject.query("/doubleKey")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -306,13 +316,13 @@ public class JSONObjectTest {
|
||||||
// validate JSON
|
// validate JSON
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("expected 8 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 8);
|
assertTrue("expected 8 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 8);
|
||||||
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey")));
|
|
||||||
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey")));
|
|
||||||
assertTrue("expected hello world!","hello world!".equals(JsonPath.read(doc, "$.stringKey")));
|
|
||||||
assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.escapeStringKey")));
|
|
||||||
assertTrue("expected 42", Integer.valueOf("42").equals(JsonPath.read(doc, "$.intKey")));
|
|
||||||
assertTrue("expected -23.45e7", Double.valueOf("-23.45e7").equals(JsonPath.read(doc, "$.doubleKey")));
|
|
||||||
assertTrue("expected 0 items in stringReaderKey", ((Map<?, ?>) (JsonPath.read(doc, "$.stringReaderKey"))).size() == 0);
|
assertTrue("expected 0 items in stringReaderKey", ((Map<?, ?>) (JsonPath.read(doc, "$.stringReaderKey"))).size() == 0);
|
||||||
|
assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueKey")));
|
||||||
|
assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseKey")));
|
||||||
|
assertTrue("expected hello world!","hello world!".equals(jsonObject.query("/stringKey")));
|
||||||
|
assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/escapeStringKey")));
|
||||||
|
assertTrue("expected 42", Integer.valueOf("42").equals(jsonObject.query("/intKey")));
|
||||||
|
assertTrue("expected -23.45e7", Double.valueOf("-23.45e7").equals(jsonObject.query("/doubleKey")));
|
||||||
// sorry, mockito artifact
|
// sorry, mockito artifact
|
||||||
assertTrue("expected 2 callbacks items", ((List<?>)(JsonPath.read(doc, "$.callbacks"))).size() == 2);
|
assertTrue("expected 2 callbacks items", ((List<?>)(JsonPath.read(doc, "$.callbacks"))).size() == 2);
|
||||||
assertTrue("expected 0 handler items", ((Map<?,?>)(JsonPath.read(doc, "$.callbacks[0].handler"))).size() == 0);
|
assertTrue("expected 0 handler items", ((Map<?,?>)(JsonPath.read(doc, "$.callbacks[0].handler"))).size() == 0);
|
||||||
|
@ -335,8 +345,8 @@ public class JSONObjectTest {
|
||||||
// validate JSON
|
// validate JSON
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
assertTrue("expected \"publicString\":\"abc\"", "abc".equals(JsonPath.read(doc, "$.publicString")));
|
assertTrue("expected \"publicString\":\"abc\"", "abc".equals(jsonObject.query("/publicString")));
|
||||||
assertTrue("expected \"publicInt\":42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.publicInt")));
|
assertTrue("expected \"publicInt\":42", Integer.valueOf(42).equals(jsonObject.query("/publicInt")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -353,11 +363,11 @@ public class JSONObjectTest {
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
assertTrue("expected 2 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 2);
|
||||||
assertTrue("expected 2 greetings items", ((Map<?,?>)(JsonPath.read(doc, "$.greetings"))).size() == 2);
|
assertTrue("expected 2 greetings items", ((Map<?,?>)(JsonPath.read(doc, "$.greetings"))).size() == 2);
|
||||||
assertTrue("expected \"hello\":\"Hello, \"", "Hello, ".equals(JsonPath.read(doc, "$.greetings.hello")));
|
assertTrue("expected \"hello\":\"Hello, \"", "Hello, ".equals(jsonObject.query("/greetings/hello")));
|
||||||
assertTrue("expected \"world\":\"World!\"", "World!".equals(JsonPath.read(doc, "$.greetings.world")));
|
assertTrue("expected \"world\":\"World!\"", "World!".equals(jsonObject.query("/greetings/world")));
|
||||||
assertTrue("expected 2 farewells items", ((Map<?,?>)(JsonPath.read(doc, "$.farewells"))).size() == 2);
|
assertTrue("expected 2 farewells items", ((Map<?,?>)(JsonPath.read(doc, "$.farewells"))).size() == 2);
|
||||||
assertTrue("expected \"later\":\"Later, \"", "Later, ".equals(JsonPath.read(doc, "$.farewells.later")));
|
assertTrue("expected \"later\":\"Later, \"", "Later, ".equals(jsonObject.query("/farewells/later")));
|
||||||
assertTrue("expected \"world\":\"World!\"", "Alligator!".equals(JsonPath.read(doc, "$.farewells.gator")));
|
assertTrue("expected \"world\":\"World!\"", "Alligator!".equals(jsonObject.query("/farewells/gator")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -383,12 +393,12 @@ public class JSONObjectTest {
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
|
assertTrue("expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
|
||||||
assertTrue("expected 6 myArray items", ((List<?>)(JsonPath.read(doc, "$.myArray"))).size() == 6);
|
assertTrue("expected 6 myArray items", ((List<?>)(JsonPath.read(doc, "$.myArray"))).size() == 6);
|
||||||
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.myArray[0]")));
|
assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/myArray/0")));
|
||||||
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.myArray[1]")));
|
assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/myArray/1")));
|
||||||
assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$.myArray[2]")));
|
assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/myArray/2")));
|
||||||
assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.myArray[3]")));
|
assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/myArray/3")));
|
||||||
assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.myArray[4]")));
|
assertTrue("expected 42", Integer.valueOf(42).equals(jsonObject.query("/myArray/4")));
|
||||||
assertTrue("expected -23.45e7", Double.valueOf(-23.45e7).equals(JsonPath.read(doc, "$.myArray[5]")));
|
assertTrue("expected -23.45e7", Double.valueOf(-23.45e7).equals(jsonObject.query("/myArray/5")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -413,12 +423,12 @@ public class JSONObjectTest {
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
|
assertTrue("expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
|
||||||
assertTrue("expected 6 myArray items", ((List<?>)(JsonPath.read(doc, "$.myArray"))).size() == 6);
|
assertTrue("expected 6 myArray items", ((List<?>)(JsonPath.read(doc, "$.myArray"))).size() == 6);
|
||||||
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.myArray[0]")));
|
assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/myArray/0")));
|
||||||
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.myArray[1]")));
|
assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/myArray/1/")));
|
||||||
assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$.myArray[2]")));
|
assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/myArray/2")));
|
||||||
assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc,"$.myArray[3]")));
|
assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/myArray/3")));
|
||||||
assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.myArray[4]")));
|
assertTrue("expected 42", Integer.valueOf(42).equals(jsonObject.query("/myArray/4")));
|
||||||
assertTrue("expected -23.45e7", Double.valueOf(-23.45e7).equals(JsonPath.read(doc, "$.myArray[5]")));
|
assertTrue("expected -23.45e7", Double.valueOf(-23.45e7).equals(jsonObject.query("/myArray/5")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -519,7 +529,9 @@ public class JSONObjectTest {
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void stringToValueNumbersTest() {
|
public void stringToValueNumbersTest() {
|
||||||
|
assertTrue("-0 Should be a Double!",JSONObject.stringToValue("-0") instanceof Double);
|
||||||
|
assertTrue("-0.0 Should be a Double!",JSONObject.stringToValue("-0.0") instanceof Double);
|
||||||
|
assertTrue("'-' Should be a String!",JSONObject.stringToValue("-") instanceof String);
|
||||||
assertTrue( "0.2 should be a Double!",
|
assertTrue( "0.2 should be a Double!",
|
||||||
JSONObject.stringToValue( "0.2" ) instanceof Double );
|
JSONObject.stringToValue( "0.2" ) instanceof Double );
|
||||||
assertTrue( "Doubles should be Doubles, even when incorrectly converting floats!",
|
assertTrue( "Doubles should be Doubles, even when incorrectly converting floats!",
|
||||||
|
@ -1144,11 +1156,11 @@ public class JSONObjectTest {
|
||||||
// validate JSON
|
// validate JSON
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
|
assertTrue("expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
|
||||||
assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$.keyInt")));
|
assertTrue("expected 3", Integer.valueOf(3).equals(jsonObject.query("/keyInt")));
|
||||||
assertTrue("expected 9999999993", Long.valueOf(9999999993L).equals(JsonPath.read(doc, "$.keyLong")));
|
assertTrue("expected 9999999993", Long.valueOf(9999999993L).equals(jsonObject.query("/keyLong")));
|
||||||
assertTrue("expected 3.1", Double.valueOf(3.1).equals(JsonPath.read(doc, "$.keyDouble")));
|
assertTrue("expected 3.1", Double.valueOf(3.1).equals(jsonObject.query("/keyDouble")));
|
||||||
assertTrue("expected 123456789123456789123456789123456781", new BigInteger("123456789123456789123456789123456781").equals(JsonPath.read(doc, "$.keyBigInt")));
|
assertTrue("expected 123456789123456789123456789123456781", new BigInteger("123456789123456789123456789123456781").equals(jsonObject.query("/keyBigInt")));
|
||||||
assertTrue("expected 123456789123456789123456789123456781.1", new BigDecimal("123456789123456789123456789123456781.1").equals(JsonPath.read(doc, "$.keyBigDec")));
|
assertTrue("expected 123456789123456789123456789123456781.1", new BigDecimal("123456789123456789123456789123456781.1").equals(jsonObject.query("/keyBigDec")));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Should work the same way on any platform! @see https://docs.oracle
|
* Should work the same way on any platform! @see https://docs.oracle
|
||||||
|
@ -1173,7 +1185,7 @@ public class JSONObjectTest {
|
||||||
* missing bits would not fit into the 32 bit float, i.e. the
|
* missing bits would not fit into the 32 bit float, i.e. the
|
||||||
* information needed simply is not there!
|
* information needed simply is not there!
|
||||||
*/
|
*/
|
||||||
assertTrue("expected 3.0999999046325684", Double.valueOf(3.0999999046325684).equals(JsonPath.read(doc, "$.keyFloat")));
|
assertTrue("expected 3.0999999046325684", Double.valueOf(3.0999999046325684).equals(jsonObject.query("/keyFloat")));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* float f = 3.1f; double df = (double) f; double d = 3.1d;
|
* float f = 3.1f; double df = (double) f; double d = 3.1d;
|
||||||
|
@ -1283,17 +1295,17 @@ public class JSONObjectTest {
|
||||||
// validate JSON
|
// validate JSON
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("expected 4 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 4);
|
assertTrue("expected 4 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 4);
|
||||||
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey")));
|
assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueKey")));
|
||||||
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey")));
|
assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseKey")));
|
||||||
assertTrue("expected 3 arrayKey items", ((List<?>)(JsonPath.read(doc, "$.arrayKey"))).size() == 3);
|
assertTrue("expected 3 arrayKey items", ((List<?>)(JsonPath.read(doc, "$.arrayKey"))).size() == 3);
|
||||||
assertTrue("expected 0", Integer.valueOf(0).equals(JsonPath.read(doc, "$.arrayKey[0]")));
|
assertTrue("expected 0", Integer.valueOf(0).equals(jsonObject.query("/arrayKey/0")));
|
||||||
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$.arrayKey[1]")));
|
assertTrue("expected 1", Integer.valueOf(1).equals(jsonObject.query("/arrayKey/1")));
|
||||||
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.arrayKey[2]")));
|
assertTrue("expected 2", Integer.valueOf(2).equals(jsonObject.query("/arrayKey/2")));
|
||||||
assertTrue("expected 4 objectKey items", ((Map<?,?>)(JsonPath.read(doc, "$.objectKey"))).size() == 4);
|
assertTrue("expected 4 objectKey items", ((Map<?,?>)(JsonPath.read(doc, "$.objectKey"))).size() == 4);
|
||||||
assertTrue("expected myVal1", "myVal1".equals(JsonPath.read(doc, "$.objectKey.myKey1")));
|
assertTrue("expected myVal1", "myVal1".equals(jsonObject.query("/objectKey/myKey1")));
|
||||||
assertTrue("expected myVal2", "myVal2".equals(JsonPath.read(doc, "$.objectKey.myKey2")));
|
assertTrue("expected myVal2", "myVal2".equals(jsonObject.query("/objectKey/myKey2")));
|
||||||
assertTrue("expected myVal3", "myVal3".equals(JsonPath.read(doc, "$.objectKey.myKey3")));
|
assertTrue("expected myVal3", "myVal3".equals(jsonObject.query("/objectKey/myKey3")));
|
||||||
assertTrue("expected myVal4", "myVal4".equals(JsonPath.read(doc, "$.objectKey.myKey4")));
|
assertTrue("expected myVal4", "myVal4".equals(jsonObject.query("/objectKey/myKey4")));
|
||||||
|
|
||||||
jsonObject.remove("trueKey");
|
jsonObject.remove("trueKey");
|
||||||
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
JSONObject expectedJsonObject = new JSONObject(expectedStr);
|
||||||
|
@ -1346,17 +1358,17 @@ public class JSONObjectTest {
|
||||||
// validate JSON
|
// validate JSON
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("expected 4 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 4);
|
assertTrue("expected 4 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 4);
|
||||||
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueKey")));
|
assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueKey")));
|
||||||
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseKey")));
|
assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseKey")));
|
||||||
assertTrue("expected 3 arrayKey items", ((List<?>)(JsonPath.read(doc, "$.arrayKey"))).size() == 3);
|
assertTrue("expected 3 arrayKey items", ((List<?>)(JsonPath.read(doc, "$.arrayKey"))).size() == 3);
|
||||||
assertTrue("expected 0", Integer.valueOf(0).equals(JsonPath.read(doc, "$.arrayKey[0]")));
|
assertTrue("expected 0", Integer.valueOf(0).equals(jsonObject.query("/arrayKey/0")));
|
||||||
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$.arrayKey[1]")));
|
assertTrue("expected 1", Integer.valueOf(1).equals(jsonObject.query("/arrayKey/1")));
|
||||||
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.arrayKey[2]")));
|
assertTrue("expected 2", Integer.valueOf(2).equals(jsonObject.query("/arrayKey/2")));
|
||||||
assertTrue("expected 4 objectKey items", ((Map<?,?>)(JsonPath.read(doc, "$.objectKey"))).size() == 4);
|
assertTrue("expected 4 objectKey items", ((Map<?,?>)(JsonPath.read(doc, "$.objectKey"))).size() == 4);
|
||||||
assertTrue("expected myVal1", "myVal1".equals(JsonPath.read(doc, "$.objectKey.myKey1")));
|
assertTrue("expected myVal1", "myVal1".equals(jsonObject.query("/objectKey/myKey1")));
|
||||||
assertTrue("expected myVal2", "myVal2".equals(JsonPath.read(doc, "$.objectKey.myKey2")));
|
assertTrue("expected myVal2", "myVal2".equals(jsonObject.query("/objectKey/myKey2")));
|
||||||
assertTrue("expected myVal3", "myVal3".equals(JsonPath.read(doc, "$.objectKey.myKey3")));
|
assertTrue("expected myVal3", "myVal3".equals(jsonObject.query("/objectKey/myKey3")));
|
||||||
assertTrue("expected myVal4", "myVal4".equals(JsonPath.read(doc, "$.objectKey.myKey4")));
|
assertTrue("expected myVal4", "myVal4".equals(jsonObject.query("/objectKey/myKey4")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1377,7 +1389,7 @@ public class JSONObjectTest {
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
|
assertTrue("expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
|
||||||
assertTrue("expected 1 key item", ((Map<?,?>)(JsonPath.read(doc, "$.key"))).size() == 1);
|
assertTrue("expected 1 key item", ((Map<?,?>)(JsonPath.read(doc, "$.key"))).size() == 1);
|
||||||
assertTrue("expected def", "def".equals(JsonPath.read(doc, "$.key.abc")));
|
assertTrue("expected def", "def".equals(jsonObject.query("/key/abc")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1399,7 +1411,7 @@ public class JSONObjectTest {
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
|
assertTrue("expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
|
||||||
assertTrue("expected 1 key item", ((List<?>)(JsonPath.read(doc, "$.key"))).size() == 1);
|
assertTrue("expected 1 key item", ((List<?>)(JsonPath.read(doc, "$.key"))).size() == 1);
|
||||||
assertTrue("expected abc", "abc".equals(JsonPath.read(doc, "$.key[0]")));
|
assertTrue("expected abc", "abc".equals(jsonObject.query("/key/0")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1515,9 +1527,9 @@ public class JSONObjectTest {
|
||||||
// validate JSON
|
// validate JSON
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
||||||
assertTrue("expected 3 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 3);
|
assertTrue("expected 3 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 3);
|
||||||
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]")));
|
assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/0")));
|
||||||
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]")));
|
assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/1")));
|
||||||
assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]")));
|
assertTrue("expected 3", Integer.valueOf(3).equals(jsonArray.query("/2")));
|
||||||
|
|
||||||
// wrap Array returns JSONArray
|
// wrap Array returns JSONArray
|
||||||
Integer[] array = { new Integer(1), new Integer(2), new Integer(3) };
|
Integer[] array = { new Integer(1), new Integer(2), new Integer(3) };
|
||||||
|
@ -1526,16 +1538,16 @@ public class JSONObjectTest {
|
||||||
// validate JSON
|
// validate JSON
|
||||||
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
||||||
assertTrue("expected 3 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 3);
|
assertTrue("expected 3 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 3);
|
||||||
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]")));
|
assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/0")));
|
||||||
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]")));
|
assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/1")));
|
||||||
assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]")));
|
assertTrue("expected 3", Integer.valueOf(3).equals(jsonArray.query("/2")));
|
||||||
|
|
||||||
// validate JSON
|
// validate JSON
|
||||||
doc = Configuration.defaultConfiguration().jsonProvider().parse(integerArrayJsonArray.toString());
|
doc = Configuration.defaultConfiguration().jsonProvider().parse(integerArrayJsonArray.toString());
|
||||||
assertTrue("expected 3 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 3);
|
assertTrue("expected 3 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 3);
|
||||||
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$[0]")));
|
assertTrue("expected 1", Integer.valueOf(1).equals(jsonArray.query("/0")));
|
||||||
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$[1]")));
|
assertTrue("expected 2", Integer.valueOf(2).equals(jsonArray.query("/1")));
|
||||||
assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$[2]")));
|
assertTrue("expected 3", Integer.valueOf(3).equals(jsonArray.query("/2")));
|
||||||
|
|
||||||
// wrap map returns JSONObject
|
// wrap map returns JSONObject
|
||||||
Map<String, String> map = new HashMap<String, String>();
|
Map<String, String> map = new HashMap<String, String>();
|
||||||
|
@ -1547,9 +1559,9 @@ public class JSONObjectTest {
|
||||||
// validate JSON
|
// validate JSON
|
||||||
doc = Configuration.defaultConfiguration().jsonProvider().parse(mapJsonObject.toString());
|
doc = Configuration.defaultConfiguration().jsonProvider().parse(mapJsonObject.toString());
|
||||||
assertTrue("expected 3 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 3);
|
assertTrue("expected 3 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 3);
|
||||||
assertTrue("expected val1", "val1".equals(JsonPath.read(doc, "$.key1")));
|
assertTrue("expected val1", "val1".equals(mapJsonObject.query("/key1")));
|
||||||
assertTrue("expected val2", "val2".equals(JsonPath.read(doc, "$.key2")));
|
assertTrue("expected val2", "val2".equals(mapJsonObject.query("/key2")));
|
||||||
assertTrue("expected val3", "val3".equals(JsonPath.read(doc, "$.key3")));
|
assertTrue("expected val3", "val3".equals(mapJsonObject.query("/key3")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1897,4 +1909,25 @@ public class JSONObjectTest {
|
||||||
String sNull = XML.toString(jsonObjectNull);
|
String sNull = XML.toString(jsonObjectNull);
|
||||||
assertTrue("null should emit an empty string", "".equals(sNull));
|
assertTrue("null should emit an empty string", "".equals(sNull));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expected = JSONPointerException.class)
|
||||||
|
public void queryWithNoResult() {
|
||||||
|
new JSONObject().query("/a/b");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void optQueryWithNoResult() {
|
||||||
|
assertNull(new JSONObject().optQuery("/a/b"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void optQueryWithSyntaxError() {
|
||||||
|
new JSONObject().optQuery("invalid");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = JSONException.class)
|
||||||
|
public void invalidEscapeSequence() {
|
||||||
|
String json = "{ \"\\url\": \"value\" }";
|
||||||
|
new JSONObject(json);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
212
src/test/org/json/junit/JSONPointerTest.java
Normal file
212
src/test/org/json/junit/JSONPointerTest.java
Normal file
|
@ -0,0 +1,212 @@
|
||||||
|
package org.json.junit;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import org.json.*;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class JSONPointerTest {
|
||||||
|
|
||||||
|
private static final JSONObject document;
|
||||||
|
|
||||||
|
static {
|
||||||
|
document = new JSONObject(new JSONTokener(
|
||||||
|
JSONPointerTest.class.getClassLoader().getResourceAsStream("jsonpointer-testdoc.json")));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object query(String pointer) {
|
||||||
|
return new JSONPointer(pointer).queryFrom(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void emptyPointer() {
|
||||||
|
assertSame(document, query(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = NullPointerException.class)
|
||||||
|
public void nullPointer() {
|
||||||
|
new JSONPointer((String) null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void objectPropertyQuery() {
|
||||||
|
assertSame(document.get("foo"), query("/foo"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void arrayIndexQuery() {
|
||||||
|
assertSame(document.getJSONArray("foo").get(0), query("/foo/0"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = JSONPointerException.class)
|
||||||
|
public void stringPropOfArrayFailure() {
|
||||||
|
query("/foo/bar");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void queryByEmptyKey() {
|
||||||
|
assertSame(document.get(""), query("/"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void slashEscaping() {
|
||||||
|
assertSame(document.get("a/b"), query("/a~1b"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void tildeEscaping() {
|
||||||
|
assertSame(document.get("m~n"), query("/m~0n"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void backslashEscaping() {
|
||||||
|
assertSame(document.get("i\\j"), query("/i\\\\j"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void quotationEscaping() {
|
||||||
|
assertSame(document.get("k\"l"), query("/k\\\\\\\"l"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whitespaceKey() {
|
||||||
|
assertSame(document.get(" "), query("/ "));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void uriFragmentNotation() {
|
||||||
|
assertSame(document.get("foo"), query("#/foo"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void uriFragmentPercentHandling() {
|
||||||
|
assertSame(document.get("c%d"), query("#/c%25d"));
|
||||||
|
assertSame(document.get("e^f"), query("#/e%5Ef"));
|
||||||
|
assertSame(document.get("g|h"), query("#/g%7Ch"));
|
||||||
|
assertSame(document.get("m~n"), query("#/m~0n"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void syntaxError() {
|
||||||
|
new JSONPointer("key");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = JSONPointerException.class)
|
||||||
|
public void arrayIndexFailure() {
|
||||||
|
query("/foo/2");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = JSONPointerException.class)
|
||||||
|
public void primitiveFailure() {
|
||||||
|
query("/obj/key/failure");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void builderTest() {
|
||||||
|
JSONPointer pointer = JSONPointer.builder()
|
||||||
|
.append("obj")
|
||||||
|
.append("other~key").append("another/key")
|
||||||
|
.append(0)
|
||||||
|
.build();
|
||||||
|
assertEquals("val", pointer.queryFrom(document));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = NullPointerException.class)
|
||||||
|
public void nullToken() {
|
||||||
|
JSONPointer.builder().append(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void toStringEscaping() {
|
||||||
|
JSONPointer pointer = JSONPointer.builder()
|
||||||
|
.append("obj")
|
||||||
|
.append("other~key").append("another/key")
|
||||||
|
.append("\"")
|
||||||
|
.append(0)
|
||||||
|
.build();
|
||||||
|
assertEquals("/obj/other~0key/another~1key/\\\"/0", pointer.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void emptyPointerToString() {
|
||||||
|
assertEquals("", new JSONPointer("").toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void toURIFragment() {
|
||||||
|
assertEquals("#/c%25d", new JSONPointer("/c%d").toURIFragment());
|
||||||
|
assertEquals("#/e%5Ef", new JSONPointer("/e^f").toURIFragment());
|
||||||
|
assertEquals("#/g%7Ch", new JSONPointer("/g|h").toURIFragment());
|
||||||
|
assertEquals("#/m%7En", new JSONPointer("/m~n").toURIFragment());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void tokenListIsCopiedInConstructor() {
|
||||||
|
JSONPointer.Builder b = JSONPointer.builder().append("key1");
|
||||||
|
JSONPointer jp1 = b.build();
|
||||||
|
b.append("key2");
|
||||||
|
JSONPointer jp2 = b.build();
|
||||||
|
if(jp1.toString().equals(jp2.toString())) {
|
||||||
|
fail("Oops, my pointers are sharing a backing array");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Coverage for JSONObject queryFrom()
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void queryFromJSONObject() {
|
||||||
|
String str = "{"+
|
||||||
|
"\"stringKey\":\"hello world!\","+
|
||||||
|
"\"arrayKey\":[0,1,2],"+
|
||||||
|
"\"objectKey\": {"+
|
||||||
|
"\"a\":\"aVal\","+
|
||||||
|
"\"b\":\"bVal\""+
|
||||||
|
"}"+
|
||||||
|
"}";
|
||||||
|
JSONObject jsonObject = new JSONObject(str);
|
||||||
|
Object obj = jsonObject.query("/stringKey");
|
||||||
|
assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
|
||||||
|
obj = jsonObject.query("/arrayKey/1");
|
||||||
|
assertTrue("Expected 1", Integer.valueOf(1).equals(obj));
|
||||||
|
obj = jsonObject.query("/objectKey/b");
|
||||||
|
assertTrue("Expected bVal", "bVal".equals(obj));
|
||||||
|
try {
|
||||||
|
obj = jsonObject.query("/a/b/c");
|
||||||
|
assertTrue("Expected JSONPointerException", false);
|
||||||
|
} catch (JSONPointerException e) {
|
||||||
|
assertTrue("Expected bad key/value exception",
|
||||||
|
"value [null] is not an array or object therefore its key b cannot be resolved".
|
||||||
|
equals(e.getMessage()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Coverage for JSONArray queryFrom()
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void queryFromJSONArray() {
|
||||||
|
String str = "["+
|
||||||
|
"\"hello world!\","+
|
||||||
|
"[0,1,2],"+
|
||||||
|
"{"+
|
||||||
|
"\"a\":\"aVal\","+
|
||||||
|
"\"b\":\"bVal\""+
|
||||||
|
"}"+
|
||||||
|
"]";
|
||||||
|
JSONArray jsonArray = new JSONArray(str);
|
||||||
|
Object obj = jsonArray.query("/0");
|
||||||
|
assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
|
||||||
|
obj = jsonArray.query("/1/1");
|
||||||
|
assertTrue("Expected 1", Integer.valueOf(1).equals(obj));
|
||||||
|
obj = jsonArray.query("/2/b");
|
||||||
|
assertTrue("Expected bVal", "bVal".equals(obj));
|
||||||
|
try {
|
||||||
|
obj = jsonArray.query("/a/b/c");
|
||||||
|
assertTrue("Expected JSONPointerException", false);
|
||||||
|
} catch (JSONPointerException e) {
|
||||||
|
assertTrue("Expected bad index exception",
|
||||||
|
"a is not an array index".equals(e.getMessage()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -194,13 +194,13 @@ public class JSONStringerTest {
|
||||||
// validate JSON content
|
// validate JSON content
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
|
||||||
assertTrue("expected 7 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 7);
|
assertTrue("expected 7 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 7);
|
||||||
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueValue")));
|
assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueValue")));
|
||||||
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseValue")));
|
assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseValue")));
|
||||||
assertTrue("expected null", null == JsonPath.read(doc, "$.nullValue"));
|
assertTrue("expected null", JSONObject.NULL.equals(jsonObject.query("/nullValue")));
|
||||||
assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$.stringValue")));
|
assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/stringValue")));
|
||||||
assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, "$.complexStringValue")));
|
assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/complexStringValue")));
|
||||||
assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.intValue")));
|
assertTrue("expected 42", Integer.valueOf(42).equals(jsonObject.query("/intValue")));
|
||||||
assertTrue("expected -23.45e67", Double.valueOf(-23.45e67).equals(JsonPath.read(doc, "$.doubleValue")));
|
assertTrue("expected -23.45e67", Double.valueOf(-23.45e67).equals(jsonObject.query("/doubleValue")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -224,12 +224,12 @@ public class JSONStringerTest {
|
||||||
// validate JSON content
|
// validate JSON content
|
||||||
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonArray.toString());
|
||||||
assertTrue("expected 6 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 6);
|
assertTrue("expected 6 top level items", ((List<?>)(JsonPath.read(doc, "$"))).size() == 6);
|
||||||
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$[0]")));
|
assertTrue("expected true", Boolean.TRUE.equals(jsonArray.query("/0")));
|
||||||
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$[1]")));
|
assertTrue("expected false", Boolean.FALSE.equals(jsonArray.query("/1")));
|
||||||
assertTrue("expected null", null == JsonPath.read(doc, "$[2]"));
|
assertTrue("expected null", JSONObject.NULL.equals(jsonArray.query("/2")));
|
||||||
assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$[3]")));
|
assertTrue("expected hello world!", "hello world!".equals(jsonArray.query("/3")));
|
||||||
assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$[4]")));
|
assertTrue("expected 42", Integer.valueOf(42).equals(jsonArray.query("/4")));
|
||||||
assertTrue("expected -23.45e67", Double.valueOf(-23.45e67).equals(JsonPath.read(doc, "$[5]")));
|
assertTrue("expected -23.45e67", Double.valueOf(-23.45e67).equals(jsonArray.query("/5")));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -281,27 +281,27 @@ public class JSONStringerTest {
|
||||||
assertTrue("expected 5 array1 items", ((List<?>)(JsonPath.read(doc, "$.object2.array1"))).size() == 5);
|
assertTrue("expected 5 array1 items", ((List<?>)(JsonPath.read(doc, "$.object2.array1"))).size() == 5);
|
||||||
assertTrue("expected 4 array[2] items", ((Map<?,?>)(JsonPath.read(doc, "$.object2.array1[2]"))).size() == 4);
|
assertTrue("expected 4 array[2] items", ((Map<?,?>)(JsonPath.read(doc, "$.object2.array1[2]"))).size() == 4);
|
||||||
assertTrue("expected 4 array1[2].array2 items", ((List<?>)(JsonPath.read(doc, "$.object2.array1[2].array2"))).size() == 4);
|
assertTrue("expected 4 array1[2].array2 items", ((List<?>)(JsonPath.read(doc, "$.object2.array1[2].array2"))).size() == 4);
|
||||||
assertTrue("expected true", Boolean.TRUE.equals(JsonPath.read(doc, "$.trueValue")));
|
assertTrue("expected true", Boolean.TRUE.equals(jsonObject.query("/trueValue")));
|
||||||
assertTrue("expected false", Boolean.FALSE.equals(JsonPath.read(doc, "$.falseValue")));
|
assertTrue("expected false", Boolean.FALSE.equals(jsonObject.query("/falseValue")));
|
||||||
assertTrue("expected null", null == JsonPath.read(doc, "$.nullValue"));
|
assertTrue("expected null", JSONObject.NULL.equals(jsonObject.query("/nullValue")));
|
||||||
assertTrue("expected hello world!", "hello world!".equals(JsonPath.read(doc, "$.stringValue")));
|
assertTrue("expected hello world!", "hello world!".equals(jsonObject.query("/stringValue")));
|
||||||
assertTrue("expected 42", Integer.valueOf(42).equals(JsonPath.read(doc, "$.intValue")));
|
assertTrue("expected 42", Integer.valueOf(42).equals(jsonObject.query("/intValue")));
|
||||||
assertTrue("expected -23.45e67", Double.valueOf(-23.45e67).equals(JsonPath.read(doc, "$.doubleValue")));
|
assertTrue("expected -23.45e67", Double.valueOf(-23.45e67).equals(jsonObject.query("/doubleValue")));
|
||||||
assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(JsonPath.read(doc, "$.complexStringValue")));
|
assertTrue("expected h\be\tllo w\u1234orld!", "h\be\tllo w\u1234orld!".equals(jsonObject.query("/complexStringValue")));
|
||||||
assertTrue("expected v1", "v1".equals(JsonPath.read(doc, "$.object2.k1")));
|
assertTrue("expected v1", "v1".equals(jsonObject.query("/object2/k1")));
|
||||||
assertTrue("expected v2", "v2".equals(JsonPath.read(doc, "$.object2.k2")));
|
assertTrue("expected v2", "v2".equals(jsonObject.query("/object2/k2")));
|
||||||
assertTrue("expected v3", "v3".equals(JsonPath.read(doc, "$.object2.k3")));
|
assertTrue("expected v3", "v3".equals(jsonObject.query("/object2/k3")));
|
||||||
assertTrue("expected 1", Integer.valueOf(1).equals(JsonPath.read(doc, "$.object2.array1[0]")));
|
assertTrue("expected 1", Integer.valueOf(1).equals(jsonObject.query("/object2/array1/0")));
|
||||||
assertTrue("expected 2", Integer.valueOf(2).equals(JsonPath.read(doc, "$.object2.array1[1]")));
|
assertTrue("expected 2", Integer.valueOf(2).equals(jsonObject.query("/object2/array1/1")));
|
||||||
assertTrue("expected v4", "v4".equals(JsonPath.read(doc, "$.object2.array1[2].k4")));
|
assertTrue("expected v4", "v4".equals(jsonObject.query("/object2/array1/2/k4")));
|
||||||
assertTrue("expected v5", "v5".equals(JsonPath.read(doc, "$.object2.array1[2].k5")));
|
assertTrue("expected v5", "v5".equals(jsonObject.query("/object2/array1/2/k5")));
|
||||||
assertTrue("expected v6", "v6".equals(JsonPath.read(doc, "$.object2.array1[2].k6")));
|
assertTrue("expected v6", "v6".equals(jsonObject.query("/object2/array1/2/k6")));
|
||||||
assertTrue("expected 5", Integer.valueOf(5).equals(JsonPath.read(doc, "$.object2.array1[2].array2[0]")));
|
assertTrue("expected 5", Integer.valueOf(5).equals(jsonObject.query("/object2/array1/2/array2/0")));
|
||||||
assertTrue("expected 6", Integer.valueOf(6).equals(JsonPath.read(doc, "$.object2.array1[2].array2[1]")));
|
assertTrue("expected 6", Integer.valueOf(6).equals(jsonObject.query("/object2/array1/2/array2/1")));
|
||||||
assertTrue("expected 7", Integer.valueOf(7).equals(JsonPath.read(doc, "$.object2.array1[2].array2[2]")));
|
assertTrue("expected 7", Integer.valueOf(7).equals(jsonObject.query("/object2/array1/2/array2/2")));
|
||||||
assertTrue("expected 8", Integer.valueOf(8).equals(JsonPath.read(doc, "$.object2.array1[2].array2[3]")));
|
assertTrue("expected 8", Integer.valueOf(8).equals(jsonObject.query("/object2/array1/2/array2/3")));
|
||||||
assertTrue("expected 3", Integer.valueOf(3).equals(JsonPath.read(doc, "$.object2.array1[3]")));
|
assertTrue("expected 3", Integer.valueOf(3).equals(jsonObject.query("/object2/array1/3")));
|
||||||
assertTrue("expected 4", Integer.valueOf(4).equals(JsonPath.read(doc, "$.object2.array1[4]")));
|
assertTrue("expected 4", Integer.valueOf(4).equals(jsonObject.query("/object2/array1/4")));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,8 @@ import org.junit.runners.Suite;
|
||||||
JSONStringerTest.class,
|
JSONStringerTest.class,
|
||||||
JSONObjectTest.class,
|
JSONObjectTest.class,
|
||||||
JSONArrayTest.class,
|
JSONArrayTest.class,
|
||||||
EnumTest.class
|
EnumTest.class,
|
||||||
|
JSONPointerTest.class
|
||||||
})
|
})
|
||||||
public class JunitTestSuite {
|
public class JunitTestSuite {
|
||||||
}
|
}
|
||||||
|
|
24
src/test/resources/jsonpointer-testdoc.json
Normal file
24
src/test/resources/jsonpointer-testdoc.json
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
{
|
||||||
|
"foo":
|
||||||
|
[
|
||||||
|
"bar",
|
||||||
|
"baz"
|
||||||
|
],
|
||||||
|
"": 0,
|
||||||
|
"a/b": 1,
|
||||||
|
"c%d": 2,
|
||||||
|
"e^f": 3,
|
||||||
|
"g|h": 4,
|
||||||
|
"i\\j": 5,
|
||||||
|
"k\"l": 6,
|
||||||
|
" ": 7,
|
||||||
|
"m~n": 8,
|
||||||
|
"obj" : {
|
||||||
|
"key" : "value",
|
||||||
|
"other~key" : {
|
||||||
|
"another/key" : [
|
||||||
|
"val"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue