Browse Source

DATACMNS-108 - Fixed ClassCastException in RepositoryFactorySupport.

pull/13/merge
Oliver Gierke 14 years ago
parent
commit
ca4d2f504a
  1. 2
      spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java
  2. 94
      spring-data-commons-core/src/test/java/org/springframework/data/repository/core/support/RepositoryFactorySupportUnitTests.java

2
spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java

@ -133,7 +133,7 @@ public abstract class RepositoryFactorySupport { @@ -133,7 +133,7 @@ public abstract class RepositoryFactorySupport {
// Create proxy
ProxyFactory result = new ProxyFactory();
result.setTarget(target);
result.setInterfaces(new Class[] { repositoryInterface });
result.setInterfaces(new Class[] { repositoryInterface, Repository.class });
for (RepositoryProxyPostProcessor processor : postProcessors) {
processor.postProcess(result);

94
spring-data-commons-core/src/test/java/org/springframework/data/repository/core/support/RepositoryFactorySupportUnitTests.java

@ -15,7 +15,8 @@ @@ -15,7 +15,8 @@
*/
package org.springframework.data.repository.core.support;
import static org.mockito.Matchers.*;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.io.Serializable;
@ -25,6 +26,7 @@ import java.util.List; @@ -25,6 +26,7 @@ import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
@ -32,6 +34,7 @@ import org.springframework.data.domain.Pageable; @@ -32,6 +34,7 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.RepositoryDefinition;
import org.springframework.data.repository.core.EntityInformation;
import org.springframework.data.repository.core.NamedQueries;
import org.springframework.data.repository.core.RepositoryMetadata;
@ -39,17 +42,16 @@ import org.springframework.data.repository.query.QueryLookupStrategy; @@ -39,17 +42,16 @@ import org.springframework.data.repository.query.QueryLookupStrategy;
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
import org.springframework.data.repository.query.RepositoryQuery;
/**
* Unit tests for {@link RepositoryFactorySupport}.
*
*
* @author Oliver Gierke
*/
@RunWith(MockitoJUnitRunner.class)
public class RepositoryFactorySupportUnitTests {
RepositoryFactorySupport factory = new DummyRepositoryFactory();
@Mock
PagingAndSortingRepository<Object, Serializable> backingRepo;
@Mock
@ -60,50 +62,57 @@ public class RepositoryFactorySupportUnitTests { @@ -60,50 +62,57 @@ public class RepositoryFactorySupportUnitTests {
@Mock
PlainQueryCreationListener otherListener;
@Test
public void invokesCustomQueryCreationListenerForSpecialRepositoryQueryOnly()
throws Exception {
public void invokesCustomQueryCreationListenerForSpecialRepositoryQueryOnly() throws Exception {
factory.addQueryCreationListener(listener);
factory.addQueryCreationListener(otherListener);
factory.getRepository(ObjectRepository.class);
verify(listener, times(1)).onCreation(any(MyRepositoryQuery.class));
verify(otherListener, times(2)).onCreation(any(RepositoryQuery.class));
verify(listener, times(1)).onCreation(Mockito.any(MyRepositoryQuery.class));
verify(otherListener, times(2)).onCreation(Mockito.any(RepositoryQuery.class));
}
@Test
public void routesCallToRedeclaredMethodIntoTarget() {
ObjectRepository repository = factory.getRepository(ObjectRepository.class);
repository.save(repository);
verify(backingRepo, times(1)).save(any(Object.class));
verify(backingRepo, times(1)).save(Mockito.any(Object.class));
}
@Test
public void invokesCustomMethodIfItRedeclaresACRUDOne() {
ObjectRepository repository = factory.getRepository(ObjectRepository.class, customImplementation);
repository.findOne(1);
verify(customImplementation, times(1)).findOne(1);
verify(backingRepo, times(0)).findOne(1);
}
@Test
public void createsRepositoryInstanceWithCustomIntermediateRepository() {
CustomRepository repository = factory.getRepository(CustomRepository.class);
Pageable pageable = new PageRequest(0, 10);
repository.findAll(pageable);
verify(backingRepo, times(1)).findAll(pageable);
}
@Test
@SuppressWarnings("unchecked")
public void createsProxyForAnnotatedRepository() {
Class<?> repositoryInterface = AnnotatedRepository.class;
Class<? extends Repository<?, ?>> foo = (Class<? extends Repository<?, ?>>) repositoryInterface;
assertThat(factory.getRepository(foo), is(notNullValue()));
}
class DummyRepositoryFactory extends RepositoryFactorySupport {
/* (non-Javadoc)
@ -111,8 +120,7 @@ public class RepositoryFactorySupportUnitTests { @@ -111,8 +120,7 @@ public class RepositoryFactorySupportUnitTests {
*/
@Override
@SuppressWarnings("unchecked")
public <T, ID extends Serializable> EntityInformation<T, ID> getEntityInformation(
Class<T> domainClass) {
public <T, ID extends Serializable> EntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
return mock(EntityInformation.class);
}
@ -123,14 +131,12 @@ public class RepositoryFactorySupportUnitTests { @@ -123,14 +131,12 @@ public class RepositoryFactorySupportUnitTests {
return backingRepo;
}
@Override
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
return backingRepo.getClass();
}
@Override
protected QueryLookupStrategy getQueryLookupStrategy(Key key) {
@ -138,8 +144,9 @@ public class RepositoryFactorySupportUnitTests { @@ -138,8 +144,9 @@ public class RepositoryFactorySupportUnitTests {
RepositoryQuery queryTwo = mock(RepositoryQuery.class);
QueryLookupStrategy strategy = mock(QueryLookupStrategy.class);
when(strategy.resolveQuery(any(Method.class), any(RepositoryMetadata.class), any(NamedQueries.class)))
.thenReturn(queryOne, queryTwo);
when(
strategy.resolveQuery(Mockito.any(Method.class), Mockito.any(RepositoryMetadata.class),
Mockito.any(NamedQueries.class))).thenReturn(queryOne, queryTwo);
return strategy;
}
@ -150,45 +157,48 @@ public class RepositoryFactorySupportUnitTests { @@ -150,45 +157,48 @@ public class RepositoryFactorySupportUnitTests {
Object findByClass(Class<?> clazz);
Object findByFoo();
Object save(Object entity);
}
interface ObjectRepositoryCustom {
Object findOne(Serializable id);
}
interface PlainQueryCreationListener extends
QueryCreationListener<RepositoryQuery> {
interface PlainQueryCreationListener extends QueryCreationListener<RepositoryQuery> {
}
interface MyQueryCreationListener extends
QueryCreationListener<MyRepositoryQuery> {
interface MyQueryCreationListener extends QueryCreationListener<MyRepositoryQuery> {
}
interface MyRepositoryQuery extends RepositoryQuery {
}
interface ReadOnlyRepository<T, ID extends Serializable> extends Repository<T, ID> {
T findOne(ID id);
T findOne(ID id);
Iterable<T> findAll();
Iterable<T> findAll();
Page<T> findAll(Pageable pageable);
Page<T> findAll(Pageable pageable);
List<T> findAll(Sort sort);
List<T> findAll(Sort sort);
boolean exists(ID id);
boolean exists(ID id);
long count();
long count();
}
interface CustomRepository extends ReadOnlyRepository<Object, Long> {
}
@RepositoryDefinition(domainClass = Object.class, idClass = Long.class)
interface AnnotatedRepository {
}
}

Loading…
Cancel
Save