Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 15f46e4

Browse files
Auzelschauder
authored andcommittedFeb 23, 2022
Fixes flaky tests in PartTreeR2dbcQueryUnitTests.java
The order of selected columns depended on the nondeterministic behavior of getDeclaredFields. Original pull request spring-projects/spring-data-r2dbc/pull/691
1 parent 56ece61 commit 15f46e4

File tree

1 file changed

+122
-66
lines changed

1 file changed

+122
-66
lines changed
 

‎spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/repository/query/PartTreeR2dbcQueryUnitTests.java

Lines changed: 122 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.lang.reflect.Method;
2828
import java.util.Collection;
2929
import java.util.Collections;
30+
import java.util.List;
31+
import java.util.ArrayList;
3032
import java.util.Date;
3133

3234
import org.junit.jupiter.api.BeforeEach;
@@ -66,6 +68,7 @@
6668
* @author Mingyuan Wu
6769
* @author Myeonghyeon Lee
6870
* @author Diego Krupitza
71+
* @author Philmon Roberts
6972
*/
7073
@ExtendWith(MockitoExtension.class)
7174
@MockitoSettings(strictness = Strictness.LENIENT)
@@ -110,8 +113,8 @@ void createsQueryToFindAllEntitiesByStringAttribute() throws Exception {
110113
dataAccessStrategy);
111114
PreparedOperation<?> preparedOperation = createQuery(queryMethod, r2dbcQuery, "John");
112115

113-
assertThat(preparedOperation.get())
114-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name = $1");
116+
assertThat(formatOperation(preparedOperation))
117+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name = $1"));
115118
}
116119

117120
@Test // GH-282
@@ -122,8 +125,8 @@ void createsQueryWithIsNullCondition() throws Exception {
122125
dataAccessStrategy);
123126
PreparedOperation<?> preparedOperation = createQuery(queryMethod, r2dbcQuery, new Object[] { null });
124127

125-
assertThat(preparedOperation.get())
126-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name IS NULL");
128+
assertThat(formatOperation(preparedOperation))
129+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name IS NULL"));
127130
}
128131

129132
@Test // GH-282
@@ -147,8 +150,8 @@ void createsQueryToFindAllEntitiesByTwoStringAttributes() throws Exception {
147150
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery,
148151
getAccessor(queryMethod, new Object[] { "Doe", "John" }));
149152

150-
assertThat(preparedOperation.get()).isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE
151-
+ ".last_name = $1 AND (" + TABLE + ".first_name = $2)");
153+
assertThat(formatOperation(preparedOperation)).isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE
154+
+ ".last_name = $1 AND (" + TABLE + ".first_name = $2)"));
152155
}
153156

154157
@Test // GH-282
@@ -160,8 +163,8 @@ void createsQueryToFindAllEntitiesByOneOfTwoStringAttributes() throws Exception
160163
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery,
161164
getAccessor(queryMethod, new Object[] { "Doe", "John" }));
162165

163-
assertThat(preparedOperation.get()).isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE
164-
+ ".last_name = $1 OR (" + TABLE + ".first_name = $2)");
166+
assertThat(formatOperation(preparedOperation)).isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE
167+
+ ".last_name = $1 OR (" + TABLE + ".first_name = $2)"));
165168
}
166169

167170
@Test // GH-282, gh-349
@@ -175,8 +178,8 @@ void createsQueryToFindAllEntitiesByDateAttributeBetween() throws Exception {
175178
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { from, to });
176179
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
177180

178-
assertThat(preparedOperation.get())
179-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".date_of_birth BETWEEN $1 AND $2");
181+
assertThat(formatOperation(preparedOperation))
182+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".date_of_birth BETWEEN $1 AND $2"));
180183

181184
BindTarget bindTarget = mock(BindTarget.class);
182185
preparedOperation.bindTo(bindTarget);
@@ -194,8 +197,8 @@ void createsQueryToFindAllEntitiesByIntegerAttributeLessThan() throws Exception
194197
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { 30 });
195198
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
196199

197-
assertThat(preparedOperation.get())
198-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age < $1");
200+
assertThat(formatOperation(preparedOperation))
201+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age < $1"));
199202
}
200203

201204
@Test // GH-282
@@ -207,8 +210,8 @@ void createsQueryToFindAllEntitiesByIntegerAttributeLessThanEqual() throws Excep
207210
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { 30 });
208211
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
209212

210-
assertThat(preparedOperation.get())
211-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age <= $1");
213+
assertThat(formatOperation(preparedOperation))
214+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age <= $1"));
212215
}
213216

214217
@Test // GH-282
@@ -220,8 +223,8 @@ void createsQueryToFindAllEntitiesByIntegerAttributeGreaterThan() throws Excepti
220223
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { 30 });
221224
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
222225

