From 7c062ceafc3ef939a0c634683c0eb08dc906fe6d Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Tue, 30 Jan 2018 14:40:12 +0100 Subject: [PATCH] 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. --- .../repository/query/JpaQueryExecution.java | 14 ++++++++------ .../jpa/repository/query/JpaQueryMethod.java | 4 ++-- .../query/JpaQueryExecutionUnitTests.java | 18 +++++++++++------- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryExecution.java b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryExecution.java index 518ee4578..69fd05eb9 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryExecution.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryExecution.java @@ -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(); } diff --git a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethod.java b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethod.java index b256bf717..b71baf367 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethod.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethod.java @@ -315,7 +315,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); @@ -324,7 +324,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); diff --git a/src/test/java/org/springframework/data/jpa/repository/query/JpaQueryExecutionUnitTests.java b/src/test/java/org/springframework/data/jpa/repository/query/JpaQueryExecutionUnitTests.java index 599ab900a..c31fb48df 100644 --- a/src/test/java/org/springframework/data/jpa/repository/query/JpaQueryExecutionUnitTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/query/JpaQueryExecutionUnitTests.java @@ -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 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[] {});