Skip to content

Commit 02bb54e

Browse files
committed
DATAJPA-1233 - Polishing.
Formatting, imports.
1 parent 61bf237 commit 02bb54e

File tree

6 files changed

+93
-73
lines changed

6 files changed

+93
-73
lines changed

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@
1515
*/
1616
package org.springframework.data.jpa.repository.query;
1717

18+
import static org.springframework.data.jpa.repository.query.QueryParameterSetter.ErrorHandling.*;
19+
1820
import javax.persistence.EntityManager;
1921
import javax.persistence.Query;
2022
import javax.persistence.Tuple;
2123

22-
import org.springframework.data.jpa.repository.query.QueryParameterSetter.ErrorHandling;
23-
import org.springframework.data.repository.query.*;
24+
import org.springframework.data.repository.query.EvaluationContextProvider;
25+
import org.springframework.data.repository.query.ParameterAccessor;
26+
import org.springframework.data.repository.query.ParametersParameterAccessor;
27+
import org.springframework.data.repository.query.ResultProcessor;
28+
import org.springframework.data.repository.query.ReturnedType;
2429
import org.springframework.expression.spel.standard.SpelExpressionParser;
2530
import org.springframework.util.Assert;
2631

@@ -106,7 +111,7 @@ protected Query doCreateCountQuery(Object[] values) {
106111
? em.createNativeQuery(queryString) //
107112
: em.createQuery(queryString, Long.class);
108113

109-
return parameterBinder.get().bind(query, values, ErrorHandling.LENIENT);
114+
return parameterBinder.get().bind(query, values, LENIENT);
110115
}
111116

112117
/**

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
*/
1616
package org.springframework.data.jpa.repository.query;
1717

18+
import static org.springframework.data.jpa.repository.query.QueryParameterSetter.ErrorHandling.*;
19+
1820
import javax.persistence.EntityManager;
1921
import javax.persistence.Query;
2022
import javax.persistence.TypedQuery;
2123

2224
import org.slf4j.Logger;
2325
import org.slf4j.LoggerFactory;
2426
import org.springframework.data.jpa.provider.QueryExtractor;
25-
import org.springframework.data.jpa.repository.query.QueryParameterSetter.ErrorHandling;
2627
import org.springframework.data.repository.query.Parameters;
2728
import org.springframework.data.repository.query.QueryCreationException;
2829
import org.springframework.data.repository.query.RepositoryQuery;
@@ -171,6 +172,6 @@ protected TypedQuery<Long> doCreateCountQuery(Object[] values) {
171172
countQuery = em.createQuery(QueryUtils.createCountQueryFor(queryString, countProjection), Long.class);
172173
}
173174

174-
return parameterBinder.get().bind(countQuery, values, ErrorHandling.LENIENT);
175+
return parameterBinder.get().bind(countQuery, values, LENIENT);
175176
}
176177
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,17 @@ public ParameterBinder(JpaParameters parameters, Iterable<QueryParameterSetter>
5353
this.parameterSetters = parameterSetters;
5454
}
5555

56+
public <T extends Query> T bind(T jpaQuery, Object[] values) {
57+
return bind(jpaQuery, values, ErrorHandling.STRICT);
58+
}
59+
5660
public <T extends Query> T bind(T jpaQuery, Object[] values, ErrorHandling errorHandling) {
5761

5862
parameterSetters.forEach(it -> it.setParameter(jpaQuery, values, errorHandling));
5963

6064
return jpaQuery;
6165
}
6266

63-
public <T extends Query> T bind(T jpaQuery, Object[] values) {
64-
return bind(jpaQuery, values, ErrorHandling.STRICT);
65-
}
66-
6767
/**
6868
* Binds the parameters to the given query and applies special parameter types (e.g. pagination).
6969
*

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

+12-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.data.jpa.repository.query;
1717

18-
import static org.springframework.data.jpa.repository.query.QueryParameterSetter.ErrorHandling.LENIENT;
18+
import static org.springframework.data.jpa.repository.query.QueryParameterSetter.ErrorHandling.*;
1919

2020
import java.util.Date;
2121
import java.util.function.Function;
@@ -92,8 +92,11 @@ public void setParameter(Query query, Object[] values, ErrorHandling errorHandli
9292
} else {
9393

9494
Integer position = parameter.getPosition();
95-
if (position != null && (query.getParameters().size() >= parameter.getPosition()
96-
|| registerExcessParameters(query) || errorHandling == LENIENT)) {
95+
96+
if (position != null //
97+
&& (query.getParameters().size() >= parameter.getPosition() //
98+
|| registerExcessParameters(query) //
99+
|| errorHandling == LENIENT)) {
97100

98101
errorHandling.execute(() -> query.setParameter(parameter.getPosition(), (Date) value, temporalType));
99102
}
@@ -109,8 +112,11 @@ public void setParameter(Query query, Object[] values, ErrorHandling errorHandli
109112
} else {
110113

111114
Integer position = parameter.getPosition();
112-
if (position != null && (query.getParameters().size() >= position || errorHandling == LENIENT
113-
|| registerExcessParameters(query))) {
115+
116+
if (position != null //
117+
&& (query.getParameters().size() >= position //
118+
|| errorHandling == LENIENT //
119+
|| registerExcessParameters(query))) {
114120

115121
errorHandling.execute(() -> query.setParameter(position, value));
116122
}
@@ -142,6 +148,7 @@ public void execute(Runnable block) {
142148
},
143149

144150
LENIENT {
151+
145152
@Override
146153
public void execute(Runnable block) {
147154

src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java

+24-13
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,21 @@
1515
*/
1616
package org.springframework.data.jpa.repository;
1717

18-
import static org.assertj.core.api.Assertions.assertThat;
19-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
20-
import static org.springframework.data.domain.Example.of;
21-
import static org.springframework.data.domain.ExampleMatcher.matching;
22-
import static org.springframework.data.domain.Sort.Direction.ASC;
23-
import static org.springframework.data.domain.Sort.Direction.DESC;
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.springframework.data.domain.Example.*;
20+
import static org.springframework.data.domain.ExampleMatcher.*;
21+
import static org.springframework.data.domain.Sort.Direction.*;
22+
import static org.springframework.data.jpa.domain.Specification.*;
2423
import static org.springframework.data.jpa.domain.Specification.not;
25-
import static org.springframework.data.jpa.domain.Specification.where;
2624
import static org.springframework.data.jpa.domain.sample.UserSpecifications.*;
2725

28-
import java.util.*;
26+
import java.util.ArrayList;
27+
import java.util.Arrays;
28+
import java.util.Collection;
29+
import java.util.Collections;
30+
import java.util.HashSet;
31+
import java.util.List;
32+
import java.util.Set;
2933
import java.util.stream.Stream;
3034

3135
import javax.persistence.EntityManager;
@@ -44,12 +48,18 @@
4448
import org.springframework.dao.DataAccessException;
4549
import org.springframework.dao.IncorrectResultSizeDataAccessException;
4650
import org.springframework.dao.InvalidDataAccessApiUsageException;
47-
import org.springframework.data.domain.*;
51+
import org.springframework.data.domain.Example;
52+
import org.springframework.data.domain.ExampleMatcher;
4853
import org.springframework.data.domain.ExampleMatcher.GenericPropertyMatcher;
4954
import org.springframework.data.domain.ExampleMatcher.StringMatcher;
55+
import org.springframework.data.domain.Page;
56+
import org.springframework.data.domain.PageImpl;
57+
import org.springframework.data.domain.PageRequest;
58+
import org.springframework.data.domain.Pageable;
59+
import org.springframework.data.domain.Slice;
60+
import org.springframework.data.domain.Sort;
5061
import org.springframework.data.domain.Sort.Direction;
5162
import org.springframework.data.domain.Sort.Order;
52-
import org.springframework.data.domain.ExampleMatcher.*;
5363
import org.springframework.data.jpa.domain.Specification;
5464
import org.springframework.data.jpa.domain.sample.Address;
5565
import org.springframework.data.jpa.domain.sample.Role;
@@ -214,10 +224,10 @@ public void returnsAllIgnoreCaseSortedCorrectly() throws Exception {
214224
flushTestUsers();
215225

216226
Order order = new Order(ASC, "firstname").ignoreCase();
217-
List<User> result = repository.findAll(Sort.by(order));
218227

219-
assertThat(repository.findAll(Sort.by(order))).hasSize(4).containsExactly(thirdUser, secondUser, fourthUser,
220-
firstUser);
228+
assertThat(repository.findAll(Sort.by(order))) //
229+
.hasSize(4)//
230+
.containsExactly(thirdUser, secondUser, fourthUser, firstUser);
221231
}
222232

