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.enumtypehandler_on_annotation;
17  
18  import static org.assertj.core.api.Assertions.assertThat;
19  
20  import java.io.Reader;
21  
22  import org.apache.ibatis.BaseDataTest;
23  import org.apache.ibatis.io.Resources;
24  import org.apache.ibatis.session.SqlSession;
25  import org.apache.ibatis.session.SqlSessionFactory;
26  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27  import org.junit.jupiter.api.AfterEach;
28  import org.junit.jupiter.api.BeforeAll;
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Tests for type handler of enum using annotations.
34   *
35   * @since #444
36   *
37   * @author Kazuki Shimizu
38   *
39   * @see org.apache.ibatis.annotations.Arg
40   * @see org.apache.ibatis.annotations.Result
41   * @see org.apache.ibatis.annotations.TypeDiscriminator
42   */
43  class EnumTypeHandlerUsingAnnotationTest {
44  
45    private static SqlSessionFactory sqlSessionFactory;
46    private SqlSession sqlSession;
47  
48    @BeforeAll
49    static void initDatabase() throws Exception {
50      try (Reader reader = Resources
51          .getResourceAsReader("org/apache/ibatis/submitted/enumtypehandler_on_annotation/mybatis-config.xml")) {
52        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
53        sqlSessionFactory.getConfiguration().getMapperRegistry().addMapper(PersonMapper.class);
54      }
55  
56      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
57          "org/apache/ibatis/submitted/enumtypehandler_on_annotation/CreateDB.sql");
58    }
59  
60    @BeforeEach
61    void openSqlSession() {
62      this.sqlSession = sqlSessionFactory.openSession();
63    }
64  
65    @AfterEach
66    void closeSqlSession() {
67      sqlSession.close();
68    }
69  
70    @Test
71    void testForArg() {
72      PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
73      {
74        Person person = personMapper.findOneUsingConstructor(1);
75        assertThat(person.getId()).isEqualTo(1);
76        assertThat(person.getFirstName()).isEqualTo("John");
77        assertThat(person.getLastName()).isEqualTo("Smith");
78        assertThat(person.getPersonType()).isEqualTo(Person.PersonType.PERSON); // important
79      }
80      {
81        Person employee = personMapper.findOneUsingConstructor(2);
82        assertThat(employee.getId()).isEqualTo(2);
83        assertThat(employee.getFirstName()).isEqualTo("Mike");
84        assertThat(employee.getLastName()).isEqualTo("Jordan");
85        assertThat(employee.getPersonType()).isEqualTo(Person.PersonType.EMPLOYEE); // important
86      }
87    }
88  
89    @Test
90    void testForResult() {
91      PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
92      {
93        Person person = personMapper.findOneUsingSetter(1);
94        assertThat(person.getId()).isEqualTo(1);
95        assertThat(person.getFirstName()).isEqualTo("John");
96        assertThat(person.getLastName()).isEqualTo("Smith");
97        assertThat(person.getPersonType()).isEqualTo(Person.PersonType.PERSON); // important
98      }
99      {
100       Person employee = personMapper.findOneUsingSetter(2);
101       assertThat(employee.getId()).isEqualTo(2);
102       assertThat(employee.getFirstName()).isEqualTo("Mike");
103       assertThat(employee.getLastName()).isEqualTo("Jordan");
104       assertThat(employee.getPersonType()).isEqualTo(Person.PersonType.EMPLOYEE); // important
105     }
106   }
107 
108   @Test
109   void testForTypeDiscriminator() {
110     PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
111     {
112       Person person = personMapper.findOneUsingTypeDiscriminator(1);
113       assertThat(person.getClass()).isEqualTo(Person.class); // important
114       assertThat(person.getId()).isEqualTo(1);
115       assertThat(person.getFirstName()).isEqualTo("John");
116       assertThat(person.getLastName()).isEqualTo("Smith");
117       assertThat(person.getPersonType()).isEqualTo(Person.PersonType.PERSON);
118     }
119     {
120       Person employee = personMapper.findOneUsingTypeDiscriminator(2);
121       assertThat(employee.getClass()).isEqualTo(Employee.class); // important
122       assertThat(employee.getId()).isEqualTo(2);
123       assertThat(employee.getFirstName()).isEqualTo("Mike");
124       assertThat(employee.getLastName()).isEqualTo("Jordan");
125       assertThat(employee.getPersonType()).isEqualTo(Person.PersonType.EMPLOYEE);
126     }
127   }
128 
129 }