package org.json.junit; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; 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