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.builder;
17  
18  import static org.assertj.core.api.Assertions.assertThat;
19  
20  import org.apache.ibatis.annotations.Insert;
21  import org.apache.ibatis.annotations.Options;
22  import org.apache.ibatis.annotations.Select;
23  import org.apache.ibatis.builder.annotation.MapperAnnotationBuilder;
24  import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
25  import org.apache.ibatis.mapping.MappedStatement;
26  import org.apache.ibatis.mapping.ResultSetType;
27  import org.apache.ibatis.mapping.StatementType;
28  import org.apache.ibatis.session.Configuration;
29  import org.junit.jupiter.api.Test;
30  
31  class AnnotationMapperBuilderTest {
32  
33    @Test
34    void withOptions() {
35      Configuration configuration = new Configuration();
36      MapperAnnotationBuilder builder = new MapperAnnotationBuilder(configuration, Mapper.class);
37      builder.parse();
38  
39      MappedStatement mappedStatement = configuration.getMappedStatement("selectWithOptions");
40      assertThat(mappedStatement.getFetchSize()).isEqualTo(200);
41      assertThat(mappedStatement.getTimeout()).isEqualTo(10);
42      assertThat(mappedStatement.getStatementType()).isEqualTo(StatementType.STATEMENT);
43      assertThat(mappedStatement.getResultSetType()).isEqualTo(ResultSetType.SCROLL_INSENSITIVE);
44      assertThat(mappedStatement.isFlushCacheRequired()).isTrue();
45      assertThat(mappedStatement.isUseCache()).isFalse();
46      assertThat(mappedStatement.getResultSets()).containsExactly("resultSets");
47  
48      mappedStatement = configuration.getMappedStatement("insertWithOptions");
49      assertThat(mappedStatement.getKeyGenerator()).isInstanceOf(Jdbc3KeyGenerator.class);
50      assertThat(mappedStatement.getKeyColumns()).containsExactly("key_column");
51      assertThat(mappedStatement.getKeyProperties()).containsExactly("keyProperty");
52    }
53  
54    @Test
55    void withOptionsAndWithoutOptionsAttributesWhenSpecifyDefaultValue() {
56      Configuration configuration = new Configuration();
57      configuration.setDefaultResultSetType(ResultSetType.SCROLL_INSENSITIVE);
58      MapperAnnotationBuilder builder = new MapperAnnotationBuilder(configuration, Mapper.class);
59      builder.parse();
60  
61      MappedStatement mappedStatement = configuration.getMappedStatement("selectWithOptionsAndWithoutOptionsAttributes");
62      assertThat(mappedStatement.getResultSetType()).isEqualTo(ResultSetType.SCROLL_INSENSITIVE);
63    }
64  
65    @Test
66    void withOptionsAndWithoutOptionsAttributesWhenNotSpecifyDefaultValue() {
67      Configuration configuration = new Configuration();
68      MapperAnnotationBuilder builder = new MapperAnnotationBuilder(configuration, Mapper.class);
69      builder.parse();
70  
71      MappedStatement mappedStatement = configuration.getMappedStatement("selectWithOptionsAndWithoutOptionsAttributes");
72      assertThat(mappedStatement.getResultSetType()).isEqualTo(ResultSetType.DEFAULT);
73    }
74  
75    @Test
76    void withoutOptionsWhenSpecifyDefaultValue() {
77      Configuration configuration = new Configuration();
78      configuration.setDefaultResultSetType(ResultSetType.SCROLL_INSENSITIVE);
79      MapperAnnotationBuilder builder = new MapperAnnotationBuilder(configuration, Mapper.class);
80      builder.parse();
81  
82      MappedStatement mappedStatement = configuration.getMappedStatement("selectWithoutOptions");
83      assertThat(mappedStatement.getResultSetType()).isEqualTo(ResultSetType.SCROLL_INSENSITIVE);
84    }
85  
86    @Test
87    void withoutOptionsWhenNotSpecifyDefaultValue() {
88      Configuration configuration = new Configuration();
89      MapperAnnotationBuilder builder = new MapperAnnotationBuilder(configuration, Mapper.class);
90      builder.parse();
91  
92      MappedStatement mappedStatement = configuration.getMappedStatement("selectWithoutOptions");
93      assertThat(mappedStatement.getResultSetType()).isEqualTo(ResultSetType.DEFAULT);
94    }
95  
96    interface Mapper {
97  
98      @Insert("insert into test (name) values(#{name})")
99      @Options(useGeneratedKeys = true, keyColumn = "key_column", keyProperty = "keyProperty")
100     void insertWithOptions(String name);
101 
102     @Select("select * from test")
103     @Options(fetchSize = 200, timeout = 10, statementType = StatementType.STATEMENT, resultSetType = ResultSetType.SCROLL_INSENSITIVE, flushCache = Options.FlushCachePolicy.TRUE, useCache = false, resultSets = "resultSets")
104     String selectWithOptions(Integer id);
105 
106     @Select("select * from test")
107     @Options
108     String selectWithOptionsAndWithoutOptionsAttributes(Integer id);
109 
110     @Select("select * from test")
111     String selectWithoutOptions(Integer id);
112 
113   }
114 
115 }