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
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
79
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
93
94 Assertions.assertNotSame(actual.get(0).getTodoItems().get(0), actual.get(1).getTodoItems().get(0));
95 }
96 }