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.datasource.unpooled.UnpooledDataSource;
23  import org.apache.ibatis.io.Resources;
24  import org.apache.ibatis.parsing.PropertyParser;
25  import org.apache.ibatis.session.Configuration;
26  import org.apache.ibatis.session.SqlSessionFactory;
27  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28  import org.apache.ibatis.type.JdbcType;
29  import org.assertj.core.api.Assertions;
30  import org.junit.jupiter.api.Test;
31  
32  class ConfigurationTest {
33  
34    @Test
35    void applyDefaultValueOnXmlConfiguration() throws IOException {
36  
37      Properties props = new Properties();
38      props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
39  
40      Reader reader = Resources
41          .getResourceAsReader("org/apache/ibatis/submitted/global_variables_defaults/mybatis-config.xml");
42      SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, props);
43      Configuration configuration = factory.getConfiguration();
44  
45      Assertions.assertThat(configuration.getJdbcTypeForNull()).isEqualTo(JdbcType.NULL);
46      Assertions.assertThat(((UnpooledDataSource) configuration.getEnvironment().getDataSource()).getUrl())
47          .isEqualTo("jdbc:hsqldb:mem:global_variables_defaults");
48      Assertions.assertThat(configuration.getDatabaseId()).isEqualTo("hsql");
49      Assertions
50          .assertThat(
51              ((SupportClasses.CustomObjectFactory) configuration.getObjectFactory()).getProperties().getProperty("name"))
52          .isEqualTo("default");
53  
54    }
55  
56    @Test
57    void applyPropertyValueOnXmlConfiguration() throws IOException {
58  
59      Properties props = new Properties();
60      props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
61      props.setProperty("settings.jdbcTypeForNull", JdbcType.CHAR.name());
62      props.setProperty("db.name", "global_variables_defaults_custom");
63      props.setProperty("productName.hsql", "Hsql");
64      props.setProperty("objectFactory.name", "custom");
65  
66      Reader reader = Resources
67          .getResourceAsReader("org/apache/ibatis/submitted/global_variables_defaults/mybatis-config.xml");
68      SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, props);
69      Configuration configuration = factory.getConfiguration();
70  
71      Assertions.assertThat(configuration.getJdbcTypeForNull()).isEqualTo(JdbcType.CHAR);
72      Assertions.assertThat(((UnpooledDataSource) configuration.getEnvironment().getDataSource()).getUrl())
73          .isEqualTo("jdbc:hsqldb:mem:global_variables_defaults_custom");
74      Assertions.assertThat(configuration.getDatabaseId()).isNull();
75      Assertions
76          .assertThat(
77              ((SupportClasses.CustomObjectFactory) configuration.getObjectFactory()).getProperties().getProperty("name"))
78          .isEqualTo("custom");
79  
80    }
81  
82  }