|
| 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