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.emptycollection;
17  
18  import java.io.Reader;
19  import java.sql.Connection;
20  import java.util.List;
21  
22  import org.apache.ibatis.io.Resources;
23  import org.apache.ibatis.jdbc.ScriptRunner;
24  import org.apache.ibatis.session.SqlSession;
25  import org.apache.ibatis.session.SqlSessionFactory;
26  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
27  import org.junit.jupiter.api.AfterEach;
28  import org.junit.jupiter.api.Assertions;
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.Test;
31  
32  class DaoTest {
33    private Connection conn;
34    private Dao dao;
35    private SqlSession sqlSession;
36  
37    @BeforeEach
38    void setUp() throws Exception {
39      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/emptycollection/mybatis-config.xml")) {
40        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
41        sqlSession = sqlSessionFactory.openSession();
42      }
43      conn = sqlSession.getConnection();
44      ScriptRunner runner = new ScriptRunner(conn);
45      runner.setLogWriter(null);
46      dao = sqlSession.getMapper(Dao.class);
47    }
48  
49    @AfterEach
50    void tearDown() throws Exception {
51      conn.close();
52      sqlSession.close();
53    }
54  
55    @Test
56    void testWithEmptyList() {
57      final List<TodoLists> actual = dao.selectWithEmptyList();
58      Assertions.assertEquals(1, actual.size());
59      final List<TodoItem> todoItems = actual.get(0).getTodoItems();
60      Assertions.assertEquals(0, todoItems.size(), "expect " + todoItems + " to be empty");
61    }
62  
63    @Test
64    void testWithNonEmptyList() {
65      final List<TodoLists> actual = dao.selectWithNonEmptyList();
66      checkNonEmptyList(actual);
67    }
68  
69    @Test
70    void testWithNonEmptyList_noCollectionId() {
71      final List<TodoLists> actual = dao.selectWithNonEmptyList_noCollectionId();
72  
73      checkNonEmptyList(actual);
74    }
75  
76    private void checkNonEmptyList(final List<TodoLists> actual) {
77  //  Assertions.assertEquals("[List(1)=[a description(1), a 2nd description(2)], List(2)=[a description(1)]]", actual.toString());
78      Assertions.assertEquals(2, actual.size());
79  
80      Assertions.assertEquals(2, actual.get(0).getTodoItems().size());
81      Assertions.assertEquals(1, actual.get(0).getTodoItems().get(0).getOrder());
82      Assertions.assertEquals("a description", actual.get(0).getTodoItems().get(0).getDescription().trim());
83      Assertions.assertEquals(2, actual.get(0).getTodoItems().get(1).getOrder());
84      Assertions.assertEquals("a 2nd description", actual.get(0).getTodoItems().get(1).getDescription().trim());
85  
86      Assertions.assertEquals(1, actual.get(1).getTodoItems().size());
87      Assertions.assertEquals(1, actual.get(1).getTodoItems().get(0).getOrder());
88      Assertions.assertEquals("a description", actual.get(0).getTodoItems().get(0).getDescription().trim());
89  
90      // We should have gotten three item objects. The first item from the first list and the first item from
91      // the second list have identical properties, but they should be distinct objects
92      Assertions.assertNotSame(actual.get(0).getTodoItems().get(0), actual.get(1).getTodoItems().get(0));
93    }
94  }