View Javadoc
1   /*
2    *    Copyright 2009-2024 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      @ConstructorArgs({ @Arg(column = "id", name = "noSuchConstructorArg"), })
55      @Select("select * from users ")
56      User select();
57    }
58  
59    @Test
60    void noMatchingConstructorArgName() {
61      Configuration configuration = sqlSessionFactory.getConfiguration();
62      when(() -> configuration.addMapper(NoMatchingConstructorMapper.class));
63  
64      then(caughtException()).isInstanceOf(BuilderException.class).hasMessageContaining(
65          "'org.apache.ibatis.submitted.named_constructor_args.InvalidNamedConstructorArgsTest$NoMatchingConstructorMapper.select-void'")
66          .hasMessageContaining("'org.apache.ibatis.submitted.named_constructor_args.User'")
67          .hasMessageContaining("[noSuchConstructorArg]");
68    }
69  
70    interface ConstructorWithWrongJavaType {
71      // There is a constructor with arg name 'id', but
72      // its type is different from the specified javaType.
73      @ConstructorArgs({ @Arg(column = "id", name = "id", javaType = Integer.class), })
74      @Select("select * from users ")
75      User select();
76    }
77  
78    @Test
79    void wrongJavaType() {
80      Configuration configuration = sqlSessionFactory.getConfiguration();
81      when(() -> configuration.addMapper(ConstructorWithWrongJavaType.class));
82      then(caughtException()).isInstanceOf(BuilderException.class).hasMessageContaining(
83          "'org.apache.ibatis.submitted.named_constructor_args.InvalidNamedConstructorArgsTest$ConstructorWithWrongJavaType.select-void'")
84          .hasMessageContaining("'org.apache.ibatis.submitted.named_constructor_args.User'").hasMessageContaining("[id]");
85    }
86  
87    interface ConstructorMissingRequiresJavaType {
88      // There is a constructor with arg name 'id', but its type
89      // is different from the type of a property with the same name.
90      // javaType is required in this case.
91      // Debug log shows the detail of the matching error.
92      @ConstructorArgs({ @Arg(column = "id", name = "id"), })
93      @Select("select * from users ")
94      User select();
95    }
96  
97    @Test
98    void missingRequiredJavaType() {
99      Configuration configuration = sqlSessionFactory.getConfiguration();
100     when(() -> configuration.addMapper(ConstructorMissingRequiresJavaType.class));
101     then(caughtException()).isInstanceOf(BuilderException.class).hasMessageContaining(
102         "'org.apache.ibatis.submitted.named_constructor_args.InvalidNamedConstructorArgsTest$ConstructorMissingRequiresJavaType.select-void'")
103         .hasMessageContaining("'org.apache.ibatis.submitted.named_constructor_args.User'").hasMessageContaining("[id]");
104   }
105 }