Skip to content

Commit e847b80

Browse files
committed
Polishing.
Add MongoDB tests for property path usage. Merge JDBC tests.
1 parent b8c24a0 commit e847b80

File tree

13 files changed

+217
-245
lines changed

13 files changed

+217
-245
lines changed

jdbc/basics/src/main/java/example/springdata/jdbc/basics/simpleentity/Category.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ public class Category {
4343
private @Setter long inserted;
4444
private AgeGroup ageGroup;
4545

46+
public Category(String name, String description) {
47+
this.id = null;
48+
this.name = name;
49+
this.description = description;
50+
}
51+
4652
public Category(String name, String description, AgeGroup ageGroup) {
4753

4854
this.id = null;
@@ -58,4 +64,9 @@ public void timeStamp() {
5864
inserted = System.currentTimeMillis();
5965
}
6066
}
67+
68+
@Override
69+
public String toString() {
70+
return "%s %s".formatted(name, description);
71+
}
6172
}

jdbc/basics/src/main/java/example/springdata/jdbc/basics/simpleentity/CategoryRepository.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
package example.springdata.jdbc.basics.simpleentity;
1717

1818
import org.springframework.data.repository.CrudRepository;
19+
import org.springframework.data.repository.ListPagingAndSortingRepository;
1920

2021
/**
2122
* Repository for Categories.
2223
*
2324
* @author Jens Schauder
2425
*/
25-
interface CategoryRepository extends CrudRepository<Category, Long>, WithInsert<Category> {}
26+
public interface CategoryRepository
27+
extends CrudRepository<Category, Long>, WithInsert<Category>, ListPagingAndSortingRepository<Category, Long> {}

jdbc/basics/src/test/java/example/springdata/jdbc/basics/simpleentity/SimpleEntityTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@
2727
import org.springframework.boot.test.context.SpringBootTest;
2828

2929
/**
30-
* Demonstrates simple CRUD operations with a simple entity without any references.
30+
* Demonstrates typed property path usage.
3131
*
32-
* @author Jens Schauder
33-
* @author Divya Srivastava
32+
* @author Mark Paluch
3433
*/
3534
@SpringBootTest(classes = CategoryConfiguration.class)
3635
@AutoConfigureJdbc

jdbc/typed-property-path/src/test/java/example/springdata/jdbc/typedpropertypath/TypedPropertyPathTests.java renamed to jdbc/basics/src/test/java/example/springdata/jdbc/query/QueryTests.java

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
/*
2-
* Copyright 2026-present the original author or authors.
2+
* Copyright 2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package example.springdata.jdbc.typedpropertypath;
16+
package example.springdata.jdbc.query;
1717

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;
2125

2226
import java.util.List;
2327

2428
import org.junit.jupiter.api.BeforeEach;
2529
import org.junit.jupiter.api.Test;
30+
2631
import org.springframework.beans.factory.annotation.Autowired;
2732
import org.springframework.boot.jdbc.test.autoconfigure.AutoConfigureJdbc;
2833
import org.springframework.boot.test.context.SpringBootTest;
@@ -33,61 +38,94 @@
3338
* Demonstrates type-safe property paths with {@link Sort}.
3439
*
3540
* @author Christoph Strobl
41+
* @author Mark Paluch
3642
*/
37-
@SpringBootTest(classes = TypedPropertyPathJdbcConfiguration.class)
43+
@SpringBootTest(classes = CategoryConfiguration.class)
3844
@AutoConfigureJdbc
39-
class TypedPropertyPathTests {
45+
class QueryTests {
4046

41-
@Autowired
42-
CategoryRepository repository;
47+
@Autowired JdbcAggregateTemplate template;
4348

44-
@Autowired
45-
JdbcAggregateTemplate jdbcAggregateTemplate;
49+
@Autowired CategoryRepository repository;
4650

4751
@BeforeEach
4852
void clearData() {
4953
repository.deleteAll();
5054
}
5155

5256
@Test
53-
void queryFilterWithTypedPropertyPath() {
57+
void queryFilterWithStringPropertyPath() {
5458

5559
repository.save(new Category("Porsche", "Sports cars"));
5660
repository.save(new Category("BMW", "Luxury cars"));
5761
repository.save(new Category("Porsche", "SUV series"));
5862

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);
6264

6365
assertThat(all).hasSize(2);
6466
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");
6768
}
6869

6970
@Test
70-
void sortBySinglePropertyWithTypedPropertyPath() {
71+
void queryFilterWithTypedPropertyPath() {
7172

7273
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+
}
7592

93+
@Test
94+
void sortBySinglePropertyWithTypedPropertyPath() {
95+
96+
insertAudiMercedesAndPorsche();
7697
List<Category> all = repository.findAll(Sort.by(Category::getName));
7798

7899
assertThat(all).extracting(Category::getName).containsExactly("Audi", "Mercedes", "Porsche");
79100
}
80101

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+
81111
@Test
82112
void sortByMultiplePropertiesWithTypedPropertyPath() {
83113

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() {
84127
repository.save(new Category("Porsche", "Cayenne"));
85128
repository.save(new Category("Porsche", "911"));
86129
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");
92130
}
93131
}

jdbc/pom.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020
<module>aot-optimization</module>
2121
<module>basics</module>
2222
<module>composite-ids</module>
23+
<module>graalvm-native</module>
2324
<module>howto</module>
2425
<module>immutables</module>
2526
<module>jmolecules</module>
2627
<module>jooq</module>
2728
<module>singlequeryloading</module>
28-
<module>typed-property-path</module>
2929
<!-- <module>mybatis</module> -->
30-
<module>graalvm-native</module>
3130
</modules>
3231

3332
<dependencies>

jdbc/typed-property-path/README.adoc

Lines changed: 0 additions & 30 deletions
This file was deleted.

jdbc/typed-property-path/pom.xml

Lines changed: 0 additions & 22 deletions
This file was deleted.

jdbc/typed-property-path/src/main/java/example/springdata/jdbc/typedpropertypath/Category.java

Lines changed: 0 additions & 99 deletions
This file was deleted.

0 commit comments

Comments
 (0)