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.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.getResourceAsReader("org/apache/ibatis/submitted/global_variables_defaults/mybatis-config.xml");
40      SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, props);
41      Configuration configuration = factory.getConfiguration();
42      configuration.addMapper(XmlMapper.class);
43      SupportClasses.CustomCache cache = SupportClasses.Utils.unwrap(configuration.getCache(XmlMapper.class.getName()));
44  
45      Assertions.assertThat(cache.getName()).isEqualTo("default");
46  
47      try (SqlSession sqlSession = factory.openSession()) {
48        XmlMapper mapper = sqlSession.getMapper(XmlMapper.class);
49  
50        Assertions.assertThat(mapper.ping()).isEqualTo("Hello");
51        Assertions.assertThat(mapper.selectOne()).isEqualTo("1");
52        Assertions.assertThat(mapper.selectFromVariable()).isEqualTo("9999");
53      }
54  
55    }
56  
57    @Test
58    void applyPropertyValueOnXmlMapper() throws IOException {
59  
60      Properties props = new Properties();
61      props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
62      props.setProperty("ping.sql", "SELECT 'Hi' FROM INFORMATION_SCHEMA.SYSTEM_USERS");
63      props.setProperty("cache.name", "custom");
64      props.setProperty("select.columns", "'5555'");
65  
66      Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/global_variables_defaults/mybatis-config.xml");
67      SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, props);
68      Configuration configuration = factory.getConfiguration();
69      configuration.addMapper(XmlMapper.class);
70      SupportClasses.CustomCache cache = SupportClasses.Utils.unwrap(configuration.getCache(XmlMapper.class.getName()));
71  
72      Assertions.assertThat(cache.getName()).isEqualTo("custom");
73  
74      try (SqlSession sqlSession = factory.openSession()) {
75        XmlMapper mapper = sqlSession.getMapper(XmlMapper.class);
76  
77        Assertions.assertThat(mapper.ping()).isEqualTo("Hi");
78        Assertions.assertThat(mapper.selectOne()).isEqualTo("1");
79        Assertions.assertThat(mapper.selectFromVariable()).isEqualTo("5555");
80      }
81  
82    }
83  
84    public interface XmlMapper {
85  
86      String ping();
87  
88      String selectOne();
89  
90      String selectFromVariable();
91  
92    }
93  
94  }