1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.dynsql;
17
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19
20 import java.io.IOException;
21 import java.io.Reader;
22 import java.math.BigDecimal;
23 import java.math.BigInteger;
24 import java.sql.CallableStatement;
25 import java.sql.PreparedStatement;
26 import java.sql.ResultSet;
27 import java.sql.SQLException;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Map;
31
32 import org.apache.ibatis.BaseDataTest;
33 import org.apache.ibatis.exceptions.PersistenceException;
34 import org.apache.ibatis.io.Resources;
35 import org.apache.ibatis.session.SqlSession;
36 import org.apache.ibatis.session.SqlSessionFactory;
37 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
38 import org.apache.ibatis.type.JdbcType;
39 import org.apache.ibatis.type.TypeHandler;
40 import org.junit.jupiter.api.BeforeAll;
41 import org.junit.jupiter.api.Test;
42
43 class DynSqlTest {
44
45 protected static SqlSessionFactory sqlSessionFactory;
46
47 @BeforeAll
48 static void setUp() throws Exception {
49 try (Reader configReader = Resources.getResourceAsReader("org/apache/ibatis/submitted/dynsql/MapperConfig.xml")) {
50 sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
51 }
52
53 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
54 "org/apache/ibatis/submitted/dynsql/CreateDB.sql");
55 }
56
57 @Test
58 void testSelect() {
59 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
60 List<Integer> ids = new ArrayList<>();
61 ids.add(1);
62 ids.add(3);
63 ids.add(5);
64 Parameter parameter = new Parameter();
65 parameter.setEnabled(true);
66 parameter.setSchema("ibtest");
67 parameter.setIds(ids);
68
69 List<Map<String, Object>> answer = sqlSession.selectList("org.apache.ibatis.submitted.dynsql.select", parameter);
70
71 assertEquals(3, answer.size());
72 }
73 }
74
75 @Test
76 void testSelectSimple() {
77 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
78 List<Integer> ids = new ArrayList<>();
79 ids.add(1);
80 ids.add(3);
81 ids.add(5);
82 Parameter parameter = new Parameter();
83 parameter.setEnabled(true);
84 parameter.setSchema("ibtest");
85 parameter.setIds(ids);
86
87 List<Map<String, Object>> answer = sqlSession.selectList("org.apache.ibatis.submitted.dynsql.select_simple", parameter);
88
89 assertEquals(3, answer.size());
90 }
91 }
92
93 @Test
94 void testSelectLike() {
95 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
96
97 List<Map<String, Object>> answer = sqlSession.selectList("org.apache.ibatis.submitted.dynsql.selectLike", "Ba");
98
99 assertEquals(2, answer.size());
100 assertEquals(4, answer.get(0).get("ID"));
101 assertEquals(6, answer.get(1).get("ID"));
102 }
103 }
104
105 @Test
106 void testNumerics() {
107 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
108 List<NumericRow> answer = sqlSession.selectList("org.apache.ibatis.submitted.dynsql.selectNumerics");
109
110 assertEquals(1, answer.size());
111
112 NumericRow row = answer.get(0);
113 assertEquals(1, (int) row.getId());
114 assertEquals(2, (int) row.getTinynumber());
115 assertEquals(3, (int) row.getSmallnumber());
116 assertEquals(4L, (long) row.getLonginteger());
117 assertEquals(new BigInteger("5"), row.getBiginteger());
118 assertEquals(new BigDecimal("6.00"), row.getNumericnumber());
119 assertEquals(new BigDecimal("7.00"), row.getDecimalnumber());
120 assertEquals((Float) 8.0f, row.getRealnumber());
121 assertEquals((Float) 9.0f, row.getFloatnumber());
122 assertEquals((Double) 10.0, row.getDoublenumber());
123 }
124 }
125
126 @Test
127 void testOgnlStaticMethodCall() {
128 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
129 List<Map<String, Object>> answer = sqlSession.selectList("org.apache.ibatis.submitted.dynsql.ognlStaticMethodCall", "Rock 'n Roll");
130 assertEquals(1, answer.size());
131 assertEquals(7, answer.get(0).get("ID"));
132 }
133 }
134
135 @Test
136 void testBindNull() {
137 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
138 DynSqlMapper mapper = sqlSession.getMapper(DynSqlMapper.class);
139 String description = mapper.selectDescription(null);
140 assertEquals("Pebbles", description);
141 }
142 }
143
144
145
146
147
148
149 @Test
150 void testValueObjectWithoutParamAnnotation() {
151 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
152 DynSqlMapper mapper = sqlSession.getMapper(DynSqlMapper.class);
153 List<String> descriptions = mapper.selectDescriptionById(3);
154 assertEquals(1, descriptions.size());
155 assertEquals("Pebbles", descriptions.get(0));
156
157 assertEquals(7, mapper.selectDescriptionById(null).size());
158 }
159 }
160
161
162
163
164 @Test
165 void testNonValueObjectWithoutParamAnnotation() {
166 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
167 DynSqlMapper mapper = sqlSession.getMapper(DynSqlMapper.class);
168 DynSqlMapper.Conditions conditions = new DynSqlMapper.Conditions();
169 conditions.setId(3);
170 List<String> descriptions = mapper.selectDescriptionByConditions(conditions);
171 assertEquals(1, descriptions.size());
172 assertEquals("Pebbles", descriptions.get(0));
173
174 assertEquals(7, mapper.selectDescriptionByConditions(null).size());
175 assertEquals(7, mapper.selectDescriptionByConditions(new DynSqlMapper.Conditions()).size());
176 }
177 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
178 DynSqlMapper mapper = sqlSession.getMapper(DynSqlMapper.class);
179 DynSqlMapper.Conditions conditions = new DynSqlMapper.Conditions();
180 conditions.setId(3);
181 try {
182 mapper.selectDescriptionByConditions2(conditions);
183 } catch (PersistenceException e) {
184 assertEquals("There is no getter for property named 'conditions' in 'class org.apache.ibatis.submitted.dynsql.DynSqlMapper$Conditions'", e.getCause().getMessage());
185 }
186 assertEquals(7, mapper.selectDescriptionByConditions2(null).size());
187 }
188 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
189 DynSqlMapper mapper = sqlSession.getMapper(DynSqlMapper.class);
190 DynSqlMapper.Conditions conditions = new DynSqlMapper.Conditions();
191 conditions.setId(3);
192 try {
193 mapper.selectDescriptionByConditions3(conditions);
194 } catch (PersistenceException e) {
195 assertEquals("There is no getter for property named 'conditions' in 'class org.apache.ibatis.submitted.dynsql.DynSqlMapper$Conditions'", e.getCause().getMessage());
196 }
197 assertEquals(7, mapper.selectDescriptionByConditions3(null).size());
198 }
199
200 }
201
202
203
204
205 @Test
206 void testCustomValueObjectWithoutParamAnnotation() throws IOException {
207 SqlSessionFactory sqlSessionFactory;
208 try (Reader configReader = Resources.getResourceAsReader("org/apache/ibatis/submitted/dynsql/MapperConfig.xml")) {
209 sqlSessionFactory = new SqlSessionFactoryBuilder().build(configReader);
210
211 sqlSessionFactory.getConfiguration().getTypeHandlerRegistry().register(DynSqlMapper.Conditions.class, new TypeHandler<DynSqlMapper.Conditions>() {
212 @Override
213 public void setParameter(PreparedStatement ps, int i, DynSqlMapper.Conditions parameter, JdbcType jdbcType) throws SQLException {
214 if (parameter.getId() != null) {
215 ps.setInt(i, parameter.getId());
216 } else {
217 ps.setNull(i, JdbcType.INTEGER.TYPE_CODE);
218 }
219 }
220 @Override
221 public DynSqlMapper.Conditions getResult(ResultSet rs, String columnName) throws SQLException {
222 return null;
223 }
224 @Override
225 public DynSqlMapper.Conditions getResult(ResultSet rs, int columnIndex) throws SQLException {
226 return null;
227 }
228 @Override
229 public DynSqlMapper.Conditions getResult(CallableStatement cs, int columnIndex) throws SQLException {
230 return null;
231 }
232 });
233 }
234 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
235 DynSqlMapper mapper = sqlSession.getMapper(DynSqlMapper.class);
236 DynSqlMapper.Conditions conditions = new DynSqlMapper.Conditions();
237 conditions.setId(3);
238 List<String> descriptions = mapper.selectDescriptionByConditions(conditions);
239 assertEquals(1, descriptions.size());
240 assertEquals("Pebbles", descriptions.get(0));
241
242 assertEquals(7, mapper.selectDescriptionByConditions(null).size());
243 assertEquals(7, mapper.selectDescriptionByConditions(new DynSqlMapper.Conditions()).size());
244 }
245 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
246 DynSqlMapper mapper = sqlSession.getMapper(DynSqlMapper.class);
247 DynSqlMapper.Conditions conditions = new DynSqlMapper.Conditions();
248 conditions.setId(3);
249 List<String> descriptions = mapper.selectDescriptionByConditions2(conditions);
250 assertEquals(1, descriptions.size());
251 assertEquals("Pebbles", descriptions.get(0));
252
253 assertEquals(7, mapper.selectDescriptionByConditions2(null).size());
254 assertEquals(0, mapper.selectDescriptionByConditions2(new DynSqlMapper.Conditions()).size());
255 }
256 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
257 DynSqlMapper mapper = sqlSession.getMapper(DynSqlMapper.class);
258 DynSqlMapper.Conditions conditions = new DynSqlMapper.Conditions();
259 conditions.setId(3);
260 List<String> descriptions = mapper.selectDescriptionByConditions3(conditions);
261 assertEquals(1, descriptions.size());
262 assertEquals("Pebbles", descriptions.get(0));
263
264 assertEquals(7, mapper.selectDescriptionByConditions3(null).size());
265 assertEquals(7, mapper.selectDescriptionByConditions3(new DynSqlMapper.Conditions()).size());
266 }
267 }
268
269 }