Skip to content

Commit 437ce10

Browse files
authored
Merge pull request #91 from johnjaylward/FixEOF
Tests for stleary/JSON-java#452
2 parents cfec288 + e7f7d34 commit 437ce10

File tree

3 files changed

+115
-21
lines changed

3 files changed

+115
-21
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class CDLTest {
3030
);
3131

3232
/**
33-
* CDL.toJSONArray() adds all values asstrings, with no filtering or
33+
* CDL.toJSONArray() adds all values as strings, with no filtering or
3434
* conversions. For testing, this means that the expected JSONObject
3535
* values all must be quoted in the cases where the JSONObject parsing
3636
* might normally convert the value into a non-string.
@@ -264,8 +264,8 @@ public void checkSpecialChars() {
264264
*/
265265
@Test
266266
public void textToJSONArray() {
267-
JSONArray jsonArray = CDL.toJSONArray(lines);
268-
JSONArray expectedJsonArray = new JSONArray(expectedLines);
267+
JSONArray jsonArray = CDL.toJSONArray(this.lines);
268+
JSONArray expectedJsonArray = new JSONArray(this.expectedLines);
269269
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
270270
}
271271

@@ -289,10 +289,10 @@ public void jsonArrayToJSONArray() {
289289
*/
290290
@Test
291291
public void textToJSONArrayAndBackToString() {
292-
JSONArray jsonArray = CDL.toJSONArray(lines);
292+
JSONArray jsonArray = CDL.toJSONArray(this.lines);
293293
String jsonStr = CDL.toString(jsonArray);
294294
JSONArray finalJsonArray = CDL.toJSONArray(jsonStr);
295-
JSONArray expectedJsonArray = new JSONArray(expectedLines);
295+
JSONArray expectedJsonArray = new JSONArray(this.expectedLines);
296296
Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
297297
}
298298

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

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
import java.io.Reader;
1313
import java.io.StringReader;
1414

15+
import org.json.JSONArray;
1516
import org.json.JSONException;
17+
import org.json.JSONObject;
1618
import org.json.JSONTokener;
1719
import org.junit.Test;
1820

@@ -64,7 +66,99 @@ public void verifyBackFailureDoubleBack() throws IOException {
6466
}
6567
}
6668
}
69+
70+
@Test
71+
public void testValid() {
72+
checkValid("0",Number.class);
73+
checkValid(" 0 ",Number.class);
74+
checkValid("23",Number.class);
75+
checkValid("23.5",Number.class);
76+
checkValid(" 23.5 ",Number.class);
77+
checkValid("null",null);
78+
checkValid(" null ",null);
79+
checkValid("true",Boolean.class);
80+
checkValid(" true\n",Boolean.class);
81+
checkValid("false",Boolean.class);
82+
checkValid("\nfalse ",Boolean.class);
83+
checkValid("{}",JSONObject.class);
84+
checkValid(" {} ",JSONObject.class);
85+
checkValid("{\"a\":1}",JSONObject.class);
86+
checkValid(" {\"a\":1} ",JSONObject.class);
87+
checkValid("[]",JSONArray.class);
88+
checkValid(" [] ",JSONArray.class);
89+
checkValid("[1,2]",JSONArray.class);
90+
checkValid("\n\n[1,2]\n\n",JSONArray.class);
91+
checkValid("1 2", String.class);
92+
}
93+
94+
@Test
95+
public void testErrors() {
96+
// Check that stream can detect that a value is found after
97+
// the first one
98+
checkError(" { \"a\":1 } 4 ");
99+
checkError("null \"a\"");
100+
checkError("{} true");
101+
}
102+
103+
private Object checkValid(String testStr, Class<?> aClass) {
104+
Object result = nextValue(testStr);
67105

106+
// Check class of object returned
107+
if( null == aClass ) {
108+
if(JSONObject.NULL.equals(result)) {
109+
// OK
110+
} else {
111+
throw new JSONException("Unexpected class: "+result.getClass().getSimpleName());
112+
}
113+
} else {
114+
if( null == result ) {
115+
throw new JSONException("Unexpected null result");
116+
} else if(!aClass.isAssignableFrom(result.getClass()) ) {
117+
throw new JSONException("Unexpected class: "+result.getClass().getSimpleName());
118+
}
119+
}
120+
121+
return result;
122+
}
123+
124+
private void checkError(String testStr) {
125+
try {
126+
nextValue(testStr);
127+
128+
fail("Error should be triggered: (\""+testStr+"\")");
129+
} catch (JSONException e) {
130+
// OK
131+
}
132+
}
133+
134+
/**
135+
* Verifies that JSONTokener can read a stream that contains a value. After
136+
* the reading is done, check that the stream is left in the correct state
137+
* by reading the characters after. All valid cases should reach end of stream.
138+
* @param testStr
139+
* @return
140+
* @throws Exception
141+
*/
142+
private Object nextValue(String testStr) throws JSONException {
143+
try(StringReader sr = new StringReader(testStr);){
144+
JSONTokener tokener = new JSONTokener(sr);
145+
146+
Object result = tokener.nextValue();
147+
148+
if( result == null ) {
149+
throw new JSONException("Unable to find value token in JSON stream: ("+tokener+"): "+testStr);
150+
}
151+
152+
char c = tokener.nextClean();
153+
if( 0 != c ) {
154+
throw new JSONException("Unexpected character found at end of JSON stream: "+c+ " ("+tokener+"): "+testStr);
155+
}
156+
157+
return result;
158+
}
159+
160+
}
161+
68162
/**
69163
* Tests the failure of the skipTo method with a buffered reader. Preferably
70164
* we'd like this not to fail but at this time we don't have a good recovery.

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
import static org.junit.Assert.assertTrue;
77
import static org.junit.Assert.fail;
88

9+
import java.io.File;
10+
import java.io.FileReader;
11+
import java.io.FileWriter;
12+
import java.io.IOException;
13+
import java.io.Reader;
14+
import java.io.StringReader;
15+
916
import org.json.JSONArray;
1017
import org.json.JSONException;
1118
import org.json.JSONObject;
@@ -743,14 +750,10 @@ private void compareStringToJSONObject(String xmlStr, String expectedStr) {
743750
* @param expectedStr the expected JSON string
744751
*/
745752
private void compareReaderToJSONObject(String xmlStr, String expectedStr) {
746-
/*
747-
* Commenting out this method until the JSON-java code is updated
748-
* to support XML.toJSONObject(reader)
749753
JSONObject expectedJsonObject = new JSONObject(expectedStr);
750754
Reader reader = new StringReader(xmlStr);
751755
JSONObject jsonObject = XML.toJSONObject(reader);
752756
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
753-
*/
754757
}
755758

