Skip to content

Commit 8b07198

Browse files
committed
Polishing.
Use textblocks where possible. See #2864. Original pull request: #2874.
1 parent 9c4273c commit 8b07198

File tree

2 files changed

+262
-172
lines changed

2 files changed

+262
-172
lines changed

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/HqlQueryTransformerTests.java

+155-109
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20-
import java.util.regex.Pattern;
2120
import java.util.stream.Stream;
2221

2322
import org.assertj.core.api.SoftAssertions;
@@ -34,17 +33,13 @@
3433
* Verify that HQL queries are properly transformed through the {@link JpaQueryEnhancer} and the {@link HqlQueryParser}.
3534
*
3635
* @author Greg Turnquist
37-
* @since 3.1
3836
*/
3937
class HqlQueryTransformerTests {
4038

4139
private static final String QUERY = "select u from User u";
42-
private static final String FQ_QUERY = "select u from org.acme.domain.User$Foo_Bar u";
4340
private static final String SIMPLE_QUERY = "from User u";
4441
private static final String COUNT_QUERY = "select count(u) from User u";
45-
4642
private static final String QUERY_WITH_AS = "select u from User as u where u.username = ?1";
47-
private static final Pattern MULTI_WHITESPACE = Pattern.compile("\\s+");
4843

4944
@Test
5045
void applyingSortShouldIntroduceOrderByCriteriaWhereNoneExists() {
@@ -211,17 +206,29 @@ void applySortingAccountsForNewlinesInSubselect() {
211206

212207
Sort sort = Sort.by(Sort.Order.desc("age"));
213208

214-
assertThat(newParser("select u\n" + //
215-
"from user u\n" + //
216-
"where exists (select u2\n" + //
217-
"from user u2\n" + //
218-
")\n" + //
219-
"").applySorting(sort)).isEqualToIgnoringWhitespace("select u\n" + //
220-
"from user u\n" + //
221-
"where exists (select u2\n" + //
222-
"from user u2\n" + //
223-
")\n" + //
224-
" order by u.age desc");
209+
//
210+
//
211+
//
212+
//
213+
//
214+
//
215+
//
216+
//
217+
//
218+
//
219+
assertThat(newParser("""
220+
select u
221+
from user u
222+
where exists (select u2
223+
from user u2
224+
)
225+
""").applySorting(sort)).isEqualToIgnoringWhitespace("""
226+
select u
227+
from user u
228+
where exists (select u2
229+
from user u2
230+
)
231+
order by u.age desc""");
225232
}
226233

227234
@Test // GH-2563
@@ -355,12 +362,14 @@ void detectsConstructorExpressionInDistinctQuery() {
355362
@Test // DATAJPA-938
356363
void detectsComplexConstructorExpression() {
357364

358-
assertThat(hasConstructorExpression("select new foo.bar.Foo(ip.id, ip.name, sum(lp.amount)) " //
359-
+ "from Bar lp join lp.investmentProduct ip " //
360-
+ "where (lp.toDate is null and lp.fromDate <= :now and lp.fromDate is not null) and lp.accountId = :accountId "
361-
//
362-
+ "group by ip.id, ip.name, lp.accountId " //
363-
+ "order by ip.name ASC")).isTrue();
365+
//
366+
assertThat(hasConstructorExpression(
367+
"""
368+
select new foo.bar.Foo(ip.id, ip.name, sum(lp.amount))
369+
from Bar lp join lp.investmentProduct ip
370+
where (lp.toDate is null and lp.fromDate <= :now and lp.fromDate is not null) and lp.accountId = :accountId group by ip.id, ip.name, lp.accountId
371+
order by ip.name ASC"""))
372+
.isTrue();
364373
}
365374

366375
@Test // DATAJPA-938
@@ -485,21 +494,39 @@ void detectsAliasWithGroupAndOrderBy() {
485494
@Test // DATAJPA-1500
486495
void createCountQuerySupportsWhitespaceCharacters() {
487496

488-
assertThat(createCountQueryFor("select user from User user\n" + //
489-
" where user.age = 18\n" + //
490-
" order by user.name\n ")).isEqualToIgnoringWhitespace("select count(user) from User user\n" + //
491-
" where user.age = 18\n ");
497+
//
498+
//
499+
//
500+
assertThat(createCountQueryFor("""
501+
select user from User user
502+
where user.age = 18
503+
order by user.name
504+
""")).isEqualToIgnoringWhitespace("""
505+
select count(user) from User user
506+
where user.age = 18
507+
""");
492508
}
493509

494510
@Test
495511
void createCountQuerySupportsLineBreaksInSelectClause() {
496512

497-
assertThat(createCountQueryFor("select user.age,\n" + //
498-
" user.name\n" + //
499-
" from User user\n" + //
500-
" where user.age = 18\n" + //
501-
" order\nby\nuser.name\n ")).isEqualToIgnoringWhitespace("select count(user) from User user\n" + //
502-
" where user.age = 18\n ");
513+
//
514+
//
515+
//
516+
//
517+
//
518+
assertThat(createCountQueryFor("""
519+
select user.age,
520+
user.name
521+
from User user
522+
where user.age = 18
523+
order
524+
by
525+
user.name
526+
""")).isEqualToIgnoringWhitespace("""
527+
select count(user) from User user
528+
where user.age = 18
529+
""");
503530
}
504531

505532
@Test // DATAJPA-1061
@@ -550,11 +577,24 @@ void appliesSortCorrectlyForSimpleField() {
550577
@Test
551578
void createCountQuerySupportsLineBreakRightAfterDistinct() {
552579

553-
assertThat(createCountQueryFor("select\ndistinct\nuser.age,\n" + //
554-
"user.name\n" + //
555-
"from\nUser\nuser")).isEqualTo(createCountQueryFor("select\ndistinct user.age,\n" + //
556-
"user.name\n" + //
557-
"from\nUser\nuser"));
580+
//
581+
//
582+
//
583+
//
584+
assertThat(createCountQueryFor("""
585+
select
586+
distinct
587+
user.age,
588+
user.name
589+
from
590+
User
591+
user""")).isEqualTo(createCountQueryFor("""
592+
select
593+
distinct user.age,
594+
user.name
595+
from
596+
User
597+
user"""));
558598
}
559599

560600
@Test
@@ -723,69 +763,74 @@ void orderByShouldWorkWithSubSelectStatements() {
723763

724764
Sort sort = Sort.by(Sort.Order.desc("age"));
725765

726-
assertThat(createQueryFor("SELECT\n" //
727-
+ " foo_bar\n" //
728-
+ "FROM\n" //
729-
+ " foo foo\n" //
730-
+ "INNER JOIN\n" //
731-
+ " foo_bar_dnrmv foo_bar ON\n" //
732-
+ " foo_bar.foo_id = foo.foo_id\n" //
733-
+ "INNER JOIN\n" //
734-
+ " (\n" //
735-
+ " SELECT\n" //
736-
+ " foo_bar_action\n" //
737-
+ " FROM\n" //
738-
+ " foo_bar_action\n" //
739-
+ " WHERE\n" //
740-
+ " foo_bar_action.deleted_ts IS NULL)\n" //
741-
+ " foo_bar_action ON\n" //
742-
+ " foo_bar.foo_bar_id = foo_bar_action.foo_bar_id\n" //
743-
+ " AND ranking = 1\n" //
744-
+ "INNER JOIN\n" //
745-
+ " bar bar ON\n" //
746-
+ " foo_bar.bar_id = bar.bar_id\n" //
747-
+ "INNER JOIN\n" //
748-
+ " bar_metadata bar_metadata ON\n" //
749-
+ " bar.bar_metadata_key = bar_metadata.bar_metadata_key\n" //
750-
+ "WHERE\n" //
751-
+ " foo.tenant_id =:tenantId", sort)).endsWith("order by foo.age desc");
752-
753-
assertThat(createQueryFor("select r " //
754-
+ "From DataRecord r " //
755-
+ "where " //
756-
+ " ( " //
757-
+ " r.adusrId = :userId " //
758-
+ " or EXISTS( select 1 FROM DataRecordDvsRight dr WHERE dr.adusrId = :userId AND dr.dataRecord = r ) " //
759-
+ ")", sort)).endsWith("order by r.age desc");
760-
761-
assertThat(createQueryFor("select distinct u " //
762-
+ "from FooBar u " //
763-
+ "where u.role = 'redacted' " //
764-
+ "and (" //
765-
+ " not exists (" //
766-
+ " from FooBarGroup group " //
767-
+ " where group in :excludedGroups " //
768-
+ " and group in elements(u.groups)" //
769-
+ " )" //
770-
+ ")", sort)).endsWith("order by u.age desc");
771-
772-
assertThat(createQueryFor("SELECT i " //
773-
+ " FROM Item i " //
774-
+ " WHERE i.id IN (" //
775-
+ " SELECT max(i2.id) FROM Item i2 " //
776-
+ " WHERE i2.field.id = :fieldId " //
777-
+ " GROUP BY i2.field.id, i2.version)", sort)).endsWith("order by i.age desc");
778-
779-
assertThat(createQueryFor("select \n" //
780-
+ " f.id,\n" //
781-
+ " (\n" //
782-
+ " select timestamp from bar\n" //
783-
+ " where date(bar.timestamp) > '2022-05-21'\n" //
784-
+ " and bar.foo_id = f.id \n" //
785-
+ " order by date(bar.timestamp) desc\n" //
786-
+ " limit 1\n" //
787-
+ ") as timestamp\n" //
788-
+ "from foo f", sort)).endsWith("order by f.age desc");
766+
assertThat(createQueryFor("""
767+
SELECT
768+
foo_bar
769+
FROM
770+
foo foo
771+
INNER JOIN
772+
foo_bar_dnrmv foo_bar ON
773+
foo_bar.foo_id = foo.foo_id
774+
INNER JOIN
775+
(
776+
SELECT
777+
foo_bar_action
778+
FROM
779+
foo_bar_action
780+
WHERE
781+
foo_bar_action.deleted_ts IS NULL)
782+
foo_bar_action ON
783+
foo_bar.foo_bar_id = foo_bar_action.foo_bar_id
784+
AND ranking = 1
785+
INNER JOIN
786+
bar bar ON
787+
foo_bar.bar_id = bar.bar_id
788+
INNER JOIN
789+
bar_metadata bar_metadata ON
790+
bar.bar_metadata_key = bar_metadata.bar_metadata_key
791+
WHERE
792+
foo.tenant_id =:tenantId""", sort)).endsWith("order by foo.age desc");
793+
794+
assertThat(createQueryFor("""
795+
select r
796+
From DataRecord r
797+
where
798+
(
799+
r.adusrId = :userId
800+
or EXISTS( select 1 FROM DataRecordDvsRight dr WHERE dr.adusrId = :userId AND dr.dataRecord = r )
801+
)""", sort)).endsWith("order by r.age desc");
802+
803+
assertThat(createQueryFor("""
804+
select distinct u
805+
from FooBar u
806+
where u.role = 'redacted'
807+
and (
808+
not exists (
809+
from FooBarGroup group
810+
where group in :excludedGroups
811+
and group in elements(u.groups)
812+
)
813+
)""", sort)).endsWith("order by u.age desc");
814+
815+
assertThat(createQueryFor("""
816+
SELECT i
817+
FROM Item i
818+
WHERE i.id IN (
819+
SELECT max(i2.id) FROM Item i2
820+
WHERE i2.field.id = :fieldId
821+
GROUP BY i2.field.id, i2.version)""", sort)).endsWith("order by i.age desc");
822+
823+
assertThat(createQueryFor("""
824+
select
825+
f.id,
826+
(
827+
select timestamp from bar
828+
where date(bar.timestamp) > '2022-05-21'
829+
and bar.foo_id = f.id
830+
order by date(bar.timestamp) desc
831+
limit 1
832+
) as timestamp
833+
from foo f""", sort)).endsWith("order by f.age desc");
789834
}
790835

791836
@Test // GH-2862
@@ -836,6 +881,16 @@ where exists (
836881
""", relationshipName, joinAlias, joinAlias));
837882
}
838883

884+
static Stream<Arguments> queriesWithReservedWordsAsIdentifiers() {
885+
886+
return Stream.of( //
887+
Arguments.of("right", "rt"), //
888+
Arguments.of("left", "lt"), //
889+
Arguments.of("outer", "ou"), //
890+
Arguments.of("full", "full"), //
891+
Arguments.of("inner", "inr"));
892+
}
893+
839894
@Test // GH-2508
840895
void detectAliasWithCastCorrectly() {
841896

@@ -847,15 +902,6 @@ void detectAliasWithCastCorrectly() {
847902
.isEqualTo("u");
848903
}
849904

850-
static Stream<Arguments> queriesWithReservedWordsAsIdentifiers() {
851-
852-
return Stream.of( //
853-
Arguments.of("right", "rt"), //
854-
Arguments.of("left", "lt"), //
855-
Arguments.of("outer", "ou"), //
856-
Arguments.of("full", "full"), //
857-
Arguments.of("inner", "inr"));
858-
}
859905

860906
private void assertCountQuery(String originalQuery, String countQuery) {
861907
assertThat(createCountQueryFor(originalQuery)).isEqualTo(countQuery);

0 commit comments

Comments
 (0)