Polish and simplify Jackson2ObjectMapperFactoryBeanTests
This commit is contained in:
@@ -28,8 +28,15 @@ import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.FatalBeanException;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFilter;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
@@ -57,44 +64,34 @@ import com.fasterxml.jackson.databind.ser.std.ClassSerializer;
|
||||
import com.fasterxml.jackson.databind.ser.std.NumberSerializer;
|
||||
import com.fasterxml.jackson.databind.type.SimpleType;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.FatalBeanException;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Test cases for {@link Jackson2ObjectMapperFactoryBean} class.
|
||||
* Test cases for {@link Jackson2ObjectMapperFactoryBean}.
|
||||
*
|
||||
* @author <a href="mailto:dmitry.katsubo@gmail.com">Dmitry Katsubo</a>
|
||||
* @author Dmitry Katsubo
|
||||
* @author Brian Clozel
|
||||
* @author Sebastien Deleuze
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class Jackson2ObjectMapperFactoryBeanTests {
|
||||
|
||||
private static final String DATE_FORMAT = "yyyy-MM-dd";
|
||||
|
||||
private Jackson2ObjectMapperFactoryBean factory;
|
||||
private final SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
factory = new Jackson2ObjectMapperFactoryBean();
|
||||
}
|
||||
private final Jackson2ObjectMapperFactoryBean factory = new Jackson2ObjectMapperFactoryBean();
|
||||
|
||||
|
||||
@Test
|
||||
public void settersWithNullValues() {
|
||||
// Should not crash:
|
||||
factory.setSerializers((JsonSerializer<?>[]) null);
|
||||
factory.setSerializersByType(null);
|
||||
factory.setDeserializersByType(null);
|
||||
factory.setFeaturesToEnable((Object[]) null);
|
||||
factory.setFeaturesToDisable((Object[]) null);
|
||||
public void settingNullValuesShouldNotThrowExceptions() {
|
||||
this.factory.setSerializers((JsonSerializer<?>[]) null);
|
||||
this.factory.setSerializersByType(null);
|
||||
this.factory.setDeserializersByType(null);
|
||||
this.factory.setFeaturesToEnable((Object[]) null);
|
||||
this.factory.setFeaturesToDisable((Object[]) null);
|
||||
}
|
||||
|
||||
@Test(expected = FatalBeanException.class)
|
||||
@@ -121,63 +118,56 @@ public class Jackson2ObjectMapperFactoryBeanTests {
|
||||
assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION));
|
||||
assertFalse(objectMapper.getSerializationConfig().isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS));
|
||||
assertTrue(objectMapper.getSerializationConfig().isEnabled(SerializationFeature.INDENT_OUTPUT));
|
||||
assertTrue(objectMapper.getSerializationConfig().getSerializationInclusion() == JsonInclude.Include.ALWAYS);
|
||||
assertSame(Include.ALWAYS, objectMapper.getSerializationConfig().getSerializationInclusion());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setNotNullSerializationInclusion() {
|
||||
factory.afterPropertiesSet();
|
||||
assertTrue(factory.getObject().getSerializationConfig().getSerializationInclusion() == JsonInclude.Include.ALWAYS);
|
||||
|
||||
factory.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
factory.afterPropertiesSet();
|
||||
assertTrue(factory.getObject().getSerializationConfig().getSerializationInclusion() == JsonInclude.Include.NON_NULL);
|
||||
public void defaultSerializationInclusion() {
|
||||
this.factory.afterPropertiesSet();
|
||||
assertSame(Include.ALWAYS, this.factory.getObject().getSerializationConfig().getSerializationInclusion());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setNotDefaultSerializationInclusion() {
|
||||
factory.afterPropertiesSet();
|
||||
assertTrue(factory.getObject().getSerializationConfig().getSerializationInclusion() == JsonInclude.Include.ALWAYS);
|
||||
|
||||
factory.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
|
||||
factory.afterPropertiesSet();
|
||||
assertTrue(factory.getObject().getSerializationConfig().getSerializationInclusion() == JsonInclude.Include.NON_DEFAULT);
|
||||
public void nonNullSerializationInclusion() {
|
||||
this.factory.setSerializationInclusion(Include.NON_NULL);
|
||||
this.factory.afterPropertiesSet();
|
||||
assertSame(Include.NON_NULL, this.factory.getObject().getSerializationConfig().getSerializationInclusion());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setNotEmptySerializationInclusion() {
|
||||
factory.afterPropertiesSet();
|
||||
assertTrue(factory.getObject().getSerializationConfig().getSerializationInclusion() == JsonInclude.Include.ALWAYS);
|
||||
|
||||
factory.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
|
||||
factory.afterPropertiesSet();
|
||||
assertTrue(factory.getObject().getSerializationConfig().getSerializationInclusion() == JsonInclude.Include.NON_EMPTY);
|
||||
public void nonDefaultSerializationInclusion() {
|
||||
this.factory.setSerializationInclusion(Include.NON_DEFAULT);
|
||||
this.factory.afterPropertiesSet();
|
||||
assertSame(Include.NON_DEFAULT, this.factory.getObject().getSerializationConfig().getSerializationInclusion());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dateTimeFormatSetter() {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
|
||||
public void nonEmptySerializationInclusion() {
|
||||
this.factory.setSerializationInclusion(Include.NON_EMPTY);
|
||||
this.factory.afterPropertiesSet();
|
||||
assertSame(Include.NON_EMPTY, this.factory.getObject().getSerializationConfig().getSerializationInclusion());
|
||||
}
|
||||
|
||||
this.factory.setDateFormat(dateFormat);
|
||||
@Test
|
||||
public void setDateFormat() {
|
||||
this.factory.setDateFormat(this.dateFormat);
|
||||
this.factory.afterPropertiesSet();
|
||||
|
||||
assertEquals(dateFormat, this.factory.getObject().getSerializationConfig().getDateFormat());
|
||||
assertEquals(dateFormat, this.factory.getObject().getDeserializationConfig().getDateFormat());
|
||||
assertEquals(this.dateFormat, this.factory.getObject().getSerializationConfig().getDateFormat());
|
||||
assertEquals(this.dateFormat, this.factory.getObject().getDeserializationConfig().getDateFormat());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleDateFormatStringSetter() {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
|
||||
|
||||
public void setSimpleDateFormat() {
|
||||
this.factory.setSimpleDateFormat(DATE_FORMAT);
|
||||
this.factory.afterPropertiesSet();
|
||||
|
||||
assertEquals(dateFormat, this.factory.getObject().getSerializationConfig().getDateFormat());
|
||||
assertEquals(dateFormat, this.factory.getObject().getDeserializationConfig().getDateFormat());
|
||||
assertEquals(this.dateFormat, this.factory.getObject().getSerializationConfig().getDateFormat());
|
||||
assertEquals(this.dateFormat, this.factory.getObject().getDeserializationConfig().getDateFormat());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void localeSetter() {
|
||||
public void setLocale() {
|
||||
this.factory.setLocale(Locale.FRENCH);
|
||||
this.factory.afterPropertiesSet();
|
||||
|
||||
@@ -186,7 +176,7 @@ public class Jackson2ObjectMapperFactoryBeanTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void timeZoneSetter() {
|
||||
public void setTimeZone() {
|
||||
TimeZone timeZone = TimeZone.getTimeZone("Europe/Paris");
|
||||
|
||||
this.factory.setTimeZone(timeZone);
|
||||
@@ -197,22 +187,8 @@ public class Jackson2ObjectMapperFactoryBeanTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void timeZoneStringSetter() {
|
||||
String zoneId = "Europe/Paris";
|
||||
|
||||
this.factory.setTimeZone(TimeZone.getTimeZone(zoneId));
|
||||
this.factory.afterPropertiesSet();
|
||||
|
||||
TimeZone timeZone = TimeZone.getTimeZone(zoneId);
|
||||
assertEquals(timeZone, this.factory.getObject().getSerializationConfig().getTimeZone());
|
||||
assertEquals(timeZone, this.factory.getObject().getDeserializationConfig().getTimeZone());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void wrongTimeZoneStringSetter() {
|
||||
String zoneId = "foo";
|
||||
|
||||
this.factory.setTimeZone(TimeZone.getTimeZone(zoneId));
|
||||
public void setTimeZoneWithInvalidZoneId() {
|
||||
this.factory.setTimeZone(TimeZone.getTimeZone("bogusZoneId"));
|
||||
this.factory.afterPropertiesSet();
|
||||
|
||||
TimeZone timeZone = TimeZone.getTimeZone("GMT");
|
||||
@@ -222,16 +198,16 @@ public class Jackson2ObjectMapperFactoryBeanTests {
|
||||
|
||||
@Test
|
||||
public void setModules() {
|
||||
NumberSerializer serializer1 = new NumberSerializer(Integer.class);
|
||||
NumberSerializer serializer = new NumberSerializer(Integer.class);
|
||||
SimpleModule module = new SimpleModule();
|
||||
module.addSerializer(Integer.class, serializer1);
|
||||
module.addSerializer(Integer.class, serializer);
|
||||
|
||||
this.factory.setModules(Arrays.asList(new Module[]{module}));
|
||||
this.factory.afterPropertiesSet();
|
||||
ObjectMapper objectMapper = this.factory.getObject();
|
||||
|
||||
Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
|
||||
assertTrue(serializers.findSerializer(null, SimpleType.construct(Integer.class), null) == serializer1);
|
||||
assertSame(serializer, serializers.findSerializer(null, SimpleType.construct(Integer.class), null));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -281,7 +257,7 @@ public class Jackson2ObjectMapperFactoryBeanTests {
|
||||
|
||||
@Test
|
||||
public void undefinedObjectType() {
|
||||
assertEquals(null, this.factory.getObjectType());
|
||||
assertNull(this.factory.getObjectType());
|
||||
}
|
||||
|
||||
private static SerializerFactoryConfig getSerializerFactoryConfig(ObjectMapper objectMapper) {
|
||||
@@ -318,7 +294,7 @@ public class Jackson2ObjectMapperFactoryBeanTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filters() throws JsonProcessingException {
|
||||
public void setFilters() throws JsonProcessingException {
|
||||
this.factory.setFilters(new SimpleFilterProvider().setFailOnUnknownId(false));
|
||||
this.factory.afterPropertiesSet();
|
||||
ObjectMapper objectMapper = this.factory.getObject();
|
||||
@@ -334,7 +310,7 @@ public class Jackson2ObjectMapperFactoryBeanTests {
|
||||
NopAnnotationIntrospector annotationIntrospector = NopAnnotationIntrospector.instance;
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
factory.setObjectMapper(objectMapper);
|
||||
this.factory.setObjectMapper(objectMapper);
|
||||
assertTrue(this.factory.isSingleton());
|
||||
assertEquals(ObjectMapper.class, this.factory.getObjectType());
|
||||
|
||||
@@ -344,11 +320,12 @@ public class Jackson2ObjectMapperFactoryBeanTests {
|
||||
JsonSerializer<Class<?>> serializer1 = new ClassSerializer();
|
||||
JsonSerializer<Number> serializer2 = new NumberSerializer(Integer.class);
|
||||
|
||||
factory.setModules(new ArrayList<>()); // Disable well-known modules detection
|
||||
factory.setSerializers(serializer1);
|
||||
factory.setSerializersByType(Collections.<Class<?>, JsonSerializer<?>> singletonMap(Boolean.class, serializer2));
|
||||
factory.setDeserializersByType(deserializers);
|
||||
factory.setAnnotationIntrospector(annotationIntrospector);
|
||||
// Disable well-known modules detection
|
||||
this.factory.setModules(new ArrayList<>());
|
||||
this.factory.setSerializers(serializer1);
|
||||
this.factory.setSerializersByType(Collections.singletonMap(Boolean.class, serializer2));
|
||||
this.factory.setDeserializersByType(deserializers);
|
||||
this.factory.setAnnotationIntrospector(annotationIntrospector);
|
||||
|
||||
this.factory.setFeaturesToEnable(SerializationFeature.FAIL_ON_EMPTY_BEANS,
|
||||
DeserializationFeature.UNWRAP_ROOT_VALUE,
|
||||
@@ -363,20 +340,20 @@ public class Jackson2ObjectMapperFactoryBeanTests {
|
||||
assertFalse(getSerializerFactoryConfig(objectMapper).hasSerializers());
|
||||
assertFalse(getDeserializerFactoryConfig(objectMapper).hasDeserializers());
|
||||
|
||||
this.factory.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
this.factory.setSerializationInclusion(Include.NON_NULL);
|
||||
this.factory.afterPropertiesSet();
|
||||
|
||||
assertTrue(objectMapper == this.factory.getObject());
|
||||
assertSame(objectMapper, this.factory.getObject());
|
||||
assertTrue(getSerializerFactoryConfig(objectMapper).hasSerializers());
|
||||
assertTrue(getDeserializerFactoryConfig(objectMapper).hasDeserializers());
|
||||
|
||||
Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next();
|
||||
assertTrue(serializers.findSerializer(null, SimpleType.construct(Class.class), null) == serializer1);
|
||||
assertTrue(serializers.findSerializer(null, SimpleType.construct(Boolean.class), null) == serializer2);
|
||||
assertSame(serializer1, serializers.findSerializer(null, SimpleType.construct(Class.class), null));
|
||||
assertSame(serializer2, serializers.findSerializer(null, SimpleType.construct(Boolean.class), null));
|
||||
assertNull(serializers.findSerializer(null, SimpleType.construct(Number.class), null));
|
||||
|
||||
assertTrue(annotationIntrospector == objectMapper.getSerializationConfig().getAnnotationIntrospector());
|
||||
assertTrue(annotationIntrospector == objectMapper.getDeserializationConfig().getAnnotationIntrospector());
|
||||
assertSame(annotationIntrospector, objectMapper.getSerializationConfig().getAnnotationIntrospector());
|
||||
assertSame(annotationIntrospector, objectMapper.getDeserializationConfig().getAnnotationIntrospector());
|
||||
|
||||
assertTrue(objectMapper.getSerializationConfig().isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS));
|
||||
assertTrue(objectMapper.getDeserializationConfig().isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE));
|
||||
@@ -389,11 +366,11 @@ public class Jackson2ObjectMapperFactoryBeanTests {
|
||||
assertFalse(objectMapper.getDeserializationConfig().isEnabled(MapperFeature.AUTO_DETECT_FIELDS));
|
||||
assertFalse(objectMapper.getFactory().isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE));
|
||||
assertFalse(objectMapper.getFactory().isEnabled(JsonGenerator.Feature.QUOTE_FIELD_NAMES));
|
||||
assertTrue(objectMapper.getSerializationConfig().getSerializationInclusion() == JsonInclude.Include.NON_NULL);
|
||||
assertSame(Include.NON_NULL, objectMapper.getSerializationConfig().getSerializationInclusion());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void xmlMapper() {
|
||||
public void setObjectMapper() {
|
||||
this.factory.setObjectMapper(new XmlMapper());
|
||||
this.factory.afterPropertiesSet();
|
||||
|
||||
@@ -403,7 +380,7 @@ public class Jackson2ObjectMapperFactoryBeanTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createXmlMapper() {
|
||||
public void setCreateXmlMapper() {
|
||||
this.factory.setCreateXmlMapper(true);
|
||||
this.factory.afterPropertiesSet();
|
||||
|
||||
@@ -448,17 +425,15 @@ public class Jackson2ObjectMapperFactoryBeanTests {
|
||||
@JsonFilter("myJacksonFilter")
|
||||
public static class JacksonFilteredBean {
|
||||
|
||||
public JacksonFilteredBean() {
|
||||
}
|
||||
private String property1;
|
||||
private String property2;
|
||||
|
||||
|
||||
public JacksonFilteredBean(String property1, String property2) {
|
||||
this.property1 = property1;
|
||||
this.property2 = property2;
|
||||
}
|
||||
|
||||
private String property1;
|
||||
private String property2;
|
||||
|
||||
public String getProperty1() {
|
||||
return property1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user