Skip to content

Commit 5c7b1f8

Browse files
committed
Polishing.
Reformat code. Add Kotlin test to verify Criteria usage.
1 parent 3f9bed2 commit 5c7b1f8

File tree

5 files changed

+91
-15
lines changed

5 files changed

+91
-15
lines changed

spring-data-relational/src/main/java/org/springframework/data/relational/core/query/Criteria.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public static CriteriaStep where(String column) {
166166
* @return a new {@link CriteriaStep} object to complete the first {@link Criteria}.
167167
* @since 4.1
168168
*/
169-
public static <T, P> CriteriaStep where(TypedPropertyPath<T,P> property) {
169+
public static <T, P> CriteriaStep where(TypedPropertyPath<T, P> property) {
170170
return where(TypedPropertyPath.of(property).toDotPath());
171171
}
172172

@@ -198,8 +198,8 @@ protected Criteria createCriteria(Comparator comparator, @Nullable Object value)
198198
* @since 4.1
199199
*/
200200
@CheckReturnValue
201-
public <T,P> CriteriaStep and(TypedPropertyPath<T,P> property) {
202-
return and(TypedPropertyPath.of(property).toDotPath());
201+
public <T, P> CriteriaStep and(TypedPropertyPath<T, P> property) {
202+
return and(property.toDotPath());
203203
}
204204

205205
/**
@@ -260,8 +260,8 @@ protected Criteria createCriteria(Comparator comparator, @Nullable Object value)
260260
* @since 4.1
261261
*/
262262
@CheckReturnValue
263-
public <T,P> CriteriaStep or(TypedPropertyPath<T,P> property) {
264-
return or(TypedPropertyPath.of(property).toDotPath());
263+
public <T, P> CriteriaStep or(TypedPropertyPath<T, P> property) {
264+
return or(property.toDotPath());
265265
}
266266

267267
/**
@@ -524,11 +524,9 @@ private void render(CriteriaDefinition criteria, StringBuilder stringBuilder) {
524524
}
525525

526526
SqlIdentifier column = criteria.getColumn();
527-
528527
Assert.state(column != null, "Column must not be null");
529528

530529
Comparator comparator = criteria.getComparator();
531-
532530
Assert.state(comparator != null, "Comparator must not be null");
533531

534532
stringBuilder.append(column.toSql(IdentifierProcessing.NONE)).append(' ').append(comparator.getComparator());

spring-data-relational/src/main/java/org/springframework/data/relational/core/query/Update.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static Update update(String column, @Nullable Object value) {
7575
* @return new instance of {@link Update}.
7676
* @since 4.1
7777
*/
78-
public static <T,P> Update update(TypedPropertyPath<T,P> property, @Nullable Object value) {
78+
public static <T, P> Update update(TypedPropertyPath<T, P> property, @Nullable Object value) {
7979
return update(TypedPropertyPath.of(property).toDotPath(), value);
8080
}
8181

@@ -103,7 +103,7 @@ public Update set(String column, @Nullable Object value) {
103103
* @since 4.1
104104
*/
105105
@CheckReturnValue
106-
public <T,P> Update set(TypedPropertyPath<T,P> property, @Nullable Object value) {
106+
public <T, P> Update set(TypedPropertyPath<T, P> property, @Nullable Object value) {
107107
return set(TypedPropertyPath.of(property).toDotPath(), value);
108108
}
109109

spring-data-relational/src/test/java/org/springframework/data/relational/core/query/CriteriaUnitTests.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* @author Mark Paluch
3131
* @author Jens Schauder
3232
* @author Roman Chigvintsev
33+
* @author Christoph Strobl
3334
*/
3435
class CriteriaUnitTests {
3536

@@ -46,6 +47,11 @@ void fromCriteria() {
4647
assertThat(criteria).hasToString("(foo IS NOT NULL AND foo IS NULL)");
4748
}
4849

50+
@Test // GH-2226
51+
void whereWithTypedPropertyPathIsEqualToStringColumnName() {
52+
assertThat(where(Person::getName).is("o")).isEqualTo(where("name").is("o"));
53+
}
54+
4955
@Test // DATAJDBC-513
5056
void fromCriteriaOptimized() {
5157

@@ -95,6 +101,13 @@ void andChainedCriteria() {
95101
assertThat(criteria.getValue()).isEqualTo("bar");
96102
}
97103

104+
@Test // GH-2226
105+
void andChainedCriteriaWithTypedPropertyPathIsEqualToStringColumnName() {
106+
107+
assertThat(where(Person::getName).is("o").and(Person::getAge).isNotNull())
108+
.isEqualTo(where("name").is("o").and("age").isNotNull());
109+
}
110+
98111
@Test // DATAJDBC-513
99112
void andGroupedCriteria() {
100113

@@ -131,6 +144,13 @@ void orChainedCriteria() {
131144
assertThat(criteria.getValue()).isEqualTo("bar");
132145
}
133146

147+
@Test // GH-2226
148+
void orChainedCriteriaWithTypedPropertyPathIsEqualToStringColumnName() {
149+
150+
assertThat(where(Person::getName).is("o").or(Person::getAge).isNotNull())
151+
.isEqualTo(where("name").is("o").or("age").isNotNull());
152+
}
153+
134154
@Test // DATAJDBC-513
135155
void orGroupedCriteria() {
136156

@@ -323,4 +343,18 @@ void notIdenticallyCreatedCriteriaAreNotEqual() {
323343
}
324344
}
325345
}
346+
347+
static class Person {
348+
349+
private String name;
350+
private Integer age;
351+
352+
public String getName() {
353+
return name;
354+
}
355+
356+
public Integer getAge() {
357+
return this.age;
358+
}
359+
}
326360
}

spring-data-relational/src/test/java/org/springframework/data/relational/core/query/UpdateUnitTests.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
*/
1616
package org.springframework.data.relational.core.query;
1717

18-
import org.junit.jupiter.api.Test;
19-
2018
import static org.assertj.core.api.Assertions.*;
2119

20+
import org.junit.jupiter.api.Test;
21+
2222
/**
2323
* Unit tests for {@link Update}.
2424
*
@@ -28,21 +28,20 @@
2828
public class UpdateUnitTests {
2929

3030
@Test // DATAJDBC-513
31-
public void shouldRenderUpdateToString() {
32-
31+
void shouldRenderUpdateToString() {
3332
assertThat(Update.update("foo", "baz").set("bar", 42)).hasToString("SET foo = 'baz', bar = 42");
3433
}
3534

3635
@Test // GH-2226
37-
public void shouldRenderUpdateWithTypedPropertyPathToString() {
36+
void shouldRenderUpdateWithTypedPropertyPathToString() {
3837
assertThat(Update.update(Person::getFirstName, "baz").set("bar", 42)).hasToString("SET firstName = 'baz', bar = 42");
3938
}
4039

4140
static class Person {
4241
private String firstName;
4342
private String lastName;
4443

45-
public String getFirstName() {
44+
String getFirstName() {
4645
return firstName;
4746
}
4847

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 org.springframework.data.relational.core.query
17+
18+
import org.assertj.core.api.Assertions.assertThat
19+
import org.junit.jupiter.api.Test
20+
import org.springframework.data.core.div
21+
22+
/**
23+
* Kotlin unit tests for [Criteria].
24+
*
25+
* @author Mark Paluch
26+
*/
27+
class CriteriaKtUnitTests {
28+
29+
@Test
30+
fun shouldAcceptKProperty() {
31+
assertThat(
32+
Criteria.where(Person::lastName).isEqual("foo")
33+
).isEqualTo(Criteria.where("lastName").isEqual("foo"))
34+
}
35+
36+
@Test
37+
fun shouldAcceptKPropertyPath() {
38+
assertThat(
39+
Criteria.where(Person::parent / Person::lastName).isEqual("foo")
40+
).isEqualTo(Criteria.where("parent.lastName").isEqual("foo"))
41+
}
42+
43+
data class Person(val firstName: String, val lastName: String, val parent: Person)
44+
45+
}

0 commit comments

Comments
 (0)