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.assertArrayEquals;
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 java.io.InputStream;
24  import java.sql.Blob;
25  
26  import org.junit.jupiter.api.Test;
27  import org.mockito.ArgumentMatchers;
28  import org.mockito.Mock;
29  
30  class BlobTypeHandlerTest extends BaseTypeHandlerTest {
31  
32    private static final TypeHandler<byte[]> TYPE_HANDLER = new BlobTypeHandler();
33  
34    @Mock
35    protected Blob blob;
36  
37    @Override
38    @Test
39    public void shouldSetParameter() throws Exception {
40      TYPE_HANDLER.setParameter(ps, 1, new byte[] { 1, 2, 3 }, null);
41      verify(ps).setBinaryStream(ArgumentMatchers.eq(1), ArgumentMatchers.any(InputStream.class), ArgumentMatchers.eq(3));
42    }
43  
44    @Override
45    @Test
46    public void shouldGetResultFromResultSetByName() throws Exception {
47      when(rs.getBlob("column")).thenReturn(blob);
48      when(blob.length()).thenReturn(3L);
49      when(blob.getBytes(1, 3)).thenReturn(new byte[] { 1, 2, 3 });
50      assertArrayEquals(new byte[] { 1, 2, 3 }, TYPE_HANDLER.getResult(rs, "column"));
51    }
52  
53    @Override
54    @Test
55    public void shouldGetResultNullFromResultSetByName() throws Exception {
56      when(rs.getBlob("column")).thenReturn(null);
57      assertNull(TYPE_HANDLER.getResult(rs, "column"));
58    }
59  
60    @Override
61    @Test
62    public void shouldGetResultFromResultSetByPosition() throws Exception {
63      when(rs.getBlob(1)).thenReturn(blob);
64      when(blob.length()).thenReturn(3L);
65      when(blob.getBytes(1, 3)).thenReturn(new byte[] { 1, 2, 3 });
66      assertArrayEquals(new byte[] { 1, 2, 3 }, TYPE_HANDLER.getResult(rs, 1));
67    }
68  
69    @Override
70    @Test
71    public void shouldGetResultNullFromResultSetByPosition() throws Exception {
72      when(rs.getBlob(1)).thenReturn(null);
73      assertNull(TYPE_HANDLER.getResult(rs, 1));
74    }
75  
76    @Override
77    @Test
78    public void shouldGetResultFromCallableStatement() throws Exception {
79      when(cs.getBlob(1)).thenReturn(blob);
80      when(blob.length()).thenReturn(3L);
81      when(blob.getBytes(1, 3)).thenReturn(new byte[] { 1, 2, 3 });
82      assertArrayEquals(new byte[] { 1, 2, 3 }, TYPE_HANDLER.getResult(cs, 1));
83    }
84  
85    @Override
86    @Test
87    public void shouldGetResultNullFromCallableStatement() throws Exception {
88      when(cs.getBlob(1)).thenReturn(null);
89      assertNull(TYPE_HANDLER.getResult(cs, 1));
90    }
91  
92  }