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.type;
17  
18  import static org.junit.jupiter.api.Assertions.*;
19  import static org.mockito.Mockito.*;
20  
21  import java.sql.Timestamp;
22  import java.time.Instant;
23  
24  import org.junit.jupiter.api.Test;
25  
26  class InstantTypeHandlerTest extends BaseTypeHandlerTest {
27  
28    private static final TypeHandler<Instant> TYPE_HANDLER = new InstantTypeHandler();
29    private static final Instant INSTANT = Instant.now();
30    private static final Timestamp TIMESTAMP = Timestamp.from(INSTANT);
31  
32    @Override
33    @Test
34    public void shouldSetParameter() throws Exception {
35      TYPE_HANDLER.setParameter(ps, 1, INSTANT, null);
36      verify(ps).setTimestamp(1, TIMESTAMP);
37    }
38  
39    @Override
40    @Test
41    public void shouldGetResultFromResultSetByName() throws Exception {
42      when(rs.getTimestamp("column")).thenReturn(TIMESTAMP);
43      assertEquals(INSTANT, TYPE_HANDLER.getResult(rs, "column"));
44      verify(rs, never()).wasNull();
45    }
46  
47    @Override
48    @Test
49    public void shouldGetResultNullFromResultSetByName() throws Exception {
50      when(rs.getTimestamp("column")).thenReturn(null);
51      assertNull(TYPE_HANDLER.getResult(rs, "column"));
52      verify(rs, never()).wasNull();
53    }
54  
55    @Override
56    @Test
57    public void shouldGetResultFromResultSetByPosition() throws Exception {
58      when(rs.getTimestamp(1)).thenReturn(TIMESTAMP);
59      assertEquals(INSTANT, TYPE_HANDLER.getResult(rs, 1));
60      verify(rs, never()).wasNull();
61    }
62  
63    @Override
64    @Test
65    public void shouldGetResultNullFromResultSetByPosition() throws Exception {
66      when(rs.getTimestamp(1)).thenReturn(null);
67      assertNull(TYPE_HANDLER.getResult(rs, 1));
68      verify(rs, never()).wasNull();
69    }
70  
71    @Override
72    @Test
73    public void shouldGetResultFromCallableStatement() throws Exception {
74      when(cs.getTimestamp(1)).thenReturn(TIMESTAMP);
75      assertEquals(INSTANT, TYPE_HANDLER.getResult(cs, 1));
76      verify(cs, never()).wasNull();
77    }
78  
79    @Override
80    @Test
81    public void shouldGetResultNullFromCallableStatement() throws Exception {
82      when(cs.getTimestamp(1)).thenReturn(null);
83      assertNull(TYPE_HANDLER.getResult(cs, 1));
84      verify(cs, never()).wasNull();
85    }
86  }