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.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
40          .getResourceAsReader("org/apache/ibatis/submitted/emptycollection/mybatis-config.xml")) {
41        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
42        sqlSession = sqlSessionFactory.openSession();
43      }
44      conn = sqlSession.getConnection();
45      ScriptRunner runner = new ScriptRunner(conn);
46      runner.setLogWriter(null);
47      dao = sqlSession.getMapper(Dao.class);
48    }
49  
50    @AfterEach
51    void tearDown() throws Exception {
52      conn.close();
53      sqlSession.close();
54    }
55  
56    @Test
57    void testWithEmptyList() {
58      final List<TodoLists> actual = dao.selectWithEmptyList();
59      Assertions.assertEquals(1, actual.size());
60      final List<TodoItem> todoItems = actual.get(0).getTodoItems();
61      Assertions.assertEquals(0, todoItems.size(), "expect " + todoItems + " to be empty");
62    }
63  
64    @Test
65    void testWithNonEmptyList() {
66      final List<TodoLists> actual = dao.selectWithNonEmptyList();
67      checkNonEmptyList(actual);
68    }
69  
70    @Test
71    void testWithNonEmptyList_noCollectionId() {
72      final List<TodoLists> actual = dao.selectWithNonEmptyList_noCollectionId();
73  
74      checkNonEmptyList(actual);
75    }
76  
77    private void checkNonEmptyList(final List<TodoLists> actual) {
78      // Assertions.assertEquals("[List(1)=[a description(1), a 2nd description(2)], List(2)=[a description(1)]]",
79      // actual.toString());
80      Assertions.assertEquals(2, actual.size());
81  
82      Assertions.assertEquals(2, actual.get(0).getTodoItems().size());
83      Assertions.assertEquals(1, actual.get(0).getTodoItems().get(0).getOrder());
84      Assertions.assertEquals("a description", actual.get(0).getTodoItems().get(0).getDescription().trim());
85      Assertions.assertEquals(2, actual.get(0).getTodoItems().get(1).getOrder());
86      Assertions.assertEquals("a 2nd description", actual.get(0).getTodoItems().get(1).getDescription().trim());
87  
88      Assertions.assertEquals(1, actual.get(1).getTodoItems().size());
89      Assertions.assertEquals(1, actual.get(1).getTodoItems().get(0).getOrder());
90      Assertions.assertEquals("a description", actual.get(0).getTodoItems().get(0).getDescription().trim());
91  
92      // We should have gotten three item objects. The first item from the first list and the first item from
93      // the second list have identical properties, but they should be distinct objects
94      Assertions.assertNotSame(actual.get(0).getTodoItems().get(0), actual.get(1).getTodoItems().get(0));
95    }
96  }