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