|
17 | 17 |
|
18 | 18 | import static org.assertj.core.api.Assertions.*;
|
19 | 19 |
|
| 20 | +import java.util.regex.Matcher; |
| 21 | +import java.util.regex.Pattern; |
20 | 22 | import java.util.stream.Stream;
|
21 | 23 |
|
22 | 24 | import org.assertj.core.api.SoftAssertions;
|
23 | 25 | import org.junit.jupiter.api.Test;
|
24 | 26 | import org.junit.jupiter.params.ParameterizedTest;
|
25 | 27 | import org.junit.jupiter.params.provider.Arguments;
|
26 | 28 | import org.junit.jupiter.params.provider.MethodSource;
|
| 29 | +import org.junit.jupiter.params.provider.ValueSource; |
27 | 30 | import org.springframework.dao.InvalidDataAccessApiUsageException;
|
28 | 31 | import org.springframework.data.domain.PageRequest;
|
29 | 32 | import org.springframework.data.domain.Sort;
|
30 | 33 | import org.springframework.data.jpa.domain.JpaSort;
|
31 | 34 | import org.springframework.lang.Nullable;
|
| 35 | +import org.springframework.util.StringUtils; |
32 | 36 |
|
33 | 37 | /**
|
34 | 38 | * Verify that HQL queries are properly transformed through the {@link JpaQueryEnhancer} and the {@link HqlQueryParser}.
|
@@ -1060,6 +1064,40 @@ void createsCountQueryUsingAliasCorrectly() {
|
1060 | 1064 | assertCountQuery("select distinct a, count(b) as c from Employee GROUP BY n","select count(distinct a, count(b)) from Employee AS __ GROUP BY n");
|
1061 | 1065 | }
|
1062 | 1066 |
|
| 1067 | + @Test // GH-3427 |
| 1068 | + void sortShouldBeAppendedWithSpacingInCaseOfSetOperator() { |
| 1069 | + |
| 1070 | + String source = "SELECT tb FROM Test tb WHERE (tb.type='A') UNION SELECT tb FROM Test tb WHERE (tb.type='B')"; |
| 1071 | + String target = createQueryFor(source, Sort.by("Type").ascending()); |
| 1072 | + |
| 1073 | + assertThat(target).isEqualTo("SELECT tb FROM Test tb WHERE (tb.type = 'A') UNION SELECT tb FROM Test tb WHERE (tb.type = 'B') order by tb.Type asc"); |
| 1074 | + } |
| 1075 | + |
| 1076 | + @ParameterizedTest // GH-3427 |
| 1077 | + @ValueSource(strings = {"", "res"}) |
| 1078 | + void sortShouldBeAppendedToSubSelectWithSetOperatorInSubselect(String alias) { |
| 1079 | + |
| 1080 | + String prefix = StringUtils.hasText(alias) ? (alias + ".") : ""; |
| 1081 | + String source = "SELECT %sname FROM (SELECT c.name as name FROM Category c UNION SELECT t.name as name FROM Tag t)".formatted(prefix); |
| 1082 | + if(StringUtils.hasText(alias)) { |
| 1083 | + source = source + " %s".formatted(alias); |
| 1084 | + } |
| 1085 | + |
| 1086 | + String target = createQueryFor(source, Sort.by("name").ascending()); |
| 1087 | + |
| 1088 | + assertThat(target).contains(" UNION SELECT ").doesNotContainPattern(Pattern.compile(".*\\SUNION")); |
| 1089 | + assertThat(target).endsWith("order by %sname asc".formatted(prefix)).satisfies(it -> { |
| 1090 | + Pattern pattern = Pattern.compile("order by %sname".formatted(prefix)); |
| 1091 | + Matcher matcher = pattern.matcher(target); |
| 1092 | + int count = 0; |
| 1093 | + while(matcher.find()) { |
| 1094 | + count++; |
| 1095 | + } |
| 1096 | + assertThat(count).describedAs("Found order by clause more than once in: \n%s", it).isOne(); |
| 1097 | + }); |
| 1098 | + |
| 1099 | + } |
| 1100 | + |
1063 | 1101 | private void assertCountQuery(String originalQuery, String countQuery) {
|
1064 | 1102 | assertThat(createCountQueryFor(originalQuery)).isEqualTo(countQuery);
|
1065 | 1103 | }
|
|
0 commit comments