From bb76f07d6c444ff21af76f661600699347d5cf70 Mon Sep 17 00:00:00 2001 From: jonyschak Date: Sun, 13 Feb 2022 01:31:57 -0600 Subject: [PATCH] Correcting sort object reference in FetchableFluentQueryByExample.sortBy and FetchableFluentQueryByPredicate.sortBy Closes #2438 --- .../FetchableFluentQueryByExample.java | 7 ++-- .../FetchableFluentQueryByPredicate.java | 7 ++-- ...etchableFluentQueryByExampleUnitTests.java | 41 +++++++++++++++++++ ...chableFluentQueryByPredicateUnitTests.java | 40 ++++++++++++++++++ 4 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 src/test/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByExampleUnitTests.java create mode 100644 src/test/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByPredicateUnitTests.java diff --git a/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByExample.java b/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByExample.java index cb2645bfc1..e7039cc42c 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByExample.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByExample.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 the original author or authors. + * Copyright 2021-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,6 +45,7 @@ * @author Greg Turnquist * @author Mark Paluch * @author Jens Schauder + * @author J.R. Onyschak * @since 2.6 */ class FetchableFluentQueryByExample extends FluentQuerySupport implements FetchableFluentQuery { @@ -86,8 +87,8 @@ public FetchableFluentQuery sortBy(Sort sort) { Assert.notNull(sort, "Sort must not be null!"); - return new FetchableFluentQueryByExample<>(example, entityType, resultType, sort.and(sort), properties, finder, - countOperation, existsOperation, entityManager, escapeCharacter); + return new FetchableFluentQueryByExample<>(example, entityType, resultType, this.sort.and(sort), properties, + finder, countOperation, existsOperation, entityManager, escapeCharacter); } /* diff --git a/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByPredicate.java b/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByPredicate.java index f71eeacc0b..a5f9d7be01 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByPredicate.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByPredicate.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 the original author or authors. + * Copyright 2021-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,6 +46,7 @@ * @author Greg Turnquist * @author Mark Paluch * @author Jens Schauder + * @author J.R. Onyschak * @since 2.6 */ class FetchableFluentQueryByPredicate extends FluentQuerySupport implements FetchableFluentQuery { @@ -89,8 +90,8 @@ public FetchableFluentQuery sortBy(Sort sort) { Assert.notNull(sort, "Sort must not be null!"); - return new FetchableFluentQueryByPredicate<>(predicate, entityType, resultType, sort.and(sort), properties, finder, - pagedFinder, countOperation, existsOperation, entityManager); + return new FetchableFluentQueryByPredicate<>(predicate, entityType, resultType, this.sort.and(sort), properties, + finder, pagedFinder, countOperation, existsOperation, entityManager); } /* diff --git a/src/test/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByExampleUnitTests.java b/src/test/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByExampleUnitTests.java new file mode 100644 index 0000000000..238e291071 --- /dev/null +++ b/src/test/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByExampleUnitTests.java @@ -0,0 +1,41 @@ +/* + * Copyright 2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.jpa.repository.support; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; +import org.springframework.data.domain.Example; +import org.springframework.data.domain.Sort; +import org.springframework.data.domain.Sort.Order; + +/** + * Unit tests for {@link FetchableFluentQueryByExample}. + * + * @author J.R. Onyschak + */ +class FetchableFluentQueryByExampleUnitTests { + + @Test // GH-2438 + @SuppressWarnings({ "rawtypes", "unchecked" }) + void multipleSortBy() { + Sort s1 = Sort.by(Order.by("s1")); + Sort s2 = Sort.by(Order.by("s2")); + FetchableFluentQueryByExample f = new FetchableFluentQueryByExample(Example.of(""), null, null, null, null, null); + f = (FetchableFluentQueryByExample) f.sortBy(s1).sortBy(s2); + assertThat(f.sort).isEqualTo(s1.and(s2)); + } +} diff --git a/src/test/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByPredicateUnitTests.java b/src/test/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByPredicateUnitTests.java new file mode 100644 index 0000000000..ea21cbb122 --- /dev/null +++ b/src/test/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByPredicateUnitTests.java @@ -0,0 +1,40 @@ +/* + * Copyright 2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.jpa.repository.support; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; +import org.springframework.data.domain.Sort; +import org.springframework.data.domain.Sort.Order; + +/** + * Unit tests for {@link FetchableFluentQueryByPredicate}. + * + * @author J.R. Onyschak + */ +class FetchableFluentQueryByPredicateUnitTests { + + @Test // GH-2438 + @SuppressWarnings({ "rawtypes", "unchecked" }) + void multipleSortBy() { + Sort s1 = Sort.by(Order.by("s1")); + Sort s2 = Sort.by(Order.by("s2")); + FetchableFluentQueryByPredicate f = new FetchableFluentQueryByPredicate(null, null, null, null, null, null, null); + f = (FetchableFluentQueryByPredicate) f.sortBy(s1).sortBy(s2); + assertThat(f.sort).isEqualTo(s1.and(s2)); + } +}