1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }