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.time.Month;
22  
23  import org.apache.ibatis.executor.result.ResultMapException;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   *
28   * @author Eduardo Macarron
29   */
30  class MonthTypeHandlerTest extends BaseTypeHandlerTest {
31  
32    private static final TypeHandler<Month> TYPE_HANDLER = new MonthTypeHandler();
33    private static final Month INSTANT = Month.JANUARY;
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      try {
48        TYPE_HANDLER.getResult(rs, "column");
49        fail();
50      } catch (ResultMapException e) {
51        assertEquals(
52            "Error attempting to get column 'column' from result set.  Cause: java.time.DateTimeException: Invalid value for MonthOfYear: 0",
53            e.getMessage());
54      }
55    }
56  
57    @Override
58    @Test
59    public void shouldGetResultNullFromResultSetByName() throws Exception {
60      when(rs.getInt("column")).thenReturn(0);
61      when(rs.wasNull()).thenReturn(true);
62      assertNull(TYPE_HANDLER.getResult(rs, "column"));
63    }
64  
65    @Override
66    @Test
67    public void shouldGetResultFromResultSetByPosition() throws Exception {
68      when(rs.getInt(1)).thenReturn(INSTANT.getValue(), 0);
69      assertEquals(INSTANT, TYPE_HANDLER.getResult(rs, 1));
70      try {
71        TYPE_HANDLER.getResult(rs, 1);
72        fail();
73      } catch (ResultMapException e) {
74        assertEquals(
75            "Error attempting to get column #1 from result set.  Cause: java.time.DateTimeException: Invalid value for MonthOfYear: 0",
76            e.getMessage());
77      }
78    }
79  
80    @Override
81    @Test
82    public void shouldGetResultNullFromResultSetByPosition() throws Exception {
83      when(rs.getInt(1)).thenReturn(0);
84      when(rs.wasNull()).thenReturn(true);
85      assertNull(TYPE_HANDLER.getResult(rs, 1));
86    }
87  
88    @Override
89    @Test
90    public void shouldGetResultFromCallableStatement() throws Exception {
91      when(cs.getInt(1)).thenReturn(INSTANT.getValue(), 0);
92      assertEquals(INSTANT, TYPE_HANDLER.getResult(cs, 1));
93      try {
94        TYPE_HANDLER.getResult(cs, 1);
95        fail();
96      } catch (ResultMapException e) {
97        assertEquals(
98            "Error attempting to get column #1 from callable statement.  Cause: java.time.DateTimeException: Invalid value for MonthOfYear: 0",
99            e.getMessage());
100     }
101   }
102 
103   @Override
104   @Test
105   public void shouldGetResultNullFromCallableStatement() throws Exception {
106     when(cs.getInt(1)).thenReturn(0);
107     when(cs.wasNull()).thenReturn(true);
108     assertNull(TYPE_HANDLER.getResult(cs, 1));
109   }
110 
111 }