Skip to content

Commit e3aa027

Browse files
committed
Improve test coverage for protobuf codec
1 parent ba3da00 commit e3aa027

File tree

5 files changed

+211
-27
lines changed

5 files changed

+211
-27
lines changed

protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/ProtobufParser.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -2470,37 +2470,37 @@ private void _reportErrorF(String format, Object... args) throws JsonParseExcept
24702470

24712471
private void _reportIncompatibleType(ProtobufField field, int wireType) throws JsonParseException
24722472
{
2473-
_reportErrorF
2473+
_reportError(String.format
24742474
("Incompatible wire type (0x%x) for field '%s': not valid for field of type %s (expected 0x%x)",
2475-
wireType, field.name, field.type, field.type.getWireType());
2475+
wireType, field.name, field.type, field.type.getWireType()));
24762476
}
24772477

24782478
private void _reportInvalidLength(int len) throws JsonParseException {
2479-
_reportErrorF("Invalid length (%d): must be positive number", len);
2479+
_reportError("Invalid length (%d): must be positive number", len);
24802480
}
24812481

2482-
protected void _reportTooLongVInt(int fifth) throws JsonParseException {
2483-
_reportErrorF("Too long tag VInt: fifth byte 0x%x", fifth);
2482+
private void _reportTooLongVInt(int fifth) throws JsonParseException {
2483+
_reportError("Too long tag VInt: fifth byte 0x%x", fifth);
24842484
}
24852485

2486-
protected void _reportTooLongVLong(int fifth) throws JsonParseException {
2487-
_reportErrorF("Too long tag VLong: tenth byte 0x%x", fifth);
2486+
private void _reportTooLongVLong(int fifth) throws JsonParseException {
2487+
_reportError("Too long tag VLong: tenth byte 0x%x", fifth);
24882488
}
24892489

2490-
protected void _reportInvalidInitial(int mask) throws JsonParseException {
2491-
_reportErrorF("Invalid UTF-8 start byte 0x%x", mask);
2490+
private void _reportInvalidInitial(int mask) throws JsonParseException {
2491+
_reportError("Invalid UTF-8 start byte 0x%x", mask);
24922492
}
24932493

2494-
protected void _reportInvalidOther(int mask) throws JsonParseException {
2495-
_reportErrorF("Invalid UTF-8 middle byte 0x%x", mask);
2494+
private void _reportInvalidOther(int mask) throws JsonParseException {
2495+
_reportError("Invalid UTF-8 middle byte 0x%x", mask);
24962496
}
24972497

2498-
protected void _reportInvalidOther(int mask, int ptr) throws JsonParseException {
2498+
private void _reportInvalidOther(int mask, int ptr) throws JsonParseException {
24992499
_inputPtr = ptr;
25002500
_reportInvalidOther(mask);
25012501
}
25022502

2503-
protected void _reportInvalidChar(int c) throws JsonParseException {
2503+
private void _reportInvalidChar(int c) throws JsonParseException {
25042504
// Either invalid WS or illegal UTF-8 start char
25052505
if (c < ' ') {
25062506
_throwInvalidSpace(c);

protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/ReadSimpleTest.java

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.fasterxml.jackson.dataformat.protobuf;
22

3+
import java.io.ByteArrayOutputStream;
34
import java.io.StringWriter;
45

56
import com.fasterxml.jackson.core.JsonParser;
@@ -211,8 +212,25 @@ public void testStringArraySimple() throws Exception
211212
assertToken(JsonToken.END_ARRAY, p.nextToken());
212213
assertToken(JsonToken.END_OBJECT, p.nextToken());
213214
p.close();
214-
}
215215

216+
// and, ditto, but skipping
217+
p = MAPPER.getFactory().createParser(bytes);
218+
p.setSchema(schema);
219+
assertToken(JsonToken.START_OBJECT, p.nextToken());
220+
assertToken(JsonToken.FIELD_NAME, p.nextToken());
221+
assertToken(JsonToken.START_ARRAY, p.nextToken());
222+
223+
assertToken(JsonToken.VALUE_STRING, p.nextToken());
224+
assertToken(JsonToken.VALUE_STRING, p.nextToken());
225+
assertToken(JsonToken.VALUE_STRING, p.nextToken());
226+
// but only skip first couple, not last
227+
assertEquals(input.values[2], p.getText());
228+
229+
assertToken(JsonToken.END_ARRAY, p.nextToken());
230+
assertToken(JsonToken.END_OBJECT, p.nextToken());
231+
p.close();
232+
assertNull(p.nextToken());
233+
}
216234

217235
public void testStringArrayPacked() throws Exception
218236
{
@@ -268,6 +286,7 @@ public void testStringArrayWithName() throws Exception
268286
assertToken(JsonToken.START_ARRAY, p.nextToken());
269287

270288
assertToken(JsonToken.VALUE_STRING, p.nextToken());
289+
assertFalse(p.hasTextCharacters());
271290
assertEquals(input.values[0], p.getText());
272291
assertToken(JsonToken.VALUE_STRING, p.nextToken());
273292
assertEquals(input.values[1], p.getText());
@@ -279,6 +298,20 @@ public void testStringArrayWithName() throws Exception
279298
assertToken(JsonToken.END_ARRAY, p.nextToken());
280299
assertToken(JsonToken.END_OBJECT, p.nextToken());
281300
p.close();
301+
302+
// also, for fun: partial read
303+
p = MAPPER.getFactory().createParser(bytes);
304+
p.setSchema(schema);
305+
assertToken(JsonToken.START_OBJECT, p.nextToken());
306+
307+
assertToken(JsonToken.FIELD_NAME, p.nextToken());
308+
assertEquals("name", p.getCurrentName());
309+
ByteArrayOutputStream b = new ByteArrayOutputStream();
310+
int count = p.releaseBuffered(b);
311+
assertEquals(count, b.size());
312+
assertEquals(18, count);
313+
314+
p.close();
282315
}
283316

284317
public void testSearchMessage() throws Exception

protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/WriteBinaryTest.java

+41-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.fasterxml.jackson.dataformat.protobuf;
22

3+
import com.fasterxml.jackson.core.JsonParser;
4+
import com.fasterxml.jackson.core.JsonToken;
35
import com.fasterxml.jackson.databind.*;
46
import com.fasterxml.jackson.dataformat.protobuf.schema.*;
57

@@ -47,10 +49,46 @@ public void testSimpleBinary() throws Exception
4749
assertEquals(input.id, result.id);
4850
assertEquals(input.trailer, result.trailer);
4951
assertNotNull(result.data);
50-
assertEquals(data.length, result.data.length);
5152

52-
for (int i = 0, len = data.length; i < len; ++i) {
53-
if (data[i] != result.data[i]) {
53+
_verify(data, result.data);
54+
55+
// and via JsonParser too
56+
JsonParser p = MAPPER.getFactory().createParser(bytes);
57+
p.setSchema(schema);
58+
assertToken(JsonToken.START_OBJECT, p.nextToken());
59+
60+
assertToken(JsonToken.FIELD_NAME, p.nextToken());
61+
assertFalse(p.hasTextCharacters());
62+
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
63+
assertToken(JsonToken.FIELD_NAME, p.nextToken());
64+
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
65+
assertEquals(input.trailer, p.getIntValue());
66+
assertToken(JsonToken.FIELD_NAME, p.nextToken());
67+
assertEquals("data", p.getCurrentName());
68+
assertToken(JsonToken.VALUE_EMBEDDED_OBJECT, p.nextToken());
69+
_verify(data, p.getBinaryValue());
70+
71+
assertToken(JsonToken.END_OBJECT, p.nextToken());
72+
p.close();
73+
74+
// and with skipping of binary data
75+
p = MAPPER.getFactory().createParser(bytes);
76+
p.setSchema(schema);
77+
assertToken(JsonToken.START_OBJECT, p.nextToken());
78+
assertToken(JsonToken.FIELD_NAME, p.nextToken());
79+
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
80+
assertToken(JsonToken.FIELD_NAME, p.nextToken());
81+
assertEquals(input.trailer, p.nextIntValue(-1));
82+
assertToken(JsonToken.FIELD_NAME, p.nextToken());
83+
assertToken(JsonToken.VALUE_EMBEDDED_OBJECT, p.nextToken());
84+
assertToken(JsonToken.END_OBJECT, p.nextToken());
85+
p.close();
86+
}
87+
88+
private void _verify(byte[] dataExp, byte[] dataAct) {
89+
assertEquals(dataExp.length, dataAct.length);
90+
for (int i = 0, len = dataExp.length; i < len; ++i) {
91+
if (dataExp[i] != dataAct[i]) {
5492
fail("Binary data differs at #"+i);
5593
}
5694
}

protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/WriteLongString94Test.java

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
44

5-
import com.fasterxml.jackson.dataformat.protobuf.*;
65
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema;
76

87
public class WriteLongString94Test extends ProtobufTestBase

protobuf/src/test/java/com/fasterxml/jackson/dataformat/protobuf/WritePrimitiveArrayTest.java

+123-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package com.fasterxml.jackson.dataformat.protobuf;
22

3+
import java.math.BigDecimal;
4+
import java.math.BigInteger;
5+
36
import org.junit.Assert;
47

8+
import com.fasterxml.jackson.core.JsonParser;
9+
import com.fasterxml.jackson.core.JsonToken;
10+
import com.fasterxml.jackson.core.JsonParser.NumberType;
511
import com.fasterxml.jackson.databind.ObjectMapper;
612
import com.fasterxml.jackson.databind.ObjectWriter;
713
import com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema;
@@ -177,10 +183,9 @@ public void testIntAsLongArraySparse() throws Exception
177183
{
178184
ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_INT64_ARRAY_SPARSE);
179185
final ObjectWriter w = MAPPER.writer(schema);
180-
IntArray input = new IntArray(3, -1, -2);
186+
IntArray input = new IntArray(3, -1, -2225, 1235909);
181187
byte[] bytes = w.writeValueAsBytes(input);
182-
// 3 x 9 bytes per value (typed tag, value) -> 30
183-
assertEquals(27, bytes.length);
188+
assertEquals(36, bytes.length);
184189

185190
IntArray result = MAPPER.readerFor(IntArray.class).with(schema)
186191
.readValue(bytes);
@@ -191,10 +196,10 @@ public void testIntAsLongArrayPacked() throws Exception
191196
{
192197
ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_INT64_ARRAY_PACKED);
193198
final ObjectWriter w = MAPPER.writer(schema);
194-
IntArray input = new IntArray(3, -1, -2);
199+
IntArray input = new IntArray(3, -1, -2225, 1235909);
195200
byte[] bytes = w.writeValueAsBytes(input);
196201
// 1 byte for typed tag, 1 byte for length, 3 x 8 byte per value -> 26
197-
assertEquals(26, bytes.length);
202+
assertEquals(34, bytes.length);
198203

199204
IntArray result = MAPPER.readerFor(IntArray.class).with(schema)
200205
.readValue(bytes);
@@ -207,28 +212,67 @@ public void testLongArraySparse() throws Exception
207212
{
208213
ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_INT64_ARRAY_SPARSE);
209214
final ObjectWriter w = MAPPER.writer(schema);
210-
LongArray input = new LongArray(Integer.MAX_VALUE, -1, Long.MIN_VALUE);
215+
LongArray input = new LongArray(Integer.MAX_VALUE, -1, 3L + Integer.MAX_VALUE, Long.MIN_VALUE);
211216
byte[] bytes = w.writeValueAsBytes(input);
212-
assertEquals(27, bytes.length);
217+
assertEquals(36, bytes.length);
213218

214219
LongArray result = MAPPER.readerFor(LongArray.class).with(schema)
215220
.readValue(bytes);
216221
Assert.assertArrayEquals(input.values, result.values);
222+
223+
_verifyLongArray(bytes, schema, input.values);
217224
}
218225

219226
public void testLongArrayPacked() throws Exception
220227
{
221228
ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_INT64_ARRAY_PACKED);
222229
final ObjectWriter w = MAPPER.writer(schema);
223-
LongArray input = new LongArray(Integer.MIN_VALUE, -1, Long.MAX_VALUE);
230+
LongArray input = new LongArray(Integer.MIN_VALUE, 7L + Integer.MAX_VALUE, -1, Long.MAX_VALUE);
224231
byte[] bytes = w.writeValueAsBytes(input);
225-
assertEquals(26, bytes.length);
232+
assertEquals(34, bytes.length);
226233

227234
LongArray result = MAPPER.readerFor(LongArray.class).with(schema)
228235
.readValue(bytes);
229236
Assert.assertArrayEquals(input.values, result.values);
237+
238+
_verifyLongArray(bytes, schema, input.values);
230239
}
231240

241+
private void _verifyLongArray(byte[] doc, ProtobufSchema schema,
242+
long[] inputValues)
243+
throws Exception
244+
{
245+
// also via streaming API
246+
JsonParser p = MAPPER.getFactory().createParser(doc);
247+
p.setSchema(schema);
248+
249+
assertToken(JsonToken.START_OBJECT, p.nextToken());
250+
assertToken(JsonToken.FIELD_NAME, p.nextToken());
251+
assertEquals("values", p.getCurrentName());
252+
253+
assertToken(JsonToken.START_ARRAY, p.nextToken());
254+
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
255+
assertEquals(NumberType.LONG, p.getNumberType());
256+
assertEquals(Long.valueOf(inputValues[0]), p.getNumberValue());
257+
assertFalse(p.isNaN());
258+
assertEquals(BigInteger.valueOf(inputValues[0]), p.getBigIntegerValue());
259+
260+
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
261+
// skip value
262+
assertNull(p.nextFieldName()); // just for funsies
263+
264+
assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
265+
assertEquals(inputValues[3], p.getLongValue());
266+
assertEquals((double) inputValues[3], p.getDoubleValue());
267+
assertEquals((float) inputValues[3], p.getFloatValue());
268+
269+
assertToken(JsonToken.END_ARRAY, p.nextToken());
270+
271+
assertToken(JsonToken.END_OBJECT, p.nextToken());
272+
273+
p.close();
274+
}
275+
232276
/*
233277
/**********************************************************
234278
/* Test methods, floating-point arrays
@@ -246,6 +290,8 @@ public void testDoubleArraySparse() throws Exception
246290
DoubleArray result = MAPPER.readerFor(DoubleArray.class).with(schema)
247291
.readValue(bytes);
248292
_assertEquals(input.values, result.values);
293+
294+
_verifyDoubleArray(bytes, schema, input.values);
249295
}
250296

251297
public void testDoubleArrayPacked() throws Exception
@@ -259,6 +305,8 @@ public void testDoubleArrayPacked() throws Exception
259305
DoubleArray result = MAPPER.readerFor(DoubleArray.class).with(schema)
260306
.readValue(bytes);
261307
_assertEquals(input.values, result.values);
308+
309+
_verifyDoubleArray(bytes, schema, input.values);
262310
}
263311

264312
private void _assertEquals(double[] exp, double[] act)
@@ -272,6 +320,38 @@ private void _assertEquals(double[] exp, double[] act)
272320
}
273321
}
274322

323+
private void _verifyDoubleArray(byte[] doc, ProtobufSchema schema,
324+
double[] inputValues)
325+
throws Exception
326+
{
327+
// also via streaming API
328+
JsonParser p = MAPPER.getFactory().createParser(doc);
329+
p.setSchema(schema);
330+
331+
assertToken(JsonToken.START_OBJECT, p.nextToken());
332+
assertToken(JsonToken.FIELD_NAME, p.nextToken());
333+
assertEquals("values", p.getCurrentName());
334+
335+
assertToken(JsonToken.START_ARRAY, p.nextToken());
336+
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
337+
assertEquals(NumberType.DOUBLE, p.getNumberType());
338+
assertEquals(Double.valueOf(inputValues[0]), p.getNumberValue());
339+
assertFalse(p.isNaN());
340+
assertEquals(new BigDecimal(inputValues[0]), p.getDecimalValue());
341+
342+
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
343+
assertNull(p.nextFieldName()); // just for funsies
344+
345+
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
346+
assertEquals(Double.valueOf(inputValues[3]), p.getDoubleValue());
347+
assertEquals((long) inputValues[3], p.getLongValue());
348+
assertToken(JsonToken.END_ARRAY, p.nextToken());
349+
350+
assertToken(JsonToken.END_OBJECT, p.nextToken());
351+
352+
p.close();
353+
}
354+
275355
public void testFloatArraySparse() throws Exception
276356
{
277357
ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_FLOAT_ARRAY_SPARSE);
@@ -283,6 +363,8 @@ public void testFloatArraySparse() throws Exception
283363
FloatArray result = MAPPER.readerFor(FloatArray.class).with(schema)
284364
.readValue(bytes);
285365
_assertEquals(input.values, result.values);
366+
367+
_verifyFloatArray(bytes, schema, input.values);
286368
}
287369

288370
public void testFloatArrayPacked() throws Exception
@@ -296,8 +378,40 @@ public void testFloatArrayPacked() throws Exception
296378
FloatArray result = MAPPER.readerFor(FloatArray.class).with(schema)
297379
.readValue(bytes);
298380
_assertEquals(input.values, result.values);
381+
382+
_verifyFloatArray(bytes, schema, input.values);
299383
}
300384

385+
private void _verifyFloatArray(byte[] doc, ProtobufSchema schema,
386+
float[] inputValues)
387+
throws Exception
388+
{
389+
// also via streaming API
390+
JsonParser p = MAPPER.getFactory().createParser(doc);
391+
p.setSchema(schema);
392+
393+
assertToken(JsonToken.START_OBJECT, p.nextToken());
394+
assertToken(JsonToken.FIELD_NAME, p.nextToken());
395+
assertEquals("values", p.getCurrentName());
396+
397+
assertToken(JsonToken.START_ARRAY, p.nextToken());
398+
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
399+
assertEquals(NumberType.FLOAT, p.getNumberType());
400+
assertEquals(Float.valueOf(inputValues[0]), p.getNumberValue());
401+
assertFalse(p.isNaN());
402+
403+
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
404+
405+
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
406+
assertEquals(Float.valueOf(inputValues[2]), p.getFloatValue());
407+
assertEquals((int) inputValues[2], p.getIntValue());
408+
assertToken(JsonToken.END_ARRAY, p.nextToken());
409+
410+
assertToken(JsonToken.END_OBJECT, p.nextToken());
411+
412+
p.close();
413+
}
414+
301415
private void _assertEquals(float[] exp, float[] act)
302416
{
303417
assertEquals(exp.length, act.length);

0 commit comments

Comments
 (0)