223-
assertThat(preparedOperation.get())
224-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age > $1");
226+
assertThat(formatOperation(preparedOperation))
227+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age > $1"));
225228
}
226229

227230
@Test // GH-282
@@ -233,8 +236,8 @@ void createsQueryToFindAllEntitiesByIntegerAttributeGreaterThanEqual() throws Ex
233236
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { 30 });
234237
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
235238

236-
assertThat(preparedOperation.get())
237-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age >= $1");
239+
assertThat(formatOperation(preparedOperation))
240+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age >= $1"));
238241
}
239242

240243
@Test // GH-282
@@ -246,8 +249,8 @@ void createsQueryToFindAllEntitiesByDateAttributeAfter() throws Exception {
246249
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { new Date() });
247250
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
248251

249-
assertThat(preparedOperation.get())
250-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".date_of_birth > $1");
252+
assertThat(formatOperation(preparedOperation))
253+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".date_of_birth > $1"));
251254
}
252255

253256
@Test // GH-282
@@ -258,8 +261,8 @@ void createsQueryToFindAllEntitiesByDateAttributeBefore() throws Exception {
258261
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { new Date() });
259262
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
260263

261-
assertThat(preparedOperation.get())
262-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".date_of_birth < $1");
264+
assertThat(formatOperation(preparedOperation))
265+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".date_of_birth < $1"));
263266
}
264267

265268
@Test // GH-282
@@ -271,8 +274,8 @@ void createsQueryToFindAllEntitiesByIntegerAttributeIsNull() throws Exception {
271274
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[0]);
272275
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
273276

274-
assertThat(preparedOperation.get())
275-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age IS NULL");
277+
assertThat(formatOperation(preparedOperation))
278+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age IS NULL"));
276279
}
277280

278281
@Test // GH-282
@@ -284,8 +287,8 @@ void createsQueryToFindAllEntitiesByIntegerAttributeIsNotNull() throws Exception
284287
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[0]);
285288
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
286289

287-
assertThat(preparedOperation.get())
288-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age IS NOT NULL");
290+
assertThat(formatOperation(preparedOperation))
291+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age IS NOT NULL"));
289292
}
290293

291294
@Test // GH-282
@@ -297,8 +300,8 @@ void createsQueryToFindAllEntitiesByStringAttributeLike() throws Exception {
297300
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { "%John%" });
298301
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
299302

300-
assertThat(preparedOperation.get())
301-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name LIKE $1");
303+
assertThat(formatOperation(preparedOperation))
304+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name LIKE $1"));
302305
}
303306

304307
@Test // GH-282
@@ -310,8 +313,8 @@ void createsQueryToFindAllEntitiesByStringAttributeNotLike() throws Exception {
310313
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { "%John%" });
311314
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
312315

313-
assertThat(preparedOperation.get())
314-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name NOT LIKE $1");
316+
assertThat(formatOperation(preparedOperation))
317+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name NOT LIKE $1"));
315318
}
316319

317320
@Test // GH-282
@@ -323,8 +326,8 @@ void createsQueryToFindAllEntitiesByStringAttributeStartingWith() throws Excepti
323326
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { "Jo" });
324327
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
325328

326-
assertThat(preparedOperation.get())
327-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name LIKE $1");
329+
assertThat(formatOperation(preparedOperation))
330+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name LIKE $1"));
328331
}
329332

330333
@Test // GH-282
@@ -350,8 +353,8 @@ void createsQueryToFindAllEntitiesByStringAttributeEndingWith() throws Exception
350353
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { "hn" });
351354
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
352355

353-
assertThat(preparedOperation.get())
354-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name LIKE $1");
356+
assertThat(formatOperation(preparedOperation))
357+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name LIKE $1"));
355358
}
356359

357360
@Test // GH-282
@@ -377,8 +380,8 @@ void createsQueryToFindAllEntitiesByStringAttributeContaining() throws Exception
377380
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { "oh" });
378381
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
379382

380-
assertThat(preparedOperation.get())
381-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name LIKE $1");
383+
assertThat(formatOperation(preparedOperation))
384+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name LIKE $1"));
382385
}
383386

384387
@Test // GH-282
@@ -404,8 +407,8 @@ void createsQueryToFindAllEntitiesByStringAttributeNotContaining() throws Except
404407
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { "oh" });
405408
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
406409

407-
assertThat(preparedOperation.get())
408-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name NOT LIKE $1");
410+
assertThat(formatOperation(preparedOperation))
411+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name NOT LIKE $1"));
409412
}
410413

