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