View Javadoc
1   /*
2    *    Copyright 2009-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.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(),
39          new UnpooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:annots", null));
40      configuration.setEnvironment(environment);
41      configuration.addMapper(SubstitutionInAnnotsMapper.class);
42      sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
43  
44      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
45          "org/apache/ibatis/submitted/substitution_in_annots/CreateDB.sql");
46    }
47  
48    @Test
49    void testSubstitutionWithXml() {
50      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
51        SubstitutionInAnnotsMapper mapper = sqlSession.getMapper(SubstitutionInAnnotsMapper.class);
52        assertEquals("Barney", mapper.getPersonNameByIdWithXml(4));
53      }
54    }
55  
56    @Test
57    void testSubstitutionWithAnnotsValue() {
58      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
59        SubstitutionInAnnotsMapper mapper = sqlSession.getMapper(SubstitutionInAnnotsMapper.class);
60        assertEquals("Barney", mapper.getPersonNameByIdWithAnnotsValue(4));
61      }
62    }
63  
64    @Test
65    void testSubstitutionWithAnnotsParameter() {
66      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
67        SubstitutionInAnnotsMapper mapper = sqlSession.getMapper(SubstitutionInAnnotsMapper.class);
68        assertEquals("Barney", mapper.getPersonNameByIdWithAnnotsParameter(4));
69      }
70    }
71  
72    @Test
73    void testSubstitutionWithAnnotsParamAnnot() {
74      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
75        SubstitutionInAnnotsMapper mapper = sqlSession.getMapper(SubstitutionInAnnotsMapper.class);
76        assertEquals("Barney", mapper.getPersonNameByIdWithAnnotsParamAnnot(4));
77      }
78    }
79  
80  }