1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
46 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
47 "org/apache/ibatis/submitted/include_property/CreateDB.sql");
48 }
49
50 @Test
51 void simpleProperty() {
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 propertyContext() {
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 nestedDynamicValue() {
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 emptyString() {
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 propertyInRefid() {
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 configVar() {
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 runtimeVar() {
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 nestedInclude() {
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 parametersInAttribute() {
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 }