DATACMNS-133 - Fixed bean lookup in DomainClassConverter.
Previously DomainClassConverter looked up the RepositoryInformation instances from the entire BeanFactory hierarchy but only looked the actual repository up from the current BeanFactory and not the entire hierarchy. Fixed and added test case.
This commit is contained in:
@@ -113,7 +113,8 @@ public class DomainClassConverter<T extends ConversionService & ConverterRegistr
|
||||
|
||||
EntityInformation<Object, Serializable> metadata = entry.getEntityInformation();
|
||||
Class<CrudRepository<Object, Serializable>> objectType = entry.getRepositoryInterface();
|
||||
CrudRepository<Object, Serializable> repository = BeanFactoryUtils.beanOfType(context, objectType);
|
||||
CrudRepository<Object, Serializable> repository = BeanFactoryUtils.beanOfTypeIncludingAncestors(context,
|
||||
objectType);
|
||||
|
||||
this.repositories.put(metadata, repository);
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ public class DomainClassConverterUnitTests {
|
||||
Map<String, RepositoryFactoryInformation> providers;
|
||||
|
||||
@Mock
|
||||
ApplicationContext context;
|
||||
ApplicationContext context, parent;
|
||||
@Mock
|
||||
UserRepository repository;
|
||||
@Mock
|
||||
@@ -77,9 +77,6 @@ public class DomainClassConverterUnitTests {
|
||||
sourceDescriptor = TypeDescriptor.valueOf(String.class);
|
||||
targetDescriptor = TypeDescriptor.valueOf(User.class);
|
||||
|
||||
Map<String, UserRepository> map = getBeanAsMap(repository);
|
||||
when(context.getBeansOfType(UserRepository.class)).thenReturn(map);
|
||||
when(context.getBeansOfType(RepositoryFactoryInformation.class)).thenReturn(providers);
|
||||
when(provider.getEntityInformation()).thenReturn(information);
|
||||
when(provider.getRepositoryInterface()).thenReturn((Class) UserRepository.class);
|
||||
when(information.getJavaType()).thenReturn(User.class);
|
||||
@@ -96,7 +93,7 @@ public class DomainClassConverterUnitTests {
|
||||
@Test
|
||||
public void matchesIfConversionInBetweenIsPossible() throws Exception {
|
||||
|
||||
letContextContain(provider);
|
||||
letContextContain(context, provider);
|
||||
converter.setApplicationContext(context);
|
||||
|
||||
when(service.canConvert(String.class, Long.class)).thenReturn(true);
|
||||
@@ -107,7 +104,7 @@ public class DomainClassConverterUnitTests {
|
||||
@Test
|
||||
public void matchFailsIfNoIntermediateConversionIsPossible() throws Exception {
|
||||
|
||||
letContextContain(provider);
|
||||
letContextContain(context, provider);
|
||||
converter.setApplicationContext(context);
|
||||
|
||||
when(service.canConvert(String.class, Long.class)).thenReturn(false);
|
||||
@@ -123,7 +120,7 @@ public class DomainClassConverterUnitTests {
|
||||
@Test
|
||||
public void convertsStringToUserCorrectly() throws Exception {
|
||||
|
||||
letContextContain(provider);
|
||||
letContextContain(context, provider);
|
||||
converter.setApplicationContext(context);
|
||||
|
||||
when(service.canConvert(String.class, Long.class)).thenReturn(true);
|
||||
@@ -135,12 +132,38 @@ public class DomainClassConverterUnitTests {
|
||||
assertThat(user, is((Object) USER));
|
||||
}
|
||||
|
||||
private void letContextContain(Object bean) {
|
||||
/**
|
||||
* @see DATACMNS-133
|
||||
*/
|
||||
@Test
|
||||
public void discoversFactoryAndRepoFromParentApplicationContext() {
|
||||
|
||||
letContextContain(parent, provider);
|
||||
when(context.getParentBeanFactory()).thenReturn(parent);
|
||||
when(service.canConvert(String.class, Long.class)).thenReturn(true);
|
||||
|
||||
converter.setApplicationContext(context);
|
||||
assertThat(converter.matches(sourceDescriptor, targetDescriptor), is(true));
|
||||
}
|
||||
|
||||
private void letContextContain(ApplicationContext context, Object bean) {
|
||||
|
||||
configureContextToReturnBeans(context, repository, provider);
|
||||
|
||||
Map<String, Object> beanMap = getBeanAsMap(bean);
|
||||
when(context.getBeansOfType(argThat(is(subtypeOf(bean.getClass()))))).thenReturn(beanMap);
|
||||
}
|
||||
|
||||
private void configureContextToReturnBeans(ApplicationContext context, UserRepository repository,
|
||||
RepositoryFactoryInformation<User, Long> provider) {
|
||||
|
||||
Map<String, UserRepository> map = getBeanAsMap(repository);
|
||||
when(context.getBeansOfType(UserRepository.class)).thenReturn(map);
|
||||
|
||||
providers.put("provider", provider);
|
||||
when(context.getBeansOfType(RepositoryFactoryInformation.class)).thenReturn(providers);
|
||||
}
|
||||
|
||||
private <T> Map<String, T> getBeanAsMap(T bean) {
|
||||
|
||||
Map<String, T> beanMap = new HashMap<String, T>();
|
||||
|
||||
Reference in New Issue
Block a user