Introduce configuration of class loader in SimpleTypeInformationMapper.
We now support configuration of the class loader in SimpleTypeInformationMapper to use a configured class loader instead of falling always back to the default/contextual class loader. In arrangements where the contextual class loader isn't able to provide access to the desired classes (e.g. parallel Streams, general Fork/Join Thread usage) the contextual class loader may not have access to the entity types. By implementing BeanClassLoaderAware, we can now propagate a configured class loader. Closes #2508
This commit is contained in:
@@ -18,6 +18,8 @@ package org.springframework.data.convert;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -29,6 +31,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.mockito.junit.jupiter.MockitoSettings;
|
||||
import org.mockito.quality.Strictness;
|
||||
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
import org.springframework.data.mapping.Alias;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
@@ -47,7 +50,7 @@ class DefaultTypeMapperUnitTests {
|
||||
static final Alias ALIAS = Alias.of(String.class.getName());
|
||||
|
||||
@Mock TypeAliasAccessor<Map<String, String>> accessor;
|
||||
@Mock TypeInformationMapper mapper;
|
||||
@Mock(extraInterfaces = BeanClassLoaderAware.class) TypeInformationMapper mapper;
|
||||
|
||||
DefaultTypeMapper<Map<String, String>> typeMapper;
|
||||
Map<String, String> source;
|
||||
@@ -102,6 +105,15 @@ class DefaultTypeMapperUnitTests {
|
||||
assertThat(typeInformation.getProperty("field").getType()).isEqualTo(Character.class);
|
||||
}
|
||||
|
||||
@Test // GH-2508
|
||||
void configuresClassLoaderOnTypeInformationMapper() {
|
||||
|
||||
ClassLoader loader = new URLClassLoader(new URL[0]);
|
||||
typeMapper.setBeanClassLoader(loader);
|
||||
|
||||
verify((BeanClassLoaderAware) mapper).setBeanClassLoader(loader);
|
||||
}
|
||||
|
||||
static class TypeWithAbstractGenericType<T> {
|
||||
AbstractBar<T> abstractBar;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ package org.springframework.data.convert;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.data.classloadersupport.HidingClassLoader;
|
||||
import org.springframework.data.mapping.Alias;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
@@ -26,10 +28,11 @@ import org.springframework.data.util.TypeInformation;
|
||||
* Unit tests for {@link SimpleTypeInformationMapper}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
class SimpleTypeInformationMapperUnitTests {
|
||||
|
||||
TypeInformationMapper mapper = new SimpleTypeInformationMapper();
|
||||
SimpleTypeInformationMapper mapper = new SimpleTypeInformationMapper();
|
||||
|
||||
@Test
|
||||
void resolvesTypeByLoadingClass() {
|
||||
@@ -41,6 +44,16 @@ class SimpleTypeInformationMapperUnitTests {
|
||||
assertThat(type).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test // GH-2508
|
||||
void usesConfiguredClassloader() {
|
||||
|
||||
mapper.setBeanClassLoader(HidingClassLoader.hide(SimpleTypeInformationMapperUnitTests.class));
|
||||
TypeInformation<?> type = mapper
|
||||
.resolveTypeFrom(Alias.of("org.springframework.data.convert.SimpleTypeInformationMapperUnitTests.User"));
|
||||
|
||||
assertThat(type).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void returnsNullForNonStringKey() {
|
||||
assertThat(mapper.resolveTypeFrom(Alias.of(new Object()))).isNull();
|
||||
@@ -63,4 +76,8 @@ class SimpleTypeInformationMapperUnitTests {
|
||||
assertThat(mapper.createAliasFor(ClassTypeInformation.from(String.class)))
|
||||
.isEqualTo(Alias.of(String.class.getName()));
|
||||
}
|
||||
|
||||
static class User {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user