Debian Codegraph @

src/test/java/com/fasterxml/jackson/dataformat/smile/TestParserLocation.java

raw
1 package com.fasterxml.jackson.dataformat.smile;
2
3 import java.io.*;
4
5 import com.fasterxml.jackson.core.*;
6
7 public class TestParserLocation
8 extends SmileTestBase
9 {
10 /**
11 * Basic unit test to verify that [JACKSON-] has been resolved.
12 */
13 public void testSimpleOffsets() throws IOException
14 {
15 byte[] data = _smileDoc("[ true, null, false, 511 ]", true); // true -> write header
16
17 JsonParser p = _smileParser(data);
18 assertNull(p.getCurrentToken());
19 JsonLocation loc = p.getCurrentLocation();
20 assertNotNull(loc);
21 // first: -1 for "not known", for character-based stuff
22 assertEquals(-1, loc.getCharOffset());
23 // column will indicate offset, so:
24 assertEquals(4, loc.getColumnNr());
25 assertEquals(-1, loc.getLineNr());
26 // but first 4 bytes are for header
27 assertEquals(4, loc.getByteOffset());
28
29 // array marker is a single byte, so:
30 assertToken(JsonToken.START_ARRAY, p.nextToken());
31 assertEquals(5, p.getCurrentLocation().getByteOffset());
32 assertEquals(4, p.getTokenLocation().getByteOffset());
33
34 // same for true and others except for last int
35 assertToken(JsonToken.VALUE_TRUE, p.nextToken());
36 assertEquals(6, p.getCurrentLocation().getByteOffset());
37 assertEquals(5, p.getTokenLocation().getByteOffset());
38
39 assertToken(JsonToken.VALUE_NULL, p.nextToken());
40 assertEquals(7, p.getCurrentLocation().getByteOffset());
41 assertEquals(6, p.getTokenLocation().getByteOffset());
42
43 assertToken(JsonToken.VALUE_FALSE, p.nextToken());
44 assertEquals(8, p.getCurrentLocation().getByteOffset());
45 assertEquals(7, p.getTokenLocation().getByteOffset());
46
47 // 0x1FF takes 3 bytes (type byte, 7/6 bit segments)
48 assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
49 assertEquals(511, p.getIntValue());
50 assertEquals(11, p.getCurrentLocation().getByteOffset());
51 assertEquals(8, p.getTokenLocation().getByteOffset());
52
53 assertToken(JsonToken.END_ARRAY, p.nextToken());
54 assertEquals(12, p.getCurrentLocation().getByteOffset());
55 assertEquals(11, p.getTokenLocation().getByteOffset());
56
57 assertNull(p.nextToken());
58 p.close();
59 }
60
61 // for [databind-smile#24]
62 public void testAscendingOffsets() throws Exception
63 {
64 // need to create big enough document, say at least 64k
65 // but as importantly, try to create buffer boundaries by using 6-char (7-byte) ASCII strings
66 final int COUNT = 57000;
67 final int SIZE = COUNT * 7;
68
69 ByteArrayOutputStream bytes = new ByteArrayOutputStream(COUNT + 10);
70 SmileGenerator gen = smileGenerator(bytes, true);
71 gen.disable(SmileGenerator.Feature.CHECK_SHARED_STRING_VALUES);
72 gen.writeStartArray();
73 for (int i = 0; i < COUNT; ++i) {
74 gen.writeString("abc123");
75 }
76 gen.writeEndArray();
77 gen.close();
78 byte[] b = bytes.toByteArray();
79 assertEquals(4 + 2 + SIZE, b.length);
80
81 SmileParser p = _smileParser(new ByteArrayInputStream(b));
82 assertToken(JsonToken.START_ARRAY, p.nextToken());
83 // 4 byte header, start array read, so 4 bytes down:
84 assertEquals(5, p.getCurrentLocation().getByteOffset());
85 for (int i = 0; i < COUNT; ++i) {
86 assertToken(JsonToken.VALUE_STRING, p.nextToken());
87 assertEquals(6 + i*7, p.getCurrentLocation().getByteOffset());
88 assertEquals("abc123", p.getText());
89 }
90 assertToken(JsonToken.END_ARRAY, p.nextToken());
91 assertEquals(SIZE+6, p.getCurrentLocation().getByteOffset());
92 assertNull(p.nextToken());
93 assertEquals(SIZE+6, p.getCurrentLocation().getByteOffset());
94 p.close();
95 }
96 }