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.assertj.core.api.Assertions.assertThat;
19  import static org.mockito.Mockito.never;
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 ByteObjectArrayTypeHandlerTest extends BaseTypeHandlerTest {
26  
27    private static final TypeHandler<Byte[]> TYPE_HANDLER = new ByteObjectArrayTypeHandler();
28  
29    @Override
30    @Test
31    public void shouldSetParameter() throws Exception {
32      TYPE_HANDLER.setParameter(ps, 1, new Byte[] { 1, 2, 3 }, null);
33      verify(ps).setBytes(1, new byte[] { 1, 2, 3 });
34    }
35  
36    @Override
37    @Test
38    public void shouldGetResultFromResultSetByName() throws Exception {
39      byte[] byteArray = { 1, 2 };
40      when(rs.getBytes("column")).thenReturn(byteArray);
41      assertThat(TYPE_HANDLER.getResult(rs, "column")).isEqualTo(new Byte[] { 1, 2 });
42      verify(rs, never()).wasNull();
43    }
44  
45    @Override
46    @Test
47    public void shouldGetResultNullFromResultSetByName() throws Exception {
48      when(rs.getBytes("column")).thenReturn(null);
49      assertThat(TYPE_HANDLER.getResult(rs, "column")).isNull();
50      verify(rs, never()).wasNull();
51    }
52  
53    @Override
54    @Test
55    public void shouldGetResultFromResultSetByPosition() throws Exception {
56      byte[] byteArray = { 1, 2 };
57      when(rs.getBytes(1)).thenReturn(byteArray);
58      assertThat(TYPE_HANDLER.getResult(rs, 1)).isEqualTo(new Byte[] { 1, 2 });
59      verify(rs, never()).wasNull();
60    }
61  
62    @Override
63    @Test
64    public void shouldGetResultNullFromResultSetByPosition() throws Exception {
65      when(rs.getBytes(1)).thenReturn(null);
66      assertThat(TYPE_HANDLER.getResult(rs, 1)).isNull();
67      verify(rs, never()).wasNull();
68    }
69  
70    @Override
71    @Test
72    public void shouldGetResultFromCallableStatement() throws Exception {
73      byte[] byteArray = { 1, 2 };
74      when(cs.getBytes(1)).thenReturn(byteArray);
75      assertThat(TYPE_HANDLER.getResult(cs, 1)).isEqualTo(new Byte[] { 1, 2 });
76      verify(cs, never()).wasNull();
77    }
78  
79    @Override
80    @Test
81    public void shouldGetResultNullFromCallableStatement() throws Exception {
82      when(cs.getBytes(1)).thenReturn(null);
83      assertThat(TYPE_HANDLER.getResult(cs, 1)).isNull();
84      verify(cs, never()).wasNull();
85    }
86  
87  }