1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
34
35
36
37
38
39
40
41
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);
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);
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);
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);
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);
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);
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 }