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