View Javadoc
1   /*
2    *    Copyright 2009-2022 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 ShortTypeHandlerTest extends BaseTypeHandlerTest {
26  
27    private static final TypeHandler<Short> TYPE_HANDLER = new ShortTypeHandler();
28  
29    @Override
30    @Test
31    public void shouldSetParameter() throws Exception {
32      TYPE_HANDLER.setParameter(ps, 1, (short) 100, null);
33      verify(ps).setShort(1, (short) 100);
34    }
35  
36    @Override
37    @Test
38    public void shouldGetResultFromResultSetByName() throws Exception {
39      when(rs.getShort("column")).thenReturn((short) 100, (short) 0);
40      assertEquals(Short.valueOf((short) 100), TYPE_HANDLER.getResult(rs, "column"));
41      assertEquals(Short.valueOf((short) 0), TYPE_HANDLER.getResult(rs, "column"));
42    }
43  
44    @Override
45    @Test
46    public void shouldGetResultNullFromResultSetByName() throws Exception {
47      when(rs.getShort("column")).thenReturn((short) 0);
48      when(rs.wasNull()).thenReturn(true);
49      assertNull(TYPE_HANDLER.getResult(rs, "column"));
50    }
51  
52    @Override
53    @Test
54    public void shouldGetResultFromResultSetByPosition() throws Exception {
55      when(rs.getShort(1)).thenReturn((short) 100, (short) 0);
56      assertEquals(Short.valueOf((short) 100), TYPE_HANDLER.getResult(rs, 1));
57      assertEquals(Short.valueOf((short) 0), TYPE_HANDLER.getResult(rs, 1));
58    }
59  
60    @Override
61    @Test
62    public void shouldGetResultNullFromResultSetByPosition() throws Exception {
63      when(rs.getShort(1)).thenReturn((short) 0);
64      when(rs.wasNull()).thenReturn(true);
65      assertNull(TYPE_HANDLER.getResult(rs, 1));
66    }
67  
68    @Override
69    @Test
70    public void shouldGetResultFromCallableStatement() throws Exception {
71      when(cs.getShort(1)).thenReturn((short) 100, (short) 0);
72      assertEquals(Short.valueOf((short) 100), TYPE_HANDLER.getResult(cs, 1));
73      assertEquals(Short.valueOf((short) 0), TYPE_HANDLER.getResult(cs, 1));
74    }
75  
76    @Override
77    @Test
78    public void shouldGetResultNullFromCallableStatement() throws Exception {
79      when(cs.getShort(1)).thenReturn((short) 0);
80      when(cs.wasNull()).thenReturn(true);
81      assertNull(TYPE_HANDLER.getResult(cs, 1));
82    }
83  
84  }