1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.session;
17
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19 import static org.junit.jupiter.api.Assertions.assertNotNull;
20 import static org.junit.jupiter.api.Assertions.assertNull;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22 import static org.junit.jupiter.api.Assertions.fail;
23
24 import java.io.Reader;
25 import java.util.List;
26
27 import org.apache.ibatis.BaseDataTest;
28 import org.apache.ibatis.domain.blog.Author;
29 import org.apache.ibatis.domain.blog.PostLite;
30 import org.apache.ibatis.domain.blog.mappers.AuthorMapper;
31 import org.apache.ibatis.exceptions.PersistenceException;
32 import org.apache.ibatis.io.Resources;
33 import org.junit.jupiter.api.BeforeAll;
34 import org.junit.jupiter.api.Test;
35
36 class SqlSessionManagerTest extends BaseDataTest {
37
38 private static SqlSessionManager manager;
39
40 @BeforeAll
41 static void setup() throws Exception {
42 createBlogDataSource();
43 final String resource = "org/apache/ibatis/builder/MapperConfig.xml";
44 final Reader reader = Resources.getResourceAsReader(resource);
45 manager = SqlSessionManager.newInstance(reader);
46 }
47
48 @Test
49 void shouldThrowExceptionIfMappedStatementDoesNotExistAndSqlSessionIsOpen() {
50 try {
51 manager.startManagedSession();
52 manager.selectList("ThisStatementDoesNotExist");
53 fail("Expected exception to be thrown due to statement that does not exist.");
54 } catch (PersistenceException e) {
55 assertTrue(e.getMessage().contains("does not contain value for ThisStatementDoesNotExist"));
56 } finally {
57 manager.close();
58 }
59 }
60
61 @Test
62 void shouldCommitInsertedAuthor() {
63 try {
64 manager.startManagedSession();
65 AuthorMapper mapper = manager.getMapper(AuthorMapper.class);
66 Author expected = new Author(500, "cbegin", "******", "cbegin@somewhere.com", "Something...", null);
67 mapper.insertAuthor(expected);
68 manager.commit();
69 Author actual = mapper.selectAuthor(500);
70 assertNotNull(actual);
71 } finally {
72 manager.close();
73 }
74 }
75
76 @Test
77 void shouldRollbackInsertedAuthor() {
78 try {
79 manager.startManagedSession();
80 AuthorMapper mapper = manager.getMapper(AuthorMapper.class);
81 Author expected = new Author(501, "lmeadors", "******", "lmeadors@somewhere.com", "Something...", null);
82 mapper.insertAuthor(expected);
83 manager.rollback();
84 Author actual = mapper.selectAuthor(501);
85 assertNull(actual);
86 } finally {
87 manager.close();
88 }
89 }
90
91 @Test
92 void shouldImplicitlyRollbackInsertedAuthor() {
93 manager.startManagedSession();
94 AuthorMapper mapper = manager.getMapper(AuthorMapper.class);
95 Author expected = new Author(502, "emacarron", "******", "emacarron@somewhere.com", "Something...", null);
96 mapper.insertAuthor(expected);
97 manager.close();
98 Author actual = mapper.selectAuthor(502);
99 assertNull(actual);
100 }
101
102 @Test
103 void shouldFindAllPostLites() throws Exception {
104 List<PostLite> posts = manager.selectList("org.apache.ibatis.domain.blog.mappers.PostMapper.selectPostLite");
105 assertEquals(2, posts.size());
106 }
107
108 @Test
109 void shouldFindAllMutablePostLites() throws Exception {
110 List<PostLite> posts = manager.selectList("org.apache.ibatis.domain.blog.mappers.PostMapper.selectMutablePostLite");
111 assertEquals(2, posts.size());
112 }
113
114 }