diff --git a/src/test/java/org/json/junit/CDLTest.java b/src/test/java/org/json/junit/CDLTest.java index b99e17c..b1f9561 100644 --- a/src/test/java/org/json/junit/CDLTest.java +++ b/src/test/java/org/json/junit/CDLTest.java @@ -81,7 +81,7 @@ public class CDLTest { fail("Expecting an exception"); } catch (JSONException e) { 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()); } @@ -117,7 +117,7 @@ public class CDLTest { fail("Expecting an exception"); } catch (JSONException e) { 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()); } @@ -168,7 +168,7 @@ public class CDLTest { } catch (JSONException e) { System.out.println("Message" + e.getMessage()); 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()); } diff --git a/src/test/java/org/json/junit/JSONPointerTest.java b/src/test/java/org/json/junit/JSONPointerTest.java index 0904b9e..19dac47 100644 --- a/src/test/java/org/json/junit/JSONPointerTest.java +++ b/src/test/java/org/json/junit/JSONPointerTest.java @@ -1,8 +1,18 @@ 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; public class JSONPointerTest { @@ -10,8 +20,12 @@ public class JSONPointerTest { private static final JSONObject document; static { - document = new JSONObject(new JSONTokener( - JSONPointerTest.class.getClassLoader().getResourceAsStream("jsonpointer-testdoc.json"))); + @SuppressWarnings("resource") + 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) { diff --git a/src/test/java/org/json/junit/JSONTokenerTest.java b/src/test/java/org/json/junit/JSONTokenerTest.java new file mode 100644 index 0000000..5fe2259 --- /dev/null +++ b/src/test/java/org/json/junit/JSONTokenerTest.java @@ -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