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