View Javadoc
1   /*
2    *    Copyright 2015-2022 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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.io.Resources;
24  import org.apache.ibatis.jdbc.ScriptRunner;
25  import org.apache.ibatis.mapping.Environment;
26  import org.apache.ibatis.session.Configuration;
27  import org.apache.ibatis.session.SqlSession;
28  import org.apache.ibatis.session.SqlSessionFactory;
29  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
30  import org.apache.ibatis.transaction.TransactionFactory;
31  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
32  import org.hsqldb.jdbc.JDBCDataSource;
33  import org.junit.jupiter.api.Assertions;
34  import org.junit.jupiter.api.BeforeAll;
35  import org.junit.jupiter.api.Test;
36  
37  /**
38   * Test of using FreeMarker in annotations-driven mapper.
39   *
40   * @author elwood
41   */
42  class FreeMarkerInAnnotationsTest {
43    private static SqlSessionFactory sqlSessionFactory;
44  
45    @BeforeAll
46    static void setUp() throws Exception {
47      Class.forName("org.hsqldb.jdbcDriver");
48  
49      JDBCDataSource dataSource = new JDBCDataSource();
50      dataSource.setUrl("jdbc:hsqldb:mem:db1");
51      dataSource.setUser("sa");
52      dataSource.setPassword("");
53  
54      try (Connection conn = dataSource.getConnection()) {
55        try (Reader reader = Resources.getResourceAsReader("org/mybatis/scripting/freemarker/create-db.sql")) {
56          ScriptRunner runner = new ScriptRunner(conn);
57          runner.setLogWriter(null);
58          runner.setErrorLogWriter(null);
59          runner.runScript(reader);
60          conn.commit();
61        }
62      }
63  
64      TransactionFactory transactionFactory = new JdbcTransactionFactory();
65      Environment environment = new Environment("development", transactionFactory, dataSource);
66  
67      // You can call configuration.setDefaultScriptingLanguage(FreeMarkerLanguageDriver.class)
68      // after this to use FreeMarker driver by default.
69      Configuration configuration = new Configuration(environment);
70  
71      configuration.addMapper(NameMapper.class);
72      sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
73    }
74  
75    @Test
76    void testNoParamsCall() {
77      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
78        NameMapper mapper = sqlSession.getMapper(NameMapper.class);
79        List<Name> allNames = mapper.getAllNames();
80        Assertions.assertEquals(5, allNames.size());
81      }
82    }
83  
84    @Test
85    void testMyBatisParamCall() {
86      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
87        NameMapper mapper = sqlSession.getMapper(NameMapper.class);
88        Name pebble = mapper.findName("Pebbles");
89        Assertions.assertTrue(pebble != null && pebble.getFirstName().equals("Pebbles"));
90      }
91    }
92  
93    @Test
94    void testInQuery() {
95      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
96        NameMapper mapper = sqlSession.getMapper(NameMapper.class);
97        List<Name> namesByIds = mapper.findNamesByIds(new ArrayList<Integer>() {
98          private static final long serialVersionUID = 1L;
99          {
100           add(1);
101           add(3);
102           add(4);
103         }
104       });
105       Assertions.assertEquals(3, namesByIds.size());
106     }
107   }
108 
109   @Test
110   void testParamObject() {
111     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
112       NameMapper mapper = sqlSession.getMapper(NameMapper.class);
113       Name name = mapper.find(new NameParam(4));
114       Assertions.assertTrue(name != null && name.getId() == 4);
115     }
116   }
117 
118   @Test
119   void testStringParam() {
120     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
121       NameMapper mapper = sqlSession.getMapper(NameMapper.class);
122       List<Name> stdLangResult = mapper.getNamesOddBehaviourStdLang("Pebbles");
123       List<Name> freeMarkerLangResult = mapper.getNamesOddBehaviourFreeMarkerLang("Pebbles");
124       Assertions.assertEquals(1, stdLangResult.size());
125       Assertions.assertEquals("Pebbles", stdLangResult.get(0).getFirstName());
126       Assertions.assertEquals(1, freeMarkerLangResult.size());
127       Assertions.assertEquals("Pebbles", freeMarkerLangResult.get(0).getFirstName());
128     }
129   }
130 }