1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
91
92 Assertions.assertNotSame(actual.get(0).getTodoItems().get(0), actual.get(1).getTodoItems().get(0));
93 }
94 }