DATACMNS-108 - Fixed ClassCastException in RepositoryFactorySupport.
This commit is contained in:
@@ -133,7 +133,7 @@ public abstract class RepositoryFactorySupport {
|
|||||||
// Create proxy
|
// Create proxy
|
||||||
ProxyFactory result = new ProxyFactory();
|
ProxyFactory result = new ProxyFactory();
|
||||||
result.setTarget(target);
|
result.setTarget(target);
|
||||||
result.setInterfaces(new Class[] { repositoryInterface });
|
result.setInterfaces(new Class[] { repositoryInterface, Repository.class });
|
||||||
|
|
||||||
for (RepositoryProxyPostProcessor processor : postProcessors) {
|
for (RepositoryProxyPostProcessor processor : postProcessors) {
|
||||||
processor.postProcess(result);
|
processor.postProcess(result);
|
||||||
|
|||||||
@@ -15,7 +15,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.springframework.data.repository.core.support;
|
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 static org.mockito.Mockito.*;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
@@ -25,6 +26,7 @@ import java.util.List;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.Mockito;
|
||||||
import org.mockito.runners.MockitoJUnitRunner;
|
import org.mockito.runners.MockitoJUnitRunner;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
@@ -32,6 +34,7 @@ import org.springframework.data.domain.Pageable;
|
|||||||
import org.springframework.data.domain.Sort;
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||||
import org.springframework.data.repository.Repository;
|
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.EntityInformation;
|
||||||
import org.springframework.data.repository.core.NamedQueries;
|
import org.springframework.data.repository.core.NamedQueries;
|
||||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||||
@@ -39,7 +42,6 @@ import org.springframework.data.repository.query.QueryLookupStrategy;
|
|||||||
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
|
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
|
||||||
import org.springframework.data.repository.query.RepositoryQuery;
|
import org.springframework.data.repository.query.RepositoryQuery;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit tests for {@link RepositoryFactorySupport}.
|
* Unit tests for {@link RepositoryFactorySupport}.
|
||||||
*
|
*
|
||||||
@@ -60,18 +62,16 @@ public class RepositoryFactorySupportUnitTests {
|
|||||||
@Mock
|
@Mock
|
||||||
PlainQueryCreationListener otherListener;
|
PlainQueryCreationListener otherListener;
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void invokesCustomQueryCreationListenerForSpecialRepositoryQueryOnly()
|
public void invokesCustomQueryCreationListenerForSpecialRepositoryQueryOnly() throws Exception {
|
||||||
throws Exception {
|
|
||||||
|
|
||||||
factory.addQueryCreationListener(listener);
|
factory.addQueryCreationListener(listener);
|
||||||
factory.addQueryCreationListener(otherListener);
|
factory.addQueryCreationListener(otherListener);
|
||||||
|
|
||||||
factory.getRepository(ObjectRepository.class);
|
factory.getRepository(ObjectRepository.class);
|
||||||
|
|
||||||
verify(listener, times(1)).onCreation(any(MyRepositoryQuery.class));
|
verify(listener, times(1)).onCreation(Mockito.any(MyRepositoryQuery.class));
|
||||||
verify(otherListener, times(2)).onCreation(any(RepositoryQuery.class));
|
verify(otherListener, times(2)).onCreation(Mockito.any(RepositoryQuery.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -80,7 +80,7 @@ public class RepositoryFactorySupportUnitTests {
|
|||||||
ObjectRepository repository = factory.getRepository(ObjectRepository.class);
|
ObjectRepository repository = factory.getRepository(ObjectRepository.class);
|
||||||
repository.save(repository);
|
repository.save(repository);
|
||||||
|
|
||||||
verify(backingRepo, times(1)).save(any(Object.class));
|
verify(backingRepo, times(1)).save(Mockito.any(Object.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -103,6 +103,15 @@ public class RepositoryFactorySupportUnitTests {
|
|||||||
verify(backingRepo, times(1)).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 {
|
class DummyRepositoryFactory extends RepositoryFactorySupport {
|
||||||
|
|
||||||
@@ -111,8 +120,7 @@ public class RepositoryFactorySupportUnitTests {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <T, ID extends Serializable> EntityInformation<T, ID> getEntityInformation(
|
public <T, ID extends Serializable> EntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
|
||||||
Class<T> domainClass) {
|
|
||||||
|
|
||||||
return mock(EntityInformation.class);
|
return mock(EntityInformation.class);
|
||||||
}
|
}
|
||||||
@@ -123,14 +131,12 @@ public class RepositoryFactorySupportUnitTests {
|
|||||||
return backingRepo;
|
return backingRepo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
|
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
|
||||||
|
|
||||||
return backingRepo.getClass();
|
return backingRepo.getClass();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected QueryLookupStrategy getQueryLookupStrategy(Key key) {
|
protected QueryLookupStrategy getQueryLookupStrategy(Key key) {
|
||||||
|
|
||||||
@@ -138,8 +144,9 @@ public class RepositoryFactorySupportUnitTests {
|
|||||||
RepositoryQuery queryTwo = mock(RepositoryQuery.class);
|
RepositoryQuery queryTwo = mock(RepositoryQuery.class);
|
||||||
|
|
||||||
QueryLookupStrategy strategy = mock(QueryLookupStrategy.class);
|
QueryLookupStrategy strategy = mock(QueryLookupStrategy.class);
|
||||||
when(strategy.resolveQuery(any(Method.class), any(RepositoryMetadata.class), any(NamedQueries.class)))
|
when(
|
||||||
.thenReturn(queryOne, queryTwo);
|
strategy.resolveQuery(Mockito.any(Method.class), Mockito.any(RepositoryMetadata.class),
|
||||||
|
Mockito.any(NamedQueries.class))).thenReturn(queryOne, queryTwo);
|
||||||
|
|
||||||
return strategy;
|
return strategy;
|
||||||
}
|
}
|
||||||
@@ -159,13 +166,11 @@ public class RepositoryFactorySupportUnitTests {
|
|||||||
Object findOne(Serializable id);
|
Object findOne(Serializable id);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface PlainQueryCreationListener extends
|
interface PlainQueryCreationListener extends QueryCreationListener<RepositoryQuery> {
|
||||||
QueryCreationListener<RepositoryQuery> {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface MyQueryCreationListener extends
|
interface MyQueryCreationListener extends QueryCreationListener<MyRepositoryQuery> {
|
||||||
QueryCreationListener<MyRepositoryQuery> {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,20 +180,25 @@ public class RepositoryFactorySupportUnitTests {
|
|||||||
|
|
||||||
interface ReadOnlyRepository<T, ID extends Serializable> extends Repository<T, ID> {
|
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> {
|
interface CustomRepository extends ReadOnlyRepository<Object, Long> {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RepositoryDefinition(domainClass = Object.class, idClass = Long.class)
|
||||||
|
interface AnnotatedRepository {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user