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:
Jens Schauder
2018-01-30 14:40:12 +01:00
parent 86a4934808
commit 247f67d33c
3 changed files with 21 additions and 15 deletions

View File

@@ -55,6 +55,7 @@ import org.springframework.util.ReflectionUtils;
* @author Mark Paluch
* @author Christoph Strobl
* @author Nicolas Cirigliano
* @author Jens Schauder
*/
public abstract class JpaQueryExecution {
@@ -225,12 +226,13 @@ 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, @Nullable EntityManager em) {
public ModifyingExecution(JpaQueryMethod method, EntityManager em) {
Assert.notNull(em, "The EntityManager must not be null.");
Class<?> returnType = method.getReturnType();
@@ -247,13 +249,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();
}

View File

@@ -316,7 +316,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);
@@ -325,7 +325,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);

View File

@@ -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.Silent.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[] {});