From ca4d2f504ad372eb00efbb8aa48668702c19b351 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 5 Dec 2011 18:58:49 +0100 Subject: [PATCH] DATACMNS-108 - Fixed ClassCastException in RepositoryFactorySupport. --- .../support/RepositoryFactorySupport.java | 2 +- .../RepositoryFactorySupportUnitTests.java | 94 ++++++++++--------- 2 files changed, 53 insertions(+), 43 deletions(-) diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java index 3ab915fe2..31722b581 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java @@ -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); diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/core/support/RepositoryFactorySupportUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/core/support/RepositoryFactorySupportUnitTests.java index de1b7e9db..ac8280566 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/core/support/RepositoryFactorySupportUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/core/support/RepositoryFactorySupportUnitTests.java @@ -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; 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; 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; 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 backingRepo; @Mock @@ -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> foo = (Class>) repositoryInterface; + + assertThat(factory.getRepository(foo), is(notNullValue())); + } + class DummyRepositoryFactory extends RepositoryFactorySupport { /* (non-Javadoc) @@ -111,8 +120,7 @@ public class RepositoryFactorySupportUnitTests { */ @Override @SuppressWarnings("unchecked") - public EntityInformation getEntityInformation( - Class domainClass) { + public EntityInformation getEntityInformation(Class domainClass) { return mock(EntityInformation.class); } @@ -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 { 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 { Object findByClass(Class clazz); Object findByFoo(); - + Object save(Object entity); } - + interface ObjectRepositoryCustom { - + Object findOne(Serializable id); } - interface PlainQueryCreationListener extends - QueryCreationListener { + interface PlainQueryCreationListener extends QueryCreationListener { } - interface MyQueryCreationListener extends - QueryCreationListener { + interface MyQueryCreationListener extends QueryCreationListener { } interface MyRepositoryQuery extends RepositoryQuery { } - + interface ReadOnlyRepository extends Repository { - T findOne(ID id); + T findOne(ID id); - Iterable findAll(); + Iterable findAll(); - Page findAll(Pageable pageable); + Page findAll(Pageable pageable); - List findAll(Sort sort); + List findAll(Sort sort); - boolean exists(ID id); + boolean exists(ID id); - long count(); + long count(); } - + interface CustomRepository extends ReadOnlyRepository { - + + } + + @RepositoryDefinition(domainClass = Object.class, idClass = Long.class) + interface AnnotatedRepository { + } }