View Javadoc
1   /*
2    *    Copyright 2009-2021 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 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  }