1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
49 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
50 "org/apache/ibatis/submitted/named_constructor_args/CreateDB.sql");
51 }
52
53 interface NoMatchingConstructorMapper {
54
55 @ConstructorArgs({
56 @Arg(column = "id", name = "noSuchConstructorArg"),
57 })
58
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
76
77
78 @ConstructorArgs({
79 @Arg(column = "id", name = "id", javaType = Integer.class),
80 })
81
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
97
98
99
100
101 @ConstructorArgs({
102 @Arg(column = "id", name = "id"),
103 })
104
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 }