Skip to content

Commit ddc75d7

Browse files
committed
Add ChooseSqlNode otherwise fallback test
1 parent 2c92da8 commit ddc75d7

File tree

9 files changed

+230
-3
lines changed

9 files changed

+230
-3
lines changed

src/test/java/org/apache/ibatis/plugin/PluginTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2024 the original author or authors.
2+
* Copyright 2009-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.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2009-2025 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.apache.ibatis.submitted.choosewhen;
17+
18+
import java.util.List;
19+
import java.util.Map;
20+
21+
public interface ChooseWhenMapper {
22+
List<User> selectUser(Map<String, Object> param);
23+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2009-2025 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.apache.ibatis.submitted.choosewhen;
17+
18+
import static org.junit.jupiter.api.Assertions.assertTrue;
19+
20+
import java.io.Reader;
21+
import java.util.HashMap;
22+
import java.util.List;
23+
import java.util.Map;
24+
25+
import org.apache.ibatis.BaseDataTest;
26+
import org.apache.ibatis.io.Resources;
27+
import org.apache.ibatis.session.SqlSession;
28+
import org.apache.ibatis.session.SqlSessionFactory;
29+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
30+
import org.junit.jupiter.api.BeforeAll;
31+
import org.junit.jupiter.api.Test;
32+
33+
public class ChooseWhenTest {
34+
35+
private static SqlSessionFactory sqlSessionFactory;
36+
37+
@BeforeAll
38+
static void initDatabase() throws Exception {
39+
try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/choosewhen/mybatis-config.xml")) {
40+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
41+
sqlSessionFactory.getConfiguration().setLazyLoadingEnabled(true);
42+
sqlSessionFactory.getConfiguration().setAggressiveLazyLoading(false);
43+
}
44+
45+
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
46+
"org/apache/ibatis/submitted/choosewhen/CreateDB.sql");
47+
}
48+
49+
@Test
50+
public void shouldApplyOtherwiseWhenNoParam() {
51+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
52+
ChooseWhenMapper mapper = sqlSession.getMapper(ChooseWhenMapper.class);
53+
Map<String, Object> param = new HashMap<>(); // name 없음
54+
List<User> users = mapper.selectUser(param);
55+
assertTrue(users.stream().allMatch(u -> "ACTIVE".equals(u.getStatus())));
56+
}
57+
}
58+
59+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2009-2025 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.apache.ibatis.submitted.choosewhen;
17+
18+
public class User {
19+
private String name;
20+
private String status;
21+
22+
// getters/setters
23+
public String getName() {
24+
return name;
25+
}
26+
27+
public void setName(String name) {
28+
this.name = name;
29+
}
30+
31+
public String getStatus() {
32+
return status;
33+
}
34+
35+
public void setStatus(String status) {
36+
this.status = status;
37+
}
38+
}

src/test/java/org/apache/ibatis/submitted/optional_on_mapper_method/OptionalOnMapperMethodTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2025 the original author or authors.
2+
* Copyright 2009-2024 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.

src/test/java/org/apache/ibatis/submitted/overwritingproperties/FooMapperTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2024 the original author or authors.
2+
* Copyright 2009-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.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<!--
3+
4+
Copyright 2009-2025 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
https://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
20+
<!DOCTYPE mapper
21+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
22+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
23+
24+
<mapper namespace="org.apache.ibatis.submitted.choosewhen.ChooseWhenMapper">
25+
26+
<select id="selectUser" resultType="org.apache.ibatis.submitted.choosewhen.User">
27+
SELECT * FROM users
28+
<where>
29+
<choose>
30+
<when test="name != null">
31+
AND name = #{name}
32+
</when>
33+
<otherwise>
34+
AND status = 'ACTIVE'
35+
</otherwise>
36+
</choose>
37+
</where>
38+
</select>
39+
40+
</mapper>
41+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--
2+
-- Copyright 2009-2025 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+
17+
CREATE TABLE users (
18+
id INT AUTO_INCREMENT PRIMARY KEY,
19+
name VARCHAR(100),
20+
status VARCHAR(20)
21+
);
22+
23+
INSERT INTO users (name, status) VALUES ('Alice', 'ACTIVE');
24+
INSERT INTO users (name, status) VALUES ('Bob', 'ACTIVE');
25+
INSERT INTO users (name, status) VALUES (null, 'ACTIVE');
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
4+
Copyright 2009-2025 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
https://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<!DOCTYPE configuration
20+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
21+
"https://mybatis.org/dtd/mybatis-3-config.dtd">
22+
23+
<configuration>
24+
25+
<environments default="development">
26+
<environment id="development">
27+
<transactionManager type="JDBC" />
28+
<dataSource type="UNPOOLED">
29+
<property name="driver" value="org.h2.Driver" />
30+
<property name="url" value="jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1" />
31+
<property name="username" value="sa" />
32+
<property name="password" value="" />
33+
</dataSource>
34+
</environment>
35+
</environments>
36+
37+
<mappers>
38+
<mapper resource="org/apache/ibatis/submitted/choosewhen/ChooseWhenMapper.xml"/>
39+
</mappers>
40+
41+
</configuration>

0 commit comments

Comments
 (0)