Skip to content

Commit 0e0f3f2

Browse files
authored
Merge pull request #1 from stleary/master
update from origin
2 parents d3b197b + 40f170b commit 0e0f3f2

12 files changed

Lines changed: 642 additions & 276 deletions

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/bin/
2+
build
3+
.classpath
4+
.project
5+
.settings/

README.md

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@ Unit tests to validate the JSON-Java GitHub project code<br>
44

55
https://github.com/douglascrockford/JSON-java<br>
66

7-
*These tests are a work in progress. Help from interested developers is welcome.*<br>
8-
More coverage is needed, but more importantly, improvements to test quality is needed.<br>
9-
10-
Eclipse is the recommended development environment.<br>
7+
Gradle and Eclipse is the recommended build tool and IDE.<br>
118
Run individual tests or <b>JunitTestSuite</b> using <b>EclEmma Coverage</b>, or execute the **TestRunner** application directly.<br>
129

13-
**You will need the following libraries for testing:**<br>
10+
**The following libraries are required:**<br>
1411
* asm-1.0.2.jar<br>
1512
* commons-io-2.1.jar<br>
1613
* commons-lang-2.6.jar<br>
@@ -23,40 +20,33 @@ Run individual tests or <b>JunitTestSuite</b> using <b>EclEmma Coverage</b>, or
2320
* slf-simple-1.7.12.jar<br>
2421
* JSON-java.jar<br>
2522

26-
**To build from the command line using gradle:**
23+
**To build from the command line using gradle:**<br>
24+
Until the unit tests are merged into the JSON-Java project, the code has to be wired by hand. <br>
25+
\# In an empty directory of your choice, clone JSON-Java-unit-test:<br>
2726
````
28-
build.gradle
29-
# In this example, both the JSON-java jar and the test code is created
30-
# from the same build file, in the test code directory. 3rd party jars are
31-
# obtained from the maven repository.
32-
apply plugin: 'java'
33-
jar.baseName = 'JSON-java'
34-
35-
sourceSets {
36-
main {
37-
java {
38-
srcDir '../JSON-java/src/org/json'
39-
}
40-
}
41-
test {
42-
java {
43-
srcDir 'src/org/json/junit'
44-
}
45-
}
46-
}
47-
48-
repositories {
49-
mavenCentral()
50-
}
51-
52-
dependencies {
53-
testCompile group: 'junit', name: 'junit', version: '4.+'
54-
testCompile group: 'com.jayway.jsonpath', name: 'json-path', version: '2.1.0'
55-
testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.5'
56-
}
27+
git clone https://github.com/stleary/JSON-Java-unit-test.git .
5728
````
58-
59-
To measure coverage: http://www.eclemma.org/ (just install the latest in Eclipse)<br>
29+
\# Create a directory structure for the JSON-Java code
30+
````
31+
# Windows version
32+
md /s src\org\json
33+
````
34+
\# clone JSON-Java
35+
````
36+
git clone https://github.com/stleary/JSON-Java.git src\org\json
37+
````
38+
\# Build, then execute the unit tests and code coverage
39+
````
40+
gradle clean build test jacocoTestReport
41+
````
42+
Unit test results will be in build\reports\tests\index.html<br>
43+
Code coverage will be in build\reports\jacoco\html\index.html
44+
45+
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>
46+
Select File > Import > Gradle > Gradle project <br>
47+
Browse to the directory where you cloned JSON-Java-unit-test<br>
48+
Select Build model<br>
49+
Select built project<br>
6050

6151
<b>Conventions</b><br>
6252
Test filenames should consist of the name of the module being tested, with the suffix "Test".

build.gradle

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,48 @@
11
apply plugin: 'java'
22
apply plugin: 'eclipse'
3-
4-
jar.baseName = 'JSON-java'
3+
apply plugin: 'jacoco'
54

