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.substitution_in_annots;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  
20  import org.apache.ibatis.BaseDataTest;
21  import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
22  import org.apache.ibatis.mapping.Environment;
23  import org.apache.ibatis.session.Configuration;
24  import org.apache.ibatis.session.SqlSession;
25  import org.apache.ibatis.session.SqlSessionFactory;
26  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
28  import org.junit.jupiter.api.BeforeAll;
29  import org.junit.jupiter.api.Test;
30  
31  class SubstitutionInAnnotsTest {
32  
33    protected static SqlSessionFactory sqlSessionFactory;
34  
35    @BeforeAll
36    static void setUp() throws Exception {
37      Configuration configuration = new Configuration();
38      Environment environment = new Environment("test", new JdbcTransactionFactory(), new UnpooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:annots", null));
39      configuration.setEnvironment(environment);
40      configuration.addMapper(SubstitutionInAnnotsMapper.class);
41      sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
42  
43      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
44              "org/apache/ibatis/submitted/substitution_in_annots/CreateDB.sql");
45    }
46  
47    @Test
48    void testSubstitutionWithXml() {
49      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
50        SubstitutionInAnnotsMapper mapper = sqlSession.getMapper(SubstitutionInAnnotsMapper.class);
51        assertEquals("Barney", mapper.getPersonNameByIdWithXml(4));
52      }
53    }
54  
55    @Test
56    void testSubstitutionWithAnnotsValue() {
57      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
58        SubstitutionInAnnotsMapper mapper = sqlSession.getMapper(SubstitutionInAnnotsMapper.class);
59        assertEquals("Barney", mapper.getPersonNameByIdWithAnnotsValue(4));
60      }
61    }
62  
63    @Test
64    void testSubstitutionWithAnnotsParameter() {
65      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
66        SubstitutionInAnnotsMapper mapper = sqlSession.getMapper(SubstitutionInAnnotsMapper.class);
67        assertEquals("Barney", mapper.getPersonNameByIdWithAnnotsParameter(4));
68      }
69    }
70  
71    @Test
72    void testSubstitutionWithAnnotsParamAnnot() {
73      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
74        SubstitutionInAnnotsMapper mapper = sqlSession.getMapper(SubstitutionInAnnotsMapper.class);
75        assertEquals("Barney", mapper.getPersonNameByIdWithAnnotsParamAnnot(4));
76      }
77    }
78  
79  }