View Javadoc
1   /*
2    *    Copyright 2009-2024 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.isUseColumnLabel());
79        assertFalse(config.isUseGeneratedKeys());
80        assertEquals(ExecutorType.SIMPLE, config.getDefaultExecutorType());
81        assertNull(config.getDefaultStatementTimeout());
82        assertNull(config.getDefaultFetchSize());
83        assertFalse(config.isMapUnderscoreToCamelCase());
84        assertFalse(config.isSafeRowBoundsEnabled());
85        assertEquals(LocalCacheScope.SESSION, config.getLocalCacheScope());
86        assertEquals(JdbcType.OTHER, config.getJdbcTypeForNull());
87        assertEquals(new HashSet<>(Arrays.asList("equals", "clone", "hashCode", "toString")),
88            config.getLazyLoadTriggerMethods());
89        assertTrue(config.isSafeResultHandlerEnabled());
90        assertTrue(config.getDefaultScriptingLanguageInstance() instanceof XMLLanguageDriver);
91        assertFalse(config.isCallSettersOnNulls());
92        assertNull(config.getLogPrefix());
93        assertNull(config.getLogImpl());
94        assertNull(config.getConfigurationFactory());
95        assertFalse(config.isShrinkWhitespacesInSql());
96        assertFalse(config.isArgNameBasedConstructorAutoMapping());
97      } finally {
98        // System.clearProperty(XPathParser.KEY_USE_XSD);
99      }
100   }
101 
102   @Test
103   void shouldSuccessfullyLoadXMLConfigFile() throws Exception {
104     // System.setProperty(XPathParser.KEY_USE_XSD, "true");
105     String resource = "org/apache/ibatis/builder/xsd/CustomizedSettingsMapperConfig.xml";
106     try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
107       XMLConfigBuilder builder = new XMLConfigBuilder(inputStream);
108       Configuration config = builder.parse();
109 
110       assertEquals(AutoMappingBehavior.NONE, config.getAutoMappingBehavior());
111       assertEquals(AutoMappingUnknownColumnBehavior.WARNING, config.getAutoMappingUnknownColumnBehavior());
112       assertFalse(config.isCacheEnabled());
113       assertTrue(config.getProxyFactory() instanceof CglibProxyFactory);
114       assertTrue(config.isLazyLoadingEnabled());
115       assertTrue(config.isAggressiveLazyLoading());
116       assertFalse(config.isUseColumnLabel());
117       assertTrue(config.isUseGeneratedKeys());
118       assertEquals(ExecutorType.BATCH, config.getDefaultExecutorType());
119       assertEquals(Integer.valueOf(10), config.getDefaultStatementTimeout());
120       assertEquals(Integer.valueOf(100), config.getDefaultFetchSize());
121       assertTrue(config.isMapUnderscoreToCamelCase());
122       assertTrue(config.isSafeRowBoundsEnabled());
123       assertEquals(LocalCacheScope.STATEMENT, config.getLocalCacheScope());
124       assertEquals(JdbcType.NULL, config.getJdbcTypeForNull());
125       assertEquals(new HashSet<>(Arrays.asList("equals", "clone", "hashCode", "toString", "xxx")),
126           config.getLazyLoadTriggerMethods());
127       assertFalse(config.isSafeResultHandlerEnabled());
128       assertTrue(config.getDefaultScriptingLanguageInstance() instanceof RawLanguageDriver);
129       assertTrue(config.isCallSettersOnNulls());
130       assertEquals("mybatis_", config.getLogPrefix());
131       assertEquals(Slf4jImpl.class.getName(), config.getLogImpl().getName());
132       assertEquals(JBoss6VFS.class.getName(), config.getVfsImpl().getName());
133       assertEquals(String.class.getName(), config.getConfigurationFactory().getName());
134       assertTrue(config.isShrinkWhitespacesInSql());
135       assertTrue(config.isArgNameBasedConstructorAutoMapping());
136 
137       assertEquals(Author.class, config.getTypeAliasRegistry().getTypeAliases().get("blogauthor"));
138       assertEquals(Blog.class, config.getTypeAliasRegistry().getTypeAliases().get("blog"));
139       assertEquals(Cart.class, config.getTypeAliasRegistry().getTypeAliases().get("cart"));
140 
141       assertTrue(config.getTypeHandlerRegistry().getTypeHandler(Integer.class) instanceof CustomIntegerTypeHandler);
142       assertTrue(config.getTypeHandlerRegistry().getTypeHandler(Long.class) instanceof CustomLongTypeHandler);
143       assertTrue(config.getTypeHandlerRegistry().getTypeHandler(String.class) instanceof CustomStringTypeHandler);
144       assertTrue(config.getTypeHandlerRegistry().getTypeHandler(String.class,
145           JdbcType.VARCHAR) instanceof CustomStringTypeHandler);
146 
147       ExampleObjectFactory objectFactory = (ExampleObjectFactory) config.getObjectFactory();
148       assertEquals(1, objectFactory.getProperties().size());
149       assertEquals("100", objectFactory.getProperties().getProperty("objectFactoryProperty"));
150 
151       assertTrue(config.getObjectWrapperFactory() instanceof CustomObjectWrapperFactory);
152 
153       assertTrue(config.getReflectorFactory() instanceof CustomReflectorFactory);
154 
155       ExamplePlugin plugin = (ExamplePlugin) config.getInterceptors().get(0);
156       assertEquals(1, plugin.getProperties().size());
157       assertEquals("100", plugin.getProperties().getProperty("pluginProperty"));
158 
159       Environment environment = config.getEnvironment();
160       assertEquals("development", environment.getId());
161       assertTrue(environment.getDataSource() instanceof UnpooledDataSource);
162       assertTrue(environment.getTransactionFactory() instanceof JdbcTransactionFactory);
163 
164       assertEquals("derby", config.getDatabaseId());
165 
166       assertEquals(4, config.getMapperRegistry().getMappers().size());
167       assertTrue(config.getMapperRegistry().hasMapper(CachedAuthorMapper.class));
168       assertTrue(config.getMapperRegistry().hasMapper(CustomMapper.class));
169       assertTrue(config.getMapperRegistry().hasMapper(BlogMapper.class));
170       assertTrue(config.getMapperRegistry().hasMapper(NestedBlogMapper.class));
171     } finally {
172       // System.clearProperty(XPathParser.KEY_USE_XSD);
173     }
174   }
175 
176 }