diff --git a/src/test/java/com/fasterxml/jackson/core/read/JsonParserTest.java b/src/test/java/com/fasterxml/jackson/core/read/JsonParserTest.java index 12d2db5c89..0f120a470b 100644 --- a/src/test/java/com/fasterxml/jackson/core/read/JsonParserTest.java +++ b/src/test/java/com/fasterxml/jackson/core/read/JsonParserTest.java @@ -461,7 +461,7 @@ public void testUtf8BOMHandling() throws Exception p = JSON_FACTORY.createParser(new MockDataInput(input)); assertEquals(JsonToken.START_ARRAY, p.nextToken()); - // same BOM, but DataInput is more restrctive so can skip but offsets + // same BOM, but DataInput is more restrictive so can skip but offsets // are not reliable... loc = p.getTokenLocation(); assertNotNull(loc); diff --git a/src/test/java/com/fasterxml/jackson/failing/LocationOffsets533Test.java b/src/test/java/com/fasterxml/jackson/failing/LocationOffsets533Test.java new file mode 100644 index 0000000000..0c9205e9f5 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/failing/LocationOffsets533Test.java @@ -0,0 +1,124 @@ +package com.fasterxml.jackson.failing; + +import com.fasterxml.jackson.core.*; + +// For checking [databind#533] +public class LocationOffsets533Test extends com.fasterxml.jackson.core.BaseTest +{ + final JsonFactory JSON_F = new JsonFactory(); + + public void testOffsetWithoutInputOffset() throws Exception + { + JsonLocation loc; + JsonParser p; + // 3 spaces before, 2 after, just for padding + byte[] b = " { } ".getBytes("UTF-8"); + + // and then peel them off + p = JSON_F.createParser(/*ObjectReadContext.empty(),*/ b); + assertToken(JsonToken.START_OBJECT, p.nextToken()); + + loc = p.getTokenLocation(); + assertEquals(3L, loc.getByteOffset()); + assertEquals(-1L, loc.getCharOffset()); + assertEquals(1, loc.getLineNr()); + assertEquals(4, loc.getColumnNr()); + + loc = p.getCurrentLocation(); + assertEquals(4L, loc.getByteOffset()); + assertEquals(-1L, loc.getCharOffset()); + assertEquals(1, loc.getLineNr()); + assertEquals(5, loc.getColumnNr()); + + p.close(); + } + + // for [core#533] + public void testUtf8Bom() throws Exception + { + JsonLocation loc; + JsonParser p; + + byte[] b = withUtf8Bom("{ }".getBytes()); + + // and then peel them off + p = JSON_F.createParser(/*ObjectReadContext.empty(),*/ b); + assertToken(JsonToken.START_OBJECT, p.nextToken()); + + loc = p.getTokenLocation(); + assertEquals(3L, loc.getByteOffset()); + assertEquals(-1L, loc.getCharOffset()); + assertEquals(1, loc.getLineNr()); + assertEquals(4, loc.getColumnNr()); + + loc = p.getCurrentLocation(); + assertEquals(4L, loc.getByteOffset()); + assertEquals(-1L, loc.getCharOffset()); + assertEquals(1, loc.getLineNr()); + assertEquals(5, loc.getColumnNr()); + + p.close(); + } + + public void testUtf8BomWithPadding() throws Exception + { + JsonLocation loc; + JsonParser p; + + byte[] b = withUtf8Bom(" { }".getBytes()); + + // and then peel them off + p = JSON_F.createParser(/*ObjectReadContext.empty(),*/ b); + assertToken(JsonToken.START_OBJECT, p.nextToken()); + + loc = p.getTokenLocation(); + assertEquals(6L, loc.getByteOffset()); + assertEquals(-1L, loc.getCharOffset()); + assertEquals(1, loc.getLineNr()); + assertEquals(7, loc.getColumnNr()); + + loc = p.getCurrentLocation(); + assertEquals(7L, loc.getByteOffset()); + assertEquals(-1L, loc.getCharOffset()); + assertEquals(1, loc.getLineNr()); + assertEquals(8, loc.getColumnNr()); + + p.close(); + } + + public void testUtf8BomWithInputOffset() throws Exception + { + JsonLocation loc; + JsonParser p; + + byte[] b = withUtf8Bom(" { }".getBytes()); + + // and then peel them off + p = JSON_F.createParser(/*ObjectReadContext.empty(),*/ b); + assertToken(JsonToken.START_OBJECT, p.nextToken()); + + loc = p.getTokenLocation(); + assertEquals(6L, loc.getByteOffset()); + assertEquals(-1L, loc.getCharOffset()); + assertEquals(1, loc.getLineNr()); + assertEquals(7, loc.getColumnNr()); + + loc = p.getCurrentLocation(); + assertEquals(7L, loc.getByteOffset()); + assertEquals(-1L, loc.getCharOffset()); + assertEquals(1, loc.getLineNr()); + assertEquals(8, loc.getColumnNr()); + + p.close(); + } + + private byte[] withUtf8Bom(byte[] bytes) { + byte[] arr = new byte[bytes.length + 3]; + // write UTF-8 BOM + arr[0] = (byte) 0xEF; + arr[1] = (byte) 0xBB; + arr[2] = (byte) 0xBF; + System.arraycopy(bytes, 0, arr, 3, bytes.length); + return arr; + } +}