|
| 1 | +/* |
| 2 | + * Copyright 2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.jpa.repository.query; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import jakarta.persistence.EntityManager; |
| 21 | +import jakarta.persistence.PersistenceContext; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 25 | +import org.springframework.data.domain.ScrollPosition; |
| 26 | +import org.springframework.data.domain.Sort; |
| 27 | +import org.springframework.data.domain.Sort.Order; |
| 28 | +import org.springframework.data.jpa.domain.sample.SampleWithIdClass; |
| 29 | +import org.springframework.data.jpa.domain.sample.User; |
| 30 | +import org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation; |
| 31 | +import org.springframework.test.context.ContextConfiguration; |
| 32 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 33 | +import org.springframework.transaction.annotation.Transactional; |
| 34 | + |
| 35 | +/** |
| 36 | + * Unit tests for {@link KeysetScrollSpecification}. |
| 37 | + * |
| 38 | + * @author Mark Paluch |
| 39 | + */ |
| 40 | +@ExtendWith(SpringExtension.class) |
| 41 | +@ContextConfiguration({ "classpath:infrastructure.xml" }) |
| 42 | +@Transactional |
| 43 | +class KeysetScrollSpecificationUnitTests { |
| 44 | + |
| 45 | + @PersistenceContext EntityManager em; |
| 46 | + |
| 47 | + @Test // GH-2996 |
| 48 | + void shouldAddIdentifierToSort() { |
| 49 | + |
| 50 | + Sort sort = KeysetScrollSpecification.createSort(ScrollPosition.keyset(), Sort.by("firstname"), |
| 51 | + new JpaMetamodelEntityInformation<>(User.class, em.getMetamodel(), |
| 52 | + em.getEntityManagerFactory().getPersistenceUnitUtil())); |
| 53 | + |
| 54 | + assertThat(sort).extracting(Order::getProperty).containsExactly("firstname", "id"); |
| 55 | + } |
| 56 | + |
| 57 | + @Test // GH-2996 |
| 58 | + void shouldAddCompositeIdentifierToSort() { |
| 59 | + |
| 60 | + Sort sort = KeysetScrollSpecification.createSort(ScrollPosition.keyset(), Sort.by("first", "firstname"), |
| 61 | + new JpaMetamodelEntityInformation<>(SampleWithIdClass.class, em.getMetamodel(), |
| 62 | + em.getEntityManagerFactory().getPersistenceUnitUtil())); |
| 63 | + |
| 64 | + assertThat(sort).extracting(Order::getProperty).containsExactly("first", "firstname", "second"); |
| 65 | + } |
| 66 | + |
| 67 | + @Test // GH-2996 |
| 68 | + void shouldSkipExistingIdentifiersInSort() { |
| 69 | + |
| 70 | + Sort sort = KeysetScrollSpecification.createSort(ScrollPosition.keyset(), Sort.by("id", "firstname"), |
| 71 | + new JpaMetamodelEntityInformation<>(User.class, em.getMetamodel(), |
| 72 | + em.getEntityManagerFactory().getPersistenceUnitUtil())); |
| 73 | + |
| 74 | + assertThat(sort).extracting(Order::getProperty).containsExactly("id", "firstname"); |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments