Skip to content

DATACMNS-987 Jackson converter gets only loaded iff jackson and jayway are present #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.14.0.BUILD-SNAPSHOT</version>
<version>1.14.0.DATACMNS-987-SNAPSHOT</version>

<name>Spring Data Core</name>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2016 the original author or authors.
* Copyright 2013-2017 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.
Expand Down Expand Up @@ -44,9 +44,10 @@
/**
* Configuration class to register {@link PageableHandlerMethodArgumentResolver},
* {@link SortHandlerMethodArgumentResolver} and {@link DomainClassConverter}.
*
*
* @since 1.6
* @author Oliver Gierke
* @author Jens Schauder
*/
@Configuration
public class SpringDataWebConfiguration extends WebMvcConfigurerAdapter {
Expand All @@ -72,7 +73,7 @@ public SortHandlerMethodArgumentResolver sortResolver() {
return new SortHandlerMethodArgumentResolver();
}

/*
/*
* (non-Javadoc)
* @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#addFormatters(org.springframework.format.FormatterRegistry)
*/
Expand All @@ -93,7 +94,7 @@ public void addFormatters(FormatterRegistry registry) {
converter.setApplicationContext(context);
}

/*
/*
* (non-Javadoc)
* @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#addArgumentResolvers(java.util.List)
*/
Expand All @@ -111,14 +112,15 @@ public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentRes
argumentResolvers.add(resolver);
}

/*
/*
* (non-Javadoc)
* @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#extendMessageConverters(java.util.List)
*/
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {

if (ClassUtils.isPresent("com.jayway.jsonpath.DocumentContext", context.getClassLoader())) {
if (ClassUtils.isPresent("com.jayway.jsonpath.DocumentContext", context.getClassLoader())
&& ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", context.getClassLoader())) {

ProjectingJackson2HttpMessageConverter converter = new ProjectingJackson2HttpMessageConverter(new ObjectMapper());
converter.setBeanClassLoader(context.getClassLoader());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Copyright 2015-2017 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 static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.test.util.ReflectionTestUtils.*;

import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;

import org.hamcrest.Matcher;
import org.junit.Test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.web.ProjectingJackson2HttpMessageConverter;
import org.springframework.data.web.XmlBeamHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.instrument.classloading.ShadowingClassLoader;

/**
* Integration test for {@link SpringDataWebConfiguration}.
*
* @author Christoph Strobl
* @author Jens Schauder
*/
public class SpringDataWebConfigurationIntegrationTests {

@Test // DATACMNS-987
public void shouldNotLoadJacksonConverterWhenJacksonNotPresent() {

SpringDataWebConfiguration config = createConfigWithClassLoaderExcluding("com.fasterxml.jackson");

List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();

config.extendMessageConverters(converters);

assertThat(converters, (Matcher) not(hasItem( //
instanceWithClassName(ProjectingJackson2HttpMessageConverter.class))));
}

@Test // DATACMNS-987
public void shouldNotLoadJacksonConverterWhenJaywayNotPresent() {

SpringDataWebConfiguration config = createConfigWithClassLoaderExcluding("com.jayway");

List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();

config.extendMessageConverters(converters);

assertThat(converters, (Matcher) not(hasItem( //
instanceWithClassName(ProjectingJackson2HttpMessageConverter.class))));
}

@Test // DATACMNS-987
public void shouldNotLoadXBeamConverterWhenXBeamNotPresent() throws Exception {

SpringDataWebConfiguration config = createConfigWithClassLoaderExcluding("org.xmlbeam");

List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();

config.extendMessageConverters(converters);

assertThat(converters, (Matcher) not(hasItem( //
instanceWithClassName(XmlBeamHttpMessageConverter.class))));
}

@Test // DATACMNS-987
public void shouldLoadAllConvertersWhenDependenciesArePresent() throws Exception {

SpringDataWebConfiguration config = createConfigWithClassLoaderExcluding("load.everything");

List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();

config.extendMessageConverters(converters);

assertThat(converters,
containsInAnyOrder( //
instanceWithClassName(XmlBeamHttpMessageConverter.class), //
instanceWithClassName(ProjectingJackson2HttpMessageConverter.class)));
}

private SpringDataWebConfiguration createConfigWithClassLoaderExcluding(String excludedClassNamePrefix) {

ClassLoader classLoader = initClassLoader(excludedClassNamePrefix);

SpringDataWebConfiguration config = new SpringDataWebConfiguration();
GenericApplicationContext applicationContext = new GenericApplicationContext();
applicationContext.setClassLoader(classLoader);

setField(config, "context", applicationContext);

return config;
}

/**
* creates a Matcher that check if an object is an instance of a class with the same name as the provided class. This
* is necessary since we are dealing with multiple classloaders which would make a simple instanceof fail all the time
*
* @param expectedClass the class that is expected (possibly loaded by a different classloader).
* @return a Matcher
*/
private Matcher<Object> instanceWithClassName(Class<?> expectedClass) {
return hasProperty("class", hasProperty("name", equalTo(expectedClass.getName())));
}

private ClassLoader initClassLoader(final String excludedClassNamePrefix) {

return new ShadowingClassLoader(URLClassLoader.getSystemClassLoader()) {

@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {

if (name.startsWith(excludedClassNamePrefix)) {
throw new ClassNotFoundException();
}

return super.loadClass(name);
}

@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {

if (name.startsWith(excludedClassNamePrefix)) {
throw new ClassNotFoundException();
}

return super.findClass(name);
}
};
}

public static class ObjectFactoryImpl implements ObjectFactory<ConversionService> {

@Override
public ConversionService getObject() throws BeansException {
return null;
}
}
}

This file was deleted.