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