1818import com .fasterxml .jackson .core .JsonParser ;
1919import com .fasterxml .jackson .core .JsonProcessingException ;
2020import com .fasterxml .jackson .core .JsonToken ;
21+ import com .fasterxml .jackson .core .io .JsonEOFException ;
2122import com .fasterxml .jackson .core .type .TypeReference ;
2223import com .fasterxml .jackson .databind .DeserializationContext ;
2324import com .fasterxml .jackson .databind .DeserializationFeature ;
25+ import com .fasterxml .jackson .databind .JsonMappingException ;
2426import com .fasterxml .jackson .databind .KeyDeserializer ;
2527import com .fasterxml .jackson .databind .ObjectMapper ;
2628import com .fasterxml .jackson .databind .module .SimpleModule ;
4850import static org .hamcrest .core .IsInstanceOf .instanceOf ;
4951import static org .junit .Assert .assertArrayEquals ;
5052import static org .junit .Assert .assertEquals ;
51- import static org .junit .Assert .assertNull ;
5253import static org .junit .Assert .assertTrue ;
54+ import static org .junit .Assert .fail ;
5355
5456public class MessagePackParserTest
5557 extends MessagePackDataformatTestBase
@@ -348,8 +350,6 @@ public void testMessagePackParserDirectly()
348350 assertEquals (-1 , parser .getCurrentLocation ().getLineNr ());
349351 assertEquals (16 , parser .getCurrentLocation ().getColumnNr ());
350352
351- assertNull (parser .nextToken ());
352-
353353 parser .close ();
354354 parser .close (); // Intentional
355355 }
@@ -555,8 +555,8 @@ public void testByteArrayKey()
555555 MessagePacker messagePacker = MessagePack .newDefaultPacker (out ).packMapHeader (2 );
556556 byte [] k0 = new byte [] {0 };
557557 byte [] k1 = new byte [] {1 };
558- messagePacker .packBinaryHeader (1 ).writePayload (k0 ).packInt (2 );
559- messagePacker .packBinaryHeader (1 ).writePayload (k1 ).packInt (3 );
558+ messagePacker .packBinaryHeader (1 ).writePayload (k0 ).packInt (10 );
559+ messagePacker .packBinaryHeader (1 ).writePayload (k1 ).packInt (11 );
560560 messagePacker .close ();
561561
562562 ObjectMapper objectMapper = new ObjectMapper (new MessagePackFactory ());
@@ -577,10 +577,10 @@ public Object deserializeKey(String key, DeserializationContext ctxt)
577577 assertEquals (2 , map .size ());
578578 for (Map .Entry <byte [], Integer > entry : map .entrySet ()) {
579579 if (Arrays .equals (entry .getKey (), k0 )) {
580- assertEquals ((Integer ) 2 , entry .getValue ());
580+ assertEquals ((Integer ) 10 , entry .getValue ());
581581 }
582582 else if (Arrays .equals (entry .getKey (), k1 )) {
583- assertEquals ((Integer ) 3 , entry .getValue ());
583+ assertEquals ((Integer ) 11 , entry .getValue ());
584584 }
585585 }
586586 }
@@ -590,9 +590,9 @@ public void testIntegerKey()
590590 throws IOException
591591 {
592592 ByteArrayOutputStream out = new ByteArrayOutputStream ();
593- MessagePacker messagePacker = MessagePack .newDefaultPacker (out ).packMapHeader (3 );
593+ MessagePacker messagePacker = MessagePack .newDefaultPacker (out ).packMapHeader (2 );
594594 for (int i = 0 ; i < 2 ; i ++) {
595- messagePacker .packInt (i ).packInt (i + 2 );
595+ messagePacker .packInt (i ).packInt (i + 10 );
596596 }
597597 messagePacker .close ();
598598
@@ -612,18 +612,18 @@ public Object deserializeKey(String key, DeserializationContext ctxt)
612612 Map <Integer , Integer > map = objectMapper .readValue (
613613 out .toByteArray (), new TypeReference <Map <Integer , Integer >>() {});
614614 assertEquals (2 , map .size ());
615- assertEquals ((Integer ) 2 , map .get (0 ));
616- assertEquals ((Integer ) 3 , map .get (1 ));
615+ assertEquals ((Integer ) 10 , map .get (0 ));
616+ assertEquals ((Integer ) 11 , map .get (1 ));
617617 }
618618
619619 @ Test
620620 public void testFloatKey ()
621621 throws IOException
622622 {
623623 ByteArrayOutputStream out = new ByteArrayOutputStream ();
624- MessagePacker messagePacker = MessagePack .newDefaultPacker (out ).packMapHeader (3 );
624+ MessagePacker messagePacker = MessagePack .newDefaultPacker (out ).packMapHeader (2 );
625625 for (int i = 0 ; i < 2 ; i ++) {
626- messagePacker .packFloat (i ).packInt (i + 2 );
626+ messagePacker .packFloat (i ).packInt (i + 10 );
627627 }
628628 messagePacker .close ();
629629
@@ -643,18 +643,18 @@ public Object deserializeKey(String key, DeserializationContext ctxt)
643643 Map <Float , Integer > map = objectMapper .readValue (
644644 out .toByteArray (), new TypeReference <Map <Float , Integer >>() {});
645645 assertEquals (2 , map .size ());
646- assertEquals ((Integer ) 2 , map .get (0f ));
647- assertEquals ((Integer ) 3 , map .get (1f ));
646+ assertEquals ((Integer ) 10 , map .get (0f ));
647+ assertEquals ((Integer ) 11 , map .get (1f ));
648648 }
649649
650650 @ Test
651651 public void testBooleanKey ()
652652 throws IOException
653653 {
654654 ByteArrayOutputStream out = new ByteArrayOutputStream ();
655- MessagePacker messagePacker = MessagePack .newDefaultPacker (out ).packMapHeader (3 );
656- messagePacker .packBoolean (true ).packInt (2 );
657- messagePacker .packBoolean (false ).packInt (3 );
655+ MessagePacker messagePacker = MessagePack .newDefaultPacker (out ).packMapHeader (2 );
656+ messagePacker .packBoolean (true ).packInt (10 );
657+ messagePacker .packBoolean (false ).packInt (11 );
658658 messagePacker .close ();
659659
660660 ObjectMapper objectMapper = new ObjectMapper (new MessagePackFactory ());
@@ -673,8 +673,8 @@ public Object deserializeKey(String key, DeserializationContext ctxt)
673673 Map <Boolean , Integer > map = objectMapper .readValue (
674674 out .toByteArray (), new TypeReference <Map <Boolean , Integer >>() {});
675675 assertEquals (2 , map .size ());
676- assertEquals ((Integer ) 2 , map .get (true ));
677- assertEquals ((Integer ) 3 , map .get (false ));
676+ assertEquals ((Integer ) 10 , map .get (true ));
677+ assertEquals ((Integer ) 11 , map .get (false ));
678678 }
679679
680680 @ Test
@@ -851,4 +851,66 @@ public void deserializeStringAsBigDecimal()
851851 BigDecimal v = objectMapper .readValue (out .toByteArray (), BigDecimal .class );
852852 assertThat (v , is (bd ));
853853 }
854+
855+ @ Test
856+ public void handleMissingItemInArray ()
857+ throws IOException
858+ {
859+ MessagePacker packer = MessagePack .newDefaultPacker (out );
860+ packer .packArrayHeader (3 );
861+ packer .packString ("one" );
862+ packer .packString ("two" );
863+ packer .close ();
864+
865+ try {
866+ objectMapper .readValue (out .toByteArray (), new TypeReference <List <String >>() {});
867+ fail ();
868+ }
869+ catch (JsonMappingException e ) {
870+ assertTrue (e .getCause () instanceof JsonEOFException );
871+ }
872+ }
873+
874+ @ Test
875+ public void handleMissingKeyValueInMap ()
876+ throws IOException
877+ {
878+ MessagePacker packer = MessagePack .newDefaultPacker (out );
879+ packer .packMapHeader (3 );
880+ packer .packString ("one" );
881+ packer .packInt (1 );
882+ packer .packString ("two" );
883+ packer .packInt (2 );
884+ packer .close ();
885+
886+ try {
887+ objectMapper .readValue (out .toByteArray (), new TypeReference <Map <String , Integer >>() {});
888+ fail ();
889+ }
890+ catch (JsonEOFException e ) {
891+ assertTrue (true );
892+ }
893+ }
894+
895+ @ Test
896+ public void handleMissingValueInMap ()
897+ throws IOException
898+ {
899+ MessagePacker packer = MessagePack .newDefaultPacker (out );
900+ packer .packMapHeader (3 );
901+ packer .packString ("one" );
902+ packer .packInt (1 );
903+ packer .packString ("two" );
904+ packer .packInt (2 );
905+ packer .packString ("three" );
906+ packer .close ();
907+
908+ try {
909+ objectMapper .readValue (out .toByteArray (), new TypeReference <Map <String , Integer >>() {});
910+ fail ();
911+ }
912+ catch (JsonEOFException e ) {
913+ assertTrue (true );
914+ }
915+ }
854916}
0 commit comments