View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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   * Tests for support the {@code java.util.Optional} as return type of mapper method.
39   *
40   * @since 3.5.0
41   *
42   * @author Kazuki Shimizu
43   */
44  class OptionalOnMapperMethodTest {
45  
46    private static SqlSessionFactory sqlSessionFactory;
47  
48    @BeforeAll
49    static void setUp() throws Exception {
50      // create an SqlSessionFactory
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      // populate in-memory database
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 }