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.builder.xsd;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  import static org.junit.jupiter.api.Assertions.assertFalse;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.InputStream;
25  import java.util.Arrays;
26  import java.util.HashSet;
27  
28  import org.apache.ibatis.builder.CustomLongTypeHandler;
29  import org.apache.ibatis.builder.CustomObjectWrapperFactory;
30  import org.apache.ibatis.builder.CustomReflectorFactory;
31  import org.apache.ibatis.builder.CustomStringTypeHandler;
32  import org.apache.ibatis.builder.ExampleObjectFactory;
33  import org.apache.ibatis.builder.ExamplePlugin;
34  import org.apache.ibatis.builder.mapper.CustomMapper;
35  import org.apache.ibatis.builder.typehandler.CustomIntegerTypeHandler;
36  import org.apache.ibatis.builder.xml.XMLConfigBuilder;
37  import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
38  import org.apache.ibatis.domain.blog.Author;
39  import org.apache.ibatis.domain.blog.Blog;
40  import org.apache.ibatis.domain.blog.mappers.BlogMapper;
41  import org.apache.ibatis.domain.blog.mappers.NestedBlogMapper;
42  import org.apache.ibatis.domain.jpetstore.Cart;
43  import org.apache.ibatis.executor.loader.cglib.CglibProxyFactory;
44  import org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory;
45  import org.apache.ibatis.io.JBoss6VFS;
46  import org.apache.ibatis.io.Resources;
47  import org.apache.ibatis.logging.slf4j.Slf4jImpl;
48  import org.apache.ibatis.mapping.Environment;
49  import org.apache.ibatis.scripting.defaults.RawLanguageDriver;
50  import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver;
51  import org.apache.ibatis.session.AutoMappingBehavior;
52  import org.apache.ibatis.session.AutoMappingUnknownColumnBehavior;
53  import org.apache.ibatis.session.Configuration;
54  import org.apache.ibatis.session.ExecutorType;
55  import org.apache.ibatis.session.LocalCacheScope;
56  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
57  import org.apache.ibatis.type.JdbcType;
58  import org.junit.jupiter.api.Disabled;
59  import org.junit.jupiter.api.Test;
60  
61  @Disabled("We'll try a different approach. See #1393")
62  class XmlConfigBuilderTest {
63  
64    @Test
65    void shouldSuccessfullyLoadMinimalXMLConfigFile() throws Exception {
66      // System.setProperty(XPathParser.KEY_USE_XSD, "true");
67      String resource = "org/apache/ibatis/builder/xsd/MinimalMapperConfig.xml";
68      try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
69        XMLConfigBuilder builder = new XMLConfigBuilder(inputStream);
70        Configuration config = builder.parse();
71        assertNotNull(config);
72        assertEquals(AutoMappingBehavior.PARTIAL, config.getAutoMappingBehavior());
73        assertEquals(AutoMappingUnknownColumnBehavior.NONE, config.getAutoMappingUnknownColumnBehavior());
74        assertTrue(config.isCacheEnabled());
75        assertTrue(config.getProxyFactory() instanceof JavassistProxyFactory);
76        assertFalse(config.isLazyLoadingEnabled());
77        assertFalse(config.isAggressiveLazyLoading());
78        assertTrue(config.isMultipleResultSetsEnabled());
79        assertTrue(config.isUseColumnLabel());
80        assertFalse(config.isUseGeneratedKeys());
81        assertEquals(ExecutorType.SIMPLE, config.getDefaultExecutorType());
82        assertNull(config.getDefaultStatementTimeout());
83        assertNull(config.getDefaultFetchSize());
84        assertFalse(config.isMapUnderscoreToCamelCase());
85        assertFalse(config.isSafeRowBoundsEnabled());
86        assertEquals(LocalCacheScope.SESSION, config.getLocalCacheScope());
87        assertEquals(JdbcType.OTHER, config.getJdbcTypeForNull());
88        assertEquals(new HashSet<>(Arrays.asList("equals", "clone", "hashCode", "toString")),
89            config.getLazyLoadTriggerMethods());
90        assertTrue(config.isSafeResultHandlerEnabled());
91        assertTrue(config.getDefaultScriptingLanguageInstance() instanceof XMLLanguageDriver);
92        assertFalse(config.isCallSettersOnNulls());
93        assertNull(config.getLogPrefix());
94        assertNull(config.getLogImpl());
95        assertNull(config.getConfigurationFactory());
96        assertFalse(config.isShrinkWhitespacesInSql());
97        assertFalse(config.isArgNameBasedConstructorAutoMapping());
98      } finally {
99        // System.clearProperty(XPathParser.KEY_USE_XSD);
100     }
101   }
102 
103   @Test
104   void shouldSuccessfullyLoadXMLConfigFile() throws Exception {
105     // System.setProperty(XPathParser.KEY_USE_XSD, "true");
106     String resource = "org/apache/ibatis/builder/xsd/CustomizedSettingsMapperConfig.xml";
107     try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
108       XMLConfigBuilder builder = new XMLConfigBuilder(inputStream);
109       Configuration config = builder.parse();
110 
111       assertEquals(AutoMappingBehavior.NONE, config.getAutoMappingBehavior());
112       assertEquals(AutoMappingUnknownColumnBehavior.WARNING, config.getAutoMappingUnknownColumnBehavior());
113       assertFalse(config.isCacheEnabled());
114       assertTrue(config.getProxyFactory() instanceof CglibProxyFactory);
115       assertTrue(config.isLazyLoadingEnabled());
116       assertTrue(config.isAggressiveLazyLoading());
117       assertFalse(config.isMultipleResultSetsEnabled());
118       assertFalse(config.isUseColumnLabel());
119       assertTrue(config.isUseGeneratedKeys());
120       assertEquals(ExecutorType.BATCH, config.getDefaultExecutorType());
121       assertEquals(Integer.valueOf(10), config.getDefaultStatementTimeout());
122       assertEquals(Integer.valueOf(100), config.getDefaultFetchSize());
123       assertTrue(config.isMapUnderscoreToCamelCase());
124       assertTrue(config.isSafeRowBoundsEnabled());
125       assertEquals(LocalCacheScope.STATEMENT, config.getLocalCacheScope());
126       assertEquals(JdbcType.NULL, config.getJdbcTypeForNull());
127       assertEquals(new HashSet<>(Arrays.asList("equals", "clone", "hashCode", "toString", "xxx")),
128           config.getLazyLoadTriggerMethods());
129       assertFalse(config.isSafeResultHandlerEnabled());
130       assertTrue(config.getDefaultScriptingLanguageInstance() instanceof RawLanguageDriver);
131       assertTrue(config.isCallSettersOnNulls());
132       assertEquals("mybatis_", config.getLogPrefix());
133       assertEquals(Slf4jImpl.class.getName(), config.getLogImpl().getName());
134       assertEquals(JBoss6VFS.class.getName(), config.getVfsImpl().getName());
135       assertEquals(String.class.getName(), config.getConfigurationFactory().getName());
136       assertTrue(config.isShrinkWhitespacesInSql());
137       assertTrue(config.isArgNameBasedConstructorAutoMapping());
138 
139       assertEquals(Author.class, config.getTypeAliasRegistry().getTypeAliases().get("blogauthor"));
140       assertEquals(Blog.class, config.getTypeAliasRegistry().getTypeAliases().get("blog"));
141       assertEquals(Cart.class, config.getTypeAliasRegistry().getTypeAliases().get("cart"));
142 
143       assertTrue(config.getTypeHandlerRegistry().getTypeHandler(Integer.class) instanceof CustomIntegerTypeHandler);
144       assertTrue(config.getTypeHandlerRegistry().getTypeHandler(Long.class) instanceof CustomLongTypeHandler);
145       assertTrue(config.getTypeHandlerRegistry().getTypeHandler(String.class) instanceof CustomStringTypeHandler);
146       assertTrue(config.getTypeHandlerRegistry().getTypeHandler(String.class,
147           JdbcType.VARCHAR) instanceof CustomStringTypeHandler);
148 
149       ExampleObjectFactory objectFactory = (ExampleObjectFactory) config.getObjectFactory();
150       assertEquals(1, objectFactory.getProperties().size());
151       assertEquals("100", objectFactory.getProperties().getProperty("objectFactoryProperty"));
152 
153       assertTrue(config.getObjectWrapperFactory() instanceof CustomObjectWrapperFactory);
154 
155       assertTrue(config.getReflectorFactory() instanceof CustomReflectorFactory);
156 
157       ExamplePlugin plugin = (ExamplePlugin) config.getInterceptors().get(0);
158       assertEquals(1, plugin.getProperties().size());
159       assertEquals("100", plugin.getProperties().getProperty("pluginProperty"));
160 
161       Environment environment = config.getEnvironment();
162       assertEquals("development", environment.getId());
163       assertTrue(environment.getDataSource() instanceof UnpooledDataSource);
164       assertTrue(environment.getTransactionFactory() instanceof JdbcTransactionFactory);
165 
166       assertEquals("derby", config.getDatabaseId());
167 
168       assertEquals(4, config.getMapperRegistry().getMappers().size());
169       assertTrue(config.getMapperRegistry().hasMapper(CachedAuthorMapper.class));
170       assertTrue(config.getMapperRegistry().hasMapper(CustomMapper.class));
171       assertTrue(config.getMapperRegistry().hasMapper(BlogMapper.class));
172       assertTrue(config.getMapperRegistry().hasMapper(NestedBlogMapper.class));
173     } finally {
174       // System.clearProperty(XPathParser.KEY_USE_XSD);
175     }
176   }
177 
178 }