View Javadoc
1   /*
2    *    Copyright 2009-2021 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.include_property;
17  
18  import static org.junit.jupiter.api.Assertions.*;
19  
20  import java.io.Reader;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.ibatis.BaseDataTest;
26  import org.apache.ibatis.io.Resources;
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.BeforeAll;
31  import org.junit.jupiter.api.Test;
32  
33  class IncludePropertyTest {
34  
35    private static SqlSessionFactory sqlSessionFactory;
36  
37    @BeforeAll
38    static void setUp() throws Exception {
39      // create an SqlSessionFactory
40      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/include_property/mybatis-config.xml")) {
41        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
42      }
43  
44      // populate in-memory database
45      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
46              "org/apache/ibatis/submitted/include_property/CreateDB.sql");
47    }
48  
49    @Test
50    void testSimpleProperty() {
51      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
52        List<String> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectSimpleA");
53        assertEquals("col_a value", results.get(0));
54        results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectSimpleB");
55        assertEquals("col_b value", results.get(0));
56      }
57    }
58  
59    @Test
60    void testPropertyContext() {
61      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
62        List<Map<String, String>> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectPropertyContext");
63        Map<String, String> map = results.get(0);
64        assertEquals(2, map.size());
65        assertEquals("col_a value", map.get("COL_A"));
66        assertEquals("col_b value", map.get("COL_B"));
67      }
68    }
69  
70    @Test
71    void testNestedDynamicValue() {
72      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
73        List<String> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectNestedDynamicValue");
74        assertEquals("col_a value", results.get(0));
75      }
76    }
77  
78    @Test
79    void testEmptyString() {
80      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
81        List<String> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectEmptyProperty");
82        assertEquals("a value", results.get(0));
83      }
84    }
85  
86    @Test
87    void testPropertyInRefid() {
88      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
89        List<String> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectPropertyInRefid");
90        assertEquals("col_a value", results.get(0));
91      }
92    }
93  
94    @Test
95    void testConfigVar() {
96      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
97        List<String> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectConfigVar");
98        assertEquals("col_c value", results.get(0), "Property defined in the config file should be used.");
99      }
100   }
101 
102   @Test
103   void testRuntimeVar() {
104     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
105       Map<String, String> params = new HashMap<>();
106       params.put("suffix", "b");
107       List<String> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectRuntimeVar", params);
108       assertEquals("col_b value", results.get(0));
109     }
110   }
111 
112   @Test
113   void testNestedInclude() {
114     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
115       List<String> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectNestedInclude");
116       assertEquals("a value", results.get(0));
117     }
118   }
119 
120   @Test
121   void testParametersInAttribute() {
122     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
123       List<Map<String, String>> results = sqlSession.selectList("org.apache.ibatis.submitted.include_property.Mapper.selectPropertyInAttribute");
124       Map<String, String> map = results.get(0);
125       assertEquals(2, map.size());
126       assertEquals("col_a value", map.get("COL_1"));
127       assertEquals("col_b value", map.get("COL_2"));
128     }
129   }
130 }