65
sourceSets {
7-
test {
8-
java {
9-
srcDir 'src/test'
6+
// Uncomment main if you have merged JSON-Java and JSON-Java-unit-test code
7+
main {
8+
java {
9+
srcDir 'src'
10+
exclude 'test/'
11+
}
12+
}
13+
test {
14+
java {
15+
srcDir 'src/test'
16+
exclude 'resources/'
17+
}
18+
resources {
19+
srcDir 'resources'
20+
}
1021
}
11-
}
1222
}
1323

1424
repositories {
15-
mavenCentral()
25+
mavenCentral()
1626
}
1727

1828
dependencies {
19-
testCompile group: 'junit', name: 'junit', version: '4.+'
20-
testCompile group: 'com.jayway.jsonpath', name: 'json-path', version: '2.1.0'
21-
testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.5'
22-
// Use this line if you are testing a JSON-Java release.
23-
// Otherwise add an external jar from your local repository in Eclipse
24-
// (The gradle build won't work unless you add a main sourceSets entry and a jar.baseName entry
25-
// testCompile group: 'org.json', name: 'json', version: '20151123'
29+
testCompile group: 'junit', name: 'junit', version: '4.+'
30+
testCompile group: 'com.jayway.jsonpath', name: 'json-path', version: '2.1.0'
31+
testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.5'
32+
// Uncomment if you are testing against a JSON-Java release
33+
// testCompile 'org.json:json:20160212'
34+
// Uncomment if you have copied a local JSON-Java jar file into this project
35+
// testCompile files('./JSON-Java.jar')
2636
}
37+
38+
test { finalizedBy jacocoTestReport }
39+
jacocoTestReport{
40+
additionalSourceDirs = files(sourceSets.main.allJava.srcDirs)
41+
reports {
42+
xml.enabled false
43+
csv.enabled false
44+
html.destination "${buildDir}/reports/jacoco/html"
45+
}
46+
executionData = files('build/jacoco/test.exec')
47+
}
48+

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

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void unbalancedQuoteInName() {
6868
equals(e.getMessage()));
6969
}
7070
}
71-
71+
7272
/**
7373
* Attempts to create a JSONArray from a string with unbalanced quotes
7474
* in value line. Expects a JSONException.
@@ -104,7 +104,62 @@ public void nullInName() {
104104

105105
}
106106
}
107+
108+
/**
109+
* Attempt to create a JSONArray with unbalanced quotes and a properly escaped doubled quote.
110+
* Expects a JSONException.
111+
*/
112+
@Test
113+
public void unbalancedEscapedQuote(){
114+
String badLine = "Col1, Col2\n\"Val1, \"\"Val2\"\"";
115+
try {
116+
CDL.toJSONArray(badLine);
117+
assertTrue("Expecting an exception", false);
118+
} catch (JSONException e) {
119+
assertTrue("Expecting an exception message",
120+
"Missing close quote '\"'. at 27 [character 16 line 3]".
121+
equals(e.getMessage()));
122+
123+
}
124+
}
107125

126+
/**
127+
* Assert that there is no error for a single escaped quote within a properly embedded quote.
128+
*/
129+
@Test
130+
public void singleEscapedQuote(){
131+
String singleEscape = "Col1, Col2\nVal1, \"\"\"Val2\"";
132+
JSONArray jsonArray = CDL.toJSONArray(singleEscape);
133+
134+
String cdlStr = CDL.toString(jsonArray);
135+
assertTrue(cdlStr.contains("Col1"));
136+
assertTrue(cdlStr.contains("Col2"));
137+
assertTrue(cdlStr.contains("Val1"));
138+
assertTrue(cdlStr.contains("\"Val2"));
139+
140+
}
141+
142+
/**
143+
* Attempt to create a JSONArray with an escape quote and no enclosing quotes.
144+
* Expects a JSONException.
145+
*/
146+
@Test
147+
public void badEscapedQuote(){
148+
String badLine = "Col1, Col2\nVal1, \"\"Val2";
149+
150+
try {
151+
CDL.toJSONArray(badLine);
152+
assertTrue("Expecting an exception", false);
153+
} catch (JSONException e) {
154+
System.out.println("Message" + e.getMessage());
155+
assertTrue("Expecting an exception message",
156+
"Bad character 'V' (86). at 20 [character 9 line 3]".
157+
equals(e.getMessage()));
158+
159+
}
160+
161+
}
162+
108163
/**
109164
* call toString with a null array
110165
*/

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void simpleCookieList() {
7878
// validate JSON content
7979
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
8080
assertTrue("Expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
81-
assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(JsonPath.read(doc, "$.SID")));
81+
assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(jsonObject.query("/SID")));
8282
}
8383