411414
@Test // GH-282
@@ -431,9 +434,9 @@ void createsQueryToFindAllEntitiesByIntegerAttributeWithDescendingOrderingByStri
431434
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { "oh" });
432435
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
433436

434-
assertThat(preparedOperation.get())
437+
assertThat(formatOperation(preparedOperation))
435438
.isEqualTo(
436-
"SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age = $1 ORDER BY users.last_name DESC");
439+
formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age = $1 ORDER BY users.last_name DESC"));
437440
}
438441

439442
@Test // GH-282
@@ -444,9 +447,9 @@ void createsQueryToFindAllEntitiesByIntegerAttributeWithAscendingOrderingByStrin
444447
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { "oh" });
445448
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
446449

447-
assertThat(preparedOperation.get())
450+
assertThat(formatOperation(preparedOperation))
448451
.isEqualTo(
449-
"SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age = $1 ORDER BY users.last_name ASC");
452+
formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age = $1 ORDER BY users.last_name ASC"));
450453
}
451454

452455
@Test // GH-282
@@ -457,8 +460,8 @@ void createsQueryToFindAllEntitiesByStringAttributeNot() throws Exception {
457460
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { "Doe" });
458461
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
459462

460-
assertThat(preparedOperation.get())
461-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".last_name != $1");
463+
assertThat(formatOperation(preparedOperation))
464+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".last_name != $1"));
462465
}
463466

464467
@Test // GH-282
@@ -471,8 +474,8 @@ void createsQueryToFindAllEntitiesByIntegerAttributeIn() throws Exception {
471474
new Object[] { Collections.singleton(25) });
472475
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
473476

474-
assertThat(preparedOperation.get())
475-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age IN ($1)");
477+
assertThat(formatOperation(preparedOperation))
478+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age IN ($1)"));
476479
}
477480

478481
@Test // GH-282
@@ -484,8 +487,8 @@ void createsQueryToFindAllEntitiesByIntegerAttributeNotIn() throws Exception {
484487
new Object[] { Collections.singleton(25) });
485488
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
486489

487-
assertThat(preparedOperation.get())
488-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age NOT IN ($1)");
490+
assertThat(formatOperation(preparedOperation))
491+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".age NOT IN ($1)"));
489492
}
490493

491494
@Test // GH-282, gh-698
@@ -497,8 +500,8 @@ void createsQueryToFindAllEntitiesByBooleanAttributeTrue() throws Exception {
497500
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[0]);
498501
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
499502

500-
assertThat(preparedOperation.get())
501-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".active = $1");
503+
assertThat(formatOperation(preparedOperation))
504+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".active = $1"));
502505
}
503506

504507
@Test // GH-282, gh-698
@@ -510,8 +513,8 @@ void createsQueryToFindAllEntitiesByBooleanAttributeFalse() throws Exception {
510513
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[0]);
511514
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
512515

513-
assertThat(preparedOperation.get())
514-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".active = $1");
516+
assertThat(formatOperation(preparedOperation))
517+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".active = $1"));
515518
}
516519

517520
@Test // GH-282
@@ -523,8 +526,8 @@ void createsQueryToFindAllEntitiesByStringAttributeIgnoringCase() throws Excepti
523526
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { "John" });
524527
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
525528

526-
assertThat(preparedOperation.get())
527-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE UPPER(" + TABLE + ".first_name) = UPPER($1)");
529+
assertThat(formatOperation(preparedOperation))
530+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE UPPER(" + TABLE + ".first_name) = UPPER($1)"));
528531
}
529532

530533
@Test // GH-282
@@ -587,8 +590,8 @@ void createsQueryWithLimitToFindEntitiesByStringAttribute() throws Exception {
587590
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { "John" });
588591
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
589592

590-
assertThat(preparedOperation.get())
591-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name = $1 LIMIT 3");
593+
assertThat(formatOperation(preparedOperation))
594+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name = $1 LIMIT 3"));
592595
}
593596

594597
@Test // GH-282
@@ -600,8 +603,8 @@ void createsQueryToFindFirstEntityByStringAttribute() throws Exception {
600603
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] { "John" });
601604
PreparedOperation<?> preparedOperation = createQuery(r2dbcQuery, accessor);
602605

603-
assertThat(preparedOperation.get())
604-
.isEqualTo("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name = $1 LIMIT 1");
606+
assertThat(formatOperation(preparedOperation))
607+
.isEqualTo(formatQuery("SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name = $1 LIMIT 1"));
605608
}
606609

607610
@Test // GH-341
@@ -624,8 +627,8 @@ void createsQueryToFindAllEntitiesByStringAttributeWithDistinct() throws Excepti
624627
dataAccessStrategy);
625628
PreparedOperation<?> preparedOperation = createQuery(queryMethod, r2dbcQuery, "John");
626629

