Skip to content

Commit 5505124

Browse files
committed
Refactor the test
1 parent 906a5fd commit 5505124

File tree

1 file changed

+65
-47
lines changed

1 file changed

+65
-47
lines changed

msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackFactoryTest.java

Lines changed: 65 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.fasterxml.jackson.core.JsonFactory;
2020
import com.fasterxml.jackson.core.JsonGenerator;
2121
import com.fasterxml.jackson.core.JsonParser;
22+
import com.fasterxml.jackson.core.JsonProcessingException;
2223
import com.fasterxml.jackson.core.type.TypeReference;
2324
import com.fasterxml.jackson.databind.AnnotationIntrospector;
2425
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -58,76 +59,93 @@ public void testCreateParser()
5859
assertEquals(MessagePackParser.class, parser.getClass());
5960
}
6061

61-
@Test
62-
public void copyWithDefaultConfig()
62+
private void assertCopy(boolean advancedConfig)
6363
throws IOException
6464
{
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);
7070

71-
assertThat(copiedMessagePackFactory.getPackerConfig().isStr8FormatSupport(), is(true));
71+
MessagePack.PackerConfig msgpackPackerConfig = new MessagePack.PackerConfig().withStr8FormatSupport(false);
7272

73-
assertThat(copiedMessagePackFactory.getExtTypeCustomDesers(), is(nullValue()));
73+
MessagePackFactory messagePackFactory = new MessagePackFactory(msgpackPackerConfig);
74+
messagePackFactory.setExtTypeCustomDesers(extTypeCustomDesers);
7475

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);
7777

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);
8180

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+
}
8987

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+
}
9693

97-
MessagePack.PackerConfig msgpackPackerConfig = new MessagePack.PackerConfig().withStr8FormatSupport(false);
94+
// Copy the ObjectMapper
95+
ObjectMapper copiedObjectMapper = objectMapper.copy();
9896

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;
101101

102-
ObjectMapper objectMapper = new ObjectMapper(messagePackFactory);
102+
Collection<AnnotationIntrospector> annotationIntrospectors =
103+
copiedObjectMapper.getSerializationConfig().getAnnotationIntrospector().allIntrospectors();
104+
assertThat(annotationIntrospectors.size(), is(1));
103105

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));
106108

107-
objectMapper.setAnnotationIntrospector(new JsonArrayFormat());
109+
assertThat(copiedMessagePackFactory.getExtTypeCustomDesers().getDeser((byte) 42), is(notNullValue()));
110+
assertThat(copiedMessagePackFactory.getExtTypeCustomDesers().getDeser((byte) 43), is(nullValue()));
108111

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));
113114

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));
115119

116-
assertThat(copiedMessagePackFactory.getExtTypeCustomDesers().getDeser((byte) 42), is(notNullValue()));
117-
assertThat(copiedMessagePackFactory.getExtTypeCustomDesers().getDeser((byte) 43), is(nullValue()));
120+
assertThat(copiedMessagePackFactory.getExtTypeCustomDesers(), is(nullValue()));
118121

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));
121124

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+
}
125128

126-
HashMap<String, Integer> map = new HashMap<>();
129+
// Check the copied ObjectMapper works fine
130+
Map<String, Integer> map = new HashMap<>();
127131
map.put("one", 1);
128132
Map<String, Integer> deserialized = copiedObjectMapper
129133
.readValue(objectMapper.writeValueAsBytes(map), new TypeReference<Map<String, Integer>>() {});
130134
assertThat(deserialized.size(), is(1));
131135
assertThat(deserialized.get("one"), is(1));
132136
}
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+
}
133151
}

0 commit comments

Comments
 (0)