1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.scripting.freemarker;
17
18 import java.io.Reader;
19 import java.sql.Connection;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.apache.ibatis.exceptions.PersistenceException;
24 import org.apache.ibatis.io.Resources;
25 import org.apache.ibatis.jdbc.ScriptRunner;
26 import org.apache.ibatis.mapping.Environment;
27 import org.apache.ibatis.session.Configuration;
28 import org.apache.ibatis.session.SqlSession;
29 import org.apache.ibatis.session.SqlSessionFactory;
30 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
31 import org.apache.ibatis.transaction.TransactionFactory;
32 import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
33 import org.hsqldb.jdbc.JDBCDataSource;
34 import org.junit.jupiter.api.Assertions;
35 import org.junit.jupiter.api.BeforeAll;
36 import org.junit.jupiter.api.Test;
37
38
39
40
41
42
43 class PreparedParamsTest {
44 private static SqlSessionFactory sqlSessionFactory;
45
46 @BeforeAll
47 static void setUp() throws Exception {
48 Class.forName("org.hsqldb.jdbcDriver");
49
50 JDBCDataSource dataSource = new JDBCDataSource();
51 dataSource.setUrl("jdbc:hsqldb:mem:db3");
52 dataSource.setUser("sa");
53 dataSource.setPassword("");
54
55 try (Connection conn = dataSource.getConnection()) {
56 try (Reader reader = Resources.getResourceAsReader("org/mybatis/scripting/freemarker/create-db.sql")) {
57 ScriptRunner runner = new ScriptRunner(conn);
58 runner.setLogWriter(null);
59 runner.setErrorLogWriter(null);
60 runner.runScript(reader);
61 conn.commit();
62 }
63 }
64
65 TransactionFactory transactionFactory = new JdbcTransactionFactory();
66 Environment environment = new Environment("development", transactionFactory, dataSource);
67
68
69
70 Configuration configuration = new Configuration(environment);
71
72 configuration.addMapper(PreparedParamsMapper.class);
73 sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
74 }
75
76 @Test
77 void testInCall() {
78 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
79 PreparedParamsMapper mapper = sqlSession.getMapper(PreparedParamsMapper.class);
80 List<Name> names = mapper.findByNames(new ArrayList<String>() {
81 private static final long serialVersionUID = 1L;
82 {
83 add("Pebbles");
84 add("Barney");
85 add("Betty");
86 }
87 });
88 Assertions.assertEquals(3, names.size());
89 }
90 }
91
92
93
94
95 @Test
96 void testParamsObjectCall() {
97 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
98 final PreparedParamsMapper mapper = sqlSession.getMapper(PreparedParamsMapper.class);
99 Assertions.assertThrows(PersistenceException.class, () -> mapper.findUsingParamsObject(new PreparedParam()));
100 }
101 }
102
103 @Test
104 void testNoParamsCall() {
105 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
106 PreparedParamsMapper mapper = sqlSession.getMapper(PreparedParamsMapper.class);
107 Name name = mapper.findUsingParams(new PreparedParam.InnerClass());
108 Assertions.assertTrue(name != null && name.getFirstName().equals("Wilma"));
109 }
110 }
111 }