Browse Source

DATACMNS-891 - Moved to constructor injection for RepositoryFactoryBeanSupport.

RepositoryFactoryBeanSupport now takes the repository interface as a constructor argument instead of as a setter. This makes sure that the container induced type prediction for factory beans actually wires the interface *before* it calls getObjectType() on the instance. This allows us to remove the extra infrastructure we had in place to predict the bean types and autowiring will just work out of the box.

Adapted infrastructure code accordingly, removed obsolete infrastructure code and adapted test cases accordingly.
pull/190/head
Oliver Gierke 9 years ago
parent
commit
259a1e0958
  1. 10
      src/main/java/org/springframework/data/repository/config/RepositoryBeanDefinitionBuilder.java
  2. 2
      src/main/java/org/springframework/data/repository/config/RepositoryBeanNameGenerator.java
  3. 17
      src/main/java/org/springframework/data/repository/config/RepositoryConfigurationExtensionSupport.java
  4. 182
      src/main/java/org/springframework/data/repository/core/support/FactoryBeanTypePredictingBeanPostProcessor.java
  5. 19
      src/main/java/org/springframework/data/repository/core/support/RepositoryFactoryBeanSupport.java
  6. 9
      src/main/java/org/springframework/data/repository/core/support/TransactionalRepositoryFactoryBeanSupport.java
  7. 2
      src/test/java/org/springframework/data/repository/config/RepositoryBeanNameGeneratorUnitTests.java
  8. 25
      src/test/java/org/springframework/data/repository/config/RepositoryConfigurationExtensionSupportUnitTests.java
  9. 19
      src/test/java/org/springframework/data/repository/core/support/DummyRepositoryFactoryBean.java
  10. 78
      src/test/java/org/springframework/data/repository/core/support/FactoryBeanTypePredictingPostProcessorIntegrationTests.java
  11. 130
      src/test/java/org/springframework/data/repository/core/support/FactoryBeanTypePredictingPostProcessorUnitTests.java
  12. 8
      src/test/java/org/springframework/data/repository/core/support/RepositoryFactoryBeanSupportUnitTests.java
  13. 2
      src/test/java/org/springframework/data/repository/core/support/TransactionRepositoryFactoryBeanSupportUnitTests.java
  14. 9
      src/test/java/org/springframework/data/repository/sample/SampleConfiguration.java
  15. 2
      src/test/java/org/springframework/data/repository/support/DomainClassConverterUnitTests.java
  16. 2
      src/test/java/org/springframework/data/repository/support/DomainClassPropertyEditorRegistrarUnitTests.java
  17. 2
      src/test/java/org/springframework/data/repository/support/RepositoriesUnitTests.java

10
src/main/java/org/springframework/data/repository/config/RepositoryBeanDefinitionBuilder.java

