View Javadoc
1   /**
2    *    Copyright 2009-2020 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.session;
17  
18  import static org.assertj.core.api.Assertions.assertThat;
19  
20  import java.util.concurrent.atomic.AtomicInteger;
21  import javax.sql.DataSource;
22  
23  import org.apache.ibatis.BaseDataTest;
24  import org.apache.ibatis.annotations.Select;
25  import org.apache.ibatis.domain.blog.Author;
26  import org.apache.ibatis.exceptions.PersistenceException;
27  import org.apache.ibatis.mapping.Environment;
28  import org.apache.ibatis.transaction.TransactionFactory;
29  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
30  import org.apache.log4j.spi.LoggingEvent;
31  import org.apache.log4j.varia.NullAppender;
32  import org.junit.jupiter.api.BeforeAll;
33  import org.junit.jupiter.api.Test;
34  
35  /**
36   * Tests for specify the behavior when detects an unknown column (or unknown property type) of automatic mapping target.
37   *
38   * @since 3.4.0
39   * @author Kazuki Shimizu
40   */
41  class AutoMappingUnknownColumnBehaviorTest {
42  
43      interface Mapper {
44          @Select({
45                  "SELECT ",
46                  "  ID,",
47                  "  USERNAME as USERNAMEEEE,", // unknown column
48                  "  PASSWORD,",
49                  "  EMAIL,",
50                  "  BIO",
51                  "FROM AUTHOR WHERE ID = #{id}"})
52          Author selectAuthor(int id);
53  
54          @Select({
55                  "SELECT ",
56                  "  ID,", // unknown property type
57                  "  USERNAME",
58                  "FROM AUTHOR WHERE ID = #{id}"})
59          SimpleAuthor selectSimpleAuthor(int id);
60      }
61  
62      static class SimpleAuthor {
63          private AtomicInteger id; // unknown property type
64          private String username;
65  
66          public AtomicInteger getId() {
67              return id;
68          }
69  
70          public void setId(AtomicInteger id) {
71              this.id = id;
72          }
73  
74          public String getUsername() {
75              return username;
76          }
77  
78          public void setUsername(String username) {
79              this.username = username;
80          }
81      }
82  
83      public static class LastEventSavedAppender extends NullAppender {
84          private static LoggingEvent event;
85  
86          public void doAppend(LoggingEvent event) {
87              LastEventSavedAppender.event = event;
88          }
89      }
90  
91      private static SqlSessionFactory sqlSessionFactory;
92  
93      @BeforeAll
94      static void setup() throws Exception {
95          DataSource dataSource = BaseDataTest.createBlogDataSource();
96          TransactionFactory transactionFactory = new JdbcTransactionFactory();
97          Environment environment = new Environment("Production", transactionFactory, dataSource);
98          Configuration configuration = new Configuration(environment);
99          configuration.addMapper(Mapper.class);
100         sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
101     }
102 
103     @Test
104     void none() {
105         sqlSessionFactory.getConfiguration().setAutoMappingUnknownColumnBehavior(AutoMappingUnknownColumnBehavior.NONE);
106         try (SqlSession session = sqlSessionFactory.openSession()) {
107             Mapper mapper = session.getMapper(Mapper.class);
108             Author author = mapper.selectAuthor(101);
109             assertThat(author.getId()).isEqualTo(101);
110             assertThat(author.getUsername()).isNull();
111         }
112     }
113 
114     @Test
115     void warningCauseByUnknownPropertyType() {
116         sqlSessionFactory.getConfiguration().setAutoMappingUnknownColumnBehavior(AutoMappingUnknownColumnBehavior.WARNING);
117         try (SqlSession session = sqlSessionFactory.openSession()) {
118             Mapper mapper = session.getMapper(Mapper.class);
119             SimpleAuthor author = mapper.selectSimpleAuthor(101);
120             assertThat(author.getId()).isNull();
121             assertThat(author.getUsername()).isEqualTo("jim");
122             assertThat(LastEventSavedAppender.event.getMessage().toString()).isEqualTo("Unknown column is detected on 'org.apache.ibatis.session.AutoMappingUnknownColumnBehaviorTest$Mapper.selectSimpleAuthor' auto-mapping. Mapping parameters are [columnName=ID,propertyName=id,propertyType=java.util.concurrent.atomic.AtomicInteger]");
123         }
124     }
125 
126     @Test
127     void failingCauseByUnknownColumn() {
128         sqlSessionFactory.getConfiguration().setAutoMappingUnknownColumnBehavior(AutoMappingUnknownColumnBehavior.FAILING);
129         try (SqlSession session = sqlSessionFactory.openSession()) {
130             Mapper mapper = session.getMapper(Mapper.class);
131             mapper.selectAuthor(101);
132         } catch (PersistenceException e) {
133             assertThat(e.getCause()).isInstanceOf(SqlSessionException.class);
134             assertThat(e.getCause().getMessage()).isEqualTo("Unknown column is detected on 'org.apache.ibatis.session.AutoMappingUnknownColumnBehaviorTest$Mapper.selectAuthor' auto-mapping. Mapping parameters are [columnName=USERNAMEEEE,propertyName=USERNAMEEEE,propertyType=null]");
135         }
136     }
137 
138 }