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