1616package org .msgpack .jackson .dataformat ;
1717
1818import com .fasterxml .jackson .core .JsonEncoding ;
19+ import com .fasterxml .jackson .core .JsonFactory ;
1920import com .fasterxml .jackson .core .JsonGenerator ;
2021import com .fasterxml .jackson .core .JsonParser ;
22+ import com .fasterxml .jackson .core .type .TypeReference ;
23+ import com .fasterxml .jackson .databind .AnnotationIntrospector ;
24+ import com .fasterxml .jackson .databind .ObjectMapper ;
2125import org .junit .Test ;
26+ import org .msgpack .core .MessagePack ;
2227
2328import java .io .IOException ;
29+ import java .util .Collection ;
30+ import java .util .HashMap ;
31+ import java .util .Map ;
2432
33+ import static org .hamcrest .CoreMatchers .instanceOf ;
34+ import static org .hamcrest .CoreMatchers .is ;
35+ import static org .hamcrest .CoreMatchers .notNullValue ;
36+ import static org .hamcrest .CoreMatchers .nullValue ;
2537import static org .junit .Assert .assertEquals ;
38+ import static org .junit .Assert .assertThat ;
2639
2740public class MessagePackFactoryTest
2841 extends MessagePackDataformatTestBase
@@ -43,4 +56,48 @@ public void testCreateParser()
4356 JsonParser parser = factory .createParser (in );
4457 assertEquals (MessagePackParser .class , parser .getClass ());
4558 }
59+
60+ @ Test
61+ public void copy ()
62+ throws IOException
63+ {
64+ ExtensionTypeCustomDeserializers extTypeCustomDesers = new ExtensionTypeCustomDeserializers ();
65+ extTypeCustomDesers .addTargetClass ((byte ) 42 , TinyPojo .class );
66+
67+ MessagePack .PackerConfig msgpackPackerConfig = new MessagePack .PackerConfig ().withStr8FormatSupport (false );
68+
69+ MessagePackFactory messagePackFactory = new MessagePackFactory (msgpackPackerConfig );
70+ messagePackFactory .setExtTypeCustomDesers (extTypeCustomDesers );
71+
72+ ObjectMapper objectMapper = new ObjectMapper (messagePackFactory );
73+
74+ objectMapper .configure (JsonGenerator .Feature .AUTO_CLOSE_TARGET , false );
75+ objectMapper .configure (JsonParser .Feature .AUTO_CLOSE_SOURCE , false );
76+
77+ objectMapper .setAnnotationIntrospector (new JsonArrayFormat ());
78+
79+ ObjectMapper copiedObjectMapper = objectMapper .copy ();
80+ JsonFactory copiedFactory = copiedObjectMapper .getFactory ();
81+ assertThat (copiedFactory , is (instanceOf (MessagePackFactory .class )));
82+ MessagePackFactory copiedMessagePackFactory = (MessagePackFactory ) copiedFactory ;
83+
84+ assertThat (copiedMessagePackFactory .getPackerConfig ().isStr8FormatSupport (), is (false ));
85+
86+ assertThat (copiedMessagePackFactory .getExtTypeCustomDesers ().getDeser ((byte ) 42 ), is (notNullValue ()));
87+ assertThat (copiedMessagePackFactory .getExtTypeCustomDesers ().getDeser ((byte ) 43 ), is (nullValue ()));
88+
89+ assertThat (copiedMessagePackFactory .isEnabled (JsonGenerator .Feature .AUTO_CLOSE_TARGET ), is (false ));
90+ assertThat (copiedMessagePackFactory .isEnabled (JsonParser .Feature .AUTO_CLOSE_SOURCE ), is (false ));
91+
92+ Collection <AnnotationIntrospector > annotationIntrospectors = copiedObjectMapper .getSerializationConfig ().getAnnotationIntrospector ().allIntrospectors ();
93+ assertThat (annotationIntrospectors .size (), is (1 ));
94+ assertThat (annotationIntrospectors .stream ().findFirst ().get (), is (instanceOf (JsonArrayFormat .class )));
95+
96+ HashMap <String , Integer > map = new HashMap <>();
97+ map .put ("one" , 1 );
98+ Map <String , Integer > deserialized = copiedObjectMapper
99+ .readValue (objectMapper .writeValueAsBytes (map ), new TypeReference <Map <String , Integer >>() {});
100+ assertThat (deserialized .size (), is (1 ));
101+ assertThat (deserialized .get ("one" ), is (1 ));
102+ }
46103}
0 commit comments