|
3 | 3 | import com.fasterxml.jackson.core.*;
|
4 | 4 | import com.fasterxml.jackson.core.json.JsonFactory;
|
5 | 5 |
|
| 6 | +import java.io.IOException; |
| 7 | +import java.util.Random; |
| 8 | + |
6 | 9 | public class LocationOffsetsTest extends com.fasterxml.jackson.core.BaseTest
|
7 | 10 | {
|
8 | 11 | final JsonFactory JSON_F = new JsonFactory();
|
@@ -143,7 +146,7 @@ private void _testWithLazyStringRead(int readMode) throws Exception
|
143 | 146 | assertEquals(8, p.getCurrentLocation().getColumnNr());
|
144 | 147 | p.close();
|
145 | 148 | }
|
146 |
| - |
| 149 | + |
147 | 150 | // for [core#533]
|
148 | 151 | public void testUtf8Bom() throws Exception
|
149 | 152 | {
|
@@ -232,4 +235,72 @@ private byte[] withUtf8Bom(byte[] bytes) {
|
232 | 235 | System.arraycopy(bytes, 0, arr, 3, bytes.length);
|
233 | 236 | return arr;
|
234 | 237 | }
|
| 238 | + |
| 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 | + |
| 296 | + private String generateRandomAlpha(int length) { |
| 297 | + StringBuilder sb = new StringBuilder(length); |
| 298 | + Random rnd = new Random(length); |
| 299 | + for (int i = 0; i < length; ++i) { |
| 300 | + // let's limit it not to include surrogate pairs: |
| 301 | + char ch = (char) ('A' + rnd.nextInt(26)); |
| 302 | + sb.append(ch); |
| 303 | + } |
| 304 | + return sb.toString(); |
| 305 | + } |
235 | 306 | }
|
0 commit comments