View Javadoc
1   /**
2    *    Copyright 2009-2019 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    *       http://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.apache.ibatis.submitted.raw_sql_source;
17  
18  import java.io.Reader;
19  
20  import org.apache.ibatis.BaseDataTest;
21  import org.apache.ibatis.io.Resources;
22  import org.apache.ibatis.mapping.SqlSource;
23  import org.apache.ibatis.scripting.defaults.RawSqlSource;
24  import org.apache.ibatis.scripting.xmltags.DynamicSqlSource;
25  import org.apache.ibatis.session.SqlSession;
26  import org.apache.ibatis.session.SqlSessionFactory;
27  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28  import org.junit.jupiter.api.Assertions;
29  import org.junit.jupiter.api.BeforeAll;
30  import org.junit.jupiter.api.Test;
31  
32  class RawSqlSourceTest {
33  
34    private static SqlSessionFactory sqlSessionFactory;
35  
36    @BeforeAll
37    static void setUp() throws Exception {
38      // create an SqlSessionFactory
39      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/raw_sql_source/mybatis-config.xml")) {
40        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
41      }
42  
43      // populate in-memory database
44      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
45              "org/apache/ibatis/submitted/raw_sql_source/CreateDB.sql");
46    }
47  
48    @Test
49    void shouldUseRawSqlSourceForAnStaticStatement() {
50      test("getUser1", RawSqlSource.class);
51    }
52  
53    @Test
54    void shouldUseDynamicSqlSourceForAnStatementWithInlineArguments() {
55      test("getUser2", DynamicSqlSource.class);
56    }
57  
58    @Test
59    void shouldUseDynamicSqlSourceForAnStatementWithXmlTags() {
60      test("getUser3", DynamicSqlSource.class);
61    }
62  
63    private void test(String statement, Class<? extends SqlSource> sqlSource) {
64      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
65        Assertions.assertEquals(sqlSource, sqlSession.getConfiguration().getMappedStatement(statement).getSqlSource().getClass());
66        String sql = sqlSession.getConfiguration().getMappedStatement(statement).getSqlSource().getBoundSql('?').getSql();
67        Assertions.assertEquals("select * from users where id = ?", sql);
68        User user = sqlSession.selectOne(statement, 1);
69        Assertions.assertEquals("User1", user.getName());
70      }
71    }
72  
73  }