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