DATACMNS-993 - Polishing.
Removed custom JUnit integration as we can just create HidingClassLoader instances in the test and the integration actually causes more code being needed (additional JUnit rule, method level annotations etc.). Tweaked ShadowingClassLoader to make obvious what has been changed over the Spring Framework variant. Created upstream ticket [0] to ask for the tweaks that would allow us to remove the class again. Changed SpringDataWebConfigurationImportSelector to be lenient against the ResourceLoader not being available. Original pull request: #202. Related ticket: SPR-15439 [0] https://jira.spring.io/browse/SPR-15439
This commit is contained in:
@@ -19,19 +19,15 @@ import static org.assertj.core.api.Assertions.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import org.hamcrest.Matcher;
|
||||
import org.junit.Rule;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.data.classloadersupport.ClassLoaderConfiguration;
|
||||
import org.springframework.data.classloadersupport.ClassLoaderRule;
|
||||
import org.springframework.data.classloadersupport.HidingClassLoader;
|
||||
import org.springframework.data.geo.Distance;
|
||||
import org.springframework.data.geo.Point;
|
||||
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
|
||||
@@ -44,39 +40,29 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link EnableSpringDataWebSupport}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
|
||||
@ClassLoaderConfiguration(shadowPackage = {
|
||||
WebMvcConfigurationSupport.class,
|
||||
EnableSpringDataWebSupport.class,
|
||||
EnableSpringDataWebSupportIntegrationTests.SampleConfig.class})
|
||||
public class EnableSpringDataWebSupportIntegrationTests {
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@EnableSpringDataWebSupport
|
||||
static class SampleConfig {
|
||||
|
||||
public
|
||||
@Bean
|
||||
SampleController controller() {
|
||||
public @Bean SampleController controller() {
|
||||
return new SampleController();
|
||||
}
|
||||
}
|
||||
|
||||
@Rule public ClassLoaderRule classLoaderRule = new ClassLoaderRule();
|
||||
|
||||
|
||||
@Test // DATACMNS-330
|
||||
public void registersBasicBeanDefinitions() throws Exception {
|
||||
|
||||
@@ -100,10 +86,12 @@ public class EnableSpringDataWebSupportIntegrationTests {
|
||||
}
|
||||
|
||||
@Test // DATACMNS-330
|
||||
@ClassLoaderConfiguration(hidePackage = Link.class)
|
||||
// @ClassLoaderConfiguration(hidePackage = Link.class)
|
||||
public void doesNotRegisterHateoasSpecificComponentsIfHateoasNotPresent() throws Exception {
|
||||
|
||||
ApplicationContext context = WebTestUtils.createApplicationContext(classLoaderRule.classLoader, SampleConfig.class);
|
||||
HidingClassLoader classLoader = HidingClassLoader.hide(Link.class);
|
||||
|
||||
ApplicationContext context = WebTestUtils.createApplicationContext(classLoader, SampleConfig.class);
|
||||
|
||||
List<String> names = Arrays.asList(context.getBeanDefinitionNames());
|
||||
|
||||
@@ -121,10 +109,10 @@ public class EnableSpringDataWebSupportIntegrationTests {
|
||||
}
|
||||
|
||||
@Test // DATACMNS-475
|
||||
@ClassLoaderConfiguration(hidePackage = com.fasterxml.jackson.databind.ObjectMapper.class)
|
||||
public void doesNotRegisterJacksonSpecificComponentsIfJacksonNotPresent() throws Exception {
|
||||
|
||||
ApplicationContext context = WebTestUtils.createApplicationContext(classLoaderRule.classLoader, SampleConfig.class);
|
||||
ApplicationContext context = WebTestUtils.createApplicationContext(HidingClassLoader.hide(ObjectMapper.class),
|
||||
SampleConfig.class);
|
||||
|
||||
List<String> names = Arrays.asList(context.getBeanDefinitionNames());
|
||||
|
||||
|
||||
@@ -20,15 +20,18 @@ import static org.junit.Assert.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.hamcrest.Matcher;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.data.classloadersupport.ClassLoaderConfiguration;
|
||||
import org.springframework.data.classloadersupport.ClassLoaderRule;
|
||||
import org.springframework.data.classloadersupport.HidingClassLoader;
|
||||
import org.springframework.data.web.ProjectingJackson2HttpMessageConverter;
|
||||
import org.springframework.data.web.XmlBeamHttpMessageConverter;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.xmlbeam.ProjectionFactory;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.jayway.jsonpath.DocumentContext;
|
||||
|
||||
/**
|
||||
* Integration test for {@link SpringDataWebConfiguration}.
|
||||
@@ -39,37 +42,33 @@ import org.springframework.http.converter.HttpMessageConverter;
|
||||
*/
|
||||
public class SpringDataWebConfigurationIntegrationTests {
|
||||
|
||||
@Rule public ClassLoaderRule classLoader = new ClassLoaderRule();
|
||||
|
||||
@Test // DATACMNS-987
|
||||
@ClassLoaderConfiguration(hidePackage = com.fasterxml.jackson.databind.ObjectMapper.class)
|
||||
public void shouldNotLoadJacksonConverterWhenJacksonNotPresent() {
|
||||
|
||||
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
|
||||
|
||||
createConfigWithClassLoader().extendMessageConverters(converters);
|
||||
createConfigWithClassLoader(HidingClassLoader.hide(ObjectMapper.class)).extendMessageConverters(converters);
|
||||
|
||||
assertThat(converters, not(hasItem(instanceWithClassName(ProjectingJackson2HttpMessageConverter.class))));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-987
|
||||
@ClassLoaderConfiguration(hidePackage = com.jayway.jsonpath.DocumentContext.class)
|
||||
public void shouldNotLoadJacksonConverterWhenJaywayNotPresent() {
|
||||
|
||||
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
|
||||
|
||||
createConfigWithClassLoader().extendMessageConverters(converters);
|
||||
createConfigWithClassLoader(HidingClassLoader.hide(DocumentContext.class)).extendMessageConverters(converters);
|
||||
|
||||
assertThat(converters, not(hasItem(instanceWithClassName(ProjectingJackson2HttpMessageConverter.class))));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-987
|
||||
@ClassLoaderConfiguration(hidePackage = org.xmlbeam.ProjectionFactory.class)
|
||||
public void shouldNotLoadXBeamConverterWhenXBeamNotPresent() throws Exception {
|
||||
|
||||
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
|
||||
|
||||
createConfigWithClassLoader().extendMessageConverters(converters);
|
||||
ClassLoader classLoader = HidingClassLoader.hide(ProjectionFactory.class);
|
||||
createConfigWithClassLoader(classLoader).extendMessageConverters(converters);
|
||||
|
||||
assertThat(converters, not(hasItem(instanceWithClassName(XmlBeamHttpMessageConverter.class))));
|
||||
}
|
||||
@@ -79,17 +78,18 @@ public class SpringDataWebConfigurationIntegrationTests {
|
||||
|
||||
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
|
||||
|
||||
createConfigWithClassLoader().extendMessageConverters(converters);
|
||||
createConfigWithClassLoader(getClass().getClassLoader()).extendMessageConverters(converters);
|
||||
|
||||
assertThat(converters, hasItem(instanceWithClassName(XmlBeamHttpMessageConverter.class)));
|
||||
assertThat(converters, hasItem(instanceWithClassName(ProjectingJackson2HttpMessageConverter.class)));
|
||||
}
|
||||
|
||||
private SpringDataWebConfiguration createConfigWithClassLoader() {
|
||||
private SpringDataWebConfiguration createConfigWithClassLoader(ClassLoader classLoader) {
|
||||
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
SpringDataWebConfiguration.class);
|
||||
context.setClassLoader(classLoader.classLoader);
|
||||
|
||||
context.setClassLoader(classLoader);
|
||||
|
||||
try {
|
||||
return context.getBean(SpringDataWebConfiguration.class);
|
||||
|
||||
Reference in New Issue
Block a user