|
| 1 | +/** |
| 2 | + * Copyright 2009-2016 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 | + * http://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 org.apache.ibatis.annotations.CacheNamespace; |
| 19 | +import org.apache.ibatis.annotations.Param; |
| 20 | +import org.apache.ibatis.annotations.Property; |
| 21 | +import org.apache.ibatis.annotations.Select; |
| 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.SqlSession; |
| 27 | +import org.apache.ibatis.session.SqlSessionFactory; |
| 28 | +import org.apache.ibatis.session.SqlSessionFactoryBuilder; |
| 29 | +import org.apache.ibatis.type.JdbcType; |
| 30 | +import org.hamcrest.core.Is; |
| 31 | +import org.hamcrest.core.IsNull; |
| 32 | +import org.junit.Assert; |
| 33 | +import org.junit.Test; |
| 34 | + |
| 35 | +import java.io.IOException; |
| 36 | +import java.io.Reader; |
| 37 | +import java.util.Properties; |
| 38 | + |
| 39 | +public class CustomizationTest { |
| 40 | + |
| 41 | + @Test |
| 42 | + public void applyDefaultValueWhenCustomizeDefaultValueSeparator() throws IOException { |
| 43 | + |
| 44 | + Properties props = new Properties(); |
| 45 | + props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true"); |
| 46 | + props.setProperty(PropertyParser.KEY_DEFAULT_VALUE_SEPARATOR, "?:"); |
| 47 | + |
| 48 | + Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/global_variables_defaults/mybatis-config-custom-separator.xml"); |
| 49 | + SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, props); |
| 50 | + Configuration configuration = factory.getConfiguration(); |
| 51 | + configuration.addMapper(CustomDefaultValueSeparatorMapper.class); |
| 52 | + |
| 53 | + SupportClasses.CustomCache cache = SupportClasses.Utils.unwrap(configuration.getCache(CustomDefaultValueSeparatorMapper.class.getName())); |
| 54 | + |
| 55 | + Assert.assertThat(configuration.getJdbcTypeForNull(), Is.is(JdbcType.NULL)); |
| 56 | + Assert.assertThat(((UnpooledDataSource) configuration.getEnvironment().getDataSource()).getUrl(), |
| 57 | + Is.is("jdbc:hsqldb:mem:global_variables_defaults")); |
| 58 | + Assert.assertThat(configuration.getDatabaseId(), Is.is("hsql")); |
| 59 | + Assert.assertThat(((SupportClasses.CustomObjectFactory) configuration.getObjectFactory()).getProperties().getProperty("name"), |
| 60 | + Is.is("default")); |
| 61 | + Assert.assertThat(cache.getName(), Is.is("default")); |
| 62 | + |
| 63 | + SqlSession sqlSession = factory.openSession(); |
| 64 | + try { |
| 65 | + CustomDefaultValueSeparatorMapper mapper = sqlSession.getMapper(CustomDefaultValueSeparatorMapper.class); |
| 66 | + Assert.assertThat(mapper.selectValue(null), Is.is("default")); |
| 67 | + } finally { |
| 68 | + sqlSession.close(); |
| 69 | + } |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void applyPropertyValueWhenCustomizeDefaultValueSeparator() throws IOException { |
| 75 | + |
| 76 | + Properties props = new Properties(); |
| 77 | + props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true"); |
| 78 | + props.setProperty(PropertyParser.KEY_DEFAULT_VALUE_SEPARATOR, "?:"); |
| 79 | + props.setProperty("settings:jdbcTypeForNull", JdbcType.CHAR.name()); |
| 80 | + props.setProperty("db:name", "global_variables_defaults_custom"); |
| 81 | + props.setProperty("productName:hsql", "Hsql"); |
| 82 | + props.setProperty("objectFactory:name", "customObjectFactory"); |
| 83 | + props.setProperty("cache:name", "customCache"); |
| 84 | + |
| 85 | + Reader reader = Resources.getResourceAsReader("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.unwrap(configuration.getCache(CustomDefaultValueSeparatorMapper.class.getName())); |
| 91 | + |
| 92 | + Assert.assertThat(configuration.getJdbcTypeForNull(), Is.is(JdbcType.CHAR)); |
| 93 | + Assert.assertThat(((UnpooledDataSource) configuration.getEnvironment().getDataSource()).getUrl(), |
| 94 | + Is.is("jdbc:hsqldb:mem:global_variables_defaults_custom")); |
| 95 | + Assert.assertThat(configuration.getDatabaseId(), IsNull.nullValue()); |
| 96 | + Assert.assertThat(((SupportClasses.CustomObjectFactory) configuration.getObjectFactory()).getProperties().getProperty("name"), |
| 97 | + Is.is("customObjectFactory")); |
| 98 | + Assert.assertThat(cache.getName(), Is.is("customCache")); |
| 99 | + |
| 100 | + SqlSession sqlSession = factory.openSession(); |
| 101 | + try { |
| 102 | + CustomDefaultValueSeparatorMapper mapper = sqlSession.getMapper(CustomDefaultValueSeparatorMapper.class); |
| 103 | + Assert.assertThat(mapper.selectValue("3333"), Is.is("3333")); |
| 104 | + } finally { |
| 105 | + sqlSession.close(); |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + @CacheNamespace(implementation = SupportClasses.CustomCache.class, properties = { |
| 111 | + @Property(name = "name", value = "${cache:name?:default}") |
| 112 | + }) |
| 113 | + private interface CustomDefaultValueSeparatorMapper { |
| 114 | + @Select("SELECT '${val != null ? val : 'default'}' FROM INFORMATION_SCHEMA.SYSTEM_USERS") |
| 115 | + String selectValue(@Param("val") String val); |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments