Skip to content

Commit ed297c0

Browse files
committed
#289 - Polishing.
Refactored DefaultDatabaseClientUnitTests in order to make the relevant differences in setup easier to spot. Formatting and nullability annotations. Original pull request: #307.
1 parent 0e5a584 commit ed297c0

File tree

5 files changed

+54
-18
lines changed

5 files changed

+54
-18
lines changed

Diff for: src/main/java/org/springframework/data/r2dbc/core/StatementMapper.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,11 @@ public static SelectSpec create(String table) {
219219
* @since 1.1
220220
*/
221221
public static SelectSpec create(SqlIdentifier table) {
222-
return new SelectSpec(Table.create(table), Collections.emptyList(), Collections.emptyList(), Criteria.empty(),
223-
Sort.unsorted(), -1, -1);
222+
223+
List<String> projectedFields = Collections.emptyList();
224+
List<Expression> selectList = Collections.emptyList();
225+
return new SelectSpec(Table.create(table), projectedFields, selectList, Criteria.empty(), Sort.unsorted(), -1,
226+
-1);
224227
}
225228

226229
public SelectSpec doWithTable(BiFunction<Table, SelectSpec, SelectSpec> function) {
@@ -367,7 +370,6 @@ public List<Expression> getSelectList() {
367370
return Collections.unmodifiableList(selectList);
368371
}
369372

370-
@Nullable
371373
public Criteria getCriteria() {
372374
return this.criteria;
373375
}
@@ -460,8 +462,7 @@ public Map<SqlIdentifier, SettableValue> getAssignments() {
460462
class UpdateSpec {
461463

462464
private final SqlIdentifier table;
463-
@Nullable
464-
private final Update update;
465+
@Nullable private final Update update;
465466

466467
private final Criteria criteria;
467468

Diff for: src/main/java/org/springframework/data/r2dbc/query/Criteria.java

+1
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ private boolean doIsEmpty() {
287287
}
288288

289289
for (Criteria criteria : group) {
290+
290291
if (!criteria.isEmpty()) {
291292
return false;
292293
}

Diff for: src/main/java/org/springframework/data/r2dbc/query/QueryMapper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ Field createPropertyField(@Nullable RelationalPersistentEntity<?> entity, SqlIde
444444
return entity == null ? new Field(key) : new MetadataBackedField(key, entity, mappingContext);
445445
}
446446

447-
Class<?> getTypeHint(Object mappedValue, Class<?> propertyType, SettableValue settableValue) {
447+
Class<?> getTypeHint(@Nullable Object mappedValue, Class<?> propertyType, SettableValue settableValue) {
448448

449449
if (mappedValue == null || propertyType.equals(Object.class)) {
450450
return settableValue.getType();

Diff for: src/test/java/org/springframework/data/r2dbc/query/CriteriaUnitTests.java

+27-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
import java.util.Arrays;
2222

23+
import org.assertj.core.api.SoftAssertions;
2324
import org.junit.Test;
24-
2525
import org.springframework.data.r2dbc.query.Criteria.*;
2626
import org.springframework.data.relational.core.sql.SqlIdentifier;
2727

@@ -53,6 +53,29 @@ public void fromCriteriaOptimized() {
5353
assertThat(criteria).isSameAs(nested);
5454
}
5555

56+
@Test // gh-289
57+
public void isEmpty() {
58+
59+
SoftAssertions.assertSoftly(softly -> {
60+
61+
Criteria empty = empty();
62+
Criteria notEmpty = where("foo").is("bar");
63+
64+
assertThat(empty.isEmpty()).isTrue();
65+
assertThat(notEmpty.isEmpty()).isFalse();
66+
67+
assertThat(Criteria.from(notEmpty).isEmpty()).isFalse();
68+
assertThat(Criteria.from(notEmpty, notEmpty).isEmpty()).isFalse();
69+
70+
assertThat(Criteria.from(empty).isEmpty()).isTrue();
71+
assertThat(Criteria.from(empty, empty).isEmpty()).isTrue();
72+
73+
assertThat(Criteria.from(empty, notEmpty).isEmpty()).isFalse();
74+
assertThat(Criteria.from(notEmpty, empty).isEmpty()).isFalse();
75+
76+
});
77+
}
78+
5679
@Test // gh-64
5780
public void andChainedCriteria() {
5881

@@ -83,6 +106,7 @@ public void andGroupedCriteria() {
83106

84107
criteria = criteria.getPrevious();
85108

109+
assertThat(criteria).isNotNull();
86110
assertThat(criteria.getColumn()).isEqualTo(SqlIdentifier.unquoted("foo"));
87111
assertThat(criteria.getComparator()).isEqualTo(Comparator.EQ);
88112
assertThat(criteria.getValue()).isEqualTo("bar");
@@ -98,6 +122,7 @@ public void orChainedCriteria() {
98122

99123
criteria = criteria.getPrevious();
100124

125+
assertThat(criteria).isNotNull();
101126
assertThat(criteria.getPrevious()).isNull();
102127
assertThat(criteria.getValue()).isEqualTo("bar");
103128
}
@@ -114,6 +139,7 @@ public void orGroupedCriteria() {
114139

115140
criteria = criteria.getPrevious();
116141

142+
assertThat(criteria).isNotNull();
117143
assertThat(criteria.getColumn()).isEqualTo(SqlIdentifier.unquoted("foo"));
118144
assertThat(criteria.getComparator()).isEqualTo(Comparator.EQ);
119145
assertThat(criteria.getValue()).isEqualTo("bar");

Diff for: src/test/java/org/springframework/data/r2dbc/query/QueryMapperUnitTests.java

+19-11
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.Collections;
2323

2424
import org.junit.Test;
25-
2625
import org.springframework.data.domain.Sort;
2726
import org.springframework.data.r2dbc.convert.MappingR2dbcConverter;
2827
import org.springframework.data.r2dbc.convert.R2dbcConverter;
@@ -43,7 +42,9 @@
4342
*/
4443
public class QueryMapperUnitTests {
4544

46-
R2dbcConverter converter = new MappingR2dbcConverter(new R2dbcMappingContext());
45+
R2dbcMappingContext context = new R2dbcMappingContext();
46+
R2dbcConverter converter = new MappingR2dbcConverter(context);
47+
4748
QueryMapper mapper = new QueryMapper(PostgresDialect.INSTANCE, converter);
4849
BindTarget bindTarget = mock(BindTarget.class);
4950

@@ -90,8 +91,13 @@ public void shouldMapNestedGroup() {
9091

9192
Criteria initial = Criteria.empty();
9293

93-
Criteria criteria = initial.and(Criteria.where("name").is("Foo")).and(Criteria.where("name").is("Bar").or("age")
94-
.lessThan(49).or(Criteria.where("name").not("Bar").and("age").greaterThan(49)));
94+
Criteria criteria = initial.and(Criteria.where("name").is("Foo")) //
95+
.and(Criteria.where("name").is("Bar") //
96+
.or("age").lessThan(49) //
97+
.or(Criteria.where("name").not("Bar") //
98+
.and("age").greaterThan(49) //
99+
) //
100+
);
95101

96102
assertThat(criteria.isEmpty()).isFalse();
97103

@@ -104,8 +110,10 @@ public void shouldMapNestedGroup() {
104110
@Test // gh-289
105111
public void shouldMapFrom() {
106112

107-
Criteria criteria = Criteria.from(Criteria.where("name").is("Foo"))
108-
.and(Criteria.where("name").is("Bar").or("age").lessThan(49));
113+
Criteria criteria = Criteria.from(Criteria.where("name").is("Foo")) //
114+
.and(Criteria.where("name").is("Bar") //
115+
.or("age").lessThan(49) //
116+
);
109117

110118
assertThat(criteria.isEmpty()).isFalse();
111119

@@ -149,7 +157,7 @@ public void shouldMapExpression() {
149157
Table table = Table.create("my_table").as("my_aliased_table");
150158

151159
Expression mappedObject = mapper.getMappedObject(table.column("alternative").as("my_aliased_col"),
152-
converter.getMappingContext().getRequiredPersistentEntity(Person.class));
160+
context.getRequiredPersistentEntity(Person.class));
153161

154162
assertThat(mappedObject).hasToString("my_aliased_table.another_name AS my_aliased_col");
155163
}
@@ -160,7 +168,7 @@ public void shouldMapCountFunction() {
160168
Table table = Table.create("my_table").as("my_aliased_table");
161169

162170
Expression mappedObject = mapper.getMappedObject(Functions.count(table.column("alternative")),
163-
converter.getMappingContext().getRequiredPersistentEntity(Person.class));
171+
context.getRequiredPersistentEntity(Person.class));
164172

165173
assertThat(mappedObject).hasToString("COUNT(my_aliased_table.another_name)");
166174
}
@@ -171,7 +179,7 @@ public void shouldMapExpressionToUnknownColumn() {
171179
Table table = Table.create("my_table").as("my_aliased_table");
172180

173181
Expression mappedObject = mapper.getMappedObject(table.column("unknown").as("my_aliased_col"),
174-
converter.getMappingContext().getRequiredPersistentEntity(Person.class));
182+
context.getRequiredPersistentEntity(Person.class));
175183

176184
assertThat(mappedObject).hasToString("my_aliased_table.unknown AS my_aliased_col");
177185
}
@@ -352,7 +360,7 @@ public void shouldMapSort() {
352360

353361
Sort sort = Sort.by(desc("alternative"));
354362

355-
Sort mapped = mapper.getMappedObject(sort, converter.getMappingContext().getRequiredPersistentEntity(Person.class));
363+
Sort mapped = mapper.getMappedObject(sort, context.getRequiredPersistentEntity(Person.class));
356364

357365
assertThat(mapped.getOrderFor("another_name")).isEqualTo(desc("another_name"));
358366
assertThat(mapped.getOrderFor("alternative")).isNull();
@@ -363,7 +371,7 @@ private BoundCondition map(Criteria criteria) {
363371
BindMarkersFactory markers = BindMarkersFactory.indexed("$", 1);
364372

365373
return mapper.getMappedObject(markers.create(), criteria, Table.create("person"),
366-
converter.getMappingContext().getRequiredPersistentEntity(Person.class));
374+
context.getRequiredPersistentEntity(Person.class));
367375
}
368376

369377
static class Person {

0 commit comments

Comments
 (0)