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.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  
66    @Test
67    void withOptionsAndWithoutOptionsAttributesWhenNotSpecifyDefaultValue() {
68      Configuration configuration = new Configuration();
69      MapperAnnotationBuilder builder = new MapperAnnotationBuilder(configuration, Mapper.class);
70      builder.parse();
71  
72      MappedStatement mappedStatement = configuration.getMappedStatement("selectWithOptionsAndWithoutOptionsAttributes");
73      assertThat(mappedStatement.getResultSetType()).isEqualTo(ResultSetType.DEFAULT);
74    }
75  
76    @Test
77    void withoutOptionsWhenSpecifyDefaultValue() {
78      Configuration configuration = new Configuration();
79      configuration.setDefaultResultSetType(ResultSetType.SCROLL_INSENSITIVE);
80      MapperAnnotationBuilder builder = new MapperAnnotationBuilder(configuration, Mapper.class);
81      builder.parse();
82  
83      MappedStatement mappedStatement = configuration.getMappedStatement("selectWithoutOptions");
84      assertThat(mappedStatement.getResultSetType()).isEqualTo(ResultSetType.SCROLL_INSENSITIVE);
85    }
86  
87    @Test
88    void withoutOptionsWhenNotSpecifyDefaultValue() {
89      Configuration configuration = new Configuration();
90      MapperAnnotationBuilder builder = new MapperAnnotationBuilder(configuration, Mapper.class);
91      builder.parse();
92  
93      MappedStatement mappedStatement = configuration.getMappedStatement("selectWithoutOptions");
94      assertThat(mappedStatement.getResultSetType()).isEqualTo(ResultSetType.DEFAULT);
95    }
96  
97    interface Mapper {
98  
99      @Insert("insert into test (name) values(#{name})")
100     @Options(useGeneratedKeys = true, keyColumn = "key_column", keyProperty = "keyProperty")
101     void insertWithOptions(String name);
102 
103     @Select("select * from test")
104     @Options(fetchSize = 200, timeout = 10, statementType = StatementType.STATEMENT, resultSetType = ResultSetType.SCROLL_INSENSITIVE, flushCache = Options.FlushCachePolicy.TRUE, useCache = false, resultSets = "resultSets")
105     String selectWithOptions(Integer id);
106 
107     @Select("select * from test")
108     @Options
109     String selectWithOptionsAndWithoutOptionsAttributes(Integer id);
110 
111     @Select("select * from test")
112     String selectWithoutOptions(Integer id);
113 
114   }
115 
116 }