View Javadoc
1   /*
2    *    Copyright 2009-2021 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.column_prefix;
17  
18  import static org.junit.jupiter.api.Assertions.*;
19  
20  import java.io.Reader;
21  import java.util.List;
22  
23  import org.apache.ibatis.BaseDataTest;
24  import org.apache.ibatis.io.Resources;
25  import org.apache.ibatis.session.SqlSession;
26  import org.apache.ibatis.session.SqlSessionFactory;
27  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28  import org.junit.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  
31  class ColumnPrefixTest {
32  
33    protected SqlSessionFactory sqlSessionFactory;
34  
35    @BeforeEach
36    void setUp() throws Exception {
37      try (Reader reader = Resources.getResourceAsReader(getConfigPath())) {
38        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
39      }
40  
41      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
42              "org/apache/ibatis/submitted/column_prefix/CreateDB.sql");
43    }
44  
45    @Test
46    void testSelectPetAndRoom() {
47      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
48        List<Pet> pets = getPetAndRoom(sqlSession);
49        assertEquals(3, pets.size());
50        assertEquals("Ume", pets.get(0).getRoom().getRoomName());
51        assertNull(pets.get(1).getRoom());
52        assertEquals("Sakura", pets.get(2).getRoom().getRoomName());
53      }
54    }
55  
56    @Test
57    void testComplexPerson() {
58      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
59        List<Person> list = getPersons(sqlSession);
60        Person person1 = list.get(0);
61        assertEquals(Integer.valueOf(1), person1.getId());
62        assertEquals(Address.class, person1.getBillingAddress().getClass());
63        assertEquals(Integer.valueOf(10), person1.getBillingAddress().getId());
64        assertEquals("IL", person1.getBillingAddress().getState());
65        assertEquals("Chicago", person1.getBillingAddress().getCity());
66        assertEquals("Cardinal", person1.getBillingAddress().getStateBird());
67        assertEquals("IL", person1.getBillingAddress().getZip().getState());
68        assertEquals("Chicago", person1.getBillingAddress().getZip().getCity());
69        assertEquals(81, person1.getBillingAddress().getZip().getZipCode());
70        assertEquals("0123", person1.getBillingAddress().getPhone1().getPhone());
71        assertEquals("4567", person1.getBillingAddress().getPhone2().getPhone());
72        assertEquals(AddressWithCaution.class, person1.getShippingAddress().getClass());
73        assertEquals("Has a big dog.", ((AddressWithCaution) person1.getShippingAddress()).getCaution());
74        assertEquals(Integer.valueOf(11), person1.getShippingAddress().getId());
75        assertEquals("CA", person1.getShippingAddress().getState());
76        assertEquals("San Francisco", person1.getShippingAddress().getCity());
77        assertEquals("California Valley Quail", person1.getShippingAddress().getStateBird());
78        assertEquals("CA", person1.getShippingAddress().getZip().getState());
79        assertEquals(82, person1.getShippingAddress().getZip().getZipCode());
80        assertEquals("8888", person1.getShippingAddress().getPhone1().getPhone());
81        assertNull(person1.getShippingAddress().getPhone2());
82        assertEquals("Tsubaki", person1.getRoom().getRoomName());
83        assertEquals(2, person1.getPets().size());
84        assertEquals("Kotetsu", person1.getPets().get(0).getName());
85        assertEquals("Ume", person1.getPets().get(0).getRoom().getRoomName());
86        assertNull(person1.getPets().get(1).getRoom());
87        assertEquals("Chien", person1.getPets().get(1).getName());
88        Person person2 = list.get(1);
89        assertEquals(Integer.valueOf(2), person2.getId());
90        assertEquals(AddressWithCaution.class, person2.getBillingAddress().getClass());
91        assertEquals(Integer.valueOf(12), person2.getBillingAddress().getId());
92        assertEquals("No door bell.", ((AddressWithCaution) person2.getBillingAddress()).getCaution());
93        assertEquals("Los Angeles", person2.getBillingAddress().getCity());
94        assertEquals("California Valley Quail", person2.getBillingAddress().getStateBird());
95        assertEquals("Los Angeles", person2.getBillingAddress().getZip().getCity());
96        assertEquals(83, person2.getBillingAddress().getZip().getZipCode());
97        assertNull(person2.getBillingAddress().getPhone1());
98        assertNull(person2.getBillingAddress().getPhone2());
99        assertNull(person2.getShippingAddress());
100       assertEquals(0, person2.getPets().size());
101       Person person3 = list.get(2);
102       assertEquals(Integer.valueOf(3), person3.getId());
103       assertNull(person3.getBillingAddress());
104       assertEquals(Address.class, person3.getShippingAddress().getClass());
105       assertEquals(Integer.valueOf(13), person3.getShippingAddress().getId());
106       assertEquals("Dallas", person3.getShippingAddress().getCity());
107       assertEquals("Mockingbird", person3.getShippingAddress().getStateBird());
108       assertEquals("Dallas", person3.getShippingAddress().getZip().getCity());
109       assertEquals("9999", person3.getShippingAddress().getPhone1().getPhone());
110       assertEquals("4567", person3.getShippingAddress().getPhone2().getPhone());
111       assertEquals(1, person3.getPets().size());
112       assertEquals("Dodo", person3.getPets().get(0).getName());
113       assertEquals("Sakura", person3.getPets().get(0).getRoom().getRoomName());
114     }
115   }
116 
117   protected List<Pet> getPetAndRoom(SqlSession sqlSession) {
118     List<Pet> pets = sqlSession.selectList("org.apache.ibatis.submitted.column_prefix.Mapper.selectPets");
119     return pets;
120   }
121 
122   protected List<Person> getPersons(SqlSession sqlSession) {
123     List<Person> list = sqlSession.selectList("org.apache.ibatis.submitted.column_prefix.Mapper.selectPersons");
124     return list;
125   }
126 
127   protected String getConfigPath() {
128     return "org/apache/ibatis/submitted/column_prefix/Config.xml";
129   }
130 }