Skip to content

Commit b299a73

Browse files
committed
Migrate these test cases to @ParameterizedTest
1 parent cdfe673 commit b299a73

File tree

2 files changed

+36
-190
lines changed

2 files changed

+36
-190
lines changed

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

+18-95
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818
import static org.assertj.core.api.Assertions.*;
1919

2020
import java.util.regex.Pattern;
21+
import java.util.stream.Stream;
2122

2223
import org.assertj.core.api.SoftAssertions;
2324
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.params.ParameterizedTest;
26+
import org.junit.jupiter.params.provider.Arguments;
27+
import org.junit.jupiter.params.provider.MethodSource;
2428
import org.springframework.dao.InvalidDataAccessApiUsageException;
2529
import org.springframework.data.domain.Sort;
2630
import org.springframework.data.jpa.domain.JpaSort;
@@ -808,10 +812,11 @@ void countQueryShouldWorkEvenWithoutExplicitAlias() {
808812
"select count(b) FROM BookError b WHERE portal = :portal");
809813
}
810814

811-
@Test // GH-2864
812-
void usingRightAsARelationshipNameShouldWork() {
815+
@ParameterizedTest
816+
@MethodSource("queriesWithReservedWordsAsIdentifiers") // GH-2864
817+
void usingReservedWordAsRelationshipNameShouldWork(String relationshipName, String joinAlias) {
813818

814-
HqlQueryParser.parseQuery("""
819+
HqlQueryParser.parseQuery(String.format("""
815820
select u
816821
from UserAccountEntity u
817822
join fetch u.lossInspectorLimitConfiguration lil
@@ -822,105 +827,23 @@ where exists (
822827
join iu.roles u2r
823828
join u2r.role r
824829
join r.rights r2r
825-
join r2r.right rt
830+
join r2r.%s %s
826831
where
827-
rt.code = :rightCode
832+
%s.code = :rightCode
828833
and iu = u
829834
)
830835
and ct.id = :teamId
831-
""");
836+
""", relationshipName, joinAlias, joinAlias));
832837
}
833838

