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.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  }