DATACMNS-341 - RepositoryFactory(Bean)Support is now class loader aware.
Let both RepositoryFactorySupport and RepositoryFactoryBeanSupport implement BeanClassLoaderAware to pick up the ClassLoader used by the container. The latter class forwards the configured instance into the factory it creates.
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2013 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.repository.core.support;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link RepositoryFactoryBeanSupport}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class RepositoryFactoryBeanSupportUnitTests {
|
||||
|
||||
/**
|
||||
* @see DATACMNS-341
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void setsConfiguredClassLoaderOnRepositoryFactory() {
|
||||
|
||||
ClassLoader classLoader = mock(ClassLoader.class);
|
||||
|
||||
RepositoryFactoryBeanSupport factoryBean = new DummyRepositoryFactoryBean();
|
||||
factoryBean.setBeanClassLoader(classLoader);
|
||||
factoryBean.afterPropertiesSet();
|
||||
|
||||
Object factory = ReflectionTestUtils.getField(factoryBean, "factory");
|
||||
assertThat(ReflectionTestUtils.getField(factory, "classLoader"), is((Object) classLoader));
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,8 @@ import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.RepositoryDefinition;
|
||||
import org.springframework.data.repository.query.RepositoryQuery;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link RepositoryFactorySupport}.
|
||||
@@ -47,15 +49,11 @@ public class RepositoryFactorySupportUnitTests {
|
||||
|
||||
RepositoryFactorySupport factory;
|
||||
|
||||
@Mock
|
||||
PagingAndSortingRepository<Object, Serializable> backingRepo;
|
||||
@Mock
|
||||
ObjectRepositoryCustom customImplementation;
|
||||
@Mock PagingAndSortingRepository<Object, Serializable> backingRepo;
|
||||
@Mock ObjectRepositoryCustom customImplementation;
|
||||
|
||||
@Mock
|
||||
MyQueryCreationListener listener;
|
||||
@Mock
|
||||
PlainQueryCreationListener otherListener;
|
||||
@Mock MyQueryCreationListener listener;
|
||||
@Mock PlainQueryCreationListener otherListener;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
@@ -113,6 +111,16 @@ public class RepositoryFactorySupportUnitTests {
|
||||
assertThat(factory.getRepository(foo), is(notNullValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-341
|
||||
*/
|
||||
@Test
|
||||
public void usesDefaultClassLoaderIfNullConfigured() {
|
||||
|
||||
factory.setBeanClassLoader(null);
|
||||
assertThat(ReflectionTestUtils.getField(factory, "classLoader"), is((Object) ClassUtils.getDefaultClassLoader()));
|
||||
}
|
||||
|
||||
interface ObjectRepository extends Repository<Object, Serializable>, ObjectRepositoryCustom {
|
||||
|
||||
Object findByClass(Class<?> clazz);
|
||||
|
||||
Reference in New Issue
Block a user