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