Skip to content

Commit 4aaf6eb

Browse files
committed
Add integration tests for gh-852
1 parent 78c01a8 commit 4aaf6eb

File tree

8 files changed

+619
-0
lines changed

8 files changed

+619
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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.Property;
20+
import org.apache.ibatis.annotations.Select;
21+
import org.apache.ibatis.io.Resources;
22+
import org.apache.ibatis.parsing.PropertyParser;
23+
import org.apache.ibatis.session.Configuration;
24+
import org.apache.ibatis.session.SqlSession;
25+
import org.apache.ibatis.session.SqlSessionFactory;
26+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27+
import org.hamcrest.core.Is;
28+
import org.junit.Assert;
29+
import org.junit.Test;
30+
31+
import java.io.IOException;
32+
import java.io.Reader;
33+
import java.util.Properties;
34+
35+
public class AnnotationMapperTest {
36+
37+
@Test
38+
public void applyDefaultValueOnAnnotationMapper() throws IOException {
39+
40+
Properties props = new Properties();
41+
props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
42+
43+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/global_variables_defaults/mybatis-config.xml");
44+
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, props);
45+
Configuration configuration = factory.getConfiguration();
46+
configuration.addMapper(AnnotationMapper.class);
47+
SupportClasses.CustomCache cache = SupportClasses.Utils.unwrap(configuration.getCache(AnnotationMapper.class.getName()));
48+
49+
Assert.assertThat(cache.getName(), Is.is("default"));
50+
51+
SqlSession sqlSession = factory.openSession();
52+
try {
53+
AnnotationMapper mapper = sqlSession.getMapper(AnnotationMapper.class);
54+
55+
Assert.assertThat(mapper.ping(), Is.is("Hello"));
56+
57+
} finally {
58+
sqlSession.close();
59+
}
60+
61+
}
62+
63+
@Test
64+
public void applyPropertyValueOnAnnotationMapper() throws IOException {
65+
66+
Properties props = new Properties();
67+
props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
68+
props.setProperty("ping.sql", "SELECT 'Hi' FROM INFORMATION_SCHEMA.SYSTEM_USERS");
69+
props.setProperty("cache.name", "custom");
70+
71+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/global_variables_defaults/mybatis-config.xml");
72+
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, props);
73+
Configuration configuration = factory.getConfiguration();
74+
configuration.addMapper(AnnotationMapper.class);
75+
SupportClasses.CustomCache cache = SupportClasses.Utils.unwrap(configuration.getCache(AnnotationMapper.class.getName()));
76+
77+
Assert.assertThat(cache.getName(), Is.is("custom"));
78+
79+
SqlSession sqlSession = factory.openSession();
80+
try {
81+
AnnotationMapper mapper = sqlSession.getMapper(AnnotationMapper.class);
82+
83+
Assert.assertThat(mapper.ping(), Is.is("Hi"));
84+
85+
} finally {
86+
sqlSession.close();
87+
}
88+
89+
}
90+
91+
@CacheNamespace(implementation = SupportClasses.CustomCache.class, properties = {
92+
@Property(name = "name", value = "${cache.name:default}")
93+
})
94+
public interface AnnotationMapper {
95+
96+
@Select("${ping.sql:SELECT 'Hello' FROM INFORMATION_SCHEMA.SYSTEM_USERS}")
97+
String ping();
98+
99+
}
100+
101+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.datasource.unpooled.UnpooledDataSource;
19+
import org.apache.ibatis.io.Resources;
20+
import org.apache.ibatis.parsing.PropertyParser;
21+
import org.apache.ibatis.session.Configuration;
22+
import org.apache.ibatis.session.SqlSessionFactory;
23+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
24+
import org.apache.ibatis.type.JdbcType;
25+
import org.hamcrest.core.Is;
26+
import org.hamcrest.core.IsNull;
27+
import org.junit.Assert;
28+
import org.junit.Test;
29+
30+
import java.io.IOException;
31+
import java.io.Reader;
32+
import java.util.Properties;
33+
34+
public class ConfigurationTest {
35+
36+
@Test
37+
public void applyDefaultValueOnXmlConfiguration() throws IOException {
38+
39+
Properties props = new Properties();
40+
props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
41+
42+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/global_variables_defaults/mybatis-config.xml");
43+
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, props);
44+
Configuration configuration = factory.getConfiguration();
45+
46+
Assert.assertThat(configuration.getJdbcTypeForNull(), Is.is(JdbcType.NULL));
47+
Assert.assertThat(((UnpooledDataSource) configuration.getEnvironment().getDataSource()).getUrl(),
48+
Is.is("jdbc:hsqldb:mem:global_variables_defaults"));
49+
Assert.assertThat(configuration.getDatabaseId(), Is.is("hsql"));
50+
Assert.assertThat(((SupportClasses.CustomObjectFactory) configuration.getObjectFactory()).getProperties().getProperty("name"),
51+
Is.is("default"));
52+
53+
}
54+
55+
@Test
56+
public void applyPropertyValueOnXmlConfiguration() throws IOException {
57+
58+
Properties props = new Properties();
59+
props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
60+
props.setProperty("settings.jdbcTypeForNull", JdbcType.CHAR.name());
61+
props.setProperty("db.name", "global_variables_defaults_custom");
62+
props.setProperty("productName.hsql", "Hsql");
63+
props.setProperty("objectFactory.name", "custom");
64+
65+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/global_variables_defaults/mybatis-config.xml");
66+
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, props);
67+
Configuration configuration = factory.getConfiguration();
68+
69+
Assert.assertThat(configuration.getJdbcTypeForNull(), Is.is(JdbcType.CHAR));
70+
Assert.assertThat(((UnpooledDataSource) configuration.getEnvironment().getDataSource()).getUrl(),
71+
Is.is("jdbc:hsqldb:mem:global_variables_defaults_custom"));
72+
Assert.assertThat(configuration.getDatabaseId(), IsNull.nullValue());
73+
Assert.assertThat(((SupportClasses.CustomObjectFactory) configuration.getObjectFactory()).getProperties().getProperty("name"),
74+
Is.is("custom"));
75+
76+
}
77+
78+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.cache.Cache;
19+
import org.apache.ibatis.cache.impl.PerpetualCache;
20+
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
21+
22+
import java.lang.reflect.Field;
23+
import java.util.Properties;
24+
25+
public class SupportClasses {
26+
27+
public static class CustomObjectFactory extends DefaultObjectFactory {
28+
private static final long serialVersionUID = 4576592418878031661L;
29+
private Properties properties;
30+
31+
@Override
32+
public void setProperties(Properties properties) {
33+
this.properties = properties;
34+
}
35+
36+
public Properties getProperties() {
37+
return properties;
38+
}
39+
}
40+
41+
public static class CustomCache extends PerpetualCache {
42+
private String name;
43+
44+
public CustomCache(String id) {
45+
super(id);
46+
}
47+
48+
public String getName() {
49+
return name;
50+
}
51+
52+
public void setName(String name) {
53+
this.name = name;
54+
}
55+
}
56+
57+
static class Utils {
58+
static SupportClasses.CustomCache unwrap(Cache cache) {
59+
Field field;
60+
try {
61+
field = cache.getClass().getDeclaredField("delegate");
62+
} catch (NoSuchFieldException e) {
63+
throw new IllegalStateException(e);
64+
}
65+
try {
66+
field.setAccessible(true);
67+
return (SupportClasses.CustomCache) field.get(cache);
68+
} catch (IllegalAccessException e) {
69+
throw new IllegalStateException(e);
70+
} finally {
71+
field.setAccessible(false);
72+
}
73+
}
74+
}
75+
76+
}

0 commit comments

Comments
 (0)