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.annotations.CacheNamespace;
23  import org.apache.ibatis.annotations.Param;
24  import org.apache.ibatis.annotations.Property;
25  import org.apache.ibatis.annotations.Select;
26  import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
27  import org.apache.ibatis.io.Resources;
28  import org.apache.ibatis.parsing.PropertyParser;
29  import org.apache.ibatis.session.Configuration;
30  import org.apache.ibatis.session.SqlSession;
31  import org.apache.ibatis.session.SqlSessionFactory;
32  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
33  import org.apache.ibatis.type.JdbcType;
34  import org.assertj.core.api.Assertions;
35  import org.junit.jupiter.api.Test;
36  
37  class CustomizationTest {
38  
39    @Test
40    void applyDefaultValueWhenCustomizeDefaultValueSeparator() throws IOException {
41  
42      Properties props = new Properties();
43      props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
44      props.setProperty(PropertyParser.KEY_DEFAULT_VALUE_SEPARATOR, "?:");
45  
46      Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/global_variables_defaults/mybatis-config-custom-separator.xml");
47      SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, props);
48      Configuration configuration = factory.getConfiguration();
49      configuration.addMapper(CustomDefaultValueSeparatorMapper.class);
50  
51      SupportClasses.CustomCache cache = SupportClasses.Utils.unwrap(configuration.getCache(CustomDefaultValueSeparatorMapper.class.getName()));
52  
53      Assertions.assertThat(configuration.getJdbcTypeForNull()).isEqualTo(JdbcType.NULL);
54      Assertions.assertThat(((UnpooledDataSource) configuration.getEnvironment().getDataSource()).getUrl())
55          .isEqualTo("jdbc:hsqldb:mem:global_variables_defaults");
56      Assertions.assertThat(configuration.getDatabaseId()).isEqualTo("hsql");
57      Assertions.assertThat(((SupportClasses.CustomObjectFactory) configuration.getObjectFactory()).getProperties().getProperty("name"))
58          .isEqualTo("default");
59      Assertions.assertThat(cache.getName()).isEqualTo("default");
60  
61      try (SqlSession sqlSession = factory.openSession()) {
62        CustomDefaultValueSeparatorMapper mapper = sqlSession.getMapper(CustomDefaultValueSeparatorMapper.class);
63        Assertions.assertThat(mapper.selectValue(null)).isEqualTo("default");
64      }
65  
66    }
67  
68    @Test
69    void applyPropertyValueWhenCustomizeDefaultValueSeparator() throws IOException {
70  
71      Properties props = new Properties();
72      props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
73      props.setProperty(PropertyParser.KEY_DEFAULT_VALUE_SEPARATOR, "?:");
74      props.setProperty("settings:jdbcTypeForNull", JdbcType.CHAR.name());
75      props.setProperty("db:name", "global_variables_defaults_custom");
76      props.setProperty("productName:hsql", "Hsql");
77      props.setProperty("objectFactory:name", "customObjectFactory");
78      props.setProperty("cache:name", "customCache");
79  
80      Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/global_variables_defaults/mybatis-config-custom-separator.xml");
81      SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, props);
82      Configuration configuration = factory.getConfiguration();
83      configuration.addMapper(CustomDefaultValueSeparatorMapper.class);
84  
85      SupportClasses.CustomCache cache = SupportClasses.Utils.unwrap(configuration.getCache(CustomDefaultValueSeparatorMapper.class.getName()));
86  
87      Assertions.assertThat(configuration.getJdbcTypeForNull()).isEqualTo(JdbcType.CHAR);
88      Assertions.assertThat(((UnpooledDataSource) configuration.getEnvironment().getDataSource()).getUrl())
89          .isEqualTo("jdbc:hsqldb:mem:global_variables_defaults_custom");
90      Assertions.assertThat(configuration.getDatabaseId()).isNull();
91      Assertions.assertThat(((SupportClasses.CustomObjectFactory) configuration.getObjectFactory()).getProperties().getProperty("name"))
92           .isEqualTo("customObjectFactory");
93      Assertions.assertThat(cache.getName()).isEqualTo("customCache");
94  
95      try (SqlSession sqlSession = factory.openSession()) {
96        CustomDefaultValueSeparatorMapper mapper = sqlSession.getMapper(CustomDefaultValueSeparatorMapper.class);
97        Assertions.assertThat(mapper.selectValue("3333")).isEqualTo("3333");
98      }
99  
100   }
101 
102   @CacheNamespace(implementation = SupportClasses.CustomCache.class, properties = {
103       @Property(name = "name", value = "${cache:name?:default}")
104   })
105   private interface CustomDefaultValueSeparatorMapper {
106     @Select("SELECT '${val != null ? val : 'default'}' FROM INFORMATION_SCHEMA.SYSTEM_USERS")
107     String selectValue(@Param("val") String val);
108   }
109 
110 }