|
1 | 1 | /* |
2 | | - * Copyright 2026-present the original author or authors. |
| 2 | + * Copyright 2025 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
6 | 6 | * You may obtain a copy of the License at |
7 | 7 | * |
8 | | - * https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 | * |
10 | 10 | * Unless required by applicable law or agreed to in writing, software |
11 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | | -package example.springdata.jdbc.typedpropertypath; |
| 16 | +package example.springdata.jdbc.query; |
17 | 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; |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | +import static org.springframework.data.relational.core.query.Criteria.*; |
| 20 | +import static org.springframework.data.relational.core.query.Query.*; |
| 21 | + |
| 22 | +import example.springdata.jdbc.basics.simpleentity.Category; |
| 23 | +import example.springdata.jdbc.basics.simpleentity.CategoryConfiguration; |
| 24 | +import example.springdata.jdbc.basics.simpleentity.CategoryRepository; |
21 | 25 |
|
22 | 26 | import java.util.List; |
23 | 27 |
|
24 | 28 | import org.junit.jupiter.api.BeforeEach; |
25 | 29 | import org.junit.jupiter.api.Test; |
| 30 | + |
26 | 31 | import org.springframework.beans.factory.annotation.Autowired; |
27 | 32 | import org.springframework.boot.jdbc.test.autoconfigure.AutoConfigureJdbc; |
28 | 33 | import org.springframework.boot.test.context.SpringBootTest; |
|
33 | 38 | * Demonstrates type-safe property paths with {@link Sort}. |
34 | 39 | * |
35 | 40 | * @author Christoph Strobl |
| 41 | + * @author Mark Paluch |
36 | 42 | */ |
37 | | -@SpringBootTest(classes = TypedPropertyPathJdbcConfiguration.class) |
| 43 | +@SpringBootTest(classes = CategoryConfiguration.class) |
38 | 44 | @AutoConfigureJdbc |
39 | | -class TypedPropertyPathTests { |
| 45 | +class QueryTests { |
40 | 46 |
|
41 | | - @Autowired |
42 | | - CategoryRepository repository; |
| 47 | + @Autowired JdbcAggregateTemplate template; |
43 | 48 |
|
44 | | - @Autowired |
45 | | - JdbcAggregateTemplate jdbcAggregateTemplate; |
| 49 | + @Autowired CategoryRepository repository; |
46 | 50 |
|
47 | 51 | @BeforeEach |
48 | 52 | void clearData() { |
49 | 53 | repository.deleteAll(); |
50 | 54 | } |
51 | 55 |
|
52 | 56 | @Test |
53 | | - void queryFilterWithTypedPropertyPath() { |
| 57 | + void queryFilterWithStringPropertyPath() { |
54 | 58 |
|
55 | 59 | repository.save(new Category("Porsche", "Sports cars")); |
56 | 60 | repository.save(new Category("BMW", "Luxury cars")); |
57 | 61 | repository.save(new Category("Porsche", "SUV series")); |
58 | 62 |
|
59 | | - List<Category> all = jdbcAggregateTemplate.findAll( |
60 | | - query(where(Category::getName).is("Porsche")), |
61 | | - Category.class); |
| 63 | + List<Category> all = template.findAll(query(where("name").is("Porsche")), Category.class); |
62 | 64 |
|
63 | 65 | assertThat(all).hasSize(2); |
64 | 66 | assertThat(all).extracting(Category::getName).containsOnly("Porsche"); |
65 | | - assertThat(all).extracting(Category::getDescription) |
66 | | - .containsExactlyInAnyOrder("Sports cars", "SUV series"); |
| 67 | + assertThat(all).extracting(Category::getDescription).containsExactlyInAnyOrder("Sports cars", "SUV series"); |
67 | 68 | } |
68 | 69 |
|
69 | 70 | @Test |
70 | | - void sortBySinglePropertyWithTypedPropertyPath() { |
| 71 | + void queryFilterWithTypedPropertyPath() { |
71 | 72 |
|
72 | 73 | repository.save(new Category("Porsche", "Sports cars")); |
73 | | - repository.save(new Category("Audi", "German luxury")); |
74 | | - repository.save(new Category("Mercedes", "Premium")); |
| 74 | + repository.save(new Category("BMW", "Luxury cars")); |
| 75 | + repository.save(new Category("Porsche", "SUV series")); |
| 76 | + |
| 77 | + List<Category> all = template.findAll(query(where(Category::getName).is("Porsche")), Category.class); |
| 78 | + |
| 79 | + assertThat(all).hasSize(2); |
| 80 | + assertThat(all).extracting(Category::getName).containsOnly("Porsche"); |
| 81 | + assertThat(all).extracting(Category::getDescription).containsExactlyInAnyOrder("Sports cars", "SUV series"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void sortBySinglePropertyWithStringPropertyPath() { |
| 86 | + |
| 87 | + insertAudiMercedesAndPorsche(); |
| 88 | + List<Category> all = repository.findAll(Sort.by("name")); |
| 89 | + |
| 90 | + assertThat(all).extracting(Category::getName).containsExactly("Audi", "Mercedes", "Porsche"); |
| 91 | + } |
75 | 92 |
|
| 93 | + @Test |
| 94 | + void sortBySinglePropertyWithTypedPropertyPath() { |
| 95 | + |
| 96 | + insertAudiMercedesAndPorsche(); |
76 | 97 | List<Category> all = repository.findAll(Sort.by(Category::getName)); |
77 | 98 |
|
78 | 99 | assertThat(all).extracting(Category::getName).containsExactly("Audi", "Mercedes", "Porsche"); |
79 | 100 | } |
80 | 101 |
|
| 102 | + @Test |
| 103 | + void sortByMultiplePropertiesWithStringPropertyPath() { |
| 104 | + |
| 105 | + insertBmwAndPorsche(); |
| 106 | + List<Category> all = repository.findAll(Sort.by("name", "description")); |
| 107 | + |
| 108 | + assertThat(all).extracting(Category::toString).containsExactly("BMW 911", "Porsche 911", "Porsche Cayenne"); |
| 109 | + } |
| 110 | + |
81 | 111 | @Test |
82 | 112 | void sortByMultiplePropertiesWithTypedPropertyPath() { |
83 | 113 |
|
| 114 | + insertBmwAndPorsche(); |
| 115 | + List<Category> all = repository.findAll(Sort.by(Category::getName, Category::getDescription)); |
| 116 | + |
| 117 | + assertThat(all).extracting(Category::toString).containsExactly("BMW 911", "Porsche 911", "Porsche Cayenne"); |
| 118 | + } |
| 119 | + |
| 120 | + private void insertAudiMercedesAndPorsche() { |
| 121 | + repository.save(new Category("Porsche", "Sports cars")); |
| 122 | + repository.save(new Category("Audi", "German luxury")); |
| 123 | + repository.save(new Category("Mercedes", "Premium")); |
| 124 | + } |
| 125 | + |
| 126 | + private void insertBmwAndPorsche() { |
84 | 127 | repository.save(new Category("Porsche", "Cayenne")); |
85 | 128 | repository.save(new Category("Porsche", "911")); |
86 | 129 | 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 | 130 | } |
93 | 131 | } |
0 commit comments