View Javadoc
1   /*
2    *    Copyright 2009-2021 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    *       http://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.autoconstructor;
17  
18  import static org.junit.jupiter.api.Assertions.assertNotNull;
19  import static org.junit.jupiter.api.Assertions.assertThrows;
20  
21  import java.io.Reader;
22  import java.util.List;
23  
24  import org.apache.ibatis.BaseDataTest;
25  import org.apache.ibatis.exceptions.PersistenceException;
26  import org.apache.ibatis.io.Resources;
27  import org.apache.ibatis.session.SqlSession;
28  import org.apache.ibatis.session.SqlSessionFactory;
29  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
30  import org.assertj.core.api.Assertions;
31  import org.junit.jupiter.api.BeforeAll;
32  import org.junit.jupiter.api.Test;
33  
34  class AutoConstructorTest {
35    private static SqlSessionFactory sqlSessionFactory;
36  
37    @BeforeAll
38    static void setUp() throws Exception {
39      // create a SqlSessionFactory
40      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/autoconstructor/mybatis-config.xml")) {
41        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
42      }
43  
44      // populate in-memory database
45      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
46          "org/apache/ibatis/autoconstructor/CreateDB.sql");
47    }
48  
49    @Test
50    void fullyPopulatedSubject() {
51      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
52        final AutoConstructorMapper mapper = sqlSession.getMapper(AutoConstructorMapper.class);
53        final Object subject = mapper.getSubject(1);
54        assertNotNull(subject);
55      }
56    }
57  
58    @Test
59    void primitiveSubjects() {
60      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
61        final AutoConstructorMapper mapper = sqlSession.getMapper(AutoConstructorMapper.class);
62        assertThrows(PersistenceException.class, mapper::getSubjects);
63      }
64    }
65  
66    @Test
67    void annotatedSubject() {
68      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
69        final AutoConstructorMapper mapper = sqlSession.getMapper(AutoConstructorMapper.class);
70        verifySubjects(mapper.getAnnotatedSubjects());
71      }
72    }
73  
74    @Test
75    void badSubject() {
76      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
77        final AutoConstructorMapper mapper = sqlSession.getMapper(AutoConstructorMapper.class);
78        assertThrows(PersistenceException.class, mapper::getBadSubjects);
79      }
80    }
81  
82    @Test
83    void extensiveSubject() {
84      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
85        final AutoConstructorMapper mapper = sqlSession.getMapper(AutoConstructorMapper.class);
86        verifySubjects(mapper.getExtensiveSubjects());
87      }
88    }
89  
90    private void verifySubjects(final List<?> subjects) {
91      assertNotNull(subjects);
92      Assertions.assertThat(subjects.size()).isEqualTo(3);
93    }
94  }