1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.parent_reference_3level;
17
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19 import static org.junit.jupiter.api.Assertions.assertNotNull;
20
21 import java.io.Reader;
22
23 import org.apache.ibatis.BaseDataTest;
24 import org.apache.ibatis.io.Resources;
25 import org.apache.ibatis.session.SqlSession;
26 import org.apache.ibatis.session.SqlSessionFactory;
27 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28 import org.junit.jupiter.api.Assertions;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31
32 class BlogTest {
33
34 protected SqlSessionFactory sqlSessionFactory;
35
36 String getConfigPath() {
37 return "org/apache/ibatis/submitted/parent_reference_3level/mybatis-config.xml";
38 }
39
40 @BeforeEach
41 void setUp() throws Exception {
42 try (Reader reader = Resources.getResourceAsReader(getConfigPath())) {
43 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
44 }
45
46 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
47 "org/apache/ibatis/submitted/parent_reference_3level/CreateDB.sql");
48 }
49
50 @Test
51 void testSelectBlogWithPosts() {
52 try (SqlSession session = sqlSessionFactory.openSession()) {
53 Mapper mapper = session.getMapper(Mapper.class);
54 Blog result = mapper.selectBlogByPrimaryKey(1);
55 assertNotNull(result);
56 assertEquals("Blog with posts", result.getTitle());
57 Assertions.assertEquals(2, result.getPosts().size());
58 Post firstPost = result.getPosts().get(0);
59 Assertions.assertEquals(1, firstPost.getBlog().getId());
60 Assertions.assertEquals(2, firstPost.getComments().size());
61 Post secondPost = result.getPosts().get(1);
62 Assertions.assertEquals(1, secondPost.getComments().size());
63 Assertions.assertEquals(2, secondPost.getComments().get(0).getPost().getId());
64 }
65 }
66
67 @Test
68 void testSelectBlogWithoutPosts() {
69 try (SqlSession session = sqlSessionFactory.openSession()) {
70 Mapper mapper = session.getMapper(Mapper.class);
71 Blog result = mapper.selectBlogByPrimaryKey(2);
72 assertNotNull(result);
73 assertEquals("Blog without posts", result.getTitle());
74 Assertions.assertEquals(0, result.getPosts().size());
75 }
76 }
77
78 @Test
79 void testSelectBlogWithPostsColumnPrefix() {
80 try (SqlSession session = sqlSessionFactory.openSession()) {
81 Mapper mapper = session.getMapper(Mapper.class);
82 Blog result = mapper.selectBlogByPrimaryKeyColumnPrefix(1);
83 assertNotNull(result);
84 assertEquals("Blog with posts", result.getTitle());
85 Assertions.assertEquals(2, result.getPosts().size());
86 Post firstPost = result.getPosts().get(0);
87 Assertions.assertEquals(1, firstPost.getBlog().getId());
88 Assertions.assertEquals(2, firstPost.getComments().size());
89 Post secondPost = result.getPosts().get(1);
90 Assertions.assertEquals(1, secondPost.getComments().size());
91 Assertions.assertEquals(2, secondPost.getComments().get(0).getPost().getId());
92 }
93 }
94
95 @Test
96 void testSelectBlogWithoutPostsColumnPrefix() {
97 try (SqlSession session = sqlSessionFactory.openSession()) {
98 Mapper mapper = session.getMapper(Mapper.class);
99 Blog result = mapper.selectBlogByPrimaryKeyColumnPrefix(2);
100 assertNotNull(result);
101 assertEquals("Blog without posts", result.getTitle());
102 Assertions.assertEquals(0, result.getPosts().size());
103 }
104 }
105 }