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.sql.DriverManager;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24
25 import org.apache.ibatis.io.Resources;
26 import org.apache.ibatis.jdbc.ScriptRunner;
27 import org.apache.ibatis.session.SqlSession;
28 import org.apache.ibatis.session.SqlSessionFactory;
29 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
30 import org.junit.jupiter.api.Assertions;
31 import org.junit.jupiter.api.BeforeAll;
32 import org.junit.jupiter.api.Test;
33
34
35
36
37
38
39 class FreeMarkerInXmlTest {
40 private static SqlSessionFactory sqlSessionFactory;
41
42 @BeforeAll
43 static void setUp() throws Exception {
44 Connection conn = null;
45
46 try {
47 Class.forName("org.hsqldb.jdbcDriver");
48 conn = DriverManager.getConnection("jdbc:hsqldb:mem:db2", "sa", "");
49
50 Reader reader = Resources.getResourceAsReader("org/mybatis/scripting/freemarker/create-db.sql");
51
52 ScriptRunner runner = new ScriptRunner(conn);
53 runner.setLogWriter(null);
54 runner.setErrorLogWriter(null);
55 runner.runScript(reader);
56 conn.commit();
57 reader.close();
58
59 reader = Resources.getResourceAsReader("org/mybatis/scripting/freemarker/mapper-config.xml");
60 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
61 reader.close();
62 } finally {
63 if (conn != null) {
64 conn.close();
65 }
66 }
67 }
68
69 @Test
70 void testNoParamsCall() {
71 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
72 List<Name> allNames = sqlSession.selectList("getAllNames");
73 Assertions.assertEquals(5, allNames.size());
74 }
75 }
76
77 @Test
78 void testMyBatisParamCall() {
79 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
80 HashMap<String, Object> paramsMap = new HashMap<>();
81 paramsMap.put("name", "Pebbles");
82 Name pebble = sqlSession.selectOne("findName", paramsMap);
83 Assertions.assertTrue(pebble != null && pebble.getFirstName().equals("Pebbles"));
84 }
85 }
86
87 @Test
88 void testInQuery() {
89 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
90 HashMap<String, Object> paramsMap = new HashMap<>();
91 paramsMap.put("ids", new ArrayList<Integer>() {
92 private static final long serialVersionUID = 1L;
93 {
94 add(1);
95 add(3);
96 add(4);
97 }
98 });
99 List<Name> namesByIds = sqlSession.selectList("findNamesByIds", paramsMap);
100 Assertions.assertEquals(3, namesByIds.size());
101 }
102 }
103
104 @Test
105 void testParamObject() {
106 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
107 Name name = sqlSession.selectOne("find", new NameParam(4));
108 Assertions.assertTrue(name != null && name.getId() == 4);
109 }
110 }
111
112 @Test
113 void testStringParam() {
114 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
115 List<Name> stdLangResult = sqlSession.selectList("getNamesOddBehaviourStdLang", "Pebbles");
116 List<Name> freeMarkerLangResult = sqlSession.selectList("getNamesOddBehaviourFreeMarkerLang", "Pebbles");
117 Assertions.assertEquals(1, stdLangResult.size());
118 Assertions.assertEquals("Pebbles", stdLangResult.get(0).getFirstName());
119 Assertions.assertEquals(1, freeMarkerLangResult.size());
120 Assertions.assertEquals("Pebbles", freeMarkerLangResult.get(0).getFirstName());
121 }
122 }
123 }