Skip to content

Commit 247f67d

Browse files
committed
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.
1 parent 86a4934 commit 247f67d

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

src/main/java/org/springframework/data/jpa/repository/query/JpaQueryExecution.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
* @author Mark Paluch
5656
* @author Christoph Strobl
5757
* @author Nicolas Cirigliano
58+
* @author Jens Schauder
5859
*/
5960
public abstract class JpaQueryExecution {
6061

@@ -225,12 +226,13 @@ static class ModifyingExecution extends JpaQueryExecution {
225226

226227
/**
227228
* Creates an execution that automatically flushes the given {@link EntityManager} before execution and/or
228-
* clears the given {@link EntityManager} after execution if the given {@link EntityManager} is not
229-
* {@literal null}.
229+
* clears the given {@link EntityManager} after execution.
230230
*
231-
* @param em
231+
* @param em Must not be {@literal null}.
232232
*/
233-
public ModifyingExecution(JpaQueryMethod method, @Nullable EntityManager em) {
233+
public ModifyingExecution(JpaQueryMethod method, EntityManager em) {
234+
235+
Assert.notNull(em, "The EntityManager must not be null.");
234236

235237
Class<?> returnType = method.getReturnType();
236238

@@ -247,13 +249,13 @@ public ModifyingExecution(JpaQueryMethod method, @Nullable EntityManager em) {
247249
@Override
248250
protected Object doExecute(AbstractJpaQuery query, Object[] values) {
249251

250-
if (em != null && flush) {
252+
if (flush) {
251253
em.flush();
252254
}
253255

254256
int result = query.createQuery(values).executeUpdate();
255257

256-
if (em != null && clear) {
258+
if (clear) {
257259
em.clear();
258260
}
259261

src/main/java/org/springframework/data/jpa/repository/query/JpaQueryMethod.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ String getNamedCountQueryName() {
316316
/**
317317
* Returns whether we should flush automatically for modifying queries.
318318
*
319-
* @return
319+
* @return whether we should flush automatically.
320320
*/
321321
boolean getFlushAutomatically() {
322322
return getMergedOrDefaultAnnotationValue("flushAutomatically", Modifying.class, Boolean.class);
@@ -325,7 +325,7 @@ boolean getFlushAutomatically() {
325325
/**
326326
* Returns whether we should clear automatically for modifying queries.
327327
*
328-
* @return
328+
* @return whether we should clear automatically.
329329
*/
330330
boolean getClearAutomatically() {
331331
return getMergedOrDefaultAnnotationValue("clearAutomatically", Modifying.class, Boolean.class);

src/test/java/org/springframework/data/jpa/repository/query/JpaQueryExecutionUnitTests.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import javax.persistence.Query;
2929
import javax.persistence.TypedQuery;
3030

31+
import org.junit.Before;
3132
import org.junit.Test;
3233
import org.junit.runner.RunWith;
3334
import org.mockito.Mock;
@@ -47,6 +48,7 @@
4748
* @author Thomas Darimont
4849
* @author Mark Paluch
4950
* @author Nicolas Cirigliano
51+
* @author Jens Schauder
5052
*/
5153
@RunWith(MockitoJUnitRunner.Silent.class)
5254
public class JpaQueryExecutionUnitTests {
@@ -58,6 +60,14 @@ public class JpaQueryExecutionUnitTests {
5860

5961
@Mock TypedQuery<Long> countQuery;
6062

63+
@Before
64+
public void setUp(){
65+
66+
when(query.executeUpdate()).thenReturn(0);
67+
when(jpaQuery.createQuery(Mockito.any(Object[].class))).thenReturn(query);
68+
when(jpaQuery.getQueryMethod()).thenReturn(method);
69+
}
70+
6171
@Test(expected = IllegalArgumentException.class)
6272
public void rejectsNullQuery() {
6373

@@ -83,15 +93,12 @@ protected Object doExecute(AbstractJpaQuery query, Object[] values) {
8393
}.execute(jpaQuery, new Object[] {}), is(nullValue()));
8494
}
8595

86-
@Test
96+
@Test // DATAJPA-806
8797
@SuppressWarnings({ "unchecked", "rawtypes" })
8898
public void modifyingExecutionFlushesEntityManagerIfSet() {
8999

90-
when(query.executeUpdate()).thenReturn(0);
91100
when(method.getReturnType()).thenReturn((Class) void.class);
92101
when(method.getFlushAutomatically()).thenReturn(true);
93-
when(jpaQuery.createQuery(Mockito.any(Object[].class))).thenReturn(query);
94-
when(jpaQuery.getQueryMethod()).thenReturn(method);
95102

96103
ModifyingExecution execution = new ModifyingExecution(method, em);
97104
execution.execute(jpaQuery, new Object[] {});
@@ -104,11 +111,8 @@ public void modifyingExecutionFlushesEntityManagerIfSet() {
104111
@SuppressWarnings({ "unchecked", "rawtypes" })
105112
public void modifyingExecutionClearsEntityManagerIfSet() {
106113

107-
when(query.executeUpdate()).thenReturn(0);
108114
when(method.getReturnType()).thenReturn((Class) void.class);
109115
when(method.getClearAutomatically()).thenReturn(true);
110-
when(jpaQuery.createQuery(Mockito.any(Object[].class))).thenReturn(query);
111-
when(jpaQuery.getQueryMethod()).thenReturn(method);
112116

113117
ModifyingExecution execution = new ModifyingExecution(method, em);
114118
execution.execute(jpaQuery, new Object[] {});

0 commit comments

Comments
 (0)