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.submitted.named_constructor_args;
17  
18  import static com.googlecode.catchexception.apis.BDDCatchException.*;
19  import static org.assertj.core.api.BDDAssertions.then;
20  
21  import java.io.Reader;
22  
23  import org.apache.ibatis.BaseDataTest;
24  import org.apache.ibatis.annotations.Arg;
25  import org.apache.ibatis.annotations.ConstructorArgs;
26  import org.apache.ibatis.annotations.Select;
27  import org.apache.ibatis.builder.BuilderException;
28  import org.apache.ibatis.io.Resources;
29  import org.apache.ibatis.session.Configuration;
30  import org.apache.ibatis.session.SqlSessionFactory;
31  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
32  import org.junit.jupiter.api.BeforeAll;
33  import org.junit.jupiter.api.Test;
34  
35  class InvalidNamedConstructorArgsTest {
36  
37    private static SqlSessionFactory sqlSessionFactory;
38  
39    @BeforeAll
40    static void setUp() throws Exception {
41      // create an SqlSessionFactory
42      try (Reader reader = Resources
43          .getResourceAsReader("org/apache/ibatis/submitted/named_constructor_args/mybatis-config.xml")) {
44        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
45      }
46  
47      // populate in-memory database
48      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
49              "org/apache/ibatis/submitted/named_constructor_args/CreateDB.sql");
50    }
51  
52    interface NoMatchingConstructorMapper {
53      @ConstructorArgs({
54          @Arg(column = "id", name = "noSuchConstructorArg"),
55      })
56      @Select("select * from users ")
57      User select();
58    }
59  
60    @Test
61    void noMatchingConstructorArgName() {
62      Configuration configuration = sqlSessionFactory.getConfiguration();
63      when(() -> configuration.addMapper(NoMatchingConstructorMapper.class));
64  
65      then(caughtException()).isInstanceOf(BuilderException.class)
66        .hasMessageContaining(
67            "'org.apache.ibatis.submitted.named_constructor_args.InvalidNamedConstructorArgsTest$NoMatchingConstructorMapper.select-void'")
68        .hasMessageContaining("'org.apache.ibatis.submitted.named_constructor_args.User'")
69        .hasMessageContaining("[noSuchConstructorArg]");
70    }
71  
72    interface ConstructorWithWrongJavaType {
73      // There is a constructor with arg name 'id', but
74      // its type is different from the specified javaType.
75      @ConstructorArgs({
76          @Arg(column = "id", name = "id", javaType = Integer.class),
77      })
78      @Select("select * from users ")
79      User select();
80    }
81  
82    @Test
83    void wrongJavaType() {
84      Configuration configuration = sqlSessionFactory.getConfiguration();
85      when(() -> configuration.addMapper(ConstructorWithWrongJavaType.class));
86      then(caughtException()).isInstanceOf(BuilderException.class)
87        .hasMessageContaining(
88            "'org.apache.ibatis.submitted.named_constructor_args.InvalidNamedConstructorArgsTest$ConstructorWithWrongJavaType.select-void'")
89        .hasMessageContaining("'org.apache.ibatis.submitted.named_constructor_args.User'")
90        .hasMessageContaining("[id]");
91    }
92  
93    interface ConstructorMissingRequiresJavaType {
94      // There is a constructor with arg name 'id', but its type
95      // is different from the type of a property with the same name.
96      // javaType is required in this case.
97      // Debug log shows the detail of the matching error.
98      @ConstructorArgs({
99          @Arg(column = "id", name = "id"),
100     })
101     @Select("select * from users ")
102     User select();
103   }
104 
105   @Test
106   void missingRequiredJavaType() {
107     Configuration configuration = sqlSessionFactory.getConfiguration();
108     when(() -> configuration.addMapper(ConstructorMissingRequiresJavaType.class));
109     then(caughtException()).isInstanceOf(BuilderException.class)
110       .hasMessageContaining(
111             "'org.apache.ibatis.submitted.named_constructor_args.InvalidNamedConstructorArgsTest$ConstructorMissingRequiresJavaType.select-void'")
112       .hasMessageContaining("'org.apache.ibatis.submitted.named_constructor_args.User'")
113       .hasMessageContaining("[id]");
114   }
115 }