Skip to content

Commit 7d1191b

Browse files
committed
Add test for Configuration#useActualParamName is false
Related with gh-1237
1 parent d1346e3 commit 7d1191b

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
* Copyright 2009-2020 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+
* http://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.param_name_resolve;
17+
18+
import org.apache.ibatis.annotations.Select;
19+
import org.apache.ibatis.exceptions.PersistenceException;
20+
import org.apache.ibatis.io.Resources;
21+
import org.apache.ibatis.jdbc.ScriptRunner;
22+
import org.apache.ibatis.session.SqlSession;
23+
import org.apache.ibatis.session.SqlSessionFactory;
24+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
25+
import org.junit.jupiter.api.BeforeAll;
26+
import org.junit.jupiter.api.Test;
27+
28+
import java.io.Reader;
29+
import java.sql.Connection;
30+
import java.util.Arrays;
31+
import java.util.List;
32+
33+
import static org.junit.Assert.assertEquals;
34+
import static org.junit.Assert.fail;
35+
36+
class NoActualParamNameTest {
37+
38+
private static SqlSessionFactory sqlSessionFactory;
39+
40+
@BeforeAll
41+
static void setUp() throws Exception {
42+
// create an SqlSessionFactory
43+
try (Reader reader = Resources
44+
.getResourceAsReader("org/apache/ibatis/submitted/param_name_resolve/mybatis-config.xml")) {
45+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
46+
sqlSessionFactory.getConfiguration().addMapper(Mapper.class);
47+
sqlSessionFactory.getConfiguration().setUseActualParamName(false);
48+
}
49+
50+
// populate in-memory database
51+
try (Connection conn = sqlSessionFactory.getConfiguration().getEnvironment().getDataSource().getConnection();
52+
Reader reader = Resources
53+
.getResourceAsReader("org/apache/ibatis/submitted/param_name_resolve/CreateDB.sql")) {
54+
ScriptRunner runner = new ScriptRunner(conn);
55+
runner.setLogWriter(null);
56+
runner.runScript(reader);
57+
}
58+
}
59+
60+
@Test
61+
void testSingleListParameterWhenUseActualParamNameIsFalse() {
62+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
63+
Mapper mapper = sqlSession.getMapper(Mapper.class);
64+
// use actual name -> no available and index parameter("0") is not available too
65+
{
66+
try {
67+
mapper.getUserCountUsingList(Arrays.asList(1, 2));
68+
fail();
69+
} catch (PersistenceException e) {
70+
assertEquals("Parameter 'ids' not found. Available parameters are [collection, list]", e.getCause().getMessage());
71+
}
72+
}
73+
// use 'collection' as alias
74+
{
75+
long count = mapper.getUserCountUsingListWithAliasIsCollection(Arrays.asList(1, 2));
76+
assertEquals(2, count);
77+
}
78+
// use 'list' as alias
79+
{
80+
long count = mapper.getUserCountUsingListWithAliasIsList(Arrays.asList(1, 2));
81+
assertEquals(2, count);
82+
}
83+
}
84+
}
85+
86+
interface Mapper {
87+
@Select({
88+
"<script>",
89+
" select count(*) from users u where u.id in",
90+
" <foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>",
91+
" #{item}",
92+
" </foreach>",
93+
"</script>"
94+
})
95+
Long getUserCountUsingList(List<Integer> ids);
96+
97+
@Select({
98+
"<script>",
99+
" select count(*) from users u where u.id in",
100+
" <foreach item='item' index='index' collection='collection' open='(' separator=',' close=')'>",
101+
" #{item}",
102+
" </foreach>",
103+
"</script>"
104+
})
105+
Long getUserCountUsingListWithAliasIsCollection(List<Integer> ids);
106+
107+
@Select({
108+
"<script>",
109+
" select count(*) from users u where u.id in",
110+
" <foreach item='item' index='index' collection='list' open='(' separator=',' close=')'>",
111+
" #{item}",
112+
" </foreach>",
113+
"</script>"
114+
})
115+
Long getUserCountUsingListWithAliasIsList(List<Integer> ids);
116+
117+
}
118+
119+
}

0 commit comments

Comments
 (0)