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 org.junit.jupiter.api.Test;
24  
25  class EnumOrdinalTypeHandlerTest extends BaseTypeHandlerTest {
26  
27    enum MyEnum {
28      ONE, TWO
29    }
30  
31    private static final TypeHandler<MyEnum> TYPE_HANDLER = new EnumOrdinalTypeHandler<>(MyEnum.class);
32  
33    @Override
34    @Test
35    public void shouldSetParameter() throws Exception {
36      TYPE_HANDLER.setParameter(ps, 1, MyEnum.ONE, null);
37      verify(ps).setInt(1, 0);
38    }
39  
40    @Test
41    public void shouldSetNullParameter() throws Exception {
42      TYPE_HANDLER.setParameter(ps, 1, null, JdbcType.VARCHAR);
43      verify(ps).setNull(1, JdbcType.VARCHAR.TYPE_CODE);
44    }
45  
46    @Override
47    @Test
48    public void shouldGetResultFromResultSetByName() throws Exception {
49      when(rs.getInt("column")).thenReturn(0);
50      when(rs.wasNull()).thenReturn(false);
51      assertEquals(MyEnum.ONE, TYPE_HANDLER.getResult(rs, "column"));
52    }
53  
54    @Override
55    @Test
56    public void shouldGetResultNullFromResultSetByName() throws Exception {
57      when(rs.getInt("column")).thenReturn(0);
58      when(rs.wasNull()).thenReturn(true);
59      assertNull(TYPE_HANDLER.getResult(rs, "column"));
60    }
61  
62    @Override
63    @Test
64    public void shouldGetResultFromResultSetByPosition() throws Exception {
65      when(rs.getInt(1)).thenReturn(0);
66      when(rs.wasNull()).thenReturn(false);
67      assertEquals(MyEnum.ONE, TYPE_HANDLER.getResult(rs, 1));
68    }
69  
70    @Override
71    @Test
72    public void shouldGetResultNullFromResultSetByPosition() throws Exception {
73      when(rs.getInt(1)).thenReturn(0);
74      when(rs.wasNull()).thenReturn(true);
75      assertNull(TYPE_HANDLER.getResult(rs, 1));
76    }
77  
78    @Override
79    @Test
80    public void shouldGetResultFromCallableStatement() throws Exception {
81      when(cs.getInt(1)).thenReturn(0);
82      when(cs.wasNull()).thenReturn(false);
83      assertEquals(MyEnum.ONE, TYPE_HANDLER.getResult(cs, 1));
84    }
85  
86    @Override
87    @Test
88    public void shouldGetResultNullFromCallableStatement() throws Exception {
89      when(cs.getInt(1)).thenReturn(0);
90      when(cs.wasNull()).thenReturn(true);
91      assertNull(TYPE_HANDLER.getResult(cs, 1));
92    }
93  
94  }