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