|
39 | 39 | import org.mockito.junit.jupiter.MockitoSettings;
|
40 | 40 | import org.mockito.quality.Strictness;
|
41 | 41 |
|
| 42 | +import org.springframework.data.domain.Limit; |
42 | 43 | import org.springframework.data.domain.Pageable;
|
43 | 44 | import org.springframework.data.domain.Sort;
|
44 | 45 | import org.springframework.data.jpa.repository.Temporal;
|
|
53 | 54 | * @author Thomas Darimont
|
54 | 55 | * @author Jens Schauder
|
55 | 56 | * @author Mark Paluch
|
| 57 | + * @author Yanming Zhou |
56 | 58 | */
|
57 | 59 | @ExtendWith(MockitoExtension.class)
|
58 | 60 | @MockitoSettings(strictness = Strictness.LENIENT)
|
@@ -86,6 +88,8 @@ interface SampleRepository extends Repository<User, Long> {
|
86 | 88 |
|
87 | 89 | User validWithPageable(@Param("username") String username, Pageable pageable);
|
88 | 90 |
|
| 91 | + User validWithLimit(@Param("username") String username, Limit limit); |
| 92 | + |
89 | 93 | User validWithSort(@Param("username") String username, Sort sort);
|
90 | 94 |
|
91 | 95 | User validWithDefaultTemporalTypeParameter(@Temporal Date registerDate);
|
@@ -122,6 +126,40 @@ void bindWorksWithNullForPageable() throws Exception {
|
122 | 126 | verify(query).setParameter(eq(1), eq("foo"));
|
123 | 127 | }
|
124 | 128 |
|
| 129 | + @Test |
| 130 | + void bindAndPrepareWorksWithPageable() throws Exception { |
| 131 | + |
| 132 | + Method validWithPageable = SampleRepository.class.getMethod("validWithPageable", String.class, Pageable.class); |
| 133 | + |
| 134 | + Object[] values = { "foo", Pageable.ofSize(10).withPage(3) }; |
| 135 | + bindAndPrepare(validWithPageable, values); |
| 136 | + verify(query).setParameter(eq(1), eq("foo")); |
| 137 | + verify(query).setFirstResult(eq(30)); |
| 138 | + verify(query).setMaxResults(eq(10)); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + void bindWorksWithNullForLimit() throws Exception { |
| 143 | + |
| 144 | + Method validWithLimit = SampleRepository.class.getMethod("validWithLimit", String.class, Limit.class); |
| 145 | + |
| 146 | + Object[] values = { "foo", null }; |
| 147 | + bind(validWithLimit, values); |
| 148 | + verify(query).setParameter(eq(1), eq("foo")); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + void bindAndPrepareWorksWithLimit() throws Exception { |
| 153 | + |
| 154 | + Method validWithLimit = SampleRepository.class.getMethod("validWithLimit", String.class, Limit.class); |
| 155 | + |
| 156 | + Object[] values = { "foo", Limit.of(10) }; |
| 157 | + bindAndPrepare(validWithLimit, values); |
| 158 | + verify(query).setParameter(eq(1), eq("foo")); |
| 159 | + verify(query).setMaxResults(eq(10)); |
| 160 | + verify(query, never()).setFirstResult(anyInt()); |
| 161 | + } |
| 162 | + |
125 | 163 | @Test
|
126 | 164 | void usesIndexedParametersIfNoParamAnnotationPresent() {
|
127 | 165 |
|
@@ -236,6 +274,11 @@ private void bind(Method method, JpaParameters parameters, Object[] values) {
|
236 | 274 | getAccessor(method, values), QueryParameterSetter.ErrorHandling.STRICT);
|
237 | 275 | }
|
238 | 276 |
|
| 277 | + private void bindAndPrepare(Method method, Object[] values) { |
| 278 | + ParameterBinderFactory.createBinder(createParameters(method)).bindAndPrepare(query, |
| 279 | + new QueryParameterSetter.QueryMetadata(query), getAccessor(method, values)); |
| 280 | + } |
| 281 | + |
239 | 282 | private JpaParametersParameterAccessor getAccessor(Method method, Object... values) {
|
240 | 283 | return new JpaParametersParameterAccessor(createParameters(method), values);
|
241 | 284 | }
|
|
0 commit comments