|
| 1 | +/* |
| 2 | + * Copyright 2026-present 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 example.springdata.jdbc.typedpropertypath; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.springframework.data.relational.core.query.Criteria.where; |
| 20 | +import static org.springframework.data.relational.core.query.Query.query; |
| 21 | + |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.springframework.beans.factory.annotation.Autowired; |
| 27 | +import org.springframework.boot.jdbc.test.autoconfigure.AutoConfigureJdbc; |
| 28 | +import org.springframework.boot.test.context.SpringBootTest; |
| 29 | +import org.springframework.data.domain.Sort; |
| 30 | +import org.springframework.data.jdbc.core.JdbcAggregateTemplate; |
| 31 | + |
| 32 | +/** |
| 33 | + * Demonstrates type-safe property paths with {@link Sort}. |
| 34 | + * |
| 35 | + * @author Christoph Strobl |
| 36 | + */ |
| 37 | +@SpringBootTest(classes = TypedPropertyPathJdbcConfiguration.class) |
| 38 | +@AutoConfigureJdbc |
| 39 | +class TypedPropertyPathTests { |
| 40 | + |
| 41 | + @Autowired |
| 42 | + CategoryRepository repository; |
| 43 | + |
| 44 | + @Autowired |
| 45 | + JdbcAggregateTemplate jdbcAggregateTemplate; |
| 46 | + |
| 47 | + @BeforeEach |
| 48 | + void clearData() { |
| 49 | + repository.deleteAll(); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void queryFilterWithTypedPropertyPath() { |
| 54 | + |
| 55 | + repository.save(new Category("Porsche", "Sports cars")); |
| 56 | + repository.save(new Category("BMW", "Luxury cars")); |
| 57 | + repository.save(new Category("Porsche", "SUV series")); |
| 58 | + |
| 59 | + List<Category> all = jdbcAggregateTemplate.findAll( |
| 60 | + query(where(Category::getName).is("Porsche")), |
| 61 | + Category.class); |
| 62 | + |
| 63 | + assertThat(all).hasSize(2); |
| 64 | + assertThat(all).extracting(Category::getName).containsOnly("Porsche"); |
| 65 | + assertThat(all).extracting(Category::getDescription) |
| 66 | + .containsExactlyInAnyOrder("Sports cars", "SUV series"); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void sortBySinglePropertyWithTypedPropertyPath() { |
| 71 | + |
| 72 | + repository.save(new Category("Porsche", "Sports cars")); |
| 73 | + repository.save(new Category("Audi", "German luxury")); |
| 74 | + repository.save(new Category("Mercedes", "Premium")); |
| 75 | + |
| 76 | + List<Category> all = repository.findAll(Sort.by(Category::getName)); |
| 77 | + |
| 78 | + assertThat(all).extracting(Category::getName).containsExactly("Audi", "Mercedes", "Porsche"); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void sortByMultiplePropertiesWithTypedPropertyPath() { |
| 83 | + |
| 84 | + repository.save(new Category("Porsche", "Cayenne")); |
| 85 | + repository.save(new Category("Porsche", "911")); |
| 86 | + repository.save(new Category("BMW", "911")); |
| 87 | + |
| 88 | + List<Category> all = repository.findAll(Sort.by(Category::getName, Category::getDescription)); |
| 89 | + |
| 90 | + assertThat(all).extracting(Category::toString) |
| 91 | + .containsExactly("BMW 911", "Porsche 911", "Porsche Cayenne"); |
| 92 | + } |
| 93 | +} |
0 commit comments