1
1
/*
2
- * Copyright 2017 the original author or authors.
2
+ * Copyright 2017-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
15
15
*/
16
16
package org .springframework .data .jpa .repository .query ;
17
17
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 .*;
20
21
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 .*;
23
23
24
- import lombok .RequiredArgsConstructor ;
24
+ import lombok .Value ;
25
25
26
+ import java .util .Arrays ;
26
27
import java .util .Collections ;
27
28
import java .util .Date ;
28
29
import java .util .List ;
38
39
import org .springframework .data .jpa .repository .query .QueryParameterSetter .NamedOrIndexedQueryParameterSetter ;
39
40
40
41
/**
42
+ * Unit tests fir {@link NamedOrIndexedQueryParameterSetter}.
43
+ *
41
44
* @author Jens Schauder
45
+ * @author Oliver Gierke
42
46
*/
43
47
public class NamedOrIndexedQueryParameterSetterUnitTests {
44
48
@@ -47,7 +51,7 @@ public class NamedOrIndexedQueryParameterSetterUnitTests {
47
51
Object [] methodArguments = { new Date () };
48
52
49
53
List <TemporalType > temporalTypes = asList (null , TIME );
50
- List <Parameter > parameters = asList ( //
54
+ List <Parameter <?>> parameters = Arrays .< Parameter <?>> asList ( //
51
55
mock (ParameterExpression .class ), //
52
56
new ParameterImpl ("name" , null ), //
53
57
new ParameterImpl (null , 1 ) //
@@ -87,7 +91,7 @@ public void lenientErrorHandlingThrowsNoExceptionForAllVariationsOfParameters()
87
91
88
92
Query query = mockExceptionThrowingQueryWithNamedParameters ();
89
93
90
- for (Parameter parameter : parameters ) {
94
+ for (Parameter <?> parameter : parameters ) {
91
95
for (TemporalType temporalType : temporalTypes ) {
92
96
93
97
NamedOrIndexedQueryParameterSetter setter = new NamedOrIndexedQueryParameterSetter ( //
@@ -111,9 +115,8 @@ public void lenientErrorHandlingThrowsNoExceptionForAllVariationsOfParameters()
111
115
112
116
/**
113
117
* 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.
117
120
*/
118
121
@ Test // DATAJPA-1233
119
122
public void lenientSetsParameterWhenSuccessIsUnsure () {
@@ -130,20 +133,20 @@ public void lenientSetsParameterWhenSuccessIsUnsure() {
130
133
131
134
setter .setParameter (query , methodArguments , LENIENT );
132
135
133
- if (temporalType == null )
136
+ if (temporalType == null ) {
134
137
verify (query ).setParameter (eq (11 ), any (Date .class ));
135
- else
138
+ } else {
136
139
verify (query ).setParameter (eq (11 ), any (Date .class ), eq (temporalType ));
140
+ }
137
141
}
138
142
139
143
softly .assertAll ();
140
144
141
145
}
142
146
143
147
/**
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.
147
150
*/
148
151
@ Test // DATAJPA-1233
149
152
public void parameterNotSetWhenSuccessImpossible () {
@@ -160,57 +163,50 @@ public void parameterNotSetWhenSuccessImpossible() {
160
163
161
164
setter .setParameter (query , methodArguments , LENIENT );
162
165
163
- if (temporalType == null )
166
+ if (temporalType == null ) {
164
167
verify (query , never ()).setParameter (anyInt (), any (Date .class ));
165
- else
168
+ } else {
166
169
verify (query , never ()).setParameter (anyInt (), any (Date .class ), eq (temporalType ));
170
+ }
167
171
}
168
172
169
173
softly .assertAll ();
170
174
171
175
}
172
176
173
177
@ SuppressWarnings ("unchecked" )
174
- public Query mockExceptionThrowingQueryWithNamedParameters () {
178
+ private static Query mockExceptionThrowingQueryWithNamedParameters () {
179
+
175
180
Query query = mock (Query .class );
176
181
177
182
// 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
+
191
198
return query ;
192
199
}
193
200
194
- @ RequiredArgsConstructor
201
+ @ Value
195
202
private static class ParameterImpl implements Parameter <Object > {
196
203
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 ;
209
206
210
207
@ Override
211
208
public Class <Object > getParameterType () {
212
209
return Object .class ;
213
210
}
214
211
}
215
-
216
212
}
0 commit comments