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