1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.submitted.optional_on_mapper_method;
17
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19 import static org.junit.jupiter.api.Assertions.assertFalse;
20 import static org.junit.jupiter.api.Assertions.assertSame;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.Mockito.doReturn;
24
25 import java.io.Reader;
26 import java.util.Optional;
27
28 import org.apache.ibatis.BaseDataTest;
29 import org.apache.ibatis.io.Resources;
30 import org.apache.ibatis.session.SqlSession;
31 import org.apache.ibatis.session.SqlSessionFactory;
32 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
33 import org.junit.jupiter.api.BeforeAll;
34 import org.junit.jupiter.api.Test;
35 import org.mockito.Mockito;
36
37
38
39
40
41
42
43
44 class OptionalOnMapperMethodTest {
45
46 private static SqlSessionFactory sqlSessionFactory;
47
48 @BeforeAll
49 static void setUp() throws Exception {
50
51 try (Reader reader = Resources
52 .getResourceAsReader("org/apache/ibatis/submitted/optional_on_mapper_method/mybatis-config.xml")) {
53 sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
54 }
55
56
57 BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
58 "org/apache/ibatis/submitted/optional_on_mapper_method/CreateDB.sql");
59 }
60
61 @Test
62 void returnNotNullOnAnnotation() {
63 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
64 Mapper mapper = sqlSession.getMapper(Mapper.class);
65 Optional<User> user = mapper.getUserUsingAnnotation(1);
66 assertTrue(user.isPresent());
67 assertEquals("User1", user.get().getName());
68 }
69 }
70
71 @Test
72 void returnNullOnAnnotation() {
73 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
74 Mapper mapper = sqlSession.getMapper(Mapper.class);
75 Optional<User> user = mapper.getUserUsingAnnotation(3);
76 assertFalse(user.isPresent());
77 }
78 }
79
80 @Test
81 void returnNotNullOnXml() {
82 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
83 Mapper mapper = sqlSession.getMapper(Mapper.class);
84 Optional<User> user = mapper.getUserUsingXml(2);
85 assertTrue(user.isPresent());
86 assertEquals("User2", user.get().getName());
87 }
88 }
89
90 @Test
91 void returnNullOnXml() {
92 try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
93 Mapper mapper = sqlSession.getMapper(Mapper.class);
94 Optional<User> user = mapper.getUserUsingXml(3);
95 assertFalse(user.isPresent());
96 }
97 }
98
99 @Test
100 void returnOptionalFromSqlSession() {
101 try (SqlSession sqlSession = Mockito.spy(sqlSessionFactory.openSession())) {
102 User mockUser = new User();
103 mockUser.setName("mock user");
104 Optional<User> optionalMockUser = Optional.of(mockUser);
105 doReturn(optionalMockUser).when(sqlSession).selectOne(any(String.class), any(Object.class));
106
107 Mapper mapper = sqlSession.getMapper(Mapper.class);
108 Optional<User> user = mapper.getUserUsingAnnotation(3);
109 assertSame(optionalMockUser, user);
110 }
111 }
112
113 }