1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.global_variables_defaults;
17
18 import java.io.IOException;
19 import java.io.Reader;
20 import java.util.Properties;
21
22 import org.apache.ibatis.io.Resources;
23 import org.apache.ibatis.parsing.PropertyParser;
24 import org.apache.ibatis.session.Configuration;
25 import org.apache.ibatis.session.SqlSession;
26 import org.apache.ibatis.session.SqlSessionFactory;
27 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28 import org.assertj.core.api.Assertions;
29 import org.junit.jupiter.api.Test;
30
31 class XmlMapperTest {
32
33 @Test
34 void applyDefaultValueOnXmlMapper() throws IOException {
35
36 Properties props = new Properties();
37 props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
38
39 Reader reader = Resources
40 .getResourceAsReader("org/apache/ibatis/submitted/global_variables_defaults/mybatis-config.xml");
41 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, props);
42 Configuration configuration = factory.getConfiguration();
43 configuration.addMapper(XmlMapper.class);
44 SupportClasses.CustomCache cache = SupportClasses.Utils.unwrap(configuration.getCache(XmlMapper.class.getName()));
45
46 Assertions.assertThat(cache.getName()).isEqualTo("default");
47
48 try (SqlSession sqlSession = factory.openSession()) {
49 XmlMapper mapper = sqlSession.getMapper(XmlMapper.class);
50
51 Assertions.assertThat(mapper.ping()).isEqualTo("Hello");
52 Assertions.assertThat(mapper.selectOne()).isEqualTo("1");
53 Assertions.assertThat(mapper.selectFromVariable()).isEqualTo("9999");
54 }
55
56 }
57
58 @Test
59 void applyPropertyValueOnXmlMapper() throws IOException {
60
61 Properties props = new Properties();
62 props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
63 props.setProperty("ping.sql", "SELECT 'Hi' FROM INFORMATION_SCHEMA.SYSTEM_USERS");
64 props.setProperty("cache.name", "custom");
65 props.setProperty("select.columns", "'5555'");
66
67 Reader reader = Resources
68 .getResourceAsReader("org/apache/ibatis/submitted/global_variables_defaults/mybatis-config.xml");
69 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, props);
70 Configuration configuration = factory.getConfiguration();
71 configuration.addMapper(XmlMapper.class);
72 SupportClasses.CustomCache cache = SupportClasses.Utils.unwrap(configuration.getCache(XmlMapper.class.getName()));
73
74 Assertions.assertThat(cache.getName()).isEqualTo("custom");
75
76 try (SqlSession sqlSession = factory.openSession()) {
77 XmlMapper mapper = sqlSession.getMapper(XmlMapper.class);
78
79 Assertions.assertThat(mapper.ping()).isEqualTo("Hi");
80 Assertions.assertThat(mapper.selectOne()).isEqualTo("1");
81 Assertions.assertThat(mapper.selectFromVariable()).isEqualTo("5555");
82 }
83
84 }
85
86 public interface XmlMapper {
87
88 String ping();
89
90 String selectOne();
91
92 String selectFromVariable();
93
94 }
95
96 }