|
2 | 2 |
|
3 | 3 | import com.fasterxml.jackson.core.*;
|
4 | 4 |
|
| 5 | +import java.io.IOException; |
| 6 | +import java.util.Random; |
| 7 | + |
5 | 8 | public class LocationOffsetsTest extends com.fasterxml.jackson.core.BaseTest
|
6 | 9 | {
|
7 | 10 | final JsonFactory JSON_F = new JsonFactory();
|
@@ -104,6 +107,45 @@ public void testOffsetWithoutInputOffset() throws Exception
|
104 | 107 | p.close();
|
105 | 108 | }
|
106 | 109 |
|
| 110 | + public void testWithLazyStringReadStreaming() throws Exception |
| 111 | + { |
| 112 | + _testWithLazyStringRead(MODE_READER); |
| 113 | + _testWithLazyStringRead(MODE_INPUT_STREAM); |
| 114 | + } |
| 115 | + |
| 116 | + public void testWithLazyStringReadDataInput() throws Exception |
| 117 | + { |
| 118 | + // DataInput-backed reader does not track column, so can not |
| 119 | + // verify much; but force finishToken() regardless |
| 120 | + JsonParser p = createParser(JSON_F, MODE_DATA_INPUT, "[\"text\"]"); |
| 121 | + assertToken(JsonToken.START_ARRAY, p.nextToken()); |
| 122 | + assertToken(JsonToken.VALUE_STRING, p.nextToken()); |
| 123 | + assertEquals(1, p.getCurrentLocation().getLineNr()); |
| 124 | + p.finishToken(); |
| 125 | + assertEquals("text", p.getText()); |
| 126 | + p.close(); |
| 127 | + } |
| 128 | + |
| 129 | + private void _testWithLazyStringRead(int readMode) throws Exception |
| 130 | + { |
| 131 | + JsonParser p = createParser(JSON_F, readMode, "[\"text\"]"); |
| 132 | + assertToken(JsonToken.START_ARRAY, p.nextToken()); |
| 133 | + assertToken(JsonToken.VALUE_STRING, p.nextToken()); |
| 134 | + // initially location pointing to first character |
| 135 | + assertEquals(3, p.getCurrentLocation().getColumnNr()); |
| 136 | + p.finishToken(); |
| 137 | + // but will move once we force reading |
| 138 | + assertEquals(8, p.getCurrentLocation().getColumnNr()); |
| 139 | + // and no change if we call again (but is ok to call) |
| 140 | + p.finishToken(); |
| 141 | + assertEquals(8, p.getCurrentLocation().getColumnNr()); |
| 142 | + |
| 143 | + // also just for fun, verify content |
| 144 | + assertEquals("text", p.getText()); |
| 145 | + assertEquals(8, p.getCurrentLocation().getColumnNr()); |
| 146 | + p.close(); |
| 147 | + } |
| 148 | + |
107 | 149 | // for [core#533]
|
108 | 150 | public void testUtf8Bom() throws Exception
|
109 | 151 | {
|
@@ -192,4 +234,75 @@ private byte[] withUtf8Bom(byte[] bytes) {
|
192 | 234 | System.arraycopy(bytes, 0, arr, 3, bytes.length);
|
193 | 235 | return arr;
|
194 | 236 | }
|
| 237 | + |
| 238 | + // [core#603] |
| 239 | + public void testBigPayload() throws IOException { |
| 240 | + JsonLocation loc; |
| 241 | + JsonParser p; |
| 242 | + |
| 243 | + String doc = "{\"key\":\"" + generateRandomAlpha(50000) + "\"}"; |
| 244 | + |
| 245 | + p = createParserUsingStream(JSON_F, doc, "UTF-8"); |
| 246 | + |
| 247 | + assertToken(JsonToken.START_OBJECT, p.nextToken()); |
| 248 | + loc = p.getTokenLocation(); |
| 249 | + assertEquals(0, loc.getByteOffset()); |
| 250 | + assertEquals(-1L, loc.getCharOffset()); |
| 251 | + assertEquals(1, loc.getLineNr()); |
| 252 | + assertEquals(1, loc.getColumnNr()); |
| 253 | + loc = p.getCurrentLocation(); |
| 254 | + assertEquals(1, loc.getByteOffset()); |
| 255 | + assertEquals(-1L, loc.getCharOffset()); |
| 256 | + assertEquals(1, loc.getLineNr()); |
| 257 | + assertEquals(2, loc.getColumnNr()); |
| 258 | + |
| 259 | + assertToken(JsonToken.FIELD_NAME, p.nextToken()); |
| 260 | + loc = p.getTokenLocation(); |
| 261 | + assertEquals(1, loc.getByteOffset()); |
| 262 | + assertEquals(-1L, loc.getCharOffset()); |
| 263 | + assertEquals(1, loc.getLineNr()); |
| 264 | + assertEquals(2, loc.getColumnNr()); |
| 265 | + loc = p.getCurrentLocation(); |
| 266 | + assertEquals(8, loc.getByteOffset()); |
| 267 | + assertEquals(-1L, loc.getCharOffset()); |
| 268 | + assertEquals(1, loc.getLineNr()); |
| 269 | + assertEquals(9, loc.getColumnNr()); |
| 270 | + |
| 271 | + assertToken(JsonToken.VALUE_STRING, p.nextToken()); |
| 272 | + loc = p.getTokenLocation(); |
| 273 | + assertEquals(7, loc.getByteOffset()); |
| 274 | + assertEquals(-1L, loc.getCharOffset()); |
| 275 | + assertEquals(1, loc.getLineNr()); |
| 276 | + assertEquals(8, loc.getColumnNr()); |
| 277 | + loc = p.getCurrentLocation(); |
| 278 | + assertEquals(8, loc.getByteOffset()); |
| 279 | + assertEquals(-1L, loc.getCharOffset()); |
| 280 | + assertEquals(1, loc.getLineNr()); |
| 281 | + assertEquals(9, loc.getColumnNr()); |
| 282 | + |
| 283 | + p.getTextCharacters(); |
| 284 | + loc = p.getTokenLocation(); |
| 285 | + assertEquals(7, loc.getByteOffset()); |
| 286 | + assertEquals(-1L, loc.getCharOffset()); |
| 287 | + assertEquals(1, loc.getLineNr()); |
| 288 | + assertEquals(8, loc.getColumnNr()); |
| 289 | + loc = p.getCurrentLocation(); |
| 290 | + assertEquals(doc.length() - 1, loc.getByteOffset()); |
| 291 | + assertEquals(-1L, loc.getCharOffset()); |
| 292 | + assertEquals(1, loc.getLineNr()); |
| 293 | + assertEquals(doc.length(), loc.getColumnNr()); |
| 294 | + |
| 295 | + p.close(); |
| 296 | + } |
| 297 | + |
| 298 | + private String generateRandomAlpha(int length) { |
| 299 | + StringBuilder sb = new StringBuilder(length); |
| 300 | + Random rnd = new Random(length); |
| 301 | + for (int i = 0; i < length; ++i) { |
| 302 | + // let's limit it not to include surrogate pairs: |
| 303 | + char ch = (char) ('A' + rnd.nextInt(26)); |
| 304 | + sb.append(ch); |
| 305 | + } |
| 306 | + return sb.toString(); |
| 307 | + } |
195 | 308 | }
|
0 commit comments