diff --git a/Spring Data Commons.sonargraph b/Spring Data Commons.sonargraph index ae6bbda82..093ca1591 100644 --- a/Spring Data Commons.sonargraph +++ b/Spring Data Commons.sonargraph @@ -10,15 +10,15 @@ + + - - @@ -143,6 +143,12 @@ + + + + + + @@ -163,19 +169,11 @@ - - - - - - - - @@ -269,11 +267,6 @@ - - - - - @@ -322,7 +315,12 @@ - + + + + + + diff --git a/src/main/java/org/springframework/data/web/config/EnableSpringDataWebSupport.java b/src/main/java/org/springframework/data/web/config/EnableSpringDataWebSupport.java index 69022a097..0c9e7e28b 100644 --- a/src/main/java/org/springframework/data/web/config/EnableSpringDataWebSupport.java +++ b/src/main/java/org/springframework/data/web/config/EnableSpringDataWebSupport.java @@ -20,6 +20,8 @@ import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import java.util.ArrayList; +import java.util.List; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.ImportSelector; @@ -80,6 +82,7 @@ public @interface EnableSpringDataWebSupport { // Don't make final to allow test cases faking this to false private static boolean HATEOAS_PRESENT = ClassUtils.isPresent("org.springframework.hateoas.Link", null); + private static boolean JACKSON_PRESENT = ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", null); /* * (non-Javadoc) @@ -87,8 +90,17 @@ public @interface EnableSpringDataWebSupport { */ @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { - return new String[] { HATEOAS_PRESENT ? HateoasAwareSpringDataWebConfiguration.class.getName() - : SpringDataWebConfiguration.class.getName() }; + + List imports = new ArrayList(); + + imports.add(HATEOAS_PRESENT ? HateoasAwareSpringDataWebConfiguration.class.getName() + : SpringDataWebConfiguration.class.getName()); + + if (JACKSON_PRESENT) { + imports.add(SpringDataJacksonConfiguration.class.getName()); + } + + return imports.toArray(new String[imports.size()]); } } } diff --git a/src/main/java/org/springframework/data/web/config/SpringDataJacksonConfiguration.java b/src/main/java/org/springframework/data/web/config/SpringDataJacksonConfiguration.java new file mode 100644 index 000000000..eba007c31 --- /dev/null +++ b/src/main/java/org/springframework/data/web/config/SpringDataJacksonConfiguration.java @@ -0,0 +1,34 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.web.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.geo.GeoModule; + +/** + * JavaConfig class to export Jackson specific configuration. + * + * @author Oliver Gierke + */ +@Configuration +public class SpringDataJacksonConfiguration { + + @Bean + public GeoModule jacksonGeoModule() { + return new GeoModule(); + } +} diff --git a/src/test/java/org/springframework/data/web/config/EnableSpringDataWebSupportIntegrationTests.java b/src/test/java/org/springframework/data/web/config/EnableSpringDataWebSupportIntegrationTests.java index 5ca25a016..7e19821ff 100644 --- a/src/test/java/org/springframework/data/web/config/EnableSpringDataWebSupportIntegrationTests.java +++ b/src/test/java/org/springframework/data/web/config/EnableSpringDataWebSupportIntegrationTests.java @@ -17,7 +17,6 @@ package org.springframework.data.web.config; import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; -import static org.junit.Assume.*; import java.lang.reflect.Field; import java.util.ArrayList; @@ -26,11 +25,9 @@ import java.util.List; import org.hamcrest.Matcher; import org.junit.After; -import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Configuration; -import org.springframework.core.SpringVersion; import org.springframework.data.web.PageableHandlerMethodArgumentResolver; import org.springframework.data.web.PagedResourcesAssemblerArgumentResolver; import org.springframework.data.web.SortHandlerMethodArgumentResolver; @@ -49,6 +46,9 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl */ public class EnableSpringDataWebSupportIntegrationTests { + private static final String HATEOAS = "HATEOAS_PRESENT"; + private static final String JACKSON = "JACKSON_PRESENT"; + @Configuration @EnableWebMvc @EnableSpringDataWebSupport @@ -56,14 +56,10 @@ public class EnableSpringDataWebSupportIntegrationTests { } - @Before - public void setUp() { - assumeThat(SpringVersion.getVersion(), startsWith("3.2")); - } - @After public void tearDown() { - reEnableHateoas(); + reEnable(HATEOAS); + reEnable(JACKSON); } @Test @@ -91,7 +87,7 @@ public class EnableSpringDataWebSupportIntegrationTests { @Test public void doesNotRegisterHateoasSpecificComponentsIfHateoasNotPresent() throws Exception { - hideHateoas(); + hide(HATEOAS); ApplicationContext context = WebTestUtils.createApplicationContext(SampleConfig.class); List names = Arrays.asList(context.getBeanDefinitionNames()); @@ -100,6 +96,32 @@ public class EnableSpringDataWebSupportIntegrationTests { assertThat(names, not(hasItems("pagedResourcesAssembler", "pagedResourcesAssemblerArgumentResolver"))); } + /** + * @see DATACMNS-475 + */ + @Test + public void registersJacksonSpecificBeanDefinitions() throws Exception { + + ApplicationContext context = WebTestUtils.createApplicationContext(SampleConfig.class); + List names = Arrays.asList(context.getBeanDefinitionNames()); + + assertThat(names, hasItem("jacksonGeoModule")); + } + + /** + * @see DATACMNS-475 + */ + @Test + public void doesNotRegisterJacksonSpecificComponentsIfJacksonNotPresent() throws Exception { + + hide(JACKSON); + + ApplicationContext context = WebTestUtils.createApplicationContext(SampleConfig.class); + List names = Arrays.asList(context.getBeanDefinitionNames()); + + assertThat(names, not(hasItem("jacksonGeoModule"))); + } + @SuppressWarnings("unchecked") private static void assertResolversRegistered(ApplicationContext context, Class... resolverTypes) { @@ -116,16 +138,16 @@ public class EnableSpringDataWebSupportIntegrationTests { assertThat(resolvers, hasItems(resolverMatchers.toArray(new Matcher[resolverMatchers.size()]))); } - private static void hideHateoas() throws Exception { + private static void hide(String module) throws Exception { - Field field = ReflectionUtils.findField(SpringDataWebConfigurationImportSelector.class, "HATEOAS_PRESENT"); + Field field = ReflectionUtils.findField(SpringDataWebConfigurationImportSelector.class, module); ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, null, false); } - private static void reEnableHateoas() { + private static void reEnable(String module) { - Field field = ReflectionUtils.findField(SpringDataWebConfigurationImportSelector.class, "HATEOAS_PRESENT"); + Field field = ReflectionUtils.findField(SpringDataWebConfigurationImportSelector.class, module); ReflectionUtils.makeAccessible(field); ReflectionUtils.setField(field, null, true); }