Skip to content

Commit f06686d

Browse files
committed
Animal data nullability updates
1 parent f8205e7 commit f06686d

5 files changed

+14
-43
lines changed

Diff for: src/test/java/examples/animal/data/AnimalDataTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import java.util.Collections;
3434
import java.util.List;
3535
import java.util.Map;
36-
import java.util.Objects;
3736
import java.util.Optional;
3837

3938
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;

Diff for: src/test/java/examples/animal/data/MyInCondition.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
import org.jspecify.annotations.Nullable;
2323
import org.mybatis.dynamic.sql.SqlBuilder;
2424
import org.mybatis.dynamic.sql.where.condition.IsIn;
25+
import org.mybatis.dynamic.sql.where.condition.IsInWhenPresent;
2526

2627
public class MyInCondition {
27-
public static IsIn<String> isIn(@Nullable String...values) {
28-
return SqlBuilder.isIn(values)
29-
.filter(Objects::nonNull)
28+
public static IsInWhenPresent<String> isIn(@Nullable String...values) {
29+
return SqlBuilder.isInWhenPresent(values)
3030
.map(String::trim)
3131
.filter(not(String::isEmpty));
3232
}

Diff for: src/test/java/examples/animal/data/OptionalConditionsAnimalDataTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.apache.ibatis.session.SqlSessionFactory;
3535
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
3636
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
37+
import org.jspecify.annotations.Nullable;
3738
import org.junit.jupiter.api.BeforeEach;
3839
import org.junit.jupiter.api.Test;
3940
import org.mybatis.dynamic.sql.render.RenderingStrategies;
@@ -43,7 +44,7 @@ class OptionalConditionsAnimalDataTest {
4344

4445
private static final String JDBC_URL = "jdbc:hsqldb:mem:aname";
4546
private static final String JDBC_DRIVER = "org.hsqldb.jdbcDriver";
46-
private static final Integer NULL_INTEGER = null;
47+
private static final @Nullable Integer NULL_INTEGER = null;
4748

4849
private SqlSessionFactory sqlSessionFactory;
4950

Diff for: src/test/java/examples/animal/data/OptionalConditionsWithPredicatesAnimalDataTest.java

+7-28
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.sql.Connection;
3131
import java.sql.DriverManager;
3232
import java.util.List;
33-
import java.util.Objects;
3433

3534
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
3635
import org.apache.ibatis.jdbc.ScriptRunner;
@@ -94,26 +93,6 @@ void testAllIgnored() {
9493
}
9594
}
9695

97-
@Test
98-
void testSelectByNull() {
99-
// this method demonstrates that ignoring the null value warning will still work
100-
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
101-
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);
102-
SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight)
103-
.from(animalData)
104-
.where(id, isGreaterThan(NULL_INTEGER)) // should be an IDE warning about passing null to a nonnull method
105-
.orderBy(id)
106-
.build()
107-
.render(RenderingStrategies.MYBATIS3);
108-
List<AnimalData> animals = mapper.selectMany(selectStatement);
109-
assertAll(
110-
() -> assertThat(selectStatement.getSelectStatement()).isEqualTo("select id, animal_name, body_weight, brain_weight from AnimalData where id > #{parameters.p1,jdbcType=INTEGER} order by id"),
111-
() -> assertThat(selectStatement.getParameters()).containsEntry("p1", null),
112-
() -> assertThat(animals).isEmpty()
113-
);
114-
}
115-
}
116-
11796
@Test
11897
void testIgnoredBetweenRendered() {
11998
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
@@ -122,7 +101,7 @@ void testIgnoredBetweenRendered() {
122101
.from(animalData)
123102
.where(id, isEqualTo(3))
124103
.and(id, isNotEqualToWhenPresent(NULL_INTEGER))
125-
.or(id, isEqualTo(4).filter(Objects::nonNull))
104+
.or(id, isEqualTo(4))
126105
.orderBy(id)
127106
.build()
128107
.render(RenderingStrategies.MYBATIS3);
@@ -143,8 +122,8 @@ void testIgnoredInWhere() {
143122
SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight)
144123
.from(animalData)
145124
.where(id, isLessThanWhenPresent(NULL_INTEGER))
146-
.and(id, isEqualTo(3).filter(Objects::nonNull))
147-
.or(id, isEqualTo(4).filter(Objects::nonNull))
125+
.and(id, isEqualTo(3))
126+
.or(id, isEqualTo(4))
148127
.orderBy(id)
149128
.build()
150129
.render(RenderingStrategies.MYBATIS3);
@@ -187,8 +166,8 @@ void testIgnoredInitialWhere() {
187166
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);
188167
SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight)
189168
.from(animalData)
190-
.where(id, isLessThanWhenPresent(NULL_INTEGER), and(id, isEqualTo(3).filter(Objects::nonNull)))
191-
.or(id, isEqualTo(4).filter(Objects::nonNull))
169+
.where(id, isLessThanWhenPresent(NULL_INTEGER), and(id, isEqualTo(3)))
170+
.or(id, isEqualTo(4))
192171
.orderBy(id)
193172
.build()
194173
.render(RenderingStrategies.MYBATIS3);
@@ -802,7 +781,7 @@ void testIsLikeWhenWithValue() {
802781
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);
803782
SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight)
804783
.from(animalData)
805-
.where(animalName, isLike("%mole").filter(Objects::nonNull))
784+
.where(animalName, isLike("%mole"))
806785
.and(id, isLessThanOrEqualTo(10))
807786
.orderBy(id)
808787
.build()
@@ -882,7 +861,7 @@ void testIsNotLikeWhenWithValue() {
882861
AnimalDataMapper mapper = sqlSession.getMapper(AnimalDataMapper.class);
883862
SelectStatementProvider selectStatement = select(id, animalName, bodyWeight, brainWeight)
884863
.from(animalData)
885-
.where(animalName, isNotLike("%mole").filter(Objects::nonNull))
864+
.where(animalName, isNotLike("%mole"))
886865
.and(id, isLessThanOrEqualTo(10))
887866
.orderBy(id)
888867
.build()

Diff for: src/test/java/examples/animal/data/VariousListConditionsTest.java

+2-10
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,18 @@ void testInWithNull() {
8686

8787
SelectStatementProvider selectStatement = select(id, animalName)
8888
.from(animalData)
89-
.where(id, isIn(2, 3, null))
89+
.where(id, isInWhenPresent(2, 3, null))
9090
.orderBy(id)
9191
.build()
9292
.render(RenderingStrategies.MYBATIS3);
9393

9494
assertThat(selectStatement.getSelectStatement()).isEqualTo(
9595
"select id, animal_name from AnimalData where id " +
96-
"in (#{parameters.p1,jdbcType=INTEGER},#{parameters.p2,jdbcType=INTEGER},#{parameters.p3,jdbcType=INTEGER}) " +
96+
"in (#{parameters.p1,jdbcType=INTEGER},#{parameters.p2,jdbcType=INTEGER}) " +
9797
"order by id"
9898
);
9999
assertThat(selectStatement.getParameters()).containsEntry("p1", 2);
100100
assertThat(selectStatement.getParameters()).containsEntry("p2", 3);
101-
assertThat(selectStatement.getParameters()).containsEntry("p3", null);
102101

103102
List<Map<String, Object>> rows = mapper.selectManyMappedRows(selectStatement);
104103
assertThat(rows).hasSize(2);
@@ -171,13 +170,6 @@ void testInWhenPresentWithEmptyList() {
171170
}
172171
}
173172

174-
@Test
175-
void testInWithNullList() {
176-
assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->
177-
isIn((Collection<Integer>) null)
178-
);
179-
}
180-
181173
@Test
182174
void testInWhenPresentWithNullList() {
183175
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {

0 commit comments

Comments
 (0)