|
22 | 22 | import com.fasterxml.jackson.core.type.TypeReference; |
23 | 23 | import com.fasterxml.jackson.databind.AnnotationIntrospector; |
24 | 24 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 25 | +import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector; |
25 | 26 | import org.junit.Test; |
26 | 27 | import org.msgpack.core.MessagePack; |
27 | 28 |
|
@@ -57,47 +58,93 @@ public void testCreateParser() |
57 | 58 | assertEquals(MessagePackParser.class, parser.getClass()); |
58 | 59 | } |
59 | 60 |
|
60 | | - @Test |
61 | | - public void copy() |
| 61 | + private void assertCopy(boolean advancedConfig) |
62 | 62 | throws IOException |
63 | 63 | { |
64 | | - ExtensionTypeCustomDeserializers extTypeCustomDesers = new ExtensionTypeCustomDeserializers(); |
65 | | - extTypeCustomDesers.addTargetClass((byte) 42, TinyPojo.class); |
| 64 | + // Build base ObjectMapper |
| 65 | + ObjectMapper objectMapper; |
| 66 | + if (advancedConfig) { |
| 67 | + ExtensionTypeCustomDeserializers extTypeCustomDesers = new ExtensionTypeCustomDeserializers(); |
| 68 | + extTypeCustomDesers.addTargetClass((byte) 42, TinyPojo.class); |
| 69 | + |
| 70 | + MessagePack.PackerConfig msgpackPackerConfig = new MessagePack.PackerConfig().withStr8FormatSupport(false); |
66 | 71 |
|
67 | | - MessagePack.PackerConfig msgpackPackerConfig = new MessagePack.PackerConfig().withStr8FormatSupport(false); |
| 72 | + MessagePackFactory messagePackFactory = new MessagePackFactory(msgpackPackerConfig); |
| 73 | + messagePackFactory.setExtTypeCustomDesers(extTypeCustomDesers); |
68 | 74 |
|
69 | | - MessagePackFactory messagePackFactory = new MessagePackFactory(msgpackPackerConfig); |
70 | | - messagePackFactory.setExtTypeCustomDesers(extTypeCustomDesers); |
| 75 | + objectMapper = new ObjectMapper(messagePackFactory); |
71 | 76 |
|
72 | | - ObjectMapper objectMapper = new ObjectMapper(messagePackFactory); |
| 77 | + objectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); |
| 78 | + objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false); |
73 | 79 |
|
74 | | - objectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); |
75 | | - objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false); |
| 80 | + objectMapper.setAnnotationIntrospector(new JsonArrayFormat()); |
| 81 | + } |
| 82 | + else { |
| 83 | + MessagePackFactory messagePackFactory = new MessagePackFactory(); |
| 84 | + objectMapper = new ObjectMapper(messagePackFactory); |
| 85 | + } |
76 | 86 |
|
77 | | - objectMapper.setAnnotationIntrospector(new JsonArrayFormat()); |
| 87 | + // Use the original ObjectMapper in advance |
| 88 | + { |
| 89 | + byte[] bytes = objectMapper.writeValueAsBytes(1234); |
| 90 | + assertThat(objectMapper.readValue(bytes, Integer.class), is(1234)); |
| 91 | + } |
78 | 92 |
|
| 93 | + // Copy the ObjectMapper |
79 | 94 | ObjectMapper copiedObjectMapper = objectMapper.copy(); |
| 95 | + |
| 96 | + // Assert the copied ObjectMapper |
80 | 97 | JsonFactory copiedFactory = copiedObjectMapper.getFactory(); |
81 | 98 | assertThat(copiedFactory, is(instanceOf(MessagePackFactory.class))); |
82 | 99 | MessagePackFactory copiedMessagePackFactory = (MessagePackFactory) copiedFactory; |
83 | 100 |
|
84 | | - assertThat(copiedMessagePackFactory.getPackerConfig().isStr8FormatSupport(), is(false)); |
| 101 | + Collection<AnnotationIntrospector> annotationIntrospectors = |
| 102 | + copiedObjectMapper.getSerializationConfig().getAnnotationIntrospector().allIntrospectors(); |
| 103 | + assertThat(annotationIntrospectors.size(), is(1)); |
85 | 104 |
|
86 | | - assertThat(copiedMessagePackFactory.getExtTypeCustomDesers().getDeser((byte) 42), is(notNullValue())); |
87 | | - assertThat(copiedMessagePackFactory.getExtTypeCustomDesers().getDeser((byte) 43), is(nullValue())); |
| 105 | + if (advancedConfig) { |
| 106 | + assertThat(copiedMessagePackFactory.getPackerConfig().isStr8FormatSupport(), is(false)); |
88 | 107 |
|
89 | | - assertThat(copiedMessagePackFactory.isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET), is(false)); |
90 | | - assertThat(copiedMessagePackFactory.isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE), is(false)); |
| 108 | + assertThat(copiedMessagePackFactory.getExtTypeCustomDesers().getDeser((byte) 42), is(notNullValue())); |
| 109 | + assertThat(copiedMessagePackFactory.getExtTypeCustomDesers().getDeser((byte) 43), is(nullValue())); |
91 | 110 |
|
92 | | - Collection<AnnotationIntrospector> annotationIntrospectors = copiedObjectMapper.getSerializationConfig().getAnnotationIntrospector().allIntrospectors(); |
93 | | - assertThat(annotationIntrospectors.size(), is(1)); |
94 | | - assertThat(annotationIntrospectors.stream().findFirst().get(), is(instanceOf(JsonArrayFormat.class))); |
| 111 | + assertThat(copiedMessagePackFactory.isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET), is(false)); |
| 112 | + assertThat(copiedMessagePackFactory.isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE), is(false)); |
95 | 113 |
|
96 | | - HashMap<String, Integer> map = new HashMap<>(); |
| 114 | + assertThat(annotationIntrospectors.stream().findFirst().get(), is(instanceOf(JsonArrayFormat.class))); |
| 115 | + } |
| 116 | + else { |
| 117 | + assertThat(copiedMessagePackFactory.getPackerConfig().isStr8FormatSupport(), is(true)); |
| 118 | + |
| 119 | + assertThat(copiedMessagePackFactory.getExtTypeCustomDesers(), is(nullValue())); |
| 120 | + |
| 121 | + assertThat(copiedMessagePackFactory.isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET), is(true)); |
| 122 | + assertThat(copiedMessagePackFactory.isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE), is(true)); |
| 123 | + |
| 124 | + assertThat(annotationIntrospectors.stream().findFirst().get(), |
| 125 | + is(instanceOf(JacksonAnnotationIntrospector.class))); |
| 126 | + } |
| 127 | + |
| 128 | + // Check the copied ObjectMapper works fine |
| 129 | + Map<String, Integer> map = new HashMap<>(); |
97 | 130 | map.put("one", 1); |
98 | 131 | Map<String, Integer> deserialized = copiedObjectMapper |
99 | 132 | .readValue(objectMapper.writeValueAsBytes(map), new TypeReference<Map<String, Integer>>() {}); |
100 | 133 | assertThat(deserialized.size(), is(1)); |
101 | 134 | assertThat(deserialized.get("one"), is(1)); |
102 | 135 | } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void copyWithDefaultConfig() |
| 139 | + throws IOException |
| 140 | + { |
| 141 | + assertCopy(false); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void copyWithAdvancedConfig() |
| 146 | + throws IOException |
| 147 | + { |
| 148 | + assertCopy(true); |
| 149 | + } |
103 | 150 | } |
0 commit comments