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.assertNotNull;
19 import static org.junit.jupiter.api.Assertions.assertNull;
20 import static org.junit.jupiter.api.Assertions.assertTrue;
21 import static org.junit.jupiter.api.Assertions.fail;
22
23 import java.io.Reader;
24
25 import org.apache.ibatis.BaseDataTest;
26 import org.apache.ibatis.domain.blog.Author;
27 import org.apache.ibatis.domain.blog.mappers.AuthorMapper;
28 import org.apache.ibatis.exceptions.PersistenceException;
29 import org.apache.ibatis.io.Resources;
30 import org.junit.jupiter.api.BeforeAll;
31 import org.junit.jupiter.api.Test;
32
33 class SqlSessionManagerTest extends BaseDataTest {
34
35 private static SqlSessionManager manager;
36
37 @BeforeAll
38 static void setup() throws Exception {
39 createBlogDataSource();
40 final String resource = "org/apache/ibatis/builder/MapperConfig.xml";
41 final Reader reader = Resources.getResourceAsReader(resource);
42 manager = SqlSessionManager.newInstance(reader);
43 }
44
45 @Test
46 void shouldThrowExceptionIfMappedStatementDoesNotExistAndSqlSessionIsOpen() {
47 try {
48 manager.startManagedSession();
49 manager.selectList("ThisStatementDoesNotExist");
50 fail("Expected exception to be thrown due to statement that does not exist.");
51 } catch (PersistenceException e) {
52 assertTrue(e.getMessage().contains("does not contain value for ThisStatementDoesNotExist"));
53 } finally {
54 manager.close();
55 }
56 }
57
58 @Test
59 void shouldCommitInsertedAuthor() {
60 try {
61 manager.startManagedSession();
62 AuthorMapper mapper = manager.getMapper(AuthorMapper.class);
63 AuthorAuthor.html#Author">Author expected = new Author(500, "cbegin", "******", "cbegin@somewhere.com", "Something...", null);
64 mapper.insertAuthor(expected);
65 manager.commit();
66 Author actual = mapper.selectAuthor(500);
67 assertNotNull(actual);
68 } finally {
69 manager.close();
70 }
71 }
72
73 @Test
74 void shouldRollbackInsertedAuthor() {
75 try {
76 manager.startManagedSession();
77 AuthorMapper mapper = manager.getMapper(AuthorMapper.class);
78 AuthorAuthor.html#Author">Author expected = new Author(501, "lmeadors", "******", "lmeadors@somewhere.com", "Something...", null);
79 mapper.insertAuthor(expected);
80 manager.rollback();
81 Author actual = mapper.selectAuthor(501);
82 assertNull(actual);
83 } finally {
84 manager.close();
85 }
86 }
87
88 @Test
89 void shouldImplicitlyRollbackInsertedAuthor() {
90 manager.startManagedSession();
91 AuthorMapper mapper = manager.getMapper(AuthorMapper.class);
92 AuthorAuthor.html#Author">Author expected = new Author(502, "emacarron", "******", "emacarron@somewhere.com", "Something...", null);
93 mapper.insertAuthor(expected);
94 manager.close();
95 Author actual = mapper.selectAuthor(502);
96 assertNull(actual);
97 }
98
99 }