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