25
25
import org .junit .Test ;
26
26
import org .springframework .dao .InvalidDataAccessApiUsageException ;
27
27
import org .springframework .data .domain .Sort ;
28
+ import org .springframework .data .domain .Sort .Order ;
28
29
import org .springframework .data .jpa .domain .JpaSort ;
29
30
30
31
/**
@@ -145,22 +146,22 @@ public void detectsJoinAliasesCorrectly() {
145
146
public void doesNotPrefixOrderReferenceIfOuterJoinAliasDetected () {
146
147
147
148
String query = "select p from Person p left join p.address address" ;
148
- assertThat (applySorting (query , new Sort ("address.city" )), endsWith ("order by address.city asc" ));
149
- assertThat (applySorting (query , new Sort ("address.city" , "lastname" ), "p" ),
149
+ assertThat (applySorting (query , Sort . by ("address.city" )), endsWith ("order by address.city asc" ));
150
+ assertThat (applySorting (query , Sort . by ("address.city" , "lastname" ), "p" ),
150
151
endsWith ("order by address.city asc, p.lastname asc" ));
151
152
}
152
153
153
154
@ Test // DATAJPA-252
154
155
public void extendsExistingOrderByClausesCorrectly () {
155
156
156
157
String query = "select p from Person p order by p.lastname asc" ;
157
- assertThat (applySorting (query , new Sort ("firstname" ), "p" ), endsWith ("order by p.lastname asc, p.firstname asc" ));
158
+ assertThat (applySorting (query , Sort . by ("firstname" ), "p" ), endsWith ("order by p.lastname asc, p.firstname asc" ));
158
159
}
159
160
160
161
@ Test // DATAJPA-296
161
162
public void appliesIgnoreCaseOrderingCorrectly () {
162
163
163
- Sort sort = new Sort ( new Sort . Order ("firstname" ).ignoreCase ());
164
+ Sort sort = Sort . by ( Order . by ("firstname" ).ignoreCase ());
164
165
165
166
String query = "select p from Person p" ;
166
167
assertThat (applySorting (query , sort , "p" ), endsWith ("order by lower(p.firstname) asc" ));
@@ -169,7 +170,7 @@ public void appliesIgnoreCaseOrderingCorrectly() {
169
170
@ Test // DATAJPA-296
170
171
public void appendsIgnoreCaseOrderingCorrectly () {
171
172
172
- Sort sort = new Sort ( new Sort . Order ("firstname" ).ignoreCase ());
173
+ Sort sort = Sort . by ( Order . by ("firstname" ).ignoreCase ());
173
174
174
175
String query = "select p from Person p order by p.lastname asc" ;
175
176
assertThat (applySorting (query , sort , "p" ), endsWith ("order by p.lastname asc, lower(p.firstname) asc" ));
@@ -192,7 +193,7 @@ public void projectsCOuntQueriesForQueriesWithSubselects() {
192
193
@ Test (expected = InvalidDataAccessApiUsageException .class ) // DATAJPA-148
193
194
public void doesNotPrefixSortsIfFunction () {
194
195
195
- Sort sort = new Sort ("sum(foo)" );
196
+ Sort sort = Sort . by ("sum(foo)" );
196
197
assertThat (applySorting ("select p from Person p" , sort , "p" ), endsWith ("order by sum(foo) asc" ));
197
198
}
198
199
@@ -206,7 +207,7 @@ public void removesOrderByInGeneratedCountQueryFromOriginalQueryIfPresent() {
206
207
@ Test // DATAJPA-375
207
208
public void findsExistingOrderByIndependentOfCase () {
208
209
209
- Sort sort = new Sort ("lastname" );
210
+ Sort sort = Sort . by ("lastname" );
210
211
String query = applySorting ("select p from Person p ORDER BY p.firstname" , sort , "p" );
211
212
assertThat (query , endsWith ("ORDER BY p.firstname, p.lastname asc" ));
212
213
}
@@ -231,7 +232,7 @@ public void createCountQueryFromTheGivenCountProjection() {
231
232
public void detectsAliassesInPlainJoins () {
232
233
233
234
String query = "select p from Customer c join c.productOrder p where p.delayed = true" ;
234
- Sort sort = new Sort ("p.lineItems" );
235
+ Sort sort = Sort . by ("p.lineItems" );
235
236
236
237
assertThat (applySorting (query , sort , "c" ), endsWith ("order by p.lineItems asc" ));
237
238
}
@@ -250,7 +251,7 @@ public void detectsAliasInQueryContainingLineBreaks() {
250
251
public void doesPrefixPropertyWith () {
251
252
252
253
String query = "from Cat c join Dog d" ;
253
- Sort sort = new Sort ("dPropertyStartingWithJoinAlias" );
254
+ Sort sort = Sort . by ("dPropertyStartingWithJoinAlias" );
254
255
255
256
assertThat (applySorting (query , sort , "c" ), endsWith ("order by c.dPropertyStartingWithJoinAlias asc" ));
256
257
}
@@ -277,14 +278,13 @@ public void detectsConstructorExpressionWithLineBreaks() {
277
278
278
279
@ Test // DATAJPA-960
279
280
public void doesNotQualifySortIfNoAliasDetected () {
280
- assertThat (applySorting ("from mytable where ?1 is null" , new Sort ("firstname" )),
281
- endsWith ("order by firstname asc" ));
281
+ assertThat (applySorting ("from mytable where ?1 is null" , Sort .by ("firstname" )), endsWith ("order by firstname asc" ));
282
282
}
283
283
284
284
@ Test (expected = InvalidDataAccessApiUsageException .class ) // DATAJPA-965, DATAJPA-970
285
285
public void doesNotAllowWhitespaceInSort () {
286
286
287
- Sort sort = new Sort ("case when foo then bar" );
287
+ Sort sort = Sort . by ("case when foo then bar" );
288
288
applySorting ("select p from Person p" , sort , "p" );
289
289
}
290
290
@@ -299,7 +299,7 @@ public void doesNotPrefixUnsageJpaSortFunctionCalls() {
299
299
public void doesNotPrefixMultipleAliasedFunctionCalls () {
300
300
301
301
String query = "SELECT AVG(m.price) AS avgPrice, SUM(m.stocks) AS sumStocks FROM Magazine m" ;
302
- Sort sort = new Sort ("avgPrice" , "sumStocks" );
302
+ Sort sort = Sort . by ("avgPrice" , "sumStocks" );
303
303
304
304
assertThat (applySorting (query , sort , "m" ), endsWith ("order by avgPrice asc, sumStocks asc" ));
305
305
}
@@ -308,7 +308,7 @@ public void doesNotPrefixMultipleAliasedFunctionCalls() {
308
308
public void doesNotPrefixSingleAliasedFunctionCalls () {
309
309
310
310
String query = "SELECT AVG(m.price) AS avgPrice FROM Magazine m" ;
311
- Sort sort = new Sort ("avgPrice" );
311
+ Sort sort = Sort . by ("avgPrice" );
312
312
313
313
assertThat (applySorting (query , sort , "m" ), endsWith ("order by avgPrice asc" ));
314
314
}
@@ -317,7 +317,7 @@ public void doesNotPrefixSingleAliasedFunctionCalls() {
317
317
public void prefixesSingleNonAliasedFunctionCallRelatedSortProperty () {
318
318
319
319
String query = "SELECT AVG(m.price) AS avgPrice FROM Magazine m" ;
320
- Sort sort = new Sort ("someOtherProperty" );
320
+ Sort sort = Sort . by ("someOtherProperty" );
321
321
322
322
assertThat (applySorting (query , sort , "m" ), endsWith ("order by m.someOtherProperty asc" ));
323
323
}
@@ -326,7 +326,7 @@ public void prefixesSingleNonAliasedFunctionCallRelatedSortProperty() {
326
326
public void prefixesNonAliasedFunctionCallRelatedSortPropertyWhenSelectClauseContainesAliasedFunctionForDifferentProperty () {
327
327
328
328
String query = "SELECT m.name, AVG(m.price) AS avgPrice FROM Magazine m" ;
329
- Sort sort = new Sort ("name" , "avgPrice" );
329
+ Sort sort = Sort . by ("name" , "avgPrice" );
330
330
331
331
assertThat (applySorting (query , sort , "m" ), endsWith ("order by m.name asc, avgPrice asc" ));
332
332
}
@@ -335,7 +335,7 @@ public void prefixesNonAliasedFunctionCallRelatedSortPropertyWhenSelectClauseCon
335
335
public void doesNotPrefixAliasedFunctionCallNameWithMultipleNumericParameters () {
336
336
337
337
String query = "SELECT SUBSTRING(m.name, 2, 5) AS trimmedName FROM Magazine m" ;
338
- Sort sort = new Sort ("trimmedName" );
338
+ Sort sort = Sort . by ("trimmedName" );
339
339
340
340
assertThat (applySorting (query , sort , "m" ), endsWith ("order by trimmedName asc" ));
341
341
}
@@ -344,7 +344,7 @@ public void doesNotPrefixAliasedFunctionCallNameWithMultipleNumericParameters()
344
344
public void doesNotPrefixAliasedFunctionCallNameWithMultipleStringParameters () {
345
345
346
346
String query = "SELECT CONCAT(m.name, 'foo') AS extendedName FROM Magazine m" ;
347
- Sort sort = new Sort ("extendedName" );
347
+ Sort sort = Sort . by ("extendedName" );
348
348
349
349
assertThat (applySorting (query , sort , "m" ), endsWith ("order by extendedName asc" ));
350
350
}
@@ -353,7 +353,7 @@ public void doesNotPrefixAliasedFunctionCallNameWithMultipleStringParameters() {
353
353
public void doesNotPrefixAliasedFunctionCallNameWithUnderscores () {
354
354
355
355
String query = "SELECT AVG(m.price) AS avg_price FROM Magazine m" ;
356
- Sort sort = new Sort ("avg_price" );
356
+ Sort sort = Sort . by ("avg_price" );
357
357
358
358
assertThat (applySorting (query , sort , "m" ), endsWith ("order by avg_price asc" ));
359
359
}
@@ -362,7 +362,7 @@ public void doesNotPrefixAliasedFunctionCallNameWithUnderscores() {
362
362
public void doesNotPrefixAliasedFunctionCallNameWithDots () {
363
363
364
364
String query = "SELECT AVG(m.price) AS m.avg FROM Magazine m" ;
365
- Sort sort = new Sort ("m.avg" );
365
+ Sort sort = Sort . by ("m.avg" );
366
366
367
367
assertThat (applySorting (query , sort , "m" ), endsWith ("order by m.avg asc" ));
368
368
}
@@ -371,7 +371,7 @@ public void doesNotPrefixAliasedFunctionCallNameWithDots() {
371
371
public void doesNotPrefixAliasedFunctionCallNameWhenQueryStringContainsMultipleWhiteSpaces () {
372
372
373
373
String query = "SELECT AVG( m.price ) AS avgPrice FROM Magazine m" ;
374
- Sort sort = new Sort ("avgPrice" );
374
+ Sort sort = Sort . by ("avgPrice" );
375
375
376
376
assertThat (applySorting (query , sort , "m" ), endsWith ("order by avgPrice asc" ));
377
377
}
0 commit comments