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.Optional;
21
22 import org.apache.ibatis.io.Resources;
23 import org.apache.ibatis.jdbc.ScriptRunner;
24 import org.apache.ibatis.mapping.Environment;
25 import org.apache.ibatis.session.Configuration;
26 import org.apache.ibatis.session.SqlSession;
27 import org.apache.ibatis.session.SqlSessionFactory;
28 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
29 import org.apache.ibatis.transaction.TransactionFactory;
30 import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
31 import org.hsqldb.jdbc.JDBCDataSource;
32 import org.junit.jupiter.api.Assertions;
33 import org.junit.jupiter.api.BeforeAll;
34 import org.junit.jupiter.api.Test;
35
36
37
38
39
40
41 class PreparedDatabaseIdParamsTest {
42 private static SqlSessionFactory sqlSessionFactory;
43
44 @BeforeAll
45 static void setUp() throws Exception {
46 Class.forName("org.hsqldb.jdbcDriver");
47
48 JDBCDataSource dataSource = new JDBCDataSource();
49 dataSource.setUrl("jdbc:hsqldb:mem:db5");
50 dataSource.setUser("sa");
51 dataSource.setPassword("");
52
53 try (Connection conn = dataSource.getConnection()) {
54 try (Reader reader = Resources.getResourceAsReader("org/mybatis/scripting/freemarker/create-db.sql")) {
55 ScriptRunner runner = new ScriptRunner(conn);
56 runner.setLogWriter(null);
57 runner.setErrorLogWriter(null);
58 runner.runScript(reader);
59 conn.commit();
60 }
61 }
62
63 TransactionFactory transactionFactory = new JdbcTransactionFactory();
64 Environment environment = new Environment("development", transactionFactory, dataSource);
65
66
67
68 Configuration configuration = new Configuration(environment);
69
70
71
72
73 configuration.setDatabaseId("hsqldb");
74
75 configuration.addMapper(PreparedDatabaseIdParamsMapper.class);
76 sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
77 }
78
79 @Test
80 void testReferDatabaseIdInTemplate() throws Exception {
81 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
82 PreparedDatabaseIdParamsMapper mapper = sqlSession.getMapper(PreparedDatabaseIdParamsMapper.class);
83 Optional<Name> nameList = mapper.getDatabaseIdTest();
84 Assertions.assertTrue(nameList.isPresent());
85 Assertions.assertEquals("Fred", nameList.orElseThrow(() -> new Exception()).getFirstName());
86 Assertions.assertEquals("Flintstone", nameList.orElseThrow(() -> new Exception()).getLastName());
87 }
88 }
89
90 @Test
91 void testReferDatabaseIdInTemplateWithParam() throws Exception {
92 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
93 PreparedDatabaseIdParamsMapper mapper = sqlSession.getMapper(PreparedDatabaseIdParamsMapper.class);
94 Optional<Name> nameList = mapper.getDatabaseIdTestWithParam(new PreparedParam());
95 Assertions.assertTrue(nameList.isPresent());
96 Assertions.assertEquals("Fred", nameList.orElseThrow(() -> new Exception()).getFirstName());
97 Assertions.assertEquals("Flintstone", nameList.orElseThrow(() -> new Exception()).getLastName());
98 }
99 }
100
101 }