Codebase list jackson-core / 5f14fbd
New upstream version 2.9.9 Mechtilde 4 years ago
15 changed file(s) with 501 addition(s) and 183 deletion(s). Raw diff Collapse all Expand all
33 <groupId>com.fasterxml.jackson</groupId>
44 <!-- For 2.9.2 and beyond, new parent pom; extends jackson-bom -->
55 <artifactId>jackson-base</artifactId>
6 <version>2.9.8</version>
6 <version>2.9.9</version>
77 </parent>
88
99 <groupId>com.fasterxml.jackson.core</groupId>
1010 <artifactId>jackson-core</artifactId>
1111 <name>Jackson-core</name>
12 <version>2.9.8</version>
12 <version>2.9.9</version>
1313 <packaging>bundle</packaging>
1414 <description>Core Jackson processing abstractions (aka Streaming API), implementation for JSON</description>
1515 <inceptionYear>2008</inceptionYear>
1919 <connection>scm:git:git@github.com:FasterXML/jackson-core.git</connection>
2020 <developerConnection>scm:git:git@github.com:FasterXML/jackson-core.git</developerConnection>
2121 <url>http://github.com/FasterXML/jackson-core</url>
22 <tag>jackson-core-2.9.8</tag>
22 <tag>jackson-core-2.9.9</tag>
2323 </scm>
2424
2525 <properties>
154154 * Suggested #463: Ensure that `skipChildren()` of non-blocking `JsonParser` will throw
155155 exception if not enough input
156156 (2.9.6)
157
157
158 Alexander Eyers-Taylor (aeyerstaylor@github)
159 * Reported #510: Fix ArrayIndexOutofBoundsException found by LGTM.com
160 (2.9.9)
161
162 Henrik Gustafsson (gsson@github)
163 * Reported #516: _inputPtr off-by-one in UTF8StreamJsonParser._parseNumber2()
164 (2.9.9)
1313 === Releases ===
1414 ------------------------------------------------------------------------
1515
16 2.9.9 (16-May-2019)
17
18 #516: _inputPtr off-by-one in UTF8StreamJsonParser._parseNumber2()
19 (reported by Henrik G)
20 #531: Non-blocking parser reports incorrect locations when fed with non-zero offset
21 (reported by David N)
22
1623 2.9.8 (15-Dec-2018)
1724
1825 #488: Fail earlier on coercions from "too big" `BigInteger` into
1926 fixed-size types (`int`, `long`, `short`)
27 #510: Fix ArrayIndexOutofBoundsException found by LGTM.com
28 (reported by Alexander E-T)
2029 - Improve exception message for missing Base64 padding (see databind#2183)
2130
2231 2.9.7 (19-Sep-2018)
19591959 }
19601960 char c = _inputBuffer[_inputPtr];
19611961 int i = (int) c;
1962 if (i <= maxCode) {
1962 if (i < maxCode) {
19631963 if (codes[i] != 0) {
19641964 break;
19651965 }
14661466 _textBuffer.setCurrentLength(outPtr);
14671467 // As per #105, need separating space between root values; check here
14681468 if (_parsingContext.inRoot()) {
1469 _verifyRootSpace(_inputBuffer[_inputPtr++] & 0xFF);
1469 _verifyRootSpace(_inputBuffer[_inputPtr] & 0xFF);
14701470 }
14711471
14721472 // And there we have it!
9393 _currInputRowStart = start - (_inputEnd - _currInputRowStart);
9494
9595 // And then update buffer settings
96 _currBufferStart = start;
9697 _inputBuffer = buf;
9798 _inputPtr = start;
9899 _inputEnd = end;
7878 _testSimpleWrites(true);
7979 }
8080
81 public void _testSimpleWrites(boolean useStream) throws Exception
81 private void _testSimpleWrites(boolean useStream) throws Exception
8282 {
8383 ByteArrayOutputStream out = new ByteArrayOutputStream();
8484 StringWriter w = new StringWriter();
101101 String json = useStream ? out.toString("UTF-8") : w.toString();
102102 assertEquals("123 \"abc\" true", json);
103103 }
104
105 // [core#516]: Off-by-one read problem
106 public void testRootOffsetIssue516Bytes() throws Exception
107 {
108 // InputStream that forces _parseNumber2 to be invoked.
109 final InputStream in = new Issue516InputStream(new byte[][] {
110 "1234".getBytes("UTF-8"),
111 "5 true".getBytes("UTF-8")
112 });
113
114 JsonParser parser = JSON_F.createParser(in);
115 assertEquals(12345, parser.nextIntValue(0));
116
117 // Fails with com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'rue': was expecting ('true', 'false' or 'null')
118 assertTrue(parser.nextBooleanValue());
119
120 parser.close();
121 in.close();
122 }
123
124 // [core#516]: Off-by-one read problem
125 public void testRootOffsetIssue516Chars() throws Exception
126 {
127 // InputStream that forces _parseNumber2 to be invoked.
128 final Reader in = new Issue516Reader(new char[][] {
129 "1234".toCharArray(), "5 true".toCharArray()
130 });
131
132 JsonParser parser = JSON_F.createParser(in);
133 assertEquals(12345, parser.nextIntValue(0));
134
135 // Fails with com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'rue': was expecting ('true', 'false' or 'null')
136 assertTrue(parser.nextBooleanValue());
137
138 parser.close();
139 in.close();
140 }
141
142 static class Issue516InputStream extends InputStream
143 {
144 private final byte[][] reads;
145 private int currentRead;
146
147 public Issue516InputStream(byte[][] reads) {
148 this.reads = reads;
149 this.currentRead = 0;
150 }
151
152 @Override
153 public int read() throws IOException {
154 throw new UnsupportedOperationException();
155 }
156
157 @Override
158 public int read(byte[] b, int off, int len) throws IOException {
159 if (currentRead >= reads.length) {
160 return -1;
161 }
162 byte[] bytes = reads[currentRead++];
163 if (len < bytes.length) {
164 throw new IllegalArgumentException();
165 }
166 System.arraycopy(bytes, 0, b, off, bytes.length);
167 return bytes.length;
168 }
169 }
170
171 static class Issue516Reader extends Reader
172 {
173 private final char[][] reads;
174 private int currentRead;
175
176 public Issue516Reader(char[][] reads) {
177 this.reads = reads;
178 this.currentRead = 0;
179 }
180
181 @Override
182 public void close() { }
183
184 @Override
185 public int read() throws IOException {
186 throw new UnsupportedOperationException();
187 }
188
189 @Override
190 public int read(char[] b, int off, int len) throws IOException {
191 if (currentRead >= reads.length) {
192 return -1;
193 }
194 char[] bytes = reads[currentRead++];
195 if (len < bytes.length) {
196 throw new IllegalArgumentException();
197 }
198 System.arraycopy(bytes, 0, b, off, bytes.length);
199 return bytes.length;
200 }
201 }
104202 }
0 package com.fasterxml.jackson.core.json.async;
1
2 import com.fasterxml.jackson.core.*;
3 import com.fasterxml.jackson.core.async.AsyncTestBase;
4 import com.fasterxml.jackson.core.async.ByteArrayFeeder;
5
6 public class AsyncLocationTest extends AsyncTestBase
7 {
8 private final JsonFactory DEFAULT_F = new JsonFactory();
9
10 // for [core#531]
11 public void testLocationOffsets() throws Exception
12 {
13 JsonParser parser = DEFAULT_F.createNonBlockingByteArrayParser();
14 ByteArrayFeeder feeder = (ByteArrayFeeder) parser.getNonBlockingInputFeeder();
15
16 byte[] input = utf8Bytes("[[[");
17
18 feeder.feedInput(input, 2, 3);
19 assertEquals(JsonToken.START_ARRAY, parser.nextToken());
20 assertEquals(1, parser.getCurrentLocation().getByteOffset());
21 assertEquals(1, parser.getTokenLocation().getByteOffset());
22 assertEquals(1, parser.getCurrentLocation().getLineNr());
23 assertEquals(1, parser.getTokenLocation().getLineNr());
24 assertEquals(2, parser.getCurrentLocation().getColumnNr());
25 assertEquals(1, parser.getTokenLocation().getColumnNr());
26
27 feeder.feedInput(input, 0, 1);
28 assertEquals(JsonToken.START_ARRAY, parser.nextToken());
29 assertEquals(2, parser.getCurrentLocation().getByteOffset());
30 assertEquals(2, parser.getTokenLocation().getByteOffset());
31 assertEquals(1, parser.getCurrentLocation().getLineNr());
32 assertEquals(1, parser.getTokenLocation().getLineNr());
33 assertEquals(3, parser.getCurrentLocation().getColumnNr());
34 assertEquals(2, parser.getTokenLocation().getColumnNr());
35 parser.close();
36 }
37 }
00 package com.fasterxml.jackson.core.main;
11
2
32 import com.fasterxml.jackson.core.*;
3 import com.fasterxml.jackson.core.testsupport.ByteOutputStreamForTesting;
4 import com.fasterxml.jackson.core.testsupport.StringWriterForTesting;
45
56 import java.io.*;
67
1819 */
1920 public class TestGeneratorClosing extends BaseTest
2021 {
21 /*
22 /**********************************************************
23 /* Helper classes
24 /**********************************************************
25 */
26
27 final static class MyWriter extends StringWriter
28 {
29 boolean _isClosed = false;
30
31 public MyWriter() { }
32
33 @Override
34 public void close() throws IOException {
35 _isClosed = true;
36 super.close();
37 }
38 public boolean isClosed() { return _isClosed; }
39 }
40
41 final static class MyStream extends ByteArrayOutputStream
42 {
43 boolean _isClosed = false;
44
45 public MyStream() { }
46
47 @Override
48 public void close() throws IOException {
49 _isClosed = true;
50 super.close();
51 }
52 public boolean isClosed() { return _isClosed; }
53 }
54
55 static class MyBytes extends ByteArrayOutputStream
56 {
57 public int flushed = 0;
58
59 @Override
60 public void flush() throws IOException
61 {
62 ++flushed;
63 super.flush();
64 }
65 }
66
67 static class MyChars extends StringWriter
68 {
69 public int flushed = 0;
70
71 @Override
72 public void flush()
73 {
74 ++flushed;
75 super.flush();
76 }
77 }
78
7922 /*
8023 /**********************************************************
8124 /* Unit tests
9740 f.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
9841 assertFalse(f.isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET));
9942 @SuppressWarnings("resource")
100 MyWriter output = new MyWriter();
43 ByteOutputStreamForTesting output = new ByteOutputStreamForTesting();
10144 JsonGenerator jg = f.createGenerator(output);
10245
10346 // shouldn't be closed to begin with...
11356 JsonFactory f = new JsonFactory();
11457 f.enable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
11558 @SuppressWarnings("resource")
116 MyWriter output = new MyWriter();
59 ByteOutputStreamForTesting output = new ByteOutputStreamForTesting();
11760 JsonGenerator jg = f.createGenerator(output);
11861
11962 // shouldn't be closed to begin with...
12972 JsonFactory f = new JsonFactory();
13073 f.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
13174 @SuppressWarnings("resource")
132 MyStream output = new MyStream();
75 ByteOutputStreamForTesting output = new ByteOutputStreamForTesting();
13376 JsonGenerator jg = f.createGenerator(output, JsonEncoding.UTF8);
13477
13578 assertFalse(output.isClosed());
180123 assertEquals("{", sw.toString());
181124 }
182125
183 // [JACKSON-401]
184126 @SuppressWarnings("resource")
185127 public void testAutoFlushOrNot() throws Exception
186128 {
187129 JsonFactory f = new JsonFactory();
188130 assertTrue(f.isEnabled(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM));
189 MyChars sw = new MyChars();
131 StringWriterForTesting sw = new StringWriterForTesting();
190132 JsonGenerator jg = f.createGenerator(sw);
191133 jg.writeStartArray();
192134 jg.writeEndArray();
193 assertEquals(0, sw.flushed);
135 assertEquals(0, sw.flushCount);
194136 jg.flush();
195 assertEquals(1, sw.flushed);
137 assertEquals(1, sw.flushCount);
196138 jg.close();
197139
198140 // ditto with stream
199 MyBytes bytes = new MyBytes();
141 ByteOutputStreamForTesting bytes = new ByteOutputStreamForTesting();
200142 jg = f.createGenerator(bytes, JsonEncoding.UTF8);
201143 jg.writeStartArray();
202144 jg.writeEndArray();
203 assertEquals(0, bytes.flushed);
145 assertEquals(0, bytes.flushCount);
204146 jg.flush();
205 assertEquals(1, bytes.flushed);
147 assertEquals(1, bytes.flushCount);
206148 assertEquals(2, bytes.toByteArray().length);
207149 jg.close();
208150
209151 // then disable and we should not see flushing again...
210152 f.disable(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM);
211153 // first with a Writer
212 sw = new MyChars();
154 sw = new StringWriterForTesting();
213155 jg = f.createGenerator(sw);
214156 jg.writeStartArray();
215157 jg.writeEndArray();
216 assertEquals(0, sw.flushed);
158 assertEquals(0, sw.flushCount);
217159 jg.flush();
218 assertEquals(0, sw.flushed);
160 assertEquals(0, sw.flushCount);
219161 jg.close();
220162 assertEquals("[]", sw.toString());
221163
222164 // and then with OutputStream
223 bytes = new MyBytes();
165 bytes = new ByteOutputStreamForTesting();
224166 jg = f.createGenerator(bytes, JsonEncoding.UTF8);
225167 jg.writeStartArray();
226168 jg.writeEndArray();
227 assertEquals(0, bytes.flushed);
169 assertEquals(0, bytes.flushCount);
228170 jg.flush();
229 assertEquals(0, bytes.flushed);
171 assertEquals(0, bytes.flushCount);
230172 jg.close();
231173 assertEquals(2, bytes.toByteArray().length);
232174 }
460460
461461 p = JSON_FACTORY.createParser(new MockDataInput(input));
462462 assertEquals(JsonToken.START_ARRAY, p.nextToken());
463 // same BOM, but DataInput is more restrctive so can skip but offsets
463 // same BOM, but DataInput is more restrictive so can skip but offsets
464464 // are not reliable...
465465 loc = p.getTokenLocation();
466466 assertNotNull(loc);
44 public class NonStandardParserFeaturesTest
55 extends com.fasterxml.jackson.core.BaseTest
66 {
7 public void testSimpleUnquotedBytes() throws Exception {
8 _testSimpleUnquoted(MODE_INPUT_STREAM);
9 _testSimpleUnquoted(MODE_INPUT_STREAM_THROTTLED);
10 _testSimpleUnquoted(MODE_DATA_INPUT);
11 }
12
13 public void testSimpleUnquotedChars() throws Exception {
14 _testSimpleUnquoted(MODE_READER);
15 }
16
17 public void testLargeUnquoted() throws Exception
18 {
19 _testLargeUnquoted(MODE_INPUT_STREAM);
20 _testLargeUnquoted(MODE_INPUT_STREAM_THROTTLED);
21 _testLargeUnquoted(MODE_DATA_INPUT);
22 _testLargeUnquoted(MODE_READER);
23 }
24
257 public void testSingleQuotesDefault() throws Exception
268 {
279 _testSingleQuotesDefault(MODE_INPUT_STREAM);
9779 /****************************************************************
9880 */
9981
100 private void _testLargeUnquoted(int mode) throws Exception
101 {
102 StringBuilder sb = new StringBuilder(5000);
103 sb.append("[\n");
104 //final int REPS = 2000;
105 final int REPS = 1050;
106 for (int i = 0; i < REPS; ++i) {
107 if (i > 0) {
108 sb.append(',');
109 if ((i & 7) == 0) {
110 sb.append('\n');
111 }
112 }
113 sb.append("{");
114 sb.append("abc").append(i&127).append(':');
115 sb.append((i & 1) != 0);
116 sb.append("}\n");
117 }
118 sb.append("]");
119 String JSON = sb.toString();
120 JsonFactory f = new JsonFactory();
121 f.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
122 JsonParser p = createParser(f, mode, JSON);
123 assertToken(JsonToken.START_ARRAY, p.nextToken());
124 for (int i = 0; i < REPS; ++i) {
125 assertToken(JsonToken.START_OBJECT, p.nextToken());
126 assertToken(JsonToken.FIELD_NAME, p.nextToken());
127 assertEquals("abc"+(i&127), p.getCurrentName());
128 assertToken(((i&1) != 0) ? JsonToken.VALUE_TRUE : JsonToken.VALUE_FALSE, p.nextToken());
129 assertToken(JsonToken.END_OBJECT, p.nextToken());
130 }
131 assertToken(JsonToken.END_ARRAY, p.nextToken());
132 p.close();
133 }
134
135
136 private void _testSimpleUnquoted(int mode) throws Exception
137 {
138 final JsonFactory f = new JsonFactory();
139 f.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
140
141 String JSON = "{ a : 1, _foo:true, $:\"money!\", \" \":null }";
142 JsonParser p = createParser(f, mode, JSON);
143
144 assertToken(JsonToken.START_OBJECT, p.nextToken());
145 assertToken(JsonToken.FIELD_NAME, p.nextToken());
146 assertEquals("a", p.getCurrentName());
147 assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
148 assertToken(JsonToken.FIELD_NAME, p.nextToken());
149 assertEquals("_foo", p.getCurrentName());
150 assertToken(JsonToken.VALUE_TRUE, p.nextToken());
151 assertToken(JsonToken.FIELD_NAME, p.nextToken());
152 assertEquals("$", p.getCurrentName());
153 assertToken(JsonToken.VALUE_STRING, p.nextToken());
154 assertEquals("money!", p.getText());
155
156 // and then regular quoted one should still work too:
157 assertToken(JsonToken.FIELD_NAME, p.nextToken());
158 assertEquals(" ", p.getCurrentName());
159
160 assertToken(JsonToken.VALUE_NULL, p.nextToken());
161
162 assertToken(JsonToken.END_OBJECT, p.nextToken());
163 p.close();
164
165 // Another thing, as per [Issue#102]: numbers
166
167 JSON = "{ 123:true,4:false }";
168 p = createParser(f, mode, JSON);
169
170 assertToken(JsonToken.START_OBJECT, p.nextToken());
171 assertToken(JsonToken.FIELD_NAME, p.nextToken());
172 assertEquals("123", p.getCurrentName());
173 assertToken(JsonToken.VALUE_TRUE, p.nextToken());
174
175 assertToken(JsonToken.FIELD_NAME, p.nextToken());
176 assertEquals("4", p.getCurrentName());
177 assertToken(JsonToken.VALUE_FALSE, p.nextToken());
178
179 assertToken(JsonToken.END_OBJECT, p.nextToken());
180 p.close();
181 }
18282
18383 /**
18484 * Test to verify that the default parser settings do not
0 package com.fasterxml.jackson.core.read;
1
2 import com.fasterxml.jackson.core.JsonFactory;
3 import com.fasterxml.jackson.core.JsonParseException;
4 import com.fasterxml.jackson.core.JsonParser;
5 import com.fasterxml.jackson.core.JsonToken;
6
7 public class NonStandardUnquotedNamesTest
8 extends com.fasterxml.jackson.core.BaseTest
9 {
10 private final JsonFactory UNQUOTED_FIELDS_F = new JsonFactory();
11 {
12 UNQUOTED_FIELDS_F.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
13 }
14
15 public void testSimpleUnquotedBytes() throws Exception {
16 _testSimpleUnquoted(MODE_INPUT_STREAM);
17 _testSimpleUnquoted(MODE_INPUT_STREAM_THROTTLED);
18 _testSimpleUnquoted(MODE_DATA_INPUT);
19 }
20
21 public void testSimpleUnquotedChars() throws Exception {
22 _testSimpleUnquoted(MODE_READER);
23 }
24
25 public void testLargeUnquoted() throws Exception
26 {
27 _testLargeUnquoted(MODE_INPUT_STREAM);
28 _testLargeUnquoted(MODE_INPUT_STREAM_THROTTLED);
29 _testLargeUnquoted(MODE_DATA_INPUT);
30 _testLargeUnquoted(MODE_READER);
31 }
32
33 // [core#510]: ArrayIndexOutOfBounds
34 public void testUnquotedIssue510() throws Exception
35 {
36 // NOTE! Requires longer input buffer to trigger longer codepath
37 char[] fullChars = new char[4001];
38 for (int i = 0; i < 3998; i++) {
39 fullChars[i] = ' ';
40 }
41 fullChars[3998] = '{';
42 fullChars[3999] = 'a';
43 fullChars[4000] = 256;
44
45 JsonParser p = UNQUOTED_FIELDS_F.createParser(new java.io.StringReader(new String(fullChars)));
46 assertToken(JsonToken.START_OBJECT, p.nextToken());
47 try {
48 p.nextToken();
49 fail("Should not pass");
50 } catch (JsonParseException e) {
51 ; // should fail here
52 }
53 p.close();
54 }
55
56 /*
57 /****************************************************************
58 /* Secondary test methods
59 /****************************************************************
60 */
61
62 private void _testLargeUnquoted(int mode) throws Exception
63 {
64 StringBuilder sb = new StringBuilder(5000);
65 sb.append("[\n");
66 //final int REPS = 2000;
67 final int REPS = 1050;
68 for (int i = 0; i < REPS; ++i) {
69 if (i > 0) {
70 sb.append(',');
71 if ((i & 7) == 0) {
72 sb.append('\n');
73 }
74 }
75 sb.append("{");
76 sb.append("abc").append(i&127).append(':');
77 sb.append((i & 1) != 0);
78 sb.append("}\n");
79 }
80 sb.append("]");
81 String JSON = sb.toString();
82 JsonParser p = createParser(UNQUOTED_FIELDS_F, mode, JSON);
83 assertToken(JsonToken.START_ARRAY, p.nextToken());
84 for (int i = 0; i < REPS; ++i) {
85 assertToken(JsonToken.START_OBJECT, p.nextToken());
86 assertToken(JsonToken.FIELD_NAME, p.nextToken());
87 assertEquals("abc"+(i&127), p.getCurrentName());
88 assertToken(((i&1) != 0) ? JsonToken.VALUE_TRUE : JsonToken.VALUE_FALSE, p.nextToken());
89 assertToken(JsonToken.END_OBJECT, p.nextToken());
90 }
91 assertToken(JsonToken.END_ARRAY, p.nextToken());
92 p.close();
93 }
94
95 private void _testSimpleUnquoted(int mode) throws Exception
96 {
97 String JSON = "{ a : 1, _foo:true, $:\"money!\", \" \":null }";
98 JsonParser p = createParser(UNQUOTED_FIELDS_F, mode, JSON);
99
100 assertToken(JsonToken.START_OBJECT, p.nextToken());
101 assertToken(JsonToken.FIELD_NAME, p.nextToken());
102 assertEquals("a", p.getCurrentName());
103 assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
104 assertToken(JsonToken.FIELD_NAME, p.nextToken());
105 assertEquals("_foo", p.getCurrentName());
106 assertToken(JsonToken.VALUE_TRUE, p.nextToken());
107 assertToken(JsonToken.FIELD_NAME, p.nextToken());
108 assertEquals("$", p.getCurrentName());
109 assertToken(JsonToken.VALUE_STRING, p.nextToken());
110 assertEquals("money!", p.getText());
111
112 // and then regular quoted one should still work too:
113 assertToken(JsonToken.FIELD_NAME, p.nextToken());
114 assertEquals(" ", p.getCurrentName());
115
116 assertToken(JsonToken.VALUE_NULL, p.nextToken());
117
118 assertToken(JsonToken.END_OBJECT, p.nextToken());
119 p.close();
120
121 // Another thing, as per [Issue#102]: numbers
122
123 JSON = "{ 123:true,4:false }";
124 p = createParser(UNQUOTED_FIELDS_F, mode, JSON);
125
126 assertToken(JsonToken.START_OBJECT, p.nextToken());
127 assertToken(JsonToken.FIELD_NAME, p.nextToken());
128 assertEquals("123", p.getCurrentName());
129 assertToken(JsonToken.VALUE_TRUE, p.nextToken());
130
131 assertToken(JsonToken.FIELD_NAME, p.nextToken());
132 assertEquals("4", p.getCurrentName());
133 assertToken(JsonToken.VALUE_FALSE, p.nextToken());
134
135 assertToken(JsonToken.END_OBJECT, p.nextToken());
136 p.close();
137 }
138 }
0 package com.fasterxml.jackson.core.testsupport;
1
2 import java.io.ByteArrayOutputStream;
3 import java.io.IOException;
4
5 /**
6 * Helper class for verifying that {@link java.io.OutputStream} is (or is not)
7 * closed and/or flushed.
8 */
9 public class ByteOutputStreamForTesting extends ByteArrayOutputStream
10 {
11 public int closeCount = 0;
12 public int flushCount = 0;
13
14 public ByteOutputStreamForTesting() { }
15
16 @Override
17 public void close() throws IOException {
18 ++closeCount;
19 super.close();
20 }
21
22 @Override
23 public void flush() throws IOException
24 {
25 ++flushCount;
26 super.flush();
27 }
28
29 public boolean isClosed() { return closeCount > 0; }
30 public boolean isFlushed() { return flushCount > 0; }
31 }
0 package com.fasterxml.jackson.core.testsupport;
1
2 import java.io.IOException;
3 import java.io.StringWriter;
4
5 public class StringWriterForTesting extends StringWriter
6 {
7 public int closeCount = 0;
8 public int flushCount = 0;
9
10 public StringWriterForTesting() { }
11
12 @Override
13 public void close() throws IOException {
14 ++closeCount;
15 super.close();
16 }
17
18 @Override
19 public void flush()
20 {
21 ++flushCount;
22 super.flush();
23 }
24
25 public boolean isClosed() { return closeCount > 0; }
26 public boolean isFlushed() { return flushCount > 0; }
27 }
0 package com.fasterxml.jackson.failing;
1
2 import com.fasterxml.jackson.core.*;
3
4 // For checking [databind#533]
5 public class LocationOffsets533Test extends com.fasterxml.jackson.core.BaseTest
6 {
7 final JsonFactory JSON_F = new JsonFactory();
8
9 public void testOffsetWithoutInputOffset() throws Exception
10 {
11 JsonLocation loc;
12 JsonParser p;
13 // 3 spaces before, 2 after, just for padding
14 byte[] b = " { } ".getBytes("UTF-8");
15
16 // and then peel them off
17 p = JSON_F.createParser(/*ObjectReadContext.empty(),*/ b);
18 assertToken(JsonToken.START_OBJECT, p.nextToken());
19
20 loc = p.getTokenLocation();
21 assertEquals(3L, loc.getByteOffset());
22 assertEquals(-1L, loc.getCharOffset());
23 assertEquals(1, loc.getLineNr());
24 assertEquals(4, loc.getColumnNr());
25
26 loc = p.getCurrentLocation();
27 assertEquals(4L, loc.getByteOffset());
28 assertEquals(-1L, loc.getCharOffset());
29 assertEquals(1, loc.getLineNr());
30 assertEquals(5, loc.getColumnNr());
31
32 p.close();
33 }
34
35 // for [core#533]
36 public void testUtf8Bom() throws Exception
37 {
38 JsonLocation loc;
39 JsonParser p;
40
41 byte[] b = withUtf8Bom("{ }".getBytes());
42
43 // and then peel them off
44 p = JSON_F.createParser(/*ObjectReadContext.empty(),*/ b);
45 assertToken(JsonToken.START_OBJECT, p.nextToken());
46
47 loc = p.getTokenLocation();
48 assertEquals(3L, loc.getByteOffset());
49 assertEquals(-1L, loc.getCharOffset());
50 assertEquals(1, loc.getLineNr());
51 assertEquals(4, loc.getColumnNr());
52
53 loc = p.getCurrentLocation();
54 assertEquals(4L, loc.getByteOffset());
55 assertEquals(-1L, loc.getCharOffset());
56 assertEquals(1, loc.getLineNr());
57 assertEquals(5, loc.getColumnNr());
58
59 p.close();
60 }
61
62 public void testUtf8BomWithPadding() throws Exception
63 {
64 JsonLocation loc;
65 JsonParser p;
66
67 byte[] b = withUtf8Bom(" { }".getBytes());
68
69 // and then peel them off
70 p = JSON_F.createParser(/*ObjectReadContext.empty(),*/ b);
71 assertToken(JsonToken.START_OBJECT, p.nextToken());
72
73 loc = p.getTokenLocation();
74 assertEquals(6L, loc.getByteOffset());
75 assertEquals(-1L, loc.getCharOffset());
76 assertEquals(1, loc.getLineNr());
77 assertEquals(7, loc.getColumnNr());
78
79 loc = p.getCurrentLocation();
80 assertEquals(7L, loc.getByteOffset());
81 assertEquals(-1L, loc.getCharOffset());
82 assertEquals(1, loc.getLineNr());
83 assertEquals(8, loc.getColumnNr());
84
85 p.close();
86 }
87
88 public void testUtf8BomWithInputOffset() throws Exception
89 {
90 JsonLocation loc;
91 JsonParser p;
92
93 byte[] b = withUtf8Bom(" { }".getBytes());
94
95 // and then peel them off
96 p = JSON_F.createParser(/*ObjectReadContext.empty(),*/ b);
97 assertToken(JsonToken.START_OBJECT, p.nextToken());
98
99 loc = p.getTokenLocation();
100 assertEquals(6L, loc.getByteOffset());
101 assertEquals(-1L, loc.getCharOffset());
102 assertEquals(1, loc.getLineNr());
103 assertEquals(7, loc.getColumnNr());
104
105 loc = p.getCurrentLocation();
106 assertEquals(7L, loc.getByteOffset());
107 assertEquals(-1L, loc.getCharOffset());
108 assertEquals(1, loc.getLineNr());
109 assertEquals(8, loc.getColumnNr());
110
111 p.close();
112 }
113
114 private byte[] withUtf8Bom(byte[] bytes) {
115 byte[] arr = new byte[bytes.length + 3];
116 // write UTF-8 BOM
117 arr[0] = (byte) 0xEF;
118 arr[1] = (byte) 0xBB;
119 arr[2] = (byte) 0xBF;
120 System.arraycopy(bytes, 0, arr, 3, bytes.length);
121 return arr;
122 }
123 }