223233
@Test
@@ -228,6 +238,7 @@ public void deleteColletionOfEntities() {
228238
long before = repository.count();
229239

230240
repository.deleteAll(Arrays.asList(firstUser, secondUser));
241+
231242
assertThat(repository.existsById(firstUser.getId())).isFalse();
232243
assertThat(repository.existsById(secondUser.getId())).isFalse();
233244
assertThat(repository.count()).isEqualTo(before - 2);

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

+42-46
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 the original author or authors.
2+
* Copyright 2017-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,14 +15,15 @@
1515
*/
1616
package org.springframework.data.jpa.repository.query;
1717

18-
import static java.util.Arrays.asList;
19-
import static javax.persistence.TemporalType.TIME;
18+
import static java.util.Arrays.*;
19+
import static javax.persistence.TemporalType.*;
20+
import static org.mockito.ArgumentMatchers.*;
2021
import static org.mockito.Mockito.*;
21-
import static org.springframework.data.jpa.repository.query.QueryParameterSetter.ErrorHandling.LENIENT;
22-
import static org.springframework.data.jpa.repository.query.QueryParameterSetter.ErrorHandling.STRICT;
22+
import static org.springframework.data.jpa.repository.query.QueryParameterSetter.ErrorHandling.*;
2323

24-
import lombok.RequiredArgsConstructor;
24+
import lombok.Value;
2525

26+
import java.util.Arrays;
2627
import java.util.Collections;
2728
import java.util.Date;
2829
import java.util.List;
@@ -38,7 +39,10 @@
3839
import org.springframework.data.jpa.repository.query.QueryParameterSetter.NamedOrIndexedQueryParameterSetter;
3940

4041
/**
42+
* Unit tests fir {@link NamedOrIndexedQueryParameterSetter}.
43+
*
4144
* @author Jens Schauder
45+
* @author Oliver Gierke
4246
*/
4347
public class NamedOrIndexedQueryParameterSetterUnitTests {
4448

@@ -47,7 +51,7 @@ public class NamedOrIndexedQueryParameterSetterUnitTests {
4751
Object[] methodArguments = { new Date() };
4852

4953
List<TemporalType> temporalTypes = asList(null, TIME);
50-
List<Parameter> parameters = asList( //
54+
List<Parameter<?>> parameters = Arrays.<Parameter<?>> asList( //
5155
mock(ParameterExpression.class), //
5256
new ParameterImpl("name", null), //
5357
new ParameterImpl(null, 1) //
@@ -87,7 +91,7 @@ public void lenientErrorHandlingThrowsNoExceptionForAllVariationsOfParameters()
8791

8892
Query query = mockExceptionThrowingQueryWithNamedParameters();
8993

90-
for (Parameter parameter : parameters) {
94+
for (Parameter<?> parameter : parameters) {
9195
for (TemporalType temporalType : temporalTypes) {
9296

9397
NamedOrIndexedQueryParameterSetter setter = new NamedOrIndexedQueryParameterSetter( //
@@ -111,9 +115,8 @@ public void lenientErrorHandlingThrowsNoExceptionForAllVariationsOfParameters()
111115

112116
/**
113117
* setParameter should be called in the lenient case even if the number of parameters seems to suggest that it fails,
114-
* since the index might not be continuous due to missing parts of count queries compared to the main query.
115-
*
116-
* This happens when a parameter gets used in the ORDER BY clause which gets stripped of for the count query.
118+
* since the index might not be continuous due to missing parts of count queries compared to the main query. This
119+
* happens when a parameter gets used in the ORDER BY clause which gets stripped of for the count query.
117120
*/
118121
@Test // DATAJPA-1233
119122
public void lenientSetsParameterWhenSuccessIsUnsure() {
@@ -130,20 +133,20 @@ public void lenientSetsParameterWhenSuccessIsUnsure() {
130133

131134
setter.setParameter(query, methodArguments, LENIENT);
132135

133-
if (temporalType == null)
136+
if (temporalType == null) {
134137
verify(query).setParameter(eq(11), any(Date.class));
135-
else
138+
} else {
136139
verify(query).setParameter(eq(11), any(Date.class), eq(temporalType));
140+
}
137141
}
138142

139143
softly.assertAll();
140144

141145
}
142146

143147
/**
144-
* This scenario happens when the only (name) parameter is part of an ORDER BY clause and gets stripped of for the count query.
145-
*
146-
* Then the count query has no named parameter but the parameter provided has a {@literal null} position.
148+
* This scenario happens when the only (name) parameter is part of an ORDER BY clause and gets stripped of for the
149+
* count query. Then the count query has no named parameter but the parameter provided has a {@literal null} position.
147150
*/
148151
@Test // DATAJPA-1233
149152
public void parameterNotSetWhenSuccessImpossible() {
@@ -160,57 +163,50 @@ public void parameterNotSetWhenSuccessImpossible() {
160163

161164
setter.setParameter(query, methodArguments, LENIENT);
162165

163-
if (temporalType == null)
166+
if (temporalType == null) {
164167
verify(query, never()).setParameter(anyInt(), any(Date.class));
165-
else
168+
} else {
166169
verify(query, never()).setParameter(anyInt(), any(Date.class), eq(temporalType));
170+
}
167171
}
168172

169173
softly.assertAll();
170174

171175
}
172176

173177
@SuppressWarnings("unchecked")
174-
public Query mockExceptionThrowingQueryWithNamedParameters() {
178+
private static Query mockExceptionThrowingQueryWithNamedParameters() {
179+
175180
Query query = mock(Query.class);
176181

177182
// make it a query with named parameters
178-
doReturn(Collections.singleton(new ParameterImpl("aName", 3))).when(query).getParameters();
179-
doThrow(new RuntimeException(EXCEPTION_MESSAGE)).when(query) //
180-
.setParameter(any(Parameter.class), any(Date.class), any(TemporalType.class));
181-
doThrow(new RuntimeException(EXCEPTION_MESSAGE)).when(query) //
182-
.setParameter(any(Parameter.class), any(Date.class));
183-
doThrow(new RuntimeException(EXCEPTION_MESSAGE)).when(query) //
184-
.setParameter(anyString(), any(Date.class), any(TemporalType.class));
185-
doThrow(new RuntimeException(EXCEPTION_MESSAGE)).when(query) //
186-
.setParameter(anyString(), any(Date.class));
187-
doThrow(new RuntimeException(EXCEPTION_MESSAGE)).when(query) //
188-
.setParameter(anyInt(), any(Date.class), any(TemporalType.class));
189-
doThrow(new RuntimeException(EXCEPTION_MESSAGE)).when(query) //
190-
.setParameter(anyInt(), any(Date.class));
183+
doReturn(Collections.singleton(new ParameterImpl("aName", 3))) //
184+
.when(query).getParameters();
185+
doThrow(new RuntimeException(EXCEPTION_MESSAGE)) //
186+
.when(query).setParameter(any(Parameter.class), any(Date.class), any(TemporalType.class));
187+
doThrow(new RuntimeException(EXCEPTION_MESSAGE)) //
188+
.when(query).setParameter(any(Parameter.class), any(Date.class));
189+
doThrow(new RuntimeException(EXCEPTION_MESSAGE)) //
190+
.when(query).setParameter(anyString(), any(Date.class), any(TemporalType.class));
191+
doThrow(new RuntimeException(EXCEPTION_MESSAGE)) //
192+
.when(query).setParameter(anyString(), any(Date.class));
193+
doThrow(new RuntimeException(EXCEPTION_MESSAGE)) //
194+
.when(query).setParameter(anyInt(), any(Date.class), any(TemporalType.class));
195+
doThrow(new RuntimeException(EXCEPTION_MESSAGE)) //
196+
.when(query).setParameter(anyInt(), any(Date.class));
197+
191198
return query;
192199
}
193200

194-
@RequiredArgsConstructor
201+
@Value
195202
private static class ParameterImpl implements Parameter<Object> {
196203

197-
private final String name;
198-
private final Integer position;
199-
200-
@Override
201-
public String getName() {
202-
return name;
203-
}
204-
205-
@Override
206-
public Integer getPosition() {
207-
return position;
208-
}
204+
String name;
205+
Integer position;
209206

210207
@Override
211208
public Class<Object> getParameterType() {
212209
return Object.class;
213210
}
214211
}
215-
216212
}

0 commit comments

Comments
 (0)