8484
/**
@@ -91,7 +91,7 @@ public void simpleCookieListWithDelimiter() {
9191
// validate JSON content
9292
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
9393
assertTrue("Expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
94-
assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(JsonPath.read(doc, "$.SID")));
94+
assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(jsonObject.query("/SID")));
9595
}
9696

9797
/**
@@ -111,12 +111,12 @@ public void multiPartCookieList() {
111111
// validate JSON content
112112
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
113113
assertTrue("Expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
114-
assertTrue("expected myCookieValue1", "myCookieValue1".equals(JsonPath.read(doc, "$.name1")));
115-
assertTrue("expected myCookieValue2", "myCookieValue2".equals(JsonPath.read(doc, "$.name2")));
116-
assertTrue("expected myCookieValue3", "myCookieValue3".equals(JsonPath.read(doc, "$.name3")));
117-
assertTrue("expected myCookieValue4", "myCookieValue4".equals(JsonPath.read(doc, "$.name4")));
118-
assertTrue("expected myCookieValue5", "myCookieValue5".equals(JsonPath.read(doc, "$.name5")));
119-
assertTrue("expected myCookieValue6", "myCookieValue6".equals(JsonPath.read(doc, "$.name6")));
114+
assertTrue("expected myCookieValue1", "myCookieValue1".equals(jsonObject.query("/name1")));
115+
assertTrue("expected myCookieValue2", "myCookieValue2".equals(jsonObject.query("/name2")));
116+
assertTrue("expected myCookieValue3", "myCookieValue3".equals(jsonObject.query("/name3")));
117+
assertTrue("expected myCookieValue4", "myCookieValue4".equals(jsonObject.query("/name4")));
118+
assertTrue("expected myCookieValue5", "myCookieValue5".equals(jsonObject.query("/name5")));
119+
assertTrue("expected myCookieValue6", "myCookieValue6".equals(jsonObject.query("/name6")));
120120
}
121121

122122
/**
@@ -151,12 +151,12 @@ public void convertCookieListToString() {
151151
// validate JSON content
152152
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
153153
assertTrue("Expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
154-
assertTrue("expected myCookieValue1", "myCookieValue1".equals(JsonPath.read(doc, "$.name1")));
155-
assertTrue("expected myCookieValue2", "myCookieValue2".equals(JsonPath.read(doc, "$.name2")));
156-
assertTrue("expected myCookieValue3", "myCookieValue3".equals(JsonPath.read(doc, "$.name3")));
157-
assertTrue("expected myCookieValue4", "myCookieValue4".equals(JsonPath.read(doc, "$.name4")));
158-
assertTrue("expected myCookieValue5", "myCookieValue5".equals(JsonPath.read(doc, "$.name5")));
159-
assertTrue("expected myCookieValue6", "myCookieValue6".equals(JsonPath.read(doc, "$.name6")));
154+
assertTrue("expected myCookieValue1", "myCookieValue1".equals(jsonObject.query("/name1")));
155+
assertTrue("expected myCookieValue2", "myCookieValue2".equals(jsonObject.query("/name2")));
156+
assertTrue("expected myCookieValue3", "myCookieValue3".equals(jsonObject.query("/name3")));
157+
assertTrue("expected myCookieValue4", "myCookieValue4".equals(jsonObject.query("/name4")));
158+
assertTrue("expected myCookieValue5", "myCookieValue5".equals(jsonObject.query("/name5")));
159+
assertTrue("expected myCookieValue6", "myCookieValue6".equals(jsonObject.query("/name6")));
160160
}
161161

162162
/**
@@ -176,11 +176,11 @@ public void convertEncodedCookieListToString() {
176176
// validate JSON content
177177
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
178178
assertTrue("Expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
179-
assertTrue("expected myCookieValue1", "myCookieValue1".equals(JsonPath.read(doc, "$.name1")));
180-
assertTrue("expected my Cookie Value 2", "my Cookie Value 2".equals(JsonPath.read(doc, "$.name2")));
181-
assertTrue("expected my+Cookie&Value;3=", "my+Cookie&Value;3=".equals(JsonPath.read(doc, "$.name3")));
182-
assertTrue("expected my%CookieValue4", "my%CookieValue4".equals(JsonPath.read(doc, "$.name4")));
183-
assertTrue("expected my%CookieValue5", "myCookieValue5".equals(JsonPath.read(doc, "$.name5")));
184-
assertTrue("expected myCookieValue6", "myCookieValue6".equals(JsonPath.read(doc, "$.name6")));
179+
assertTrue("expected myCookieValue1", "myCookieValue1".equals(jsonObject.query("/name1")));
180+
assertTrue("expected my Cookie Value 2", "my Cookie Value 2".equals(jsonObject.query("/name2")));
181+
assertTrue("expected my+Cookie&Value;3=", "my+Cookie&Value;3=".equals(jsonObject.query("/name3")));
182+
assertTrue("expected my%CookieValue4", "my%CookieValue4".equals(jsonObject.query("/name4")));
183+
assertTrue("expected my%CookieValue5", "myCookieValue5".equals(jsonObject.query("/name5")));
184+
assertTrue("expected myCookieValue6", "myCookieValue6".equals(jsonObject.query("/name6")));
185185
}
186186
}

0 commit comments

Comments
 (0)