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.complex_column;
17  
18  import java.io.Reader;
19  
20  import org.apache.ibatis.BaseDataTest;
21  import org.apache.ibatis.io.Resources;
22  import org.apache.ibatis.session.SqlSession;
23  import org.apache.ibatis.session.SqlSessionFactory;
24  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
25  import org.junit.jupiter.api.Assertions;
26  import org.junit.jupiter.api.BeforeAll;
27  import org.junit.jupiter.api.Test;
28  
29  class ComplexColumnTest {
30  
31    private static SqlSessionFactory sqlSessionFactory;
32  
33    @BeforeAll
34    static void initDatabase() throws Exception {
35      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/complex_column/ibatisConfig.xml")) {
36        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
37      }
38  
39      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
40          "org/apache/ibatis/submitted/complex_column/CreateDB.sql");
41    }
42  
43    @Test
44    void testWithoutComplex() {
45      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
46        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
47        Person person = personMapper.getWithoutComplex(2L);
48        Assertions.assertNotNull(person, "person must not be null");
49        Assertions.assertEquals("Christian", person.getFirstName());
50        Assertions.assertEquals("Poitras", person.getLastName());
51        Person parent = person.getParent();
52        Assertions.assertNotNull(parent, "parent must not be null");
53        Assertions.assertEquals("John", parent.getFirstName());
54        Assertions.assertEquals("Smith", parent.getLastName());
55      }
56    }
57  
58    @Test
59    void testWithComplex() {
60      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
61        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
62        Person person = personMapper.getWithComplex(2L);
63        Assertions.assertNotNull(person, "person must not be null");
64        Assertions.assertEquals("Christian", person.getFirstName());
65        Assertions.assertEquals("Poitras", person.getLastName());
66        Person parent = person.getParent();
67        Assertions.assertNotNull(parent, "parent must not be null");
68        Assertions.assertEquals("John", parent.getFirstName());
69        Assertions.assertEquals("Smith", parent.getLastName());
70      }
71    }
72  
73    @Test
74    void testWithComplex2() {
75      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
76        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
77        Person person = personMapper.getWithComplex2(2L);
78        Assertions.assertNotNull(person, "person must not be null");
79        Assertions.assertEquals("Christian", person.getFirstName());
80        Assertions.assertEquals("Poitras", person.getLastName());
81        Person parent = person.getParent();
82        Assertions.assertNotNull(parent, "parent must not be null");
83        Assertions.assertEquals("John", parent.getFirstName());
84        Assertions.assertEquals("Smith", parent.getLastName());
85      }
86    }
87  
88    @Test
89    void testWithComplex3() {
90      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
91        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
92        Person person = personMapper.getWithComplex3(2L);
93        Assertions.assertNotNull(person, "person must not be null");
94        Assertions.assertEquals("Christian", person.getFirstName());
95        Assertions.assertEquals("Poitras", person.getLastName());
96        Person parent = person.getParent();
97        Assertions.assertNotNull(parent, "parent must not be null");
98        Assertions.assertEquals("John", parent.getFirstName());
99        Assertions.assertEquals("Smith", parent.getLastName());
100     }
101   }
102 
103   @Test
104   void testWithComplex4() {
105     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
106       PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
107       Person criteria = new Person();
108       criteria.setFirstName("Christian");
109       criteria.setLastName("Poitras");
110       Person person = personMapper.getParentWithComplex(criteria);
111       Assertions.assertNotNull(person, "person must not be null");
112       Assertions.assertEquals("Christian", person.getFirstName());
113       Assertions.assertEquals("Poitras", person.getLastName());
114       Person parent = person.getParent();
115       Assertions.assertNotNull(parent, "parent must not be null");
116       Assertions.assertEquals("John", parent.getFirstName());
117       Assertions.assertEquals("Smith", parent.getLastName());
118     }
119   }
120 
121   @Test
122   void testWithParamAttributes() {
123     try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
124       PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
125       Person person = personMapper.getComplexWithParamAttributes(2L);
126       Assertions.assertNotNull(person, "person must not be null");
127       Assertions.assertEquals("Christian", person.getFirstName());
128       Assertions.assertEquals("Poitras", person.getLastName());
129       Person parent = person.getParent();
130       Assertions.assertNotNull(parent, "parent must not be null");
131       Assertions.assertEquals("John", parent.getFirstName());
132       Assertions.assertEquals("Smith", parent.getLastName());
133     }
134   }
135 }