@ -82,13 +82,13 @@ class RepositoryBeanDefinitionBuilder { @@ -82,13 +82,13 @@ class RepositoryBeanDefinitionBuilder {
Assert.notNull(resourceLoader, "ResourceLoader must not be null!");
String factoryBeanName = configuration.getRepositoryFactoryBeanName();
factoryBeanName = StringUtils.hasText(factoryBeanName) ? factoryBeanName : extension
.getRepositoryFactoryClassName();
factoryBeanName = StringUtils.hasText(factoryBeanName) ? factoryBeanName
: extension.getRepositoryFactoryClassName();
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(factoryBeanName);
builder.getRawBeanDefinition().setSource(configuration.getSource());
builder.addPropertyValue("repositoryInterface", configuration.getRepositoryInterface());
builder.addConstructorArgValue(configuration.getRepositoryInterface());
builder.addPropertyValue("queryLookupStrategyKey", configuration.getQueryLookupStrategyKey());
builder.addPropertyValue("lazyInit", configuration.isLazyInit());
builder.addPropertyValue("repositoryBaseClass", configuration.getRepositoryBaseClassName());
@ -127,8 +127,8 @@ class RepositoryBeanDefinitionBuilder { @@ -127,8 +127,8 @@ class RepositoryBeanDefinitionBuilder {
return beanName;
}
AbstractBeanDefinition beanDefinition = implementationDetector.detectCustomImplementation(
configuration.getImplementationClassName(), configuration.getBasePackages());
AbstractBeanDefinition beanDefinition = implementationDetector
.detectCustomImplementation(configuration.getImplementationClassName(), configuration.getBasePackages());
if (null == beanDefinition) {
return null;

2
src/main/java/org/springframework/data/repository/config/RepositoryBeanNameGenerator.java

@ -65,7 +65,7 @@ public class RepositoryBeanNameGenerator implements BeanNameGenerator, BeanClass @@ -65,7 +65,7 @@ public class RepositoryBeanNameGenerator implements BeanNameGenerator, BeanClass
*/
private Class<?> getRepositoryInterfaceFrom(BeanDefinition beanDefinition) {
Object value = beanDefinition.getPropertyValues().getPropertyValue("repositoryInterface").getValue();
Object value = beanDefinition.getConstructorArgumentValues().getArgumentValue(0, Class.class).getValue();
if (value instanceof Class<?>) {
return (Class<?>) value;

17
src/main/java/org/springframework/data/repository/config/RepositoryConfigurationExtensionSupport.java

@ -33,7 +33,6 @@ import org.springframework.core.annotation.AnnotationUtils; @@ -33,7 +33,6 @@ import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.AbstractRepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@ -50,8 +49,6 @@ public abstract class RepositoryConfigurationExtensionSupport implements Reposit @@ -50,8 +49,6 @@ public abstract class RepositoryConfigurationExtensionSupport implements Reposit
private static final String CLASS_LOADING_ERROR = "%s - Could not load type %s using class loader %s.";
private static final String MULTI_STORE_DROPPED = "Spring Data {} - Could not safely identify store assignment for repository candidate {}.";
private static final String FACTORY_BEAN_TYPE_PREDICTING_POST_PROCESSOR = "org.springframework.data.repository.core.support.FactoryBeanTypePredictingBeanPostProcessor";
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfigurationExtension#getModuleName()
@ -114,18 +111,8 @@ public abstract class RepositoryConfigurationExtensionSupport implements Reposit @@ -114,18 +111,8 @@ public abstract class RepositoryConfigurationExtensionSupport implements Reposit
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryConfigurationExtension#registerBeansForRoot(org.springframework.beans.factory.support.BeanDefinitionRegistry, org.springframework.data.repository.config.RepositoryConfigurationSource)
*/
public void registerBeansForRoot(BeanDefinitionRegistry registry, RepositoryConfigurationSource configurationSource) {
String typeName = RepositoryFactoryBeanSupport.class.getName();
BeanDefinitionBuilder builder = BeanDefinitionBuilder
.rootBeanDefinition(FACTORY_BEAN_TYPE_PREDICTING_POST_PROCESSOR);
builder.addConstructorArgValue(typeName);
builder.addConstructorArgValue("repositoryInterface");
registerIfNotAlreadyRegistered(builder.getBeanDefinition(), registry, typeName.concat("_Predictor"),
configurationSource.getSource());
}
public void registerBeansForRoot(BeanDefinitionRegistry registry,
RepositoryConfigurationSource configurationSource) {}
/**
* Returns the prefix of the module to be used to create the default location for Spring Data named queries.

182
src/main/java/org/springframework/data/repository/core/support/FactoryBeanTypePredictingBeanPostProcessor.java

@ -1,182 +0,0 @@ @@ -1,182 +0,0 @@
/*
* Copyright 2016 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 java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import org.springframework.beans.factory.config.TypedStringValue;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* {@link InstantiationAwareBeanPostProcessorAdapter} to predict the bean type for {@link FactoryBean} implementations
* by interpreting a configured property of the {@link BeanDefinition} as type to be created eventually.
*
* @author Oliver Gierke
* @since 1.12
* @soundtrack Ron Spielmann - Lock Me Up (Electric Tales)
*/
public class FactoryBeanTypePredictingBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter
implements BeanFactoryAware, PriorityOrdered {
private static final Logger LOGGER = LoggerFactory.getLogger(FactoryBeanTypePredictingBeanPostProcessor.class);
private final Map<String, Class<?>> cache = new ConcurrentHashMap<String, Class<?>>();
private final Class<?> factoryBeanType;
private final List<String> properties;
private ConfigurableListableBeanFactory context;
/**
* Creates a new {@link FactoryBeanTypePredictingBeanPostProcessor} predicting the type created by the
* {@link FactoryBean} of the given type by inspecting the {@link BeanDefinition} and considering the value for the
* given property as type to be created eventually.
*
* @param factoryBeanType must not be {@literal null}.
* @param properties must not be {@literal null} or empty.
*/
public FactoryBeanTypePredictingBeanPostProcessor(Class<?> factoryBeanType, String... properties) {
Assert.notNull(factoryBeanType, "FactoryBean type must not be null!");
Assert.isTrue(FactoryBean.class.isAssignableFrom(factoryBeanType), "Given type is not a FactoryBean type!");
Assert.notEmpty(properties, "Properties must not be empty!");
for (String property : properties) {
Assert.hasText(property, "Type property must not be null!");
}
this.factoryBeanType = factoryBeanType;
this.properties = Arrays.asList(properties);
}
/*
* (non-Javadoc)
* @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
*/
public void setBeanFactory(BeanFactory beanFactory) {
if (beanFactory instanceof ConfigurableListableBeanFactory) {
this.context = (ConfigurableListableBeanFactory) beanFactory;
}
}
/*
* (non-Javadoc)
* @see org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter#predictBeanType(java.lang.Class, java.lang.String)
*/
@Override
public Class<?> predictBeanType(Class<?> beanClass, String beanName) {
if (null == context || !factoryBeanType.isAssignableFrom(beanClass)) {
return null;
}
Class<?> resolvedBeanClass = cache.get(beanName);
if (resolvedBeanClass != null) {
return resolvedBeanClass == Void.class ? null : resolvedBeanClass;
}
BeanDefinition definition = context.getBeanDefinition(beanName);
try {
for (String property : properties) {
PropertyValue value = definition.getPropertyValues().getPropertyValue(property);
resolvedBeanClass = getClassForPropertyValue(value, beanName);
if (Void.class.equals(resolvedBeanClass)) {
continue;
}
return resolvedBeanClass;
}
return null;
} finally {
cache.put(beanName, resolvedBeanClass);
}
}
/**
* Returns the class which is configured in the given {@link PropertyValue}. In case it is not a
* {@link TypedStringValue} or the value contained cannot be interpreted as {@link Class} it will return {@link Void}.
*
* @param propertyValue can be {@literal null}.
* @param beanName must not be {@literal null}.
* @return
*/
private Class<?> getClassForPropertyValue(PropertyValue propertyValue, String beanName) {
if (propertyValue == null) {
return Void.class;
}
Object value = propertyValue.getValue();
String className = null;
if (value instanceof TypedStringValue) {
className = ((TypedStringValue) value).getValue();
} else if (value instanceof String) {
className = (String) value;
} else if (value instanceof Class<?>) {
return (Class<?>) value;
} else if (value instanceof String[]) {
String[] values = (String[]) value;
if (values.length == 0) {
return Void.class;
} else {
className = values[0];
}
} else {
return Void.class;
}
try {
return ClassUtils.resolveClassName(className, context.getBeanClassLoader());
} catch (IllegalArgumentException ex) {
LOGGER.warn(
String.format("Couldn't load class %s referenced as repository interface in bean %s!", className, beanName));
return Void.class;
}
}
/*
* (non-Javadoc)
* @see org.springframework.core.Ordered#getOrder()
*/
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE - 1;
}
}

19
src/main/java/org/springframework/data/repository/core/support/RepositoryFactoryBeanSupport.java

@ -24,7 +24,6 @@ import org.springframework.beans.factory.BeanFactory; @@ -24,7 +24,6 @@ import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.data.mapping.PersistentEntity;
@ -53,10 +52,10 @@ public abstract class RepositoryFactoryBeanSupport<T extends Repository<S, ID>, @@ -53,10 +52,10 @@ public abstract class RepositoryFactoryBeanSupport<T extends Repository<S, ID>,
implements InitializingBean, RepositoryFactoryInformation<S, ID>, FactoryBean<T>, BeanClassLoaderAware,
BeanFactoryAware, ApplicationEventPublisherAware {
private RepositoryFactorySupport factory;
private final Class<? extends T> repositoryInterface;
private RepositoryFactorySupport factory;
private Key queryLookupStrategyKey;
private Class<? extends T> repositoryInterface;
private Class<?> repositoryBaseClass;
private Object customImplementation;
private NamedQueries namedQueries;
@ -72,14 +71,13 @@ public abstract class RepositoryFactoryBeanSupport<T extends Repository<S, ID>, @@ -72,14 +71,13 @@ public abstract class RepositoryFactoryBeanSupport<T extends Repository<S, ID>,
private RepositoryMetadata repositoryMetadata;
/**
* Setter to inject the repository interface to implement.
* Creates a new {@link RepositoryFactoryBeanSupport} for the given repository interface.
*
* @param repositoryInterface the repository interface to set
* @param repositoryInterface must not be {@literal null}.
*/
@Required
public void setRepositoryInterface(Class<? extends T> repositoryInterface) {
protected RepositoryFactoryBeanSupport(Class<? extends T> repositoryInterface) {
Assert.notNull(repositoryInterface);
Assert.notNull(repositoryInterface, "Repository interface must not be null!");
this.repositoryInterface = repositoryInterface;
}
@ -229,9 +227,8 @@ public abstract class RepositoryFactoryBeanSupport<T extends Repository<S, ID>, @@ -229,9 +227,8 @@ public abstract class RepositoryFactoryBeanSupport<T extends Repository<S, ID>,
* (non-Javadoc)
* @see org.springframework.beans.factory.FactoryBean#getObjectType()
*/
@SuppressWarnings("unchecked")
public Class<? extends T> getObjectType() {
return (Class<? extends T>) (null == repositoryInterface ? Repository.class : repositoryInterface);
return repositoryInterface;
}
/*
@ -248,8 +245,6 @@ public abstract class RepositoryFactoryBeanSupport<T extends Repository<S, ID>, @@ -248,8 +245,6 @@ public abstract class RepositoryFactoryBeanSupport<T extends Repository<S, ID>,
*/
public void afterPropertiesSet() {
Assert.notNull(repositoryInterface, "Repository interface must not be null on initialization!");
this.factory = createRepositoryFactory();
this.factory.setQueryLookupStrategyKey(queryLookupStrategyKey);
this.factory.setNamedQueries(namedQueries);

9
src/main/java/org/springframework/data/repository/core/support/TransactionalRepositoryFactoryBeanSupport.java

@ -40,6 +40,15 @@ public abstract class TransactionalRepositoryFactoryBeanSupport<T extends Reposi @@ -40,6 +40,15 @@ public abstract class TransactionalRepositoryFactoryBeanSupport<T extends Reposi
private RepositoryProxyPostProcessor exceptionPostProcessor;
private boolean enableDefaultTransactions = true;
/**
* Creates a new {@link TransactionalRepositoryFactoryBeanSupport} for the given repository interface.
*
* @param repositoryInterface must not be {@literal null}.
*/
protected TransactionalRepositoryFactoryBeanSupport(Class<? extends T> repositoryInterface) {
super(repositoryInterface);
}
/**
* Setter to configure which transaction manager to be used. We have to use the bean name explicitly as otherwise the
* qualifier of the {@link org.springframework.transaction.annotation.Transactional} annotation is used. By explicitly

2
src/test/java/org/springframework/data/repository/config/RepositoryBeanNameGeneratorUnitTests.java

@ -62,7 +62,7 @@ public class RepositoryBeanNameGeneratorUnitTests { @@ -62,7 +62,7 @@ public class RepositoryBeanNameGeneratorUnitTests {
private BeanDefinition getBeanDefinitionFor(Class<?> repositoryInterface) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(RepositoryFactoryBeanSupport.class);
builder.addPropertyValue("repositoryInterface", repositoryInterface.getName());
builder.addConstructorArgValue(repositoryInterface.getName());
return builder.getBeanDefinition();
}

25
src/test/java/org/springframework/data/repository/config/RepositoryConfigurationExtensionSupportUnitTests.java

@ -23,12 +23,7 @@ import java.util.Collection; @@ -23,12 +23,7 @@ import java.util.Collection;
import java.util.Collections;
import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.StandardAnnotationMetadata;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
@ -65,26 +60,6 @@ public class RepositoryConfigurationExtensionSupportUnitTests { @@ -65,26 +60,6 @@ public class RepositoryConfigurationExtensionSupportUnitTests {
assertThat(extension.isStrictRepositoryCandidate(ExtendingInterface.class), is(true));
}
/**
* @see DATACMNS-609
*/
@Test
public void registersRepositoryInterfaceAwareBeanPostProcessorOnlyOnceForMultipleConfigurations() {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
AnnotationMetadata annotationMetadata = new StandardAnnotationMetadata(SampleConfiguration.class, true);
DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
StandardEnvironment environment = new StandardEnvironment();
AnnotationRepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(
annotationMetadata, EnableRepositories.class, resourceLoader, environment);
extension.registerBeansForRoot(beanFactory, configurationSource);
extension.registerBeansForRoot(beanFactory, configurationSource);
assertThat(beanFactory.getBeanDefinitionCount(), is(1));
}
static class SampleRepositoryConfigurationExtension extends RepositoryConfigurationExtensionSupport {
@Override

19
src/test/java/org/springframework/data/repository/core/support/DummyRepositoryFactoryBean.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2016 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.
@ -25,22 +25,17 @@ import org.springframework.data.repository.Repository; @@ -25,22 +25,17 @@ import org.springframework.data.repository.Repository;
/**
* @author Oliver Gierke
*/
public class DummyRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> extends
RepositoryFactoryBeanSupport<T, S, ID> {
public class DummyRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable>
extends RepositoryFactoryBeanSupport<T, S, ID> {
private T repository;
public DummyRepositoryFactoryBean() {
setMappingContext(new SampleMappingContext());
}
public DummyRepositoryFactoryBean(Class<? extends T> repositoryInterface) {
super(repositoryInterface);
/* (non-Javadoc)
* @see org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport#setRepositoryInterface(java.lang.Class)
*/
@Override
public void setRepositoryInterface(Class<? extends T> repositoryInterface) {
this.repository = mock(repositoryInterface);
super.setRepositoryInterface(repositoryInterface);
setMappingContext(new SampleMappingContext());
}
/*

78
src/test/java/org/springframework/data/repository/core/support/FactoryBeanTypePredictingPostProcessorIntegrationTests.java

@ -1,78 +0,0 @@ @@ -1,78 +0,0 @@
/*
* Copyright 2013-2016 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 java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.data.querydsl.User;
import org.springframework.data.repository.Repository;
/**
* Integration test to make sure Spring Data repository factory beans are found without the factories already
* instantiated.
*
* @see SPR-10517
* @author Oliver Gierke
*/
public class FactoryBeanTypePredictingPostProcessorIntegrationTests {
DefaultListableBeanFactory factory;
@Before
public void setUp() {
factory = new DefaultListableBeanFactory();
// Register factory bean for repository
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(DummyRepositoryFactoryBean.class);
builder.addPropertyValue("repositoryInterface", UserRepository.class);
factory.registerBeanDefinition("repository", builder.getBeanDefinition());
// Register predicting BeanPostProcessor
FactoryBeanTypePredictingBeanPostProcessor processor = new FactoryBeanTypePredictingBeanPostProcessor(
RepositoryFactoryBeanSupport.class, "repositoryInterface");
processor.setBeanFactory(factory);
factory.addBeanPostProcessor(processor);
}
@Test
public void lookupBeforeInstantiation() {
String[] strings = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(factory, RepositoryFactoryInformation.class,
false, false);
assertThat(Arrays.asList(strings), hasItem("&repository"));
}
@Test
public void lookupAfterInstantiation() {
factory.getBean(UserRepository.class);
String[] strings = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(factory, RepositoryFactoryInformation.class,
false, false);
assertThat(Arrays.asList(strings), hasItem("&repository"));
}
interface UserRepository extends Repository<User, Long> {}
}

130
src/test/java/org/springframework/data/repository/core/support/FactoryBeanTypePredictingPostProcessorUnitTests.java

@ -1,130 +0,0 @@ @@ -1,130 +0,0 @@
/*
* Copyright 2008-2016 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.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.io.Serializable;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.data.repository.Repository;
import org.springframework.jndi.JndiObjectFactoryBean;
/**
* Unit tests for {@link RepositoryInterfaceAwareBeanPostProcessor}.
*
* @author Oliver Gierke
*/
@RunWith(MockitoJUnitRunner.class)
public class FactoryBeanTypePredictingPostProcessorUnitTests {
private static final Class<?> FACTORY_CLASS = RepositoryFactoryBeanSupport.class;
private static final String BEAN_NAME = "foo";
private static final String DAO_INTERFACE_PROPERTY = "repositoryInterface";
FactoryBeanTypePredictingBeanPostProcessor processor;
BeanDefinition beanDefinition;
@Mock ConfigurableListableBeanFactory beanFactory;
@Before
public void setUp() {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(FACTORY_CLASS)
.addPropertyValue(DAO_INTERFACE_PROPERTY, UserDao.class);
this.beanDefinition = builder.getBeanDefinition();
this.processor = new FactoryBeanTypePredictingBeanPostProcessor(FACTORY_CLASS, "repositoryInterface");
when(beanFactory.getBeanDefinition(BEAN_NAME)).thenReturn(beanDefinition);
}
@Test
public void returnsDaoInterfaceClassForFactoryBean() throws Exception {
processor.setBeanFactory(beanFactory);
assertEquals(UserDao.class, processor.predictBeanType(FACTORY_CLASS, BEAN_NAME));
}
@Test
public void doesNotResolveInterfaceForNonFactoryClasses() throws Exception {
processor.setBeanFactory(beanFactory);
assertNotTypeDetected(BeanFactory.class);
}
@Test
public void doesNotResolveInterfaceForUnloadableClass() throws Exception {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(FACTORY_CLASS)
.addPropertyValue(DAO_INTERFACE_PROPERTY, "com.acme.Foo");
when(beanFactory.getBeanDefinition(BEAN_NAME)).thenReturn(builder.getBeanDefinition());
assertNotTypeDetected(FACTORY_CLASS);
}
@Test
public void doesNotResolveTypeForPlainBeanFactory() throws Exception {
BeanFactory beanFactory = mock(BeanFactory.class);
processor.setBeanFactory(beanFactory);
assertNotTypeDetected(FACTORY_CLASS);
}
@Test(expected = IllegalArgumentException.class)
public void rejectsNonFactoryBeanType() {
new FactoryBeanTypePredictingBeanPostProcessor(Object.class, "property");
}
/**
* @see DATACMNS-821
*/
@Test
public void usesFirstValueIfPropertyIsOfArrayType() {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(JndiObjectFactoryBean.class);
builder.addPropertyValue("proxyInterfaces",
new String[] { Serializable.class.getName(), Iterable.class.getName() });
when(beanFactory.getBeanDefinition(BEAN_NAME)).thenReturn(builder.getBeanDefinition());
processor = new FactoryBeanTypePredictingBeanPostProcessor(JndiObjectFactoryBean.class, "proxyInterface",
"proxyInterfaces");
processor.setBeanFactory(beanFactory);
assertThat(processor.predictBeanType(JndiObjectFactoryBean.class, BEAN_NAME),
is(typeCompatibleWith(Serializable.class)));
}
private void assertNotTypeDetected(Class<?> beanClass) {
assertThat(processor.predictBeanType(beanClass, BEAN_NAME), is(nullValue()));
}
private class User {}
private interface UserDao extends Repository<User, Long> {}
}

8
src/test/java/org/springframework/data/repository/core/support/RepositoryFactoryBeanSupportUnitTests.java

@ -44,10 +44,9 @@ public class RepositoryFactoryBeanSupportUnitTests { @@ -44,10 +44,9 @@ public class RepositoryFactoryBeanSupportUnitTests {
ClassLoader classLoader = mock(ClassLoader.class);
RepositoryFactoryBeanSupport factoryBean = new DummyRepositoryFactoryBean();
RepositoryFactoryBeanSupport factoryBean = new DummyRepositoryFactoryBean(SampleRepository.class);
factoryBean.setBeanClassLoader(classLoader);
factoryBean.setLazyInit(true);
factoryBean.setRepositoryInterface(SampleRepository.class);
factoryBean.afterPropertiesSet();
Object factory = ReflectionTestUtils.getField(factoryBean, "factory");
@ -58,14 +57,13 @@ public class RepositoryFactoryBeanSupportUnitTests { @@ -58,14 +57,13 @@ public class RepositoryFactoryBeanSupportUnitTests {
* @see DATACMNS-432
*/
@Test
@SuppressWarnings("rawtypes")
@SuppressWarnings({ "rawtypes", "unchecked" })
public void initializationFailsWithMissingRepositoryInterface() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("Repository interface");
RepositoryFactoryBeanSupport factoryBean = new DummyRepositoryFactoryBean();
factoryBean.afterPropertiesSet();
new DummyRepositoryFactoryBean(null);
}
interface SampleRepository extends Repository<Object, Long> {}

2
src/test/java/org/springframework/data/repository/core/support/TransactionRepositoryFactoryBeanSupportUnitTests.java

@ -88,7 +88,7 @@ public class TransactionRepositoryFactoryBeanSupportUnitTests { @@ -88,7 +88,7 @@ public class TransactionRepositoryFactoryBeanSupportUnitTests {
private final CrudRepository<Object, Long> repository = mock(CrudRepository.class);
public SampleTransactionalRepositoryFactoryBean() {
setRepositoryInterface((Class) CrudRepository.class);
super((Class) CrudRepository.class);
}
@Override

9
src/test/java/org/springframework/data/repository/sample/SampleConfiguration.java

@ -24,18 +24,15 @@ public class SampleConfiguration { @@ -24,18 +24,15 @@ public class SampleConfiguration {
@Bean
public RepositoryFactoryBeanSupport<Repository<User, Long>, User, Long> userRepositoryFactory() {
DummyRepositoryFactoryBean<Repository<User, Long>, User, Long> factory = new DummyRepositoryFactoryBean<Repository<User, Long>, User, Long>();
factory.setRepositoryInterface(UserRepository.class);
return factory;
return new DummyRepositoryFactoryBean<Repository<User, Long>, User, Long>(UserRepository.class);
}
@Bean
public RepositoryFactoryBeanSupport<Repository<Product, Long>, Product, Long> productRepositoryFactory(
ProductRepository productRepository) {
DummyRepositoryFactoryBean<Repository<Product, Long>, Product, Long> factory = new DummyRepositoryFactoryBean<Repository<Product, Long>, Product, Long>();
factory.setRepositoryInterface(ProductRepository.class);
DummyRepositoryFactoryBean<Repository<Product, Long>, Product, Long> factory = new DummyRepositoryFactoryBean<Repository<Product, Long>, Product, Long>(
ProductRepository.class);
factory.setCustomImplementation(productRepository);
return factory;

2
src/test/java/org/springframework/data/repository/support/DomainClassConverterUnitTests.java

@ -214,7 +214,7 @@ public class DomainClassConverterUnitTests { @@ -214,7 +214,7 @@ public class DomainClassConverterUnitTests {
private ApplicationContext initContextWithRepo() {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(DummyRepositoryFactoryBean.class);
builder.addPropertyValue("repositoryInterface", UserRepository.class);
builder.addConstructorArgValue(UserRepository.class);
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
factory.registerBeanDefinition("provider", builder.getBeanDefinition());

2
src/test/java/org/springframework/data/repository/support/DomainClassPropertyEditorRegistrarUnitTests.java

@ -51,7 +51,7 @@ public class DomainClassPropertyEditorRegistrarUnitTests { @@ -51,7 +51,7 @@ public class DomainClassPropertyEditorRegistrarUnitTests {
public void setup() {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(DummyRepositoryFactoryBean.class);
builder.addPropertyValue("repositoryInterface", EntityRepository.class);
builder.addConstructorArgValue(EntityRepository.class);
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
factory.registerBeanDefinition("provider", builder.getBeanDefinition());

2
src/test/java/org/springframework/data/repository/support/RepositoriesUnitTests.java

@ -73,7 +73,7 @@ public class RepositoriesUnitTests { @@ -73,7 +73,7 @@ public class RepositoriesUnitTests {
private AbstractBeanDefinition getRepositoryBeanDefinition(Class<?> repositoryInterface) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(DummyRepositoryFactoryBean.class);
builder.addPropertyValue("repositoryInterface", repositoryInterface);
builder.addConstructorArgValue(repositoryInterface);
return builder.getBeanDefinition();
}

Loading…
Cancel
Save