756759
/**
@@ -764,22 +767,19 @@ private void compareReaderToJSONObject(String xmlStr, String expectedStr) {
764767
* @throws IOException
765768
*/
766769
private void compareFileToJSONObject(String xmlStr, String expectedStr) {
767-
/*
768-
* Commenting out this method until the JSON-java code is updated
769-
* to support XML.toJSONObject(reader)
770770
try {
771771
JSONObject expectedJsonObject = new JSONObject(expectedStr);
772-
File tempFile = testFolder.newFile("fileToJSONObject.xml");
773-
FileWriter fileWriter = new FileWriter(tempFile);
774-
fileWriter.write(xmlStr);
775-
fileWriter.close();
776-
Reader reader = new FileReader(tempFile);
777-
JSONObject jsonObject = XML.toJSONObject(reader);
778-
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
772+
File tempFile = this.testFolder.newFile("fileToJSONObject.xml");
773+
try(FileWriter fileWriter = new FileWriter(tempFile);){
774+
fileWriter.write(xmlStr);
775+
}
776+
try(Reader reader = new FileReader(tempFile);){
777+
JSONObject jsonObject = XML.toJSONObject(reader);
778+
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
779+
}
779780
} catch (IOException e) {
780-
assertTrue("file writer error: " +e.getMessage(), false);
781+
fail("file writer error: " +e.getMessage());
781782
}
782-
*/
783783
}
784784

785785
/**

0 commit comments

Comments
 (0)