1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.extends_with_constructor;
17
18 import static org.junit.jupiter.api.Assertions.*;
19
20 import java.util.Properties;
21
22 import org.apache.ibatis.BaseDataTest;
23 import org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory;
24 import org.apache.ibatis.mapping.Environment;
25 import org.apache.ibatis.session.AutoMappingBehavior;
26 import org.apache.ibatis.session.Configuration;
27 import org.apache.ibatis.session.SqlSession;
28 import org.apache.ibatis.session.SqlSessionFactory;
29 import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
30 import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
31 import org.junit.jupiter.api.BeforeAll;
32 import org.junit.jupiter.api.Test;
33
34
35
36
37
38
39 class NpeExtendsTest {
40
41 @BeforeAll
42 static void initDatabase() throws Exception {
43 SqlSessionFactory sqlSessionFactory = getSqlSessionFactoryWithConstructor();
44
45 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
46 "org/apache/ibatis/submitted/extends_with_constructor/CreateDB.sql");
47 }
48
49 @Test
50 void testNoConstructorConfiguration() {
51 Configuration configuration = new Configuration();
52 configuration.addMapper(StudentMapper.class);
53 configuration.addMapper(TeacherMapper.class);
54 configuration.getMappedStatementNames();
55 }
56 @Test
57 void testWithConstructorConfiguration() {
58 Configuration configuration = new Configuration();
59 configuration.addMapper(StudentConstructorMapper.class);
60 configuration.addMapper(TeacherMapper.class);
61 configuration.getMappedStatementNames();
62 }
63
64 private static SqlSessionFactory getSqlSessionFactoryWithConstructor() {
65 UnpooledDataSourceFactory unpooledDataSourceFactory = new UnpooledDataSourceFactory();
66 Properties properties = new Properties();
67 properties.setProperty("driver", "org.hsqldb.jdbcDriver");
68 properties.setProperty("url", "jdbc:hsqldb:mem:extends_with_constructor");
69 properties.setProperty("username", "sa");
70 unpooledDataSourceFactory.setProperties(properties);
71 Environment environment = new Environment("extends_with_constructor", new JdbcTransactionFactory(), unpooledDataSourceFactory.getDataSource());
72
73 Configuration configuration = new Configuration();
74 configuration.setEnvironment(environment);
75 configuration.addMapper(StudentConstructorMapper.class);
76 configuration.addMapper(TeacherMapper.class);
77 configuration.getMappedStatementNames();
78 configuration.setAutoMappingBehavior(AutoMappingBehavior.NONE);
79
80 return new DefaultSqlSessionFactory(configuration);
81 }
82 @Test
83 void testSelectWithTeacher() {
84 SqlSessionFactory sqlSessionFactory = getSqlSessionFactoryWithConstructor();
85 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
86 StudentConstructorMapper studentConstructorMapper = sqlSession.getMapper(StudentConstructorMapper.class);
87 StudentConstructor testStudent = studentConstructorMapper.selectWithTeacherById(1);
88 assertEquals(1, testStudent.getConstructors().size());
89 assertTrue(testStudent.getConstructors().contains(StudentConstructor.Constructor.ID_NAME));
90 }
91 }
92 @Test
93 void testSelectNoName() {
94 SqlSessionFactory sqlSessionFactory = getSqlSessionFactoryWithConstructor();
95 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
96 StudentConstructorMapper studentConstructorMapper = sqlSession.getMapper(StudentConstructorMapper.class);
97 StudentConstructor testStudent = studentConstructorMapper.selectNoNameById(1);
98 assertEquals(1, testStudent.getConstructors().size());
99 assertTrue(testStudent.getConstructors().contains(StudentConstructor.Constructor.ID));
100 assertNull(testStudent.getName());
101 }
102 }
103 }