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