627-
assertThat(preparedOperation.get()).isEqualTo("SELECT " + DISTINCT + " " + TABLE + ".first_name, " + TABLE
628-
+ ".foo FROM " + TABLE + " WHERE " + TABLE + ".first_name = $1");
630+
assertThat(formatOperation(preparedOperation)).isEqualTo(formatQuery("SELECT " + DISTINCT + " " + TABLE + ".first_name, " + TABLE
631+
+ ".foo FROM " + TABLE + " WHERE " + TABLE + ".first_name = $1"));
629632
}
630633

631634
@Test // GH-475
@@ -636,9 +639,9 @@ void createsQueryToFindByOpenProjection() throws Exception {
636639
dataAccessStrategy);
637640
PreparedOperation<?> preparedOperation = createQuery(queryMethod, r2dbcQuery);
638641

639-
assertThat(preparedOperation.get()).isEqualTo(
640-
"SELECT users.id, users.first_name, users.last_name, users.date_of_birth, users.age, users.active FROM "
641-
+ TABLE);
642+
assertThat(formatOperation(preparedOperation)).isEqualTo(
643+
formatQuery("SELECT users.id, users.first_name, users.last_name, users.date_of_birth, users.age, users.active FROM "
644+
+ TABLE));
642645
}
643646

644647
@Test // GH-475
@@ -649,9 +652,9 @@ void createsDtoProjectionQuery() throws Exception {
649652
dataAccessStrategy);
650653
PreparedOperation<?> preparedOperation = createQuery(queryMethod, r2dbcQuery);
651654

652-
assertThat(preparedOperation.get()).isEqualTo(
653-
"SELECT users.id, users.first_name, users.last_name, users.date_of_birth, users.age, users.active FROM "
654-
+ TABLE);
655+
assertThat(formatOperation(preparedOperation)).isEqualTo(
656+
formatQuery("SELECT users.id, users.first_name, users.last_name, users.date_of_birth, users.age, users.active FROM "
657+
+ TABLE));
655658
}
656659

657660
@Test // GH-363
@@ -716,6 +719,59 @@ private RelationalParametersParameterAccessor getAccessor(R2dbcQueryMethod query
716719
return new RelationalParametersParameterAccessor(queryMethod, values);
717720
}
718721

722+
private static String formatOperation(PreparedOperation<?> preparedOperation){
723+
return formatQuery(preparedOperation.get());
724+
}
725+
726+
private static String formatQuery(String query){
727+
String firstKeyword = "SELECT";
728+
String lastKeyword = "FROM";
729+
730+
int indexOfFirstKeyWord = query.toUpperCase().indexOf(firstKeyword);
731+
int indexOfLastKeyWord = query.toUpperCase().indexOf(lastKeyword);
732+
733+
if(indexOfFirstKeyWord!=0 || indexOfFirstKeyWord>=indexOfLastKeyWord){
734+
return query;
735+
}
736+
737+
String fields = query.substring(firstKeyword.length(), indexOfLastKeyWord);
738+
String sortedFields = sortFields(fields);
739+
740+
StringBuilder formattedQuery = new StringBuilder();
741+
formattedQuery.append(firstKeyword);
742+
formattedQuery.append(" ");
743+
formattedQuery.append(sortedFields);
744+
formattedQuery.append(" ");
745+
formattedQuery.append(query.substring(indexOfLastKeyWord, query.length()));
746+
747+
return formattedQuery.toString();
748+
}
749+
750+
private static String sortFields(String fields){
751+
List<String> sortedFieldsList = new ArrayList<>();
752+
StringBuilder fieldBuilder = new StringBuilder();
753+
StringBuilder sortedFields = new StringBuilder();
754+
755+
for(int i=0;i<fields.length();i++){
756+
if(fields.charAt(i)==','){
757+
sortedFieldsList.add(fieldBuilder.toString().trim());
758+
fieldBuilder = new StringBuilder();
759+
}else{
760+
fieldBuilder.append(fields.charAt(i));
761+
}
762+
}
763+
sortedFieldsList.add(fieldBuilder.toString().trim());
764+
Collections.sort(sortedFieldsList);
765+
766+
for(String sortedField: sortedFieldsList){
767+
if(sortedFieldsList.get(0)!=sortedField){
768+
sortedFields.append(", ");
769+
}
770+
sortedFields.append(sortedField);
771+
}
772+
return sortedFields.toString();
773+
}
774+
719775
@SuppressWarnings("ALL")
720776
interface UserRepository extends Repository<User, Long> {
721777

0 commit comments

Comments
 (0)
Please sign in to comment.