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