DATAJPA-806 - Polishing.
EntityManager is now not null in ModifyingExecution. Extracted common code from test to make the differences between tests more obvious. Improved JavaDoc a little.
This commit is contained in:
@@ -54,6 +54,7 @@ import org.springframework.util.ReflectionUtils;
|
||||
* @author Thomas Darimont
|
||||
* @author Mark Paluch
|
||||
* @author Nicolas Cirigliano
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
public abstract class JpaQueryExecution {
|
||||
|
||||
@@ -221,13 +222,14 @@ public abstract class JpaQueryExecution {
|
||||
|
||||
/**
|
||||
* Creates an execution that automatically flushes the given {@link EntityManager} before execution and/or
|
||||
* clears the given {@link EntityManager} after execution if the given {@link EntityManager} is not
|
||||
* {@literal null}.
|
||||
* clears the given {@link EntityManager} after execution.
|
||||
*
|
||||
* @param em
|
||||
* @param em Must not be {@literal null}.
|
||||
*/
|
||||
public ModifyingExecution(JpaQueryMethod method, EntityManager em) {
|
||||
|
||||
Assert.notNull(em, "The EntityManager must not be null.");
|
||||
|
||||
Class<?> returnType = method.getReturnType();
|
||||
|
||||
boolean isVoid = void.class.equals(returnType) || Void.class.equals(returnType);
|
||||
@@ -243,13 +245,13 @@ public abstract class JpaQueryExecution {
|
||||
@Override
|
||||
protected Object doExecute(AbstractJpaQuery query, Object[] values) {
|
||||
|
||||
if (em != null && flush) {
|
||||
if (flush) {
|
||||
em.flush();
|
||||
}
|
||||
|
||||
int result = query.createQuery(values).executeUpdate();
|
||||
|
||||
if (em != null && clear) {
|
||||
if (clear) {
|
||||
em.clear();
|
||||
}
|
||||
|
||||
|
||||
@@ -287,7 +287,7 @@ public class JpaQueryMethod extends QueryMethod {
|
||||
/**
|
||||
* Returns whether we should flush automatically for modifying queries.
|
||||
*
|
||||
* @return
|
||||
* @return whether we should flush automatically.
|
||||
*/
|
||||
boolean getFlushAutomatically() {
|
||||
return getMergedOrDefaultAnnotationValue("flushAutomatically", Modifying.class, Boolean.class);
|
||||
@@ -296,7 +296,7 @@ public class JpaQueryMethod extends QueryMethod {
|
||||
/**
|
||||
* Returns whether we should clear automatically for modifying queries.
|
||||
*
|
||||
* @return
|
||||
* @return whether we should clear automatically.
|
||||
*/
|
||||
boolean getClearAutomatically() {
|
||||
return getMergedOrDefaultAnnotationValue("clearAutomatically", Modifying.class, Boolean.class);
|
||||
|
||||
@@ -28,6 +28,7 @@ import javax.persistence.EntityManager;
|
||||
import javax.persistence.Query;
|
||||
import javax.persistence.TypedQuery;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
@@ -47,6 +48,7 @@ import org.springframework.data.repository.query.Parameters;
|
||||
* @author Thomas Darimont
|
||||
* @author Mark Paluch
|
||||
* @author Nicolas Cirigliano
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class JpaQueryExecutionUnitTests {
|
||||
@@ -58,6 +60,14 @@ public class JpaQueryExecutionUnitTests {
|
||||
|
||||
@Mock TypedQuery<Long> countQuery;
|
||||
|
||||
@Before
|
||||
public void setUp(){
|
||||
|
||||
when(query.executeUpdate()).thenReturn(0);
|
||||
when(jpaQuery.createQuery(Mockito.any(Object[].class))).thenReturn(query);
|
||||
when(jpaQuery.getQueryMethod()).thenReturn(method);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsNullQuery() {
|
||||
|
||||
@@ -83,15 +93,12 @@ public class JpaQueryExecutionUnitTests {
|
||||
}.execute(jpaQuery, new Object[] {}), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test // DATAJPA-806
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public void modifyingExecutionFlushesEntityManagerIfSet() {
|
||||
|
||||
when(query.executeUpdate()).thenReturn(0);
|
||||
when(method.getReturnType()).thenReturn((Class) void.class);
|
||||
when(method.getFlushAutomatically()).thenReturn(true);
|
||||
when(jpaQuery.createQuery(Mockito.any(Object[].class))).thenReturn(query);
|
||||
when(jpaQuery.getQueryMethod()).thenReturn(method);
|
||||
|
||||
ModifyingExecution execution = new ModifyingExecution(method, em);
|
||||
execution.execute(jpaQuery, new Object[] {});
|
||||
@@ -104,11 +111,8 @@ public class JpaQueryExecutionUnitTests {
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public void modifyingExecutionClearsEntityManagerIfSet() {
|
||||
|
||||
when(query.executeUpdate()).thenReturn(0);
|
||||
when(method.getReturnType()).thenReturn((Class) void.class);
|
||||
when(method.getClearAutomatically()).thenReturn(true);
|
||||
when(jpaQuery.createQuery(Mockito.any(Object[].class))).thenReturn(query);
|
||||
when(jpaQuery.getQueryMethod()).thenReturn(method);
|
||||
|
||||
ModifyingExecution execution = new ModifyingExecution(method, em);
|
||||
execution.execute(jpaQuery, new Object[] {});
|
||||
|
||||
Reference in New Issue
Block a user