|
| 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 | +} |
0 commit comments