1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package examples.animal.data;
17
18 import static org.assertj.core.api.Assertions.assertThat;
19 import static org.assertj.core.api.Assertions.fail;
20
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.sql.Connection;
24 import java.sql.DriverManager;
25 import java.sql.PreparedStatement;
26 import java.sql.ResultSet;
27 import java.sql.SQLException;
28
29 import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
30 import org.apache.ibatis.jdbc.ScriptRunner;
31 import org.apache.ibatis.mapping.Environment;
32 import org.apache.ibatis.session.Configuration;
33 import org.apache.ibatis.session.SqlSession;
34 import org.apache.ibatis.session.SqlSessionFactory;
35 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
36 import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
37 import org.junit.jupiter.api.BeforeEach;
38 import org.junit.jupiter.api.Test;
39
40
41
42
43 class BindingTest {
44
45 private static final String JDBC_URL = "jdbc:hsqldb:mem:aname";
46 private static final String JDBC_DRIVER = "org.hsqldb.jdbcDriver";
47
48 private SqlSessionFactory sqlSessionFactory;
49
50 @BeforeEach
51 void setup() throws Exception {
52 Class.forName(JDBC_DRIVER);
53 try (InputStream is = getClass().getResourceAsStream("/examples/animal/data/CreateAnimalData.sql")) {
54 assert is != null;
55 try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "");
56 InputStreamReader isr = new InputStreamReader(is)) {
57 ScriptRunner sr = new ScriptRunner(connection);
58 sr.setLogWriter(null);
59 sr.runScript(isr);
60 }
61 }
62
63 UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
64 Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
65 Configuration config = new Configuration(environment);
66 config.addMapper(AnimalDataMapper.class);
67 sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
68 }
69
70 @Test
71 void testBindInSelectList() {
72 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
73 Connection connection = sqlSession.getConnection();
74
75 PreparedStatement ps = connection.prepareStatement("select brain_weight + ? as calc from AnimalData where id = ?");
76 ps.setDouble(1, 1.0);
77 ps.setInt(2, 1);
78
79 ResultSet rs = ps.executeQuery();
80 double calculatedWeight = 0.0;
81 if (rs.next()) {
82 calculatedWeight = rs.getDouble("CALC");
83 }
84
85 rs.close();
86 ps.close();
87
88 assertThat(calculatedWeight).isEqualTo(1.005);
89 } catch (SQLException e) {
90 fail("SQL Exception", e);
91 }
92 }
93
94 @Test
95 void testBindInWeirdWhere() {
96 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
97 Connection connection = sqlSession.getConnection();
98
99 PreparedStatement ps = connection.prepareStatement("select brain_weight from AnimalData where brain_weight + ? > ? and id = ?");
100 ps.setDouble(1, 1.0);
101 ps.setDouble(2, 1.0);
102 ps.setInt(3, 1);
103
104 ResultSet rs = ps.executeQuery();
105 double calculatedWeight = 0.0;
106 if (rs.next()) {
107 calculatedWeight = rs.getDouble("BRAIN_WEIGHT");
108 }
109
110 rs.close();
111 ps.close();
112
113 assertThat(calculatedWeight).isEqualTo(.005);
114 } catch (SQLException e) {
115 fail("SQL Exception", e);
116 }
117 }
118 }