|
| 1 | +package com.fasterxml.jackson.failing; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.*; |
| 4 | + |
| 5 | +// For checking [databind#533] |
| 6 | +public class LocationOffsets533Test extends com.fasterxml.jackson.core.BaseTest |
| 7 | +{ |
| 8 | + final JsonFactory JSON_F = new JsonFactory(); |
| 9 | + |
| 10 | + public void testOffsetWithoutInputOffset() throws Exception |
| 11 | + { |
| 12 | + JsonLocation loc; |
| 13 | + JsonParser p; |
| 14 | + // 3 spaces before, 2 after, just for padding |
| 15 | + byte[] b = " { } ".getBytes("UTF-8"); |
| 16 | + |
| 17 | + // and then peel them off |
| 18 | + p = JSON_F.createParser(/*ObjectReadContext.empty(),*/ b); |
| 19 | + assertToken(JsonToken.START_OBJECT, p.nextToken()); |
| 20 | + |
| 21 | + loc = p.getTokenLocation(); |
| 22 | + assertEquals(3L, loc.getByteOffset()); |
| 23 | + assertEquals(-1L, loc.getCharOffset()); |
| 24 | + assertEquals(1, loc.getLineNr()); |
| 25 | + assertEquals(4, loc.getColumnNr()); |
| 26 | + |
| 27 | + loc = p.getCurrentLocation(); |
| 28 | + assertEquals(4L, loc.getByteOffset()); |
| 29 | + assertEquals(-1L, loc.getCharOffset()); |
| 30 | + assertEquals(1, loc.getLineNr()); |
| 31 | + assertEquals(5, loc.getColumnNr()); |
| 32 | + |
| 33 | + p.close(); |
| 34 | + } |
| 35 | + |
| 36 | + // for [core#533] |
| 37 | + public void testUtf8Bom() throws Exception |
| 38 | + { |
| 39 | + JsonLocation loc; |
| 40 | + JsonParser p; |
| 41 | + |
| 42 | + byte[] b = withUtf8Bom("{ }".getBytes()); |
| 43 | + |
| 44 | + // and then peel them off |
| 45 | + p = JSON_F.createParser(/*ObjectReadContext.empty(),*/ b); |
| 46 | + assertToken(JsonToken.START_OBJECT, p.nextToken()); |
| 47 | + |
| 48 | + loc = p.getTokenLocation(); |
| 49 | + assertEquals(3L, loc.getByteOffset()); |
| 50 | + assertEquals(-1L, loc.getCharOffset()); |
| 51 | + assertEquals(1, loc.getLineNr()); |
| 52 | + assertEquals(4, loc.getColumnNr()); |
| 53 | + |
| 54 | + loc = p.getCurrentLocation(); |
| 55 | + assertEquals(4L, loc.getByteOffset()); |
| 56 | + assertEquals(-1L, loc.getCharOffset()); |
| 57 | + assertEquals(1, loc.getLineNr()); |
| 58 | + assertEquals(5, loc.getColumnNr()); |
| 59 | + |
| 60 | + p.close(); |
| 61 | + } |
| 62 | + |
| 63 | + public void testUtf8BomWithPadding() throws Exception |
| 64 | + { |
| 65 | + JsonLocation loc; |
| 66 | + JsonParser p; |
| 67 | + |
| 68 | + byte[] b = withUtf8Bom(" { }".getBytes()); |
| 69 | + |
| 70 | + // and then peel them off |
| 71 | + p = JSON_F.createParser(/*ObjectReadContext.empty(),*/ b); |
| 72 | + assertToken(JsonToken.START_OBJECT, p.nextToken()); |
| 73 | + |
| 74 | + loc = p.getTokenLocation(); |
| 75 | + assertEquals(6L, loc.getByteOffset()); |
| 76 | + assertEquals(-1L, loc.getCharOffset()); |
| 77 | + assertEquals(1, loc.getLineNr()); |
| 78 | + assertEquals(7, loc.getColumnNr()); |
| 79 | + |
| 80 | + loc = p.getCurrentLocation(); |
| 81 | + assertEquals(7L, loc.getByteOffset()); |
| 82 | + assertEquals(-1L, loc.getCharOffset()); |
| 83 | + assertEquals(1, loc.getLineNr()); |
| 84 | + assertEquals(8, loc.getColumnNr()); |
| 85 | + |
| 86 | + p.close(); |
| 87 | + } |
| 88 | + |
| 89 | + public void testUtf8BomWithInputOffset() throws Exception |
| 90 | + { |
| 91 | + JsonLocation loc; |
| 92 | + JsonParser p; |
| 93 | + |
| 94 | + byte[] b = withUtf8Bom(" { }".getBytes()); |
| 95 | + |
| 96 | + // and then peel them off |
| 97 | + p = JSON_F.createParser(/*ObjectReadContext.empty(),*/ b); |
| 98 | + assertToken(JsonToken.START_OBJECT, p.nextToken()); |
| 99 | + |
| 100 | + loc = p.getTokenLocation(); |
| 101 | + assertEquals(6L, loc.getByteOffset()); |
| 102 | + assertEquals(-1L, loc.getCharOffset()); |
| 103 | + assertEquals(1, loc.getLineNr()); |
| 104 | + assertEquals(7, loc.getColumnNr()); |
| 105 | + |
| 106 | + loc = p.getCurrentLocation(); |
| 107 | + assertEquals(7L, loc.getByteOffset()); |
| 108 | + assertEquals(-1L, loc.getCharOffset()); |
| 109 | + assertEquals(1, loc.getLineNr()); |
| 110 | + assertEquals(8, loc.getColumnNr()); |
| 111 | + |
| 112 | + p.close(); |
| 113 | + } |
| 114 | + |
| 115 | + private byte[] withUtf8Bom(byte[] bytes) { |
| 116 | + byte[] arr = new byte[bytes.length + 3]; |
| 117 | + // write UTF-8 BOM |
| 118 | + arr[0] = (byte) 0xEF; |
| 119 | + arr[1] = (byte) 0xBB; |
| 120 | + arr[2] = (byte) 0xBF; |
| 121 | + System.arraycopy(bytes, 0, arr, 3, bytes.length); |
| 122 | + return arr; |
| 123 | + } |
| 124 | +} |
0 commit comments