View Javadoc
1   /*
2    *    Copyright 2009-2021 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    *       http://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.*;
19  import static org.mockito.Mockito.*;
20  
21  import java.io.Reader;
22  import java.util.Optional;
23  
24  import org.apache.ibatis.BaseDataTest;
25  import org.apache.ibatis.io.Resources;
26  import org.apache.ibatis.session.SqlSession;
27  import org.apache.ibatis.session.SqlSessionFactory;
28  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
29  import org.junit.jupiter.api.BeforeAll;
30  import org.junit.jupiter.api.Test;
31  import org.mockito.Mockito;
32  
33  /**
34   * Tests for support the {@code java.util.Optional} as return type of mapper method.
35   *
36   * @since 3.5.0
37   * @author Kazuki Shimizu
38   */
39  class OptionalOnMapperMethodTest {
40  
41    private static SqlSessionFactory sqlSessionFactory;
42  
43    @BeforeAll
44    static void setUp() throws Exception {
45      // create an SqlSessionFactory
46      try (Reader reader = Resources
47          .getResourceAsReader("org/apache/ibatis/submitted/optional_on_mapper_method/mybatis-config.xml")) {
48        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
49      }
50  
51      // populate in-memory database
52      BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
53          "org/apache/ibatis/submitted/optional_on_mapper_method/CreateDB.sql");
54    }
55  
56    @Test
57    void returnNotNullOnAnnotation() {
58      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
59        Mapper mapper = sqlSession.getMapper(Mapper.class);
60        Optional<User> user = mapper.getUserUsingAnnotation(1);
61        assertTrue(user.isPresent());
62        assertEquals("User1", user.get().getName());
63      }
64    }
65  
66    @Test
67    void returnNullOnAnnotation() {
68      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
69        Mapper mapper = sqlSession.getMapper(Mapper.class);
70        Optional<User> user = mapper.getUserUsingAnnotation(3);
71        assertFalse(user.isPresent());
72      }
73    }
74  
75    @Test
76    void returnNotNullOnXml() {
77      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
78        Mapper mapper = sqlSession.getMapper(Mapper.class);
79        Optional<User> user = mapper.getUserUsingXml(2);
80        assertTrue(user.isPresent());
81        assertEquals("User2", user.get().getName());
82      }
83    }
84  
85    @Test
86    void returnNullOnXml() {
87      try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
88        Mapper mapper = sqlSession.getMapper(Mapper.class);
89        Optional<User> user = mapper.getUserUsingXml(3);
90        assertFalse(user.isPresent());
91      }
92    }
93  
94    @Test
95    void returnOptionalFromSqlSession() {
96      try (SqlSession sqlSession = Mockito.spy(sqlSessionFactory.openSession())) {
97        User mockUser = new User();
98        mockUser.setName("mock user");
99        Optional<User> optionalMockUser = Optional.of(mockUser);
100       doReturn(optionalMockUser).when(sqlSession).selectOne(any(String.class), any(Object.class));
101 
102       Mapper mapper = sqlSession.getMapper(Mapper.class);
103       Optional<User> user = mapper.getUserUsingAnnotation(3);
104       assertSame(optionalMockUser, user);
105     }
106   }
107 
108 }