Skip to content

Commit 57ff897

Browse files
committed
Add test cases for #444 (Allow TypeHandler for Enum on annotation configuration)
1 parent a1650fb commit 57ff897

File tree

6 files changed

+356
-0
lines changed

6 files changed

+356
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--
2+
-- Copyright 2009-2015 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+
17+
create table person (
18+
id int,
19+
firstName varchar(100),
20+
lastName varchar(100),
21+
personType int -- important: Enum original number (starting from 0)
22+
);
23+
24+
INSERT INTO person (id, firstName, lastName, personType) VALUES (1, 'John', 'Smith', 0);
25+
INSERT INTO person (id, firstName, lastName, personType) VALUES (2, 'Mike', 'Jordan', 1);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright 2009-2015 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.submitted.enumtypehandler_on_annotation;
17+
18+
/**
19+
* @author Kazuki Shimizu
20+
* @since #444
21+
*/
22+
public class Employee extends Person {
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/**
2+
* Copyright 2009-2015 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.submitted.enumtypehandler_on_annotation;
17+
18+
import org.apache.ibatis.io.Resources;
19+
import org.apache.ibatis.jdbc.ScriptRunner;
20+
import org.apache.ibatis.session.SqlSession;
21+
import org.apache.ibatis.session.SqlSessionFactory;
22+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.BeforeClass;
26+
import org.junit.Test;
27+
28+
import java.io.Reader;
29+
import java.sql.Connection;
30+
import java.sql.DriverManager;
31+
32+
import static org.junit.Assert.*;
33+
import static org.hamcrest.core.Is.*;
34+
35+
/**
36+
* Tests for type handler of enum using annotations.
37+
*
38+
* @see org.apache.ibatis.annotations.Arg
39+
* @see org.apache.ibatis.annotations.Result
40+
* @see org.apache.ibatis.annotations.TypeDiscriminator
41+
*
42+
* @author Kazuki Shimizu
43+
* @since #444
44+
*/
45+
public class EnumTypeHandlerUsingAnnotationTest {
46+
47+
private static SqlSessionFactory sqlSessionFactory;
48+
private SqlSession sqlSession;
49+
50+
@BeforeClass
51+
public static void initDatabase() throws Exception {
52+
Connection conn = null;
53+
54+
try {
55+
Class.forName("org.hsqldb.jdbcDriver");
56+
conn = DriverManager.getConnection("jdbc:hsqldb:mem:enumtypehandler_on_annotation", "sa", "");
57+
58+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/enumtypehandler_on_annotation/CreateDB.sql");
59+
60+
ScriptRunner runner = new ScriptRunner(conn);
61+
runner.setLogWriter(null);
62+
runner.setErrorLogWriter(null);
63+
runner.runScript(reader);
64+
conn.commit();
65+
reader.close();
66+
67+
reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/enumtypehandler_on_annotation/mybatis-config.xml");
68+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
69+
sqlSessionFactory.getConfiguration().getMapperRegistry().addMapper(PersonMapper.class);
70+
reader.close();
71+
} finally {
72+
if (conn != null) {
73+
conn.close();
74+
}
75+
}
76+
}
77+
78+
@Before
79+
public void openSqlSession() {
80+
this.sqlSession = sqlSessionFactory.openSession();
81+
}
82+
83+
@After
84+
public void closeSqlSession() {
85+
sqlSession.close();
86+
}
87+
88+
@Test
89+
public void testForArg() {
90+
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
91+
{
92+
Person person = personMapper.findOneUsingConstructor(1);
93+
assertThat(person.getId(), is(1));
94+
assertThat(person.getFirstName(), is("John"));
95+
assertThat(person.getLastName(), is("Smith"));
96+
assertThat(person.getPersonType(), is(Person.PersonType.PERSON)); // important
97+
}
98+
{
99+
Person employee = personMapper.findOneUsingConstructor(2);
100+
assertThat(employee.getId(), is(2));
101+
assertThat(employee.getFirstName(), is("Mike"));
102+
assertThat(employee.getLastName(), is("Jordan"));
103+
assertThat(employee.getPersonType(), is(Person.PersonType.EMPLOYEE)); // important
104+
}
105+
}
106+
107+
@Test
108+
public void testForResult() {
109+
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
110+
{
111+
Person person = personMapper.findOneUsingSetter(1);
112+
assertThat(person.getId(), is(1));
113+
assertThat(person.getFirstName(), is("John"));
114+
assertThat(person.getLastName(), is("Smith"));
115+
assertThat(person.getPersonType(), is(Person.PersonType.PERSON)); // important
116+
}
117+
{
118+
Person employee = personMapper.findOneUsingSetter(2);
119+
assertThat(employee.getId(), is(2));
120+
assertThat(employee.getFirstName(), is("Mike"));
121+
assertThat(employee.getLastName(), is("Jordan"));
122+
assertThat(employee.getPersonType(), is(Person.PersonType.EMPLOYEE)); // important
123+
}
124+
}
125+
126+
@Test
127+
public void testForTypeDiscriminator() {
128+
PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
129+
{
130+
Person person = personMapper.findOneUsingTypeDiscriminator(1);
131+
assertTrue(person.getClass() == Person.class); // important
132+
assertThat(person.getId(), is(1));
133+
assertThat(person.getFirstName(), is("John"));
134+
assertThat(person.getLastName(), is("Smith"));
135+
assertThat(person.getPersonType(), is(Person.PersonType.PERSON));
136+
}
137+
{
138+
Person employee = personMapper.findOneUsingTypeDiscriminator(2);
139+
assertTrue(employee.getClass() == Employee.class); // important
140+
assertThat(employee.getId(), is(2));
141+
assertThat(employee.getFirstName(), is("Mike"));
142+
assertThat(employee.getLastName(), is("Jordan"));
143+
assertThat(employee.getPersonType(), is(Person.PersonType.EMPLOYEE));
144+
}
145+
}
146+
147+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Copyright 2009-2015 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.submitted.enumtypehandler_on_annotation;
17+
18+
/**
19+
* @author Kazuki Shimizu
20+
* @since #444
21+
*/
22+
public class Person {
23+
24+
enum PersonType {
25+
PERSON,
26+
EMPLOYEE
27+
}
28+
29+
private Integer id;
30+
private String firstName;
31+
private String lastName;
32+
private PersonType personType;
33+
34+
public Person() {
35+
}
36+
37+
public Person(Integer id, String firstName, String lastName, PersonType personType) {
38+
this.id = id;
39+
this.firstName = firstName;
40+
this.lastName = lastName;
41+
this.personType = personType;
42+
}
43+
44+
public String getFirstName() {
45+
return firstName;
46+
}
47+
public void setFirstName(String firstName) {
48+
this.firstName = firstName;
49+
}
50+
public String getLastName() {
51+
return lastName;
52+
}
53+
public void setLastName(String lastName) {
54+
this.lastName = lastName;
55+
}
56+
public Integer getId() {
57+
return id;
58+
}
59+
public void setId(Integer id) {
60+
this.id = id;
61+
}
62+
public PersonType getPersonType() {
63+
return personType;
64+
}
65+
public void setPersonType(PersonType personType) {
66+
this.personType = personType;
67+
}
68+
69+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Copyright 2009-2015 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.submitted.enumtypehandler_on_annotation;
17+
18+
import org.apache.ibatis.annotations.*;
19+
import org.apache.ibatis.submitted.enumtypehandler_on_annotation.Person.PersonType;
20+
import org.apache.ibatis.type.EnumOrdinalTypeHandler;
21+
22+
/**
23+
* @author Kazuki Shimizu
24+
* @since #444
25+
*/
26+
public interface PersonMapper {
27+
28+
@ConstructorArgs({
29+
@Arg(column = "id", javaType = Integer.class, id = true)
30+
, @Arg(column = "firstName", javaType = String.class)
31+
, @Arg(column = "lastName", javaType = String.class)
32+
// target for test (ordinal number -> Enum constant)
33+
, @Arg(column = "personType", javaType = PersonType.class, typeHandler = EnumOrdinalTypeHandler.class)
34+
})
35+
@Select("SELECT id, firstName, lastName, personType FROM person WHERE id = #{id}")
36+
Person findOneUsingConstructor(int id);
37+
38+
@Results({
39+
// target for test (ordinal number -> Enum constant)
40+
@Result(property = "personType", column = "personType", typeHandler = EnumOrdinalTypeHandler.class)
41+
})
42+
@Select("SELECT id, firstName, lastName, personType FROM person WHERE id = #{id}")
43+
Person findOneUsingSetter(int id);
44+
45+
@TypeDiscriminator(
46+
// target for test (ordinal number -> Enum constant)
47+
column = "personType", javaType = PersonType.class, typeHandler = EnumOrdinalTypeHandler.class,
48+
// Switch using enum constant name(PERSON or EMPLOYEE) at cases attribute
49+
cases = {
50+
@Case(value = "PERSON", type = Person.class, results = {@Result(property = "personType", column = "personType", typeHandler = EnumOrdinalTypeHandler.class)})
51+
, @Case(value = "EMPLOYEE", type = Employee.class, results = {@Result(property = "personType", column = "personType", typeHandler = EnumOrdinalTypeHandler.class)})
52+
})
53+
@Select("SELECT id, firstName, lastName, personType FROM person WHERE id = #{id}")
54+
Person findOneUsingTypeDiscriminator(int id);
55+
56+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<!--
3+
4+
Copyright 2009-2015 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<!DOCTYPE configuration
20+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
21+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
22+
23+
<configuration>
24+
25+
<environments default="test">
26+
<environment id="test">
27+
<transactionManager type="JDBC" />
28+
<dataSource type="UNPOOLED">
29+
<property name="driver" value="org.hsqldb.jdbcDriver"/>
30+
<property name="url" value="jdbc:hsqldb:mem:enumtypehandler_on_annotation"/>
31+
<property name="username" value="sa"/>
32+
</dataSource>
33+
</environment>
34+
</environments>
35+
36+
</configuration>

0 commit comments

Comments
 (0)