|
| 1 | +/** |
| 2 | + * Copyright 2009-2017 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 org.apache.ibatis.annotations.Insert; |
| 19 | +import org.apache.ibatis.annotations.Options; |
| 20 | +import org.apache.ibatis.annotations.Select; |
| 21 | +import org.apache.ibatis.builder.annotation.MapperAnnotationBuilder; |
| 22 | +import org.apache.ibatis.builder.xml.XMLMapperBuilder; |
| 23 | +import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator; |
| 24 | +import org.apache.ibatis.io.Resources; |
| 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.apache.ibatis.type.TypeHandler; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +import java.io.InputStream; |
| 33 | +import java.util.regex.Pattern; |
| 34 | + |
| 35 | +import static com.googlecode.catchexception.apis.BDDCatchException.caughtException; |
| 36 | +import static com.googlecode.catchexception.apis.BDDCatchException.when; |
| 37 | +import static org.assertj.core.api.Assertions.assertThat; |
| 38 | +import static org.assertj.core.api.BDDAssertions.then; |
| 39 | + |
| 40 | +public class AnnotationMapperBuilderTest { |
| 41 | + |
| 42 | + @Test |
| 43 | + public void withOptions() throws Exception { |
| 44 | + Configuration configuration = new Configuration(); |
| 45 | + MapperAnnotationBuilder builder = new MapperAnnotationBuilder(configuration, Mapper.class); |
| 46 | + builder.parse(); |
| 47 | + |
| 48 | + MappedStatement mappedStatement = configuration.getMappedStatement("selectWithOptions"); |
| 49 | + assertThat(mappedStatement.getFetchSize()).isEqualTo(200); |
| 50 | + assertThat(mappedStatement.getTimeout()).isEqualTo(10); |
| 51 | + assertThat(mappedStatement.getStatementType()).isEqualTo(StatementType.STATEMENT); |
| 52 | + assertThat(mappedStatement.getResultSetType()).isEqualTo(ResultSetType.SCROLL_INSENSITIVE); |
| 53 | + assertThat(mappedStatement.isFlushCacheRequired()).isTrue(); |
| 54 | + assertThat(mappedStatement.isUseCache()).isFalse(); |
| 55 | + assertThat(mappedStatement.getResultSets()).containsExactly("resultSets"); |
| 56 | + |
| 57 | + mappedStatement = configuration.getMappedStatement("insertWithOptions"); |
| 58 | + assertThat(mappedStatement.getKeyGenerator()).isInstanceOf(Jdbc3KeyGenerator.class); |
| 59 | + assertThat(mappedStatement.getKeyColumns()).containsExactly("key_column"); |
| 60 | + assertThat(mappedStatement.getKeyProperties()).containsExactly("keyProperty"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void withOptionsAndWithoutOptionsAttributesWhenSpecifyDefaultValue() throws Exception { |
| 65 | + Configuration configuration = new Configuration(); |
| 66 | + configuration.setDefaultResultSetType(ResultSetType.SCROLL_INSENSITIVE); |
| 67 | + MapperAnnotationBuilder builder = new MapperAnnotationBuilder(configuration, Mapper.class); |
| 68 | + builder.parse(); |
| 69 | + |
| 70 | + MappedStatement mappedStatement = configuration.getMappedStatement("selectWithOptionsAndWithoutOptionsAttributes"); |
| 71 | + assertThat(mappedStatement.getResultSetType()).isEqualTo(ResultSetType.SCROLL_INSENSITIVE); |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + @Test |
| 76 | + public void withOptionsAndWithoutOptionsAttributesWhenNotSpecifyDefaultValue() throws Exception { |
| 77 | + Configuration configuration = new Configuration(); |
| 78 | + MapperAnnotationBuilder builder = new MapperAnnotationBuilder(configuration, Mapper.class); |
| 79 | + builder.parse(); |
| 80 | + |
| 81 | + MappedStatement mappedStatement = configuration.getMappedStatement("selectWithOptionsAndWithoutOptionsAttributes"); |
| 82 | + assertThat(mappedStatement.getResultSetType()).isNull(); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void withoutOptionsWhenSpecifyDefaultValue() throws Exception { |
| 87 | + Configuration configuration = new Configuration(); |
| 88 | + configuration.setDefaultResultSetType(ResultSetType.SCROLL_INSENSITIVE); |
| 89 | + MapperAnnotationBuilder builder = new MapperAnnotationBuilder(configuration, Mapper.class); |
| 90 | + builder.parse(); |
| 91 | + |
| 92 | + MappedStatement mappedStatement = configuration.getMappedStatement("selectWithoutOptions"); |
| 93 | + assertThat(mappedStatement.getResultSetType()).isEqualTo(ResultSetType.SCROLL_INSENSITIVE); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void withoutOptionsWhenNotSpecifyDefaultValue() throws Exception { |
| 98 | + Configuration configuration = new Configuration(); |
| 99 | + MapperAnnotationBuilder builder = new MapperAnnotationBuilder(configuration, Mapper.class); |
| 100 | + builder.parse(); |
| 101 | + |
| 102 | + MappedStatement mappedStatement = configuration.getMappedStatement("selectWithoutOptions"); |
| 103 | + assertThat(mappedStatement.getResultSetType()).isNull(); |
| 104 | + } |
| 105 | + |
| 106 | + public interface Mapper { |
| 107 | + |
| 108 | + @Insert("insert into test (name) values(#{name})") |
| 109 | + @Options(useGeneratedKeys = true, keyColumn = "key_column", keyProperty = "keyProperty") |
| 110 | + void insertWithOptions(String name); |
| 111 | + |
| 112 | + @Select("select * from test") |
| 113 | + @Options(fetchSize = 200, timeout = 10, statementType = StatementType.STATEMENT, resultSetType = ResultSetType.SCROLL_INSENSITIVE, flushCache = Options.FlushCachePolicy.TRUE, useCache = false, resultSets = "resultSets") |
| 114 | + String selectWithOptions(Integer id); |
| 115 | + |
| 116 | + @Select("select * from test") |
| 117 | + @Options |
| 118 | + String selectWithOptionsAndWithoutOptionsAttributes(Integer id); |
| 119 | + |
| 120 | + @Select("select * from test") |
| 121 | + String selectWithoutOptions(Integer id); |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments