1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }