/spring-web/src/test/java/org/springframework/http/converter/json/JacksonObjectMapperFactoryBeanTests.java

https://github.com/keesun/spring-framework · Java · 158 lines · 107 code · 33 blank · 18 comment · 3 complexity · 449fc5f2694b5094cd2d4a7462315464 MD5 · raw file

  1. /*
  2. * Copyright 2002-2012 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.http.converter.json;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertFalse;
  19. import static org.junit.Assert.assertNotNull;
  20. import static org.junit.Assert.assertTrue;
  21. import java.text.SimpleDateFormat;
  22. import org.codehaus.jackson.JsonFactory;
  23. import org.codehaus.jackson.JsonGenerator;
  24. import org.codehaus.jackson.JsonParser;
  25. import org.codehaus.jackson.map.DeserializationConfig;
  26. import org.codehaus.jackson.map.ObjectMapper;
  27. import org.codehaus.jackson.map.SerializationConfig;
  28. import org.codehaus.jackson.map.introspect.NopAnnotationIntrospector;
  29. import org.junit.Before;
  30. import org.junit.Test;
  31. /**
  32. * @author <a href="mailto:dmitry.katsubo@gmail.com">Dmitry Katsubo</a>
  33. */
  34. public class JacksonObjectMapperFactoryBeanTests {
  35. private static final String DATE_FORMAT = "yyyy-MM-dd";
  36. private JacksonObjectMapperFactoryBean factory;
  37. @Before
  38. public void setUp() {
  39. factory = new JacksonObjectMapperFactoryBean();
  40. }
  41. @Test
  42. public void testSetFeaturesToEnableEmpty() {
  43. factory.setFeaturesToEnable(new Object[0]);
  44. factory.setFeaturesToDisable(new Object[0]);
  45. }
  46. @Test(expected = IllegalArgumentException.class)
  47. public void testUnknownFeature() {
  48. factory.setFeaturesToEnable(new Object[] { Boolean.TRUE });
  49. factory.afterPropertiesSet();
  50. }
  51. @Test
  52. public void testBooleanSetters() {
  53. factory.setAutoDetectFields(false);
  54. factory.setAutoDetectGettersSetters(false);
  55. factory.setFailOnEmptyBeans(false);
  56. factory.setIndentOutput(true);
  57. factory.afterPropertiesSet();
  58. ObjectMapper objectMapper = factory.getObject();
  59. SerializationConfig serializeConfig = objectMapper.getSerializationConfig();
  60. DeserializationConfig deserializeConfig = objectMapper.getDeserializationConfig();
  61. assertFalse(serializeConfig.isEnabled(SerializationConfig.Feature.AUTO_DETECT_FIELDS));
  62. assertFalse(deserializeConfig.isEnabled(DeserializationConfig.Feature.AUTO_DETECT_FIELDS));
  63. assertFalse(serializeConfig.isEnabled(SerializationConfig.Feature.AUTO_DETECT_GETTERS));
  64. assertFalse(deserializeConfig.isEnabled(DeserializationConfig.Feature.AUTO_DETECT_SETTERS));
  65. assertFalse(serializeConfig.isEnabled(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS));
  66. assertTrue(serializeConfig.isEnabled(SerializationConfig.Feature.INDENT_OUTPUT));
  67. }
  68. @Test
  69. public void testDateTimeFormatSetter() {
  70. SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
  71. factory.setDateFormat(dateFormat);
  72. factory.afterPropertiesSet();
  73. assertEquals(dateFormat, factory.getObject().getSerializationConfig().getDateFormat());
  74. }
  75. @Test
  76. public void testSimpleDateFormatStringSetter() {
  77. SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
  78. factory.setSimpleDateFormat(DATE_FORMAT);
  79. factory.afterPropertiesSet();
  80. assertEquals(dateFormat, factory.getObject().getSerializationConfig().getDateFormat());
  81. }
  82. @Test
  83. public void testSimpleSetup() {
  84. factory.afterPropertiesSet();
  85. assertNotNull(factory.getObject());
  86. assertTrue(factory.isSingleton());
  87. assertEquals(ObjectMapper.class, factory.getObjectType());
  88. }
  89. @Test
  90. public void testCompleteSetup() {
  91. NopAnnotationIntrospector annotationIntrospector = new NopAnnotationIntrospector();
  92. ObjectMapper objectMapper = new ObjectMapper();
  93. assertTrue(factory.isSingleton());
  94. assertEquals(ObjectMapper.class, factory.getObjectType());
  95. factory.setObjectMapper(objectMapper);
  96. factory.setAnnotationIntrospector(annotationIntrospector);
  97. factory.setFeaturesToEnable(new Object[] {
  98. SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS,
  99. DeserializationConfig.Feature.USE_ANNOTATIONS,
  100. JsonParser.Feature.ALLOW_SINGLE_QUOTES,
  101. JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS
  102. });
  103. factory.setFeaturesToDisable(new Object[] {
  104. SerializationConfig.Feature.AUTO_DETECT_GETTERS,
  105. DeserializationConfig.Feature.AUTO_DETECT_FIELDS,
  106. JsonParser.Feature.AUTO_CLOSE_SOURCE,
  107. JsonGenerator.Feature.QUOTE_FIELD_NAMES
  108. });
  109. factory.afterPropertiesSet();
  110. assertTrue(objectMapper == factory.getObject());
  111. SerializationConfig serializeConfig = objectMapper.getSerializationConfig();
  112. DeserializationConfig deserializeConfig = objectMapper.getDeserializationConfig();
  113. JsonFactory jsonFactory = objectMapper.getJsonFactory();
  114. assertTrue(annotationIntrospector == serializeConfig.getAnnotationIntrospector());
  115. assertTrue(annotationIntrospector == deserializeConfig.getAnnotationIntrospector());
  116. assertTrue(serializeConfig.isEnabled(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS));
  117. assertTrue(deserializeConfig.isEnabled(DeserializationConfig.Feature.USE_ANNOTATIONS));
  118. assertTrue(jsonFactory.isEnabled(JsonParser.Feature.ALLOW_SINGLE_QUOTES));
  119. assertTrue(jsonFactory.isEnabled(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS));
  120. assertFalse(serializeConfig.isEnabled(SerializationConfig.Feature.AUTO_DETECT_GETTERS));
  121. assertFalse(deserializeConfig.isEnabled(DeserializationConfig.Feature.AUTO_DETECT_FIELDS));
  122. assertFalse(jsonFactory.isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE));
  123. assertFalse(jsonFactory.isEnabled(JsonGenerator.Feature.QUOTE_FIELD_NAMES));
  124. }
  125. }