1
1
package com .fasterxml .jackson .dataformat .protobuf ;
2
2
3
+ import java .math .BigDecimal ;
4
+ import java .math .BigInteger ;
5
+
3
6
import org .junit .Assert ;
4
7
8
+ import com .fasterxml .jackson .core .JsonParser ;
9
+ import com .fasterxml .jackson .core .JsonToken ;
10
+ import com .fasterxml .jackson .core .JsonParser .NumberType ;
5
11
import com .fasterxml .jackson .databind .ObjectMapper ;
6
12
import com .fasterxml .jackson .databind .ObjectWriter ;
7
13
import com .fasterxml .jackson .dataformat .protobuf .schema .ProtobufSchema ;
@@ -177,10 +183,9 @@ public void testIntAsLongArraySparse() throws Exception
177
183
{
178
184
ProtobufSchema schema = ProtobufSchemaLoader .std .parse (PROTOC_INT64_ARRAY_SPARSE );
179
185
final ObjectWriter w = MAPPER .writer (schema );
180
- IntArray input = new IntArray (3 , -1 , -2 );
186
+ IntArray input = new IntArray (3 , -1 , -2225 , 1235909 );
181
187
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 );
184
189
185
190
IntArray result = MAPPER .readerFor (IntArray .class ).with (schema )
186
191
.readValue (bytes );
@@ -191,10 +196,10 @@ public void testIntAsLongArrayPacked() throws Exception
191
196
{
192
197
ProtobufSchema schema = ProtobufSchemaLoader .std .parse (PROTOC_INT64_ARRAY_PACKED );
193
198
final ObjectWriter w = MAPPER .writer (schema );
194
- IntArray input = new IntArray (3 , -1 , -2 );
199
+ IntArray input = new IntArray (3 , -1 , -2225 , 1235909 );
195
200
byte [] bytes = w .writeValueAsBytes (input );
196
201
// 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 );
198
203
199
204
IntArray result = MAPPER .readerFor (IntArray .class ).with (schema )
200
205
.readValue (bytes );
@@ -207,28 +212,67 @@ public void testLongArraySparse() throws Exception
207
212
{
208
213
ProtobufSchema schema = ProtobufSchemaLoader .std .parse (PROTOC_INT64_ARRAY_SPARSE );
209
214
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 );
211
216
byte [] bytes = w .writeValueAsBytes (input );
212
- assertEquals (27 , bytes .length );
217
+ assertEquals (36 , bytes .length );
213
218
214
219
LongArray result = MAPPER .readerFor (LongArray .class ).with (schema )
215
220
.readValue (bytes );
216
221
Assert .assertArrayEquals (input .values , result .values );
222
+
223
+ _verifyLongArray (bytes , schema , input .values );
217
224
}
218
225
219
226
public void testLongArrayPacked () throws Exception
220
227
{
221
228
ProtobufSchema schema = ProtobufSchemaLoader .std .parse (PROTOC_INT64_ARRAY_PACKED );
222
229
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 );
224
231
byte [] bytes = w .writeValueAsBytes (input );
225
- assertEquals (26 , bytes .length );
232
+ assertEquals (34 , bytes .length );
226
233
227
234
LongArray result = MAPPER .readerFor (LongArray .class ).with (schema )
228
235
.readValue (bytes );
229
236
Assert .assertArrayEquals (input .values , result .values );
237
+
238
+ _verifyLongArray (bytes , schema , input .values );
230
239
}
231
240
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
+
232
276
/*
233
277
/**********************************************************
234
278
/* Test methods, floating-point arrays
@@ -246,6 +290,8 @@ public void testDoubleArraySparse() throws Exception
246
290
DoubleArray result = MAPPER .readerFor (DoubleArray .class ).with (schema )
247
291
.readValue (bytes );
248
292
_assertEquals (input .values , result .values );
293
+
294
+ _verifyDoubleArray (bytes , schema , input .values );
249
295
}
250
296
251
297
public void testDoubleArrayPacked () throws Exception
@@ -259,6 +305,8 @@ public void testDoubleArrayPacked() throws Exception
259
305
DoubleArray result = MAPPER .readerFor (DoubleArray .class ).with (schema )
260
306
.readValue (bytes );
261
307
_assertEquals (input .values , result .values );
308
+
309
+ _verifyDoubleArray (bytes , schema , input .values );
262
310
}
263
311
264
312
private void _assertEquals (double [] exp , double [] act )
@@ -272,6 +320,38 @@ private void _assertEquals(double[] exp, double[] act)
272
320
}
273
321
}
274
322
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
+
275
355
public void testFloatArraySparse () throws Exception
276
356
{
277
357
ProtobufSchema schema = ProtobufSchemaLoader .std .parse (PROTOC_FLOAT_ARRAY_SPARSE );
@@ -283,6 +363,8 @@ public void testFloatArraySparse() throws Exception
283
363
FloatArray result = MAPPER .readerFor (FloatArray .class ).with (schema )
284
364
.readValue (bytes );
285
365
_assertEquals (input .values , result .values );
366
+
367
+ _verifyFloatArray (bytes , schema , input .values );
286
368
}
287
369
288
370
public void testFloatArrayPacked () throws Exception
@@ -296,8 +378,40 @@ public void testFloatArrayPacked() throws Exception
296
378
FloatArray result = MAPPER .readerFor (FloatArray .class ).with (schema )
297
379
.readValue (bytes );
298
380
_assertEquals (input .values , result .values );
381
+
382
+ _verifyFloatArray (bytes , schema , input .values );
299
383
}
300
384
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
+
301
415
private void _assertEquals (float [] exp , float [] act )
302
416
{
303
417
assertEquals (exp .length , act .length );
0 commit comments