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