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.doNothing;
20  import static org.mockito.Mockito.when;
21  
22  import java.io.ByteArrayInputStream;
23  import java.sql.Blob;
24  
25  import org.junit.jupiter.api.Test;
26  import org.mockito.ArgumentCaptor;
27  import org.mockito.Mock;
28  
29  class BlobByteObjectArrayTypeHandlerTest extends BaseTypeHandlerTest {
30  
31    private static final TypeHandler<Byte[]> TYPE_HANDLER = new BlobByteObjectArrayTypeHandler();
32  
33    @Mock
34    protected Blob blob;
35  
36    @Override
37    @Test
38    public void shouldSetParameter() throws Exception {
39      final ArgumentCaptor<Integer> positionCaptor = ArgumentCaptor.forClass(Integer.class);
40      final ArgumentCaptor<ByteArrayInputStream> byteArrayCaptor = ArgumentCaptor.forClass(ByteArrayInputStream.class);
41      final ArgumentCaptor<Integer> lengthCaptor = ArgumentCaptor.forClass(Integer.class);
42      doNothing().when(ps).setBinaryStream(positionCaptor.capture(), byteArrayCaptor.capture(), lengthCaptor.capture());
43      TYPE_HANDLER.setParameter(ps, 1, new Byte[] { 1, 2 }, null);
44      ByteArrayInputStream actualIn = byteArrayCaptor.getValue();
45      assertThat(positionCaptor.getValue()).isEqualTo(1);
46      assertThat(actualIn.read()).isEqualTo(1);
47      assertThat(actualIn.read()).isEqualTo(2);
48      assertThat(actualIn.read()).isEqualTo(-1);
49      assertThat(lengthCaptor.getValue()).isEqualTo(2);
50    }
51  
52    @Override
53    @Test
54    public void shouldGetResultFromResultSetByName() throws Exception {
55      byte[] byteArray = { 1, 2 };
56      when(rs.getBlob("column")).thenReturn(blob);
57      when(blob.length()).thenReturn((long) byteArray.length);
58      when(blob.getBytes(1, 2)).thenReturn(byteArray);
59      assertThat(TYPE_HANDLER.getResult(rs, "column")).isEqualTo(new Byte[] { 1, 2 });
60  
61    }
62  
63    @Override
64    @Test
65    public void shouldGetResultNullFromResultSetByName() throws Exception {
66      when(rs.getBlob("column")).thenReturn(null);
67      assertThat(TYPE_HANDLER.getResult(rs, "column")).isNull();
68    }
69  
70    @Override
71    @Test
72    public void shouldGetResultFromResultSetByPosition() throws Exception {
73      byte[] byteArray = { 1, 2 };
74      when(rs.getBlob(1)).thenReturn(blob);
75      when(blob.length()).thenReturn((long) byteArray.length);
76      when(blob.getBytes(1, 2)).thenReturn(byteArray);
77      assertThat(TYPE_HANDLER.getResult(rs, 1)).isEqualTo(new Byte[] { 1, 2 });
78    }
79  
80    @Override
81    @Test
82    public void shouldGetResultNullFromResultSetByPosition() throws Exception {
83      when(rs.getBlob(1)).thenReturn(null);
84      assertThat(TYPE_HANDLER.getResult(rs, 1)).isNull();
85    }
86  
87    @Override
88    @Test
89    public void shouldGetResultFromCallableStatement() throws Exception {
90      byte[] byteArray = { 1, 2 };
91      when(cs.getBlob(1)).thenReturn(blob);
92      when(blob.length()).thenReturn((long) byteArray.length);
93      when(blob.getBytes(1, 2)).thenReturn(byteArray);
94      assertThat(TYPE_HANDLER.getResult(cs, 1)).isEqualTo(new Byte[] { 1, 2 });
95    }
96  
97    @Override
98    @Test
99    public void shouldGetResultNullFromCallableStatement() throws Exception {
100     when(cs.getBlob(1)).thenReturn(null);
101     assertThat(TYPE_HANDLER.getResult(cs, 1)).isNull();
102   }
103 
104 }