Browse Source

DATACMNS-987 - Polishing.

Removed a bit of reflection from ApplicationContext interaction. Some generics tweaks to avoid unnecessary casts and compiler warnings. Static methods where possible. JavaDoc polish in ProjectingJackson2HttpMessageConverter.
pull/194/head
Oliver Gierke 9 years ago
parent
commit
27ce92cd16
  1. 4
      src/main/java/org/springframework/data/web/ProjectingJackson2HttpMessageConverter.java
  2. 57
      src/test/java/org/springframework/data/web/config/SpringDataWebConfigurationIntegrationTests.java

4
src/main/java/org/springframework/data/web/ProjectingJackson2HttpMessageConverter.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2016 the original author or authors. * Copyright 2016-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -42,7 +42,7 @@ import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
* {@link ProjectedPayload}. * {@link ProjectedPayload}.
* *
* @author Oliver Gierke * @author Oliver Gierke
* @soundtrack Richard Spaven -Ice Is Nice (Spaven's 5ive) * @soundtrack Richard Spaven - Ice Is Nice (Spaven's 5ive)
* @since 1.13 * @since 1.13
*/ */
public class ProjectingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter public class ProjectingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter

57
src/test/java/org/springframework/data/web/config/SpringDataWebConfigurationIntegrationTests.java

@ -17,7 +17,6 @@ package org.springframework.data.web.config;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.springframework.test.util.ReflectionTestUtils.*;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.ArrayList; import java.util.ArrayList;
@ -27,7 +26,7 @@ import org.hamcrest.Matcher;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ObjectFactory; import org.springframework.beans.factory.ObjectFactory;
import org.springframework.context.support.GenericApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.data.web.ProjectingJackson2HttpMessageConverter; import org.springframework.data.web.ProjectingJackson2HttpMessageConverter;
import org.springframework.data.web.XmlBeamHttpMessageConverter; import org.springframework.data.web.XmlBeamHttpMessageConverter;
@ -39,74 +38,62 @@ import org.springframework.instrument.classloading.ShadowingClassLoader;
* *
* @author Christoph Strobl * @author Christoph Strobl
* @author Jens Schauder * @author Jens Schauder
* @author Oliver Gierke
*/ */
public class SpringDataWebConfigurationIntegrationTests { public class SpringDataWebConfigurationIntegrationTests {
@Test // DATACMNS-987 @Test // DATACMNS-987
public void shouldNotLoadJacksonConverterWhenJacksonNotPresent() { public void shouldNotLoadJacksonConverterWhenJacksonNotPresent() {
SpringDataWebConfiguration config = createConfigWithClassLoaderExcluding("com.fasterxml.jackson");
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(); List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
config.extendMessageConverters(converters); createConfigWithClassLoaderExcluding("com.fasterxml.jackson").extendMessageConverters(converters);
assertThat(converters, (Matcher) not(hasItem( // assertThat(converters, not(hasItem(instanceWithClassName(ProjectingJackson2HttpMessageConverter.class))));
instanceWithClassName(ProjectingJackson2HttpMessageConverter.class))));
} }
@Test // DATACMNS-987 @Test // DATACMNS-987
public void shouldNotLoadJacksonConverterWhenJaywayNotPresent() { public void shouldNotLoadJacksonConverterWhenJaywayNotPresent() {
SpringDataWebConfiguration config = createConfigWithClassLoaderExcluding("com.jayway");
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(); List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
config.extendMessageConverters(converters); createConfigWithClassLoaderExcluding("com.jayway").extendMessageConverters(converters);
assertThat(converters, (Matcher) not(hasItem( // assertThat(converters, not(hasItem(instanceWithClassName(ProjectingJackson2HttpMessageConverter.class))));
instanceWithClassName(ProjectingJackson2HttpMessageConverter.class))));
} }
@Test // DATACMNS-987 @Test // DATACMNS-987
public void shouldNotLoadXBeamConverterWhenXBeamNotPresent() throws Exception { public void shouldNotLoadXBeamConverterWhenXBeamNotPresent() throws Exception {
SpringDataWebConfiguration config = createConfigWithClassLoaderExcluding("org.xmlbeam");
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(); List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
config.extendMessageConverters(converters); createConfigWithClassLoaderExcluding("org.xmlbeam").extendMessageConverters(converters);
assertThat(converters, (Matcher) not(hasItem( // assertThat(converters, not(hasItem(instanceWithClassName(XmlBeamHttpMessageConverter.class))));
instanceWithClassName(XmlBeamHttpMessageConverter.class))));
} }
@Test // DATACMNS-987 @Test // DATACMNS-987
public void shouldLoadAllConvertersWhenDependenciesArePresent() throws Exception { public void shouldLoadAllConvertersWhenDependenciesArePresent() throws Exception {
SpringDataWebConfiguration config = createConfigWithClassLoaderExcluding("load.everything");
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(); List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
config.extendMessageConverters(converters); createConfigWithClassLoaderExcluding("load.everything").extendMessageConverters(converters);
assertThat(converters, assertThat(converters, hasItem(instanceWithClassName(XmlBeamHttpMessageConverter.class)));
containsInAnyOrder( // assertThat(converters, hasItem(instanceWithClassName(ProjectingJackson2HttpMessageConverter.class)));
instanceWithClassName(XmlBeamHttpMessageConverter.class), //
instanceWithClassName(ProjectingJackson2HttpMessageConverter.class)));
} }
private SpringDataWebConfiguration createConfigWithClassLoaderExcluding(String excludedClassNamePrefix) { private static SpringDataWebConfiguration createConfigWithClassLoaderExcluding(String excludedClassNamePrefix) {
ClassLoader classLoader = initClassLoader(excludedClassNamePrefix); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
SpringDataWebConfiguration.class);
context.setClassLoader(initClassLoader(excludedClassNamePrefix));
SpringDataWebConfiguration config = new SpringDataWebConfiguration(); try {
GenericApplicationContext applicationContext = new GenericApplicationContext(); return context.getBean(SpringDataWebConfiguration.class);
applicationContext.setClassLoader(classLoader); } finally {
context.close();
setField(config, "context", applicationContext); }
return config;
} }
/** /**
@ -116,11 +103,11 @@ public class SpringDataWebConfigurationIntegrationTests {
* @param expectedClass the class that is expected (possibly loaded by a different classloader). * @param expectedClass the class that is expected (possibly loaded by a different classloader).
* @return a Matcher * @return a Matcher
*/ */
private Matcher<Object> instanceWithClassName(Class<?> expectedClass) { private static <T> Matcher<T> instanceWithClassName(Class<T> expectedClass) {
return hasProperty("class", hasProperty("name", equalTo(expectedClass.getName()))); return hasProperty("class", hasProperty("name", equalTo(expectedClass.getName())));
} }
private ClassLoader initClassLoader(final String excludedClassNamePrefix) { private static ClassLoader initClassLoader(final String excludedClassNamePrefix) {
return new ShadowingClassLoader(URLClassLoader.getSystemClassLoader()) { return new ShadowingClassLoader(URLClassLoader.getSystemClassLoader()) {

Loading…
Cancel
Save