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