Adapted refactorings in Spring Data Commons.
This commit is contained in:
@@ -19,8 +19,8 @@ import java.io.Serializable;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
|
||||
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
|
||||
import org.springframework.data.repository.support.EntityMetadata;
|
||||
|
||||
|
||||
/**
|
||||
@@ -36,7 +36,7 @@ public class CustomGenericJpaRepository<T, ID extends Serializable> extends
|
||||
* @param domainClass
|
||||
* @param entityManager
|
||||
*/
|
||||
public CustomGenericJpaRepository(EntityMetadata<T> metadata,
|
||||
public CustomGenericJpaRepository(JpaEntityInformation<T> metadata,
|
||||
EntityManager entityManager) {
|
||||
|
||||
super(metadata, entityManager);
|
||||
|
||||
@@ -21,8 +21,8 @@ import java.io.Serializable;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
|
||||
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
|
||||
import org.springframework.data.repository.support.EntityMetadata;
|
||||
import org.springframework.data.repository.support.RepositoryMetadata;
|
||||
|
||||
|
||||
@@ -55,7 +55,8 @@ public class CustomGenericJpaRepositoryFactory extends JpaRepositoryFactory {
|
||||
protected Object getTargetRepository(RepositoryMetadata metadata,
|
||||
EntityManager em) {
|
||||
|
||||
EntityMetadata<Object> entityMetadata = mock(EntityMetadata.class);
|
||||
JpaEntityInformation<Object> entityMetadata =
|
||||
mock(JpaEntityInformation.class);
|
||||
when(entityMetadata.getJavaType()).thenReturn(
|
||||
(Class<Object>) metadata.getDomainClass());
|
||||
return new CustomGenericJpaRepository<Object, Serializable>(
|
||||
|
||||
@@ -20,18 +20,17 @@ import static org.junit.Assert.*;
|
||||
import static org.mockito.Matchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.mockito.stubbing.OngoingStubbing;
|
||||
import org.springframework.data.jpa.repository.query.JpaQueryExecution.ModifyingExecution;
|
||||
import org.springframework.data.repository.support.EntityMetadata;
|
||||
|
||||
|
||||
/**
|
||||
@@ -50,15 +49,8 @@ public class JpaQueryExecutionUnitTests {
|
||||
ParameterBinder binder;
|
||||
@Mock
|
||||
Query query;
|
||||
|
||||
Method method;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
|
||||
method = Dummy.class.getMethod("voidMethod");
|
||||
}
|
||||
@Mock
|
||||
EntityMetadata<?> metadata;
|
||||
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@@ -113,20 +105,32 @@ public class JpaQueryExecutionUnitTests {
|
||||
Query param = any();
|
||||
when(binder.bind(param)).thenReturn(query);
|
||||
when(query.executeUpdate()).thenReturn(0);
|
||||
mock(metadata, void.class);
|
||||
|
||||
ModifyingExecution execution = new ModifyingExecution(method, em);
|
||||
ModifyingExecution execution = new ModifyingExecution(metadata, em);
|
||||
execution.execute(jpaQuery, binder);
|
||||
|
||||
verify(em, times(1)).clear();
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
private void mock(EntityMetadata<?> method, Class<?> type,
|
||||
Class<?>... others) {
|
||||
|
||||
OngoingStubbing stubbing = when(method.getJavaType());
|
||||
stubbing.thenReturn(type);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void allowsMethodReturnTypesForModifyingQuery() throws Exception {
|
||||
|
||||
new ModifyingExecution(Dummy.class.getMethod("voidMethod"), em);
|
||||
new ModifyingExecution(Dummy.class.getMethod("intMethod"), em);
|
||||
new ModifyingExecution(Dummy.class.getMethod("integerMethod"), em);
|
||||
mock(metadata, void.class, int.class, Integer.class);
|
||||
|
||||
new ModifyingExecution(metadata, em);
|
||||
new ModifyingExecution(metadata, em);
|
||||
new ModifyingExecution(metadata, em);
|
||||
}
|
||||
|
||||
|
||||
@@ -134,7 +138,8 @@ public class JpaQueryExecutionUnitTests {
|
||||
public void modifyingExecutionRejectsNonIntegerOrVoidReturnType()
|
||||
throws Exception {
|
||||
|
||||
new ModifyingExecution(Dummy.class.getMethod("longMethod"), em);
|
||||
mock(metadata, Long.class);
|
||||
new ModifyingExecution(metadata, em);
|
||||
}
|
||||
|
||||
static class StubQueryExecution extends JpaQueryExecution {
|
||||
@@ -153,18 +158,4 @@ public class JpaQueryExecutionUnitTests {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static interface Dummy {
|
||||
|
||||
void voidMethod();
|
||||
|
||||
|
||||
int intMethod();
|
||||
|
||||
|
||||
Integer integerMethod();
|
||||
|
||||
|
||||
Long longMethod();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ import static org.mockito.Mockito.*;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.QueryHint;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -35,9 +34,9 @@ import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.sample.User;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.query.JpaQueryExecution.CollectionExecution;
|
||||
import org.springframework.data.jpa.repository.sample.UserRepository;
|
||||
import org.springframework.data.repository.query.QueryMethod;
|
||||
import org.springframework.data.repository.query.QueryMethod.Type;
|
||||
|
||||
|
||||
/**
|
||||
@@ -53,8 +52,6 @@ public class JpaQueryMethodUnitTests {
|
||||
|
||||
@Mock
|
||||
QueryExtractor extractor;
|
||||
@Mock
|
||||
EntityManager em;
|
||||
|
||||
Method repositoryMethod, invalidReturnType, pageableAndSort, pageableTwice,
|
||||
sortableTwice, modifyingMethod;
|
||||
@@ -91,40 +88,31 @@ public class JpaQueryMethodUnitTests {
|
||||
@Test
|
||||
public void testname() {
|
||||
|
||||
JpaQueryMethod method =
|
||||
new JpaQueryMethod(repositoryMethod, extractor, em);
|
||||
JpaQueryMethod method = new JpaQueryMethod(repositoryMethod, extractor);
|
||||
|
||||
assertEquals("User.findByLastname", method.getNamedQueryName());
|
||||
assertThat(method.getExecution(), is(CollectionExecution.class));
|
||||
assertThat(method.getType(), is(Type.COLLECTION));
|
||||
}
|
||||
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void preventsNullRepositoryMethod() {
|
||||
|
||||
new JpaQueryMethod(null, extractor, em);
|
||||
}
|
||||
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void preventsNullEntityManager() {
|
||||
|
||||
new JpaQueryMethod(repositoryMethod, extractor, null);
|
||||
new JpaQueryMethod(null, extractor);
|
||||
}
|
||||
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void preventsNullQueryExtractor() {
|
||||
|
||||
new JpaQueryMethod(repositoryMethod, null, em);
|
||||
new JpaQueryMethod(repositoryMethod, null);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void returnsCorrectName() {
|
||||
|
||||
JpaQueryMethod method =
|
||||
new JpaQueryMethod(repositoryMethod, extractor, em);
|
||||
JpaQueryMethod method = new JpaQueryMethod(repositoryMethod, extractor);
|
||||
assertEquals(repositoryMethod.getName(), method.getName());
|
||||
}
|
||||
|
||||
@@ -132,8 +120,7 @@ public class JpaQueryMethodUnitTests {
|
||||
@Test
|
||||
public void returnsQueryIfAvailable() throws Exception {
|
||||
|
||||
JpaQueryMethod method =
|
||||
new JpaQueryMethod(repositoryMethod, extractor, em);
|
||||
JpaQueryMethod method = new JpaQueryMethod(repositoryMethod, extractor);
|
||||
|
||||
assertNull(method.getAnnotatedQuery());
|
||||
|
||||
@@ -141,55 +128,36 @@ public class JpaQueryMethodUnitTests {
|
||||
UserRepository.class.getMethod("findByAnnotatedQuery",
|
||||
String.class);
|
||||
|
||||
assertNotNull(new JpaQueryMethod(repositoryMethod, extractor, em)
|
||||
assertNotNull(new JpaQueryMethod(repositoryMethod, extractor)
|
||||
.getAnnotatedQuery());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void returnsCorrectDomainClassName() {
|
||||
|
||||
JpaQueryMethod method =
|
||||
new JpaQueryMethod(repositoryMethod, extractor, em);
|
||||
assertEquals(DOMAIN_CLASS, method.getDomainClass());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void returnsCorrectNumberOfParameters() {
|
||||
|
||||
JpaQueryMethod method =
|
||||
new JpaQueryMethod(repositoryMethod, extractor, em);
|
||||
assertTrue(method.isCorrectNumberOfParameters(repositoryMethod
|
||||
.getParameterTypes().length));
|
||||
}
|
||||
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void rejectsInvalidReturntypeOnPagebleFinder() {
|
||||
|
||||
new JpaQueryMethod(invalidReturnType, extractor, em);
|
||||
new JpaQueryMethod(invalidReturnType, extractor);
|
||||
}
|
||||
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void rejectsPageableAndSortInFinderMethod() {
|
||||
|
||||
new JpaQueryMethod(pageableAndSort, extractor, em);
|
||||
new JpaQueryMethod(pageableAndSort, extractor);
|
||||
}
|
||||
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void rejectsTwoPageableParameters() {
|
||||
|
||||
new JpaQueryMethod(pageableTwice, extractor, em);
|
||||
new JpaQueryMethod(pageableTwice, extractor);
|
||||
}
|
||||
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void rejectsTwoSortableParameters() {
|
||||
|
||||
new JpaQueryMethod(sortableTwice, extractor, em);
|
||||
new JpaQueryMethod(sortableTwice, extractor);
|
||||
}
|
||||
|
||||
|
||||
@@ -203,15 +171,14 @@ public class JpaQueryMethodUnitTests {
|
||||
|
||||
when(extractor.canExtractQuery()).thenReturn(false);
|
||||
|
||||
new JpaQueryMethod(method, extractor, em);
|
||||
new JpaQueryMethod(method, extractor);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void recognizesModifyingMethod() {
|
||||
|
||||
JpaQueryMethod method =
|
||||
new JpaQueryMethod(modifyingMethod, extractor, em);
|
||||
JpaQueryMethod method = new JpaQueryMethod(modifyingMethod, extractor);
|
||||
assertTrue(method.isModifyingQuery());
|
||||
}
|
||||
|
||||
@@ -223,7 +190,7 @@ public class JpaQueryMethodUnitTests {
|
||||
InvalidRepository.class.getMethod("updateMethod", String.class,
|
||||
Pageable.class);
|
||||
|
||||
new JpaQueryMethod(method, extractor, em);
|
||||
new JpaQueryMethod(method, extractor);
|
||||
}
|
||||
|
||||
|
||||
@@ -234,15 +201,14 @@ public class JpaQueryMethodUnitTests {
|
||||
InvalidRepository.class.getMethod("updateMethod", String.class,
|
||||
Sort.class);
|
||||
|
||||
new JpaQueryMethod(method, extractor, em);
|
||||
new JpaQueryMethod(method, extractor);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void discoversHintsCorrectly() {
|
||||
|
||||
JpaQueryMethod method =
|
||||
new JpaQueryMethod(repositoryMethod, extractor, em);
|
||||
JpaQueryMethod method = new JpaQueryMethod(repositoryMethod, extractor);
|
||||
List<QueryHint> hints = method.getHints();
|
||||
|
||||
assertNotNull(hints);
|
||||
|
||||
@@ -43,14 +43,14 @@ import org.springframework.data.jpa.repository.sample.UserRepository;
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class SimpleJpaQueryUnitTests {
|
||||
|
||||
private JpaQueryMethod method;
|
||||
JpaQueryMethod method;
|
||||
|
||||
@Mock
|
||||
private EntityManager em;
|
||||
EntityManager em;
|
||||
@Mock
|
||||
private QueryExtractor extractor;
|
||||
QueryExtractor extractor;
|
||||
@Mock
|
||||
private Query query;
|
||||
Query query;
|
||||
|
||||
|
||||
@Before
|
||||
@@ -61,7 +61,7 @@ public class SimpleJpaQueryUnitTests {
|
||||
|
||||
Method setUp =
|
||||
UserRepository.class.getMethod("findByLastname", String.class);
|
||||
method = new JpaQueryMethod(setUp, extractor, em);
|
||||
method = new JpaQueryMethod(setUp, extractor);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -34,6 +34,8 @@ import org.springframework.beans.factory.ListableBeanFactory;
|
||||
import org.springframework.dao.support.PersistenceExceptionTranslator;
|
||||
import org.springframework.data.domain.Persistable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.support.RepositoryFactorySupport;
|
||||
|
||||
|
||||
/**
|
||||
@@ -47,18 +49,22 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class JpaRepositoryFactoryBeanUnitTests {
|
||||
|
||||
JpaRepositoryFactoryBean<SimpleSampleRepository> factory;
|
||||
JpaRepositoryFactoryBean<SimpleSampleRepository> factoryBean;
|
||||
|
||||
@Mock
|
||||
EntityManager entityManager;
|
||||
|
||||
@Mock
|
||||
RepositoryFactorySupport factory;
|
||||
@Mock
|
||||
ListableBeanFactory beanFactory;
|
||||
@Mock
|
||||
PersistenceExceptionTranslator translator;
|
||||
@Mock
|
||||
Repository<?, ?> repository;
|
||||
|
||||
|
||||
@Before
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setUp() {
|
||||
|
||||
Map<String, PersistenceExceptionTranslator> beans =
|
||||
@@ -68,12 +74,14 @@ public class JpaRepositoryFactoryBeanUnitTests {
|
||||
beanFactory.getBeansOfType(
|
||||
eq(PersistenceExceptionTranslator.class), anyBoolean(),
|
||||
anyBoolean())).thenReturn(beans);
|
||||
when(factory.getRepository(any(Class.class), any(Object.class)))
|
||||
.thenReturn(repository);
|
||||
|
||||
// Setup standard factory configuration
|
||||
factory =
|
||||
JpaRepositoryFactoryBean.create(SimpleSampleRepository.class,
|
||||
entityManager);
|
||||
factory.setEntityManager(entityManager);
|
||||
factoryBean =
|
||||
new DummyJpaRepositoryFactoryBean<SimpleSampleRepository>();
|
||||
factoryBean.setRepositoryInterface(SimpleSampleRepository.class);
|
||||
factoryBean.setEntityManager(entityManager);
|
||||
}
|
||||
|
||||
|
||||
@@ -86,17 +94,17 @@ public class JpaRepositoryFactoryBeanUnitTests {
|
||||
@Test
|
||||
public void setsUpBasicInstanceCorrectly() throws Exception {
|
||||
|
||||
factory.setBeanFactory(beanFactory);
|
||||
factory.afterPropertiesSet();
|
||||
factoryBean.setBeanFactory(beanFactory);
|
||||
factoryBean.afterPropertiesSet();
|
||||
|
||||
assertNotNull(factory.getObject());
|
||||
assertNotNull(factoryBean.getObject());
|
||||
}
|
||||
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void requiresListableBeanFactory() throws Exception {
|
||||
|
||||
factory.setBeanFactory(mock(BeanFactory.class));
|
||||
factoryBean.setBeanFactory(mock(BeanFactory.class));
|
||||
}
|
||||
|
||||
|
||||
@@ -109,7 +117,7 @@ public class JpaRepositoryFactoryBeanUnitTests {
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void preventsNullRepositoryInterface() {
|
||||
|
||||
factory.setRepositoryInterface(null);
|
||||
factoryBean.setRepositoryInterface(null);
|
||||
}
|
||||
|
||||
|
||||
@@ -120,8 +128,25 @@ public class JpaRepositoryFactoryBeanUnitTests {
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void preventsUnsetRepositoryInterface() throws Exception {
|
||||
|
||||
factory = new JpaRepositoryFactoryBean<SimpleSampleRepository>();
|
||||
factory.afterPropertiesSet();
|
||||
factoryBean = new JpaRepositoryFactoryBean<SimpleSampleRepository>();
|
||||
factoryBean.afterPropertiesSet();
|
||||
}
|
||||
|
||||
private class DummyJpaRepositoryFactoryBean<T extends JpaRepository<?, ?>>
|
||||
extends JpaRepositoryFactoryBean<T> {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean
|
||||
* #createRepositoryFactory()
|
||||
*/
|
||||
@Override
|
||||
protected RepositoryFactorySupport doCreateRepositoryFactory() {
|
||||
|
||||
return factory;
|
||||
}
|
||||
}
|
||||
|
||||
private interface SimpleSampleRepository extends
|
||||
|
||||
@@ -30,7 +30,6 @@ import org.springframework.data.domain.Persistable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.custom.CustomGenericJpaRepositoryFactory;
|
||||
import org.springframework.data.jpa.repository.custom.UserCustomExtendedRepository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
|
||||
/**
|
||||
@@ -45,13 +44,23 @@ public class JpaRepositoryFactoryUnitTests {
|
||||
|
||||
@Mock
|
||||
EntityManager entityManager;
|
||||
@Mock
|
||||
JpaEntityInformation<?> metadata;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
// Setup standard factory configuration
|
||||
factory = new JpaRepositoryFactory(entityManager);
|
||||
factory = new JpaRepositoryFactory(entityManager) {
|
||||
|
||||
@Override
|
||||
protected JpaEntityInformation<?> getEntityMetadata(
|
||||
java.lang.Class<?> domainClass) {
|
||||
|
||||
return metadata;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -136,8 +145,6 @@ public class JpaRepositoryFactoryUnitTests {
|
||||
private interface SimpleSampleRepository extends
|
||||
JpaRepository<User, Integer> {
|
||||
|
||||
@Transactional
|
||||
User readByPrimaryKey(Integer primaryKey);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user