834-
@Test // GH-2864
835-
void usingLeftAsARelationshipNameShouldWork() {
839+
static Stream<Arguments> queriesWithReservedWordsAsIdentifiers() {
836840

837-
HqlQueryParser.parseQuery("""
838-
select u
839-
from UserAccountEntity u
840-
join fetch u.lossInspectorLimitConfiguration lil
841-
join fetch u.companyTeam ct
842-
where exists (
843-
select iu
844-
from UserAccountEntity iu
845-
join iu.roles u2r
846-
join u2r.role r
847-
join r.rights r2r
848-
join r2r.left lt
849-
where
850-
lt.code = :rightCode
851-
and iu = u
852-
)
853-
and ct.id = :teamId
854-
""");
855-
}
856-
857-
@Test // GH-2864
858-
void usingOuterAsARelationshipNameShouldWork() {
859-
860-
HqlQueryParser.parseQuery("""
861-
select u
862-
from UserAccountEntity u
863-
join fetch u.lossInspectorLimitConfiguration lil
864-
join fetch u.companyTeam ct
865-
where exists (
866-
select iu
867-
from UserAccountEntity iu
868-
join iu.roles u2r
869-
join u2r.role r
870-
join r.rights r2r
871-
join r2r.outer ou
872-
where
873-
ou.code = :rightCode
874-
and iu = u
875-
)
876-
and ct.id = :teamId
877-
""");
878-
}
879-
880-
@Test // GH-2864
881-
void usingFullAsARelationshipNameShouldWork() {
882-
883-
HqlQueryParser.parseQuery("""
884-
select u
885-
from UserAccountEntity u
886-
join fetch u.lossInspectorLimitConfiguration lil
887-
join fetch u.companyTeam ct
888-
where exists (
889-
select iu
890-
from UserAccountEntity iu
891-
join iu.roles u2r
892-
join u2r.role r
893-
join r.rights r2r
894-
join r2r.full fu
895-
where
896-
fu.code = :rightCode
897-
and iu = u
898-
)
899-
and ct.id = :teamId
900-
""");
901-
}
902-
903-
@Test // GH-2864
904-
void usingInnerAsARelationshipNameShouldWork() {
905-
906-
HqlQueryParser.parseQuery("""
907-
select u
908-
from UserAccountEntity u
909-
join fetch u.lossInspectorLimitConfiguration lil
910-
join fetch u.companyTeam ct
911-
where exists (
912-
select iu
913-
from UserAccountEntity iu
914-
join iu.roles u2r
915-
join u2r.role r
916-
join r.rights r2r
917-
join r2r.inner in
918-
where
919-
in.code = :rightCode
920-
and iu = u
921-
)
922-
and ct.id = :teamId
923-
""");
841+
return Stream.of( //
842+
Arguments.of("right", "rt"), //
843+
Arguments.of("left", "lt"), //
844+
Arguments.of("outer", "ou"), //
845+
Arguments.of("full", "full"), //
846+
Arguments.of("inner", "inr"));
924847
}
925848

926849
private void assertCountQuery(String originalQuery, String countQuery) {

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

+18-95
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818
import static org.assertj.core.api.Assertions.*;
1919

2020
import java.util.regex.Pattern;
21+
import java.util.stream.Stream;
2122

2223
import org.assertj.core.api.SoftAssertions;
2324
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.params.ParameterizedTest;
26+
import org.junit.jupiter.params.provider.Arguments;
27+
import org.junit.jupiter.params.provider.MethodSource;
2428
import org.springframework.dao.InvalidDataAccessApiUsageException;
2529
import org.springframework.data.domain.Sort;
2630
import org.springframework.data.jpa.domain.JpaSort;
@@ -679,10 +683,11 @@ void queryParserPicksCorrectAliasAmidstMultipleAlises() {
679683
assertThat(alias("select u from User as u left join u.roles as r")).isEqualTo("u");
680684
}
681685

682-
@Test // GH-2864
683-
void usingRightAsARelationshipNameShouldWork() {
686+
@ParameterizedTest
687+
@MethodSource("queriesWithReservedWordsAsIdentifiers") // GH-2864
688+
void usingReservedWordAsRelationshipNameShouldWork(String relationshipName, String joinAlias) {
684689

685-
JpqlQueryParser.parseQuery("""
690+
JpqlQueryParser.parseQuery(String.format("""
686691
select u
687692
from UserAccountEntity u
688693
join u.lossInspectorLimitConfiguration lil
@@ -693,105 +698,23 @@ where exists (
693698
join iu.roles u2r
694699
join u2r.role r
695700
join r.rights r2r
696-
join r2r.right rt
697-
where
698-
rt.code = :rightCode
699-
and iu = u
700-
)
701-
and ct.id = :teamId
702-
""");
703-
}
704-
705-
@Test // GH-2864
706-
void usingLeftAsARelationshipNameShouldWork() {
707-
708-
JpqlQueryParser.parseQuery("""
709-
select u
710-
from UserAccountEntity u
711-
join u.lossInspectorLimitConfiguration lil
712-
join u.companyTeam ct
713-
where exists (
714-
select iu
715-
from UserAccountEntity iu
716-
join iu.roles u2r
717-
join u2r.role r
718-
join r.rights r2r
719-
join r2r.left lt
720-
where
721-
lt.code = :rightCode
722-
and iu = u
723-
)
724-
and ct.id = :teamId
725-
""");
726-
}
727-
728-
@Test // GH-2864
729-
void usingOuterAsARelationshipNameShouldWork() {
730-
731-
JpqlQueryParser.parseQuery("""
732-
select u
733-
from UserAccountEntity u
734-
join u.lossInspectorLimitConfiguration lil
735-
join u.companyTeam ct
736-
where exists (
737-
select iu
738-
from UserAccountEntity iu
739-
join iu.roles u2r
740-
join u2r.role r
741-
join r.rights r2r
742-
join r2r.outer ou
743-
where
744-
ou.code = :rightCode
745-
and iu = u
746-
)
747-
and ct.id = :teamId
748-
""");
749-
}
750-
751-
@Test // GH-2864
752-
void usingFullAsARelationshipNameShouldWork() {
753-
754-
JpqlQueryParser.parseQuery("""
755-
select u
756-
from UserAccountEntity u
757-
join u.lossInspectorLimitConfiguration lil
758-
join u.companyTeam ct
759-
where exists (
760-
select iu
761-
from UserAccountEntity iu
762-
join iu.roles u2r
763-
join u2r.role r
764-
join r.rights r2r
765-
join r2r.full fu
701+
join r2r.inner inr
766702
where
767-
fu.code = :rightCode
703+
inr.code = :rightCode
768704
and iu = u
769705
)
770706
and ct.id = :teamId
771-
""");
707+
""", relationshipName, joinAlias, joinAlias));
772708
}
773709

774-
@Test // GH-2864
775-
void usingInnerAsARelationshipNameShouldWork() {
710+
static Stream<Arguments> queriesWithReservedWordsAsIdentifiers() {
776711

777-
JpqlQueryParser.parseQuery("""
778-
select u
779-
from UserAccountEntity u
780-
join u.lossInspectorLimitConfiguration lil
781-
join u.companyTeam ct
782-
where exists (
783-
select iu
784-
from UserAccountEntity iu
785-
join iu.roles u2r
786-
join u2r.role r
787-
join r.rights r2r
788-
join r2r.inner inr
789-
where
790-
inr.code = :rightCode
791-
and iu = u
792-
)
793-
and ct.id = :teamId
794-
""");
712+
return Stream.of( //
713+
Arguments.of("right", "rt"), //
714+
Arguments.of("left", "lt"), //
715+
Arguments.of("outer", "ou"), //
716+
Arguments.of("full", "full"), //
717+
Arguments.of("inner", "inr"));
795718
}
796719

797720
private void assertCountQuery(String originalQuery, String countQuery) {

0 commit comments

Comments
 (0)