Skip to content

Commit af39376

Browse files
author
John J. Aylward
committed
more fixes for testing postition information
1 parent 0e612ba commit af39376

File tree

3 files changed

+179
-7
lines changed

3 files changed

+179
-7
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void unbalancedQuoteInValue() {
8181
fail("Expecting an exception");
8282
} catch (JSONException e) {
8383
assertEquals("Expecting an exception message",
84-
"Missing close quote '\"'. at 22 [character 11 line 3]",
84+
"Missing close quote '\"'. at 22 [character 11 line 2]",
8585
e.getMessage());
8686

8787
}
@@ -117,7 +117,7 @@ public void unbalancedEscapedQuote(){
117117
fail("Expecting an exception");
118118
} catch (JSONException e) {
119119
assertEquals("Expecting an exception message",
120-
"Missing close quote '\"'. at 26 [character 15 line 3]",
120+
"Missing close quote '\"'. at 26 [character 15 line 2]",
121121
e.getMessage());
122122

123123
}
@@ -168,7 +168,7 @@ public void badEscapedQuote(){
168168
} catch (JSONException e) {
169169
System.out.println("Message" + e.getMessage());
170170
assertEquals("Expecting an exception message",
171-
"Bad character 'V' (86). at 20 [character 9 line 3]",
171+
"Bad character 'V' (86). at 20 [character 9 line 2]",
172172
e.getMessage());
173173

174174
}

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
package org.json.junit;
22

3-
import static org.junit.Assert.*;
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertSame;
5+
import static org.junit.Assert.assertTrue;
6+
import static org.junit.Assert.fail;
47

5-
import org.json.*;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
11+
import org.json.JSONArray;
12+
import org.json.JSONObject;
13+
import org.json.JSONPointer;
14+
import org.json.JSONPointerException;
15+
import org.json.JSONTokener;
616
import org.junit.Test;
717

818
public class JSONPointerTest {
919

1020
private static final JSONObject document;
1121

1222
static {
13-
document = new JSONObject(new JSONTokener(
14-
JSONPointerTest.class.getClassLoader().getResourceAsStream("jsonpointer-testdoc.json")));
23+
@SuppressWarnings("resource")
24+
InputStream resourceAsStream = JSONPointerTest.class.getClassLoader().getResourceAsStream("jsonpointer-testdoc.json");
25+
if(resourceAsStream == null) {
26+
throw new ExceptionInInitializerError("Unable to locate test file. Please check your development environment configuration");
27+
}
28+
document = new JSONObject(new JSONTokener(resourceAsStream));
1529
}
1630

1731
private Object query(String pointer) {
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package org.json.junit;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.fail;
5+
6+
import java.io.BufferedReader;
7+
import java.io.ByteArrayInputStream;
8+
import java.io.IOException;
9+
import java.io.InputStreamReader;
10+
import java.io.Reader;
11+
import java.io.StringReader;
12+
13+
import org.json.JSONException;
14+
import org.json.JSONTokener;
15+
import org.junit.Test;
16+
17+
/**
18+
* Test specific to the {@link org.json.JSONTokener} class.
19+
* @author John Aylward
20+
*
21+
*/
22+
public class JSONTokenerTest {
23+
24+
/**
25+
* verify that back() fails as expected.
26+
* @throws IOException thrown if something unexpected happens.
27+
*/
28+
@Test
29+
public void verifyBackFailureZeroIndex() throws IOException {
30+
try(Reader reader = new StringReader("some test string")) {
31+
final JSONTokener tokener = new JSONTokener(reader);
32+
try {
33+
// this should fail since the index is 0;
34+
tokener.back();
35+
fail("Expected an exception");
36+
} catch (JSONException e) {
37+
assertEquals("Stepping back two steps is not supported", e.getMessage());
38+
} catch (Exception e) {
39+
fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage());
40+
}
41+
42+
}
43+
}
44+
/**
45+
* verify that back() fails as expected.
46+
* @throws IOException thrown if something unexpected happens.
47+
*/
48+
@Test
49+
public void verifyBackFailureDoubleBack() throws IOException {
50+
try(Reader reader = new StringReader("some test string")) {
51+
final JSONTokener tokener = new JSONTokener(reader);
52+
tokener.next();
53+
tokener.back();
54+
try {
55+
// this should fail since the index is 0;
56+
tokener.back();
57+
fail("Expected an exception");
58+
} catch (JSONException e) {
59+
assertEquals("Stepping back two steps is not supported", e.getMessage());
60+
} catch (Exception e) {
61+
fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage());
62+
}
63+
}
64+
}
65+
66+
/**
67+
* Tests the failure of the skipTo method with a buffered reader. Preferably
68+
* we'd like this not to fail but at this time we don't have a good recovery.
69+
*
70+
* @throws IOException thrown if something unexpected happens.
71+
*/
72+
@Test
73+
public void testSkipToFailureWithBufferedReader() throws IOException {
74+
final byte[] superLongBuffer = new byte[1000001];
75+
// fill our buffer
76+
for(int i=0;i<superLongBuffer.length;i++) {
77+
superLongBuffer[i] = 'A';
78+
}
79+
try(Reader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(superLongBuffer)))) {
80+
final JSONTokener tokener = new JSONTokener(reader);
81+
try {
82+
// this should fail since the internal markAhead buffer is only 1,000,000
83+
// but 'B' doesn't exist in our buffer that is 1,000,001 in size
84+
tokener.skipTo('B');
85+
fail("Expected exception");
86+
} catch (JSONException e) {
87+
assertEquals("Mark invalid", e.getMessage());
88+
} catch (Exception e) {
89+
fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage());
90+
}
91+
}
92+
}
93+
94+
/**
95+
* Tests the success of the skipTo method with a String reader.
96+
*
97+
* @throws IOException thrown if something unexpected happens.
98+
*/
99+
@Test
100+
public void testSkipToSuccessWithStringReader() throws IOException {
101+
final StringBuilder superLongBuffer = new StringBuilder(1000001);
102+
// fill our buffer
103+
for(int i=0;i<superLongBuffer.length();i++) {
104+
superLongBuffer.append('A');
105+
}
106+
try(Reader reader = new StringReader(superLongBuffer.toString())) {
107+
final JSONTokener tokener = new JSONTokener(reader);
108+
try {
109+
// this should not fail since the internal markAhead is ignored for StringReaders
110+
tokener.skipTo('B');
111+
} catch (Exception e) {
112+
fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage());
113+
}
114+
}
115+
}
116+
117+
/**
118+
* Verify that next and back are working properly and tracking the correct positions
119+
* with different new line combinations.
120+
*/
121+
@Test
122+
public void testNextBackComboWithNewLines() {
123+
final String testString = "this is\nA test\r\nWith some different\rNew Lines";
124+
final JSONTokener tokener = new JSONTokener(testString);
125+
tokener.skipTo('\n');
126+
assertEquals("skipTo() improperly modifying indexes"," at 7 [character 8 line 1]", tokener.toString());
127+
assertEquals('\n',tokener.next());
128+
assertEquals(" at 8 [character 0 line 2]", tokener.toString());
129+
assertEquals('A',tokener.next());
130+
assertEquals(" at 9 [character 1 line 2]", tokener.toString());
131+
tokener.back();
132+
assertEquals(" at 8 [character 0 line 2]", tokener.toString());
133+
tokener.skipTo('\r');
134+
assertEquals("skipTo() improperly modifying indexes"," at 14 [character 6 line 2]", tokener.toString());
135+
assertEquals('\r', tokener.next());
136+
assertEquals(" at 15 [character 0 line 3]", tokener.toString());
137+
assertEquals('\n', tokener.next());
138+
assertEquals(" at 16 [character 0 line 3]", tokener.toString());
139+
tokener.back();
140+
assertEquals(" at 15 [character 6 line 2]", tokener.toString());
141+
assertEquals('\n', tokener.next());
142+
assertEquals(" at 16 [character 0 line 3]", tokener.toString());
143+
assertEquals('W', tokener.next());
144+
assertEquals(" at 17 [character 1 line 3]", tokener.toString());
145+
assertEquals('i', tokener.next());
146+
assertEquals(" at 18 [character 2 line 3]", tokener.toString());
147+
tokener.skipTo('\r');
148+
assertEquals("skipTo() improperly modifying indexes"," at 35 [character 19 line 3]", tokener.toString());
149+
assertEquals('\r', tokener.next());
150+
assertEquals(" at 36 [character 0 line 4]", tokener.toString());
151+
tokener.back();
152+
assertEquals(" at 35 [character 19 line 3]", tokener.toString());
153+
assertEquals('\r', tokener.next());
154+
assertEquals(" at 36 [character 0 line 4]", tokener.toString());
155+
assertEquals('N', tokener.next());
156+
assertEquals(" at 37 [character 1 line 4]", tokener.toString());
157+
}
158+
}

0 commit comments

Comments
 (0)