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