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.annotations.CacheNamespace;
23 import org.apache.ibatis.annotations.Property;
24 import org.apache.ibatis.annotations.Select;
25 import org.apache.ibatis.io.Resources;
26 import org.apache.ibatis.parsing.PropertyParser;
27 import org.apache.ibatis.session.Configuration;
28 import org.apache.ibatis.session.SqlSession;
29 import org.apache.ibatis.session.SqlSessionFactory;
30 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
31 import org.assertj.core.api.Assertions;
32 import org.junit.jupiter.api.Test;
33
34 class AnnotationMapperTest {
35
36 @Test
37 void applyDefaultValueOnAnnotationMapper() 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 configuration.addMapper(AnnotationMapper.class);
46 SupportClasses.CustomCache cache = SupportClasses.Utils.unwrap(configuration.getCache(AnnotationMapper.class.getName()));
47
48 Assertions.assertThat(cache.getName()).isEqualTo("default");
49
50 try (SqlSession sqlSession = factory.openSession()) {
51 AnnotationMapper mapper = sqlSession.getMapper(AnnotationMapper.class);
52
53 Assertions.assertThat(mapper.ping()).isEqualTo("Hello");
54 }
55
56 }
57
58 @Test
59 void applyPropertyValueOnAnnotationMapper() throws IOException {
60
61 Properties props = new Properties();
62 props.setProperty(PropertyParser.KEY_ENABLE_DEFAULT_VALUE, "true");
63 props.setProperty("ping.sql", "SELECT 'Hi' FROM INFORMATION_SCHEMA.SYSTEM_USERS");
64 props.setProperty("cache.name", "custom");
65
66 Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/global_variables_defaults/mybatis-config.xml");
67 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, props);
68 Configuration configuration = factory.getConfiguration();
69 configuration.addMapper(AnnotationMapper.class);
70 SupportClasses.CustomCache cache = SupportClasses.Utils.unwrap(configuration.getCache(AnnotationMapper.class.getName()));
71
72 Assertions.assertThat(cache.getName()).isEqualTo("custom");
73
74 try (SqlSession sqlSession = factory.openSession()) {
75 AnnotationMapper mapper = sqlSession.getMapper(AnnotationMapper.class);
76
77 Assertions.assertThat(mapper.ping()).isEqualTo("Hi");
78 }
79
80 }
81
82 @CacheNamespace(implementation = SupportClasses.CustomCache.class, properties = {
83 @Property(name = "name", value = "${cache.name:default}")
84 })
85 public interface AnnotationMapper {
86
87 @Select("${ping.sql:SELECT 'Hello' FROM INFORMATION_SCHEMA.SYSTEM_USERS}")
88 String ping();
89
90 }
91
92 }