1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

more fixes for testing postition information

This commit is contained in:
John J. Aylward 2017-06-23 23:25:11 -04:00
parent 0e612ba8a4
commit af39376d92
3 changed files with 179 additions and 7 deletions

View file

@ -81,7 +81,7 @@ public class CDLTest {
fail("Expecting an exception"); fail("Expecting an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertEquals("Expecting an exception message", assertEquals("Expecting an exception message",
"Missing close quote '\"'. at 22 [character 11 line 3]", "Missing close quote '\"'. at 22 [character 11 line 2]",
e.getMessage()); e.getMessage());
} }
@ -117,7 +117,7 @@ public class CDLTest {
fail("Expecting an exception"); fail("Expecting an exception");
} catch (JSONException e) { } catch (JSONException e) {
assertEquals("Expecting an exception message", assertEquals("Expecting an exception message",
"Missing close quote '\"'. at 26 [character 15 line 3]", "Missing close quote '\"'. at 26 [character 15 line 2]",
e.getMessage()); e.getMessage());
} }
@ -168,7 +168,7 @@ public class CDLTest {
} catch (JSONException e) { } catch (JSONException e) {
System.out.println("Message" + e.getMessage()); System.out.println("Message" + e.getMessage());
assertEquals("Expecting an exception message", assertEquals("Expecting an exception message",
"Bad character 'V' (86). at 20 [character 9 line 3]", "Bad character 'V' (86). at 20 [character 9 line 2]",
e.getMessage()); e.getMessage());
} }

View file

@ -1,8 +1,18 @@
package org.json.junit; package org.json.junit;
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.json.*; import java.io.IOException;
import java.io.InputStream;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONPointer;
import org.json.JSONPointerException;
import org.json.JSONTokener;
import org.junit.Test; import org.junit.Test;
public class JSONPointerTest { public class JSONPointerTest {
@ -10,8 +20,12 @@ public class JSONPointerTest {
private static final JSONObject document; private static final JSONObject document;
static { static {
document = new JSONObject(new JSONTokener( @SuppressWarnings("resource")
JSONPointerTest.class.getClassLoader().getResourceAsStream("jsonpointer-testdoc.json"))); InputStream resourceAsStream = JSONPointerTest.class.getClassLoader().getResourceAsStream("jsonpointer-testdoc.json");
if(resourceAsStream == null) {
throw new ExceptionInInitializerError("Unable to locate test file. Please check your development environment configuration");
}
document = new JSONObject(new JSONTokener(resourceAsStream));
} }
private Object query(String pointer) { private Object query(String pointer) {

View file

@ -0,0 +1,158 @@
package org.json.junit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import org.json.JSONException;
import org.json.JSONTokener;
import org.junit.Test;
/**
* Test specific to the {@link org.json.JSONTokener} class.
* @author John Aylward
*
*/
public class JSONTokenerTest {
/**
* verify that back() fails as expected.
* @throws IOException thrown if something unexpected happens.
*/
@Test
public void verifyBackFailureZeroIndex() throws IOException {
try(Reader reader = new StringReader("some test string")) {
final JSONTokener tokener = new JSONTokener(reader);
try {
// this should fail since the index is 0;
tokener.back();
fail("Expected an exception");
} catch (JSONException e) {
assertEquals("Stepping back two steps is not supported", e.getMessage());
} catch (Exception e) {
fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage());
}
}
}
/**
* verify that back() fails as expected.
* @throws IOException thrown if something unexpected happens.
*/
@Test
public void verifyBackFailureDoubleBack() throws IOException {
try(Reader reader = new StringReader("some test string")) {
final JSONTokener tokener = new JSONTokener(reader);
tokener.next();
tokener.back();
try {
// this should fail since the index is 0;
tokener.back();
fail("Expected an exception");
} catch (JSONException e) {
assertEquals("Stepping back two steps is not supported", e.getMessage());
} catch (Exception e) {
fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage());
}
}
}
/**
* Tests the failure of the skipTo method with a buffered reader. Preferably
* we'd like this not to fail but at this time we don't have a good recovery.
*
* @throws IOException thrown if something unexpected happens.
*/
@Test
public void testSkipToFailureWithBufferedReader() throws IOException {
final byte[] superLongBuffer = new byte[1000001];
// fill our buffer
for(int i=0;i<superLongBuffer.length;i++) {
superLongBuffer[i] = 'A';
}
try(Reader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(superLongBuffer)))) {
final JSONTokener tokener = new JSONTokener(reader);
try {
// this should fail since the internal markAhead buffer is only 1,000,000
// but 'B' doesn't exist in our buffer that is 1,000,001 in size
tokener.skipTo('B');
fail("Expected exception");
} catch (JSONException e) {
assertEquals("Mark invalid", e.getMessage());
} catch (Exception e) {
fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage());
}
}
}
/**
* Tests the success of the skipTo method with a String reader.
*
* @throws IOException thrown if something unexpected happens.
*/
@Test
public void testSkipToSuccessWithStringReader() throws IOException {
final StringBuilder superLongBuffer = new StringBuilder(1000001);
// fill our buffer
for(int i=0;i<superLongBuffer.length();i++) {
superLongBuffer.append('A');
}
try(Reader reader = new StringReader(superLongBuffer.toString())) {
final JSONTokener tokener = new JSONTokener(reader);
try {
// this should not fail since the internal markAhead is ignored for StringReaders
tokener.skipTo('B');
} catch (Exception e) {
fail("Unknown Exception type " + e.getClass().getCanonicalName()+" with message "+e.getMessage());
}
}
}
/**
* Verify that next and back are working properly and tracking the correct positions
* with different new line combinations.
*/
@Test
public void testNextBackComboWithNewLines() {
final String testString = "this is\nA test\r\nWith some different\rNew Lines";
final JSONTokener tokener = new JSONTokener(testString);
tokener.skipTo('\n');
assertEquals("skipTo() improperly modifying indexes"," at 7 [character 8 line 1]", tokener.toString());
assertEquals('\n',tokener.next());
assertEquals(" at 8 [character 0 line 2]", tokener.toString());
assertEquals('A',tokener.next());
assertEquals(" at 9 [character 1 line 2]", tokener.toString());
tokener.back();
assertEquals(" at 8 [character 0 line 2]", tokener.toString());
tokener.skipTo('\r');
assertEquals("skipTo() improperly modifying indexes"," at 14 [character 6 line 2]", tokener.toString());
assertEquals('\r', tokener.next());
assertEquals(" at 15 [character 0 line 3]", tokener.toString());
assertEquals('\n', tokener.next());
assertEquals(" at 16 [character 0 line 3]", tokener.toString());
tokener.back();
assertEquals(" at 15 [character 6 line 2]", tokener.toString());
assertEquals('\n', tokener.next());
assertEquals(" at 16 [character 0 line 3]", tokener.toString());
assertEquals('W', tokener.next());
assertEquals(" at 17 [character 1 line 3]", tokener.toString());
assertEquals('i', tokener.next());
assertEquals(" at 18 [character 2 line 3]", tokener.toString());
tokener.skipTo('\r');
assertEquals("skipTo() improperly modifying indexes"," at 35 [character 19 line 3]", tokener.toString());
assertEquals('\r', tokener.next());
assertEquals(" at 36 [character 0 line 4]", tokener.toString());
tokener.back();
assertEquals(" at 35 [character 19 line 3]", tokener.toString());
assertEquals('\r', tokener.next());
assertEquals(" at 36 [character 0 line 4]", tokener.toString());
assertEquals('N', tokener.next());
assertEquals(" at 37 [character 1 line 4]", tokener.toString());
}
}