Skip to content

Commit 46c9e07

Browse files
committed
Merge pull request #651 from kazuki43zoo/add-testcases-for-typehandlers
Add test cases for TypeHandlers
2 parents be0aed3 + 0d7153f commit 46c9e07

File tree

3 files changed

+298
-0
lines changed

3 files changed

+298
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* Copyright 2009-2015 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.Mock;
20+
21+
import java.sql.Array;
22+
import java.sql.Timestamp;
23+
import java.util.Date;
24+
25+
import static org.junit.Assert.assertEquals;
26+
import static org.junit.Assert.assertNull;
27+
import static org.mockito.Mockito.verify;
28+
import static org.mockito.Mockito.when;
29+
30+
public class ArrayTypeHandlerTest extends BaseTypeHandlerTest {
31+
32+
private static final TypeHandler<Object> TYPE_HANDLER = new ArrayTypeHandler();
33+
34+
@Mock
35+
Array mockArray;
36+
37+
@Override
38+
@Test
39+
public void shouldSetParameter() throws Exception {
40+
TYPE_HANDLER.setParameter(ps, 1, mockArray, null);
41+
verify(ps).setArray(1, mockArray);
42+
}
43+
44+
@Override
45+
@Test
46+
public void shouldGetResultFromResultSetByName() throws Exception {
47+
when(rs.getArray("column")).thenReturn(mockArray);
48+
when(rs.wasNull()).thenReturn(false);
49+
String[] stringArray = new String[]{"a", "b"};
50+
when(mockArray.getArray()).thenReturn(stringArray);
51+
assertEquals(stringArray, TYPE_HANDLER.getResult(rs, "column"));
52+
}
53+
54+
@Override
55+
@Test
56+
public void shouldGetResultNullFromResultSetByName() throws Exception {
57+
when(rs.getArray("column")).thenReturn(null);
58+
when(rs.wasNull()).thenReturn(true);
59+
assertNull(TYPE_HANDLER.getResult(rs, "column"));
60+
}
61+
62+
@Override
63+
@Test
64+
public void shouldGetResultFromResultSetByPosition() throws Exception {
65+
when(rs.getArray(1)).thenReturn(mockArray);
66+
when(rs.wasNull()).thenReturn(false);
67+
String[] stringArray = new String[]{"a", "b"};
68+
when(mockArray.getArray()).thenReturn(stringArray);
69+
assertEquals(stringArray, TYPE_HANDLER.getResult(rs, 1));
70+
}
71+
72+
@Override
73+
@Test
74+
public void shouldGetResultNullFromResultSetByPosition() throws Exception {
75+
when(rs.getArray(1)).thenReturn(null);
76+
when(rs.wasNull()).thenReturn(true);
77+
assertNull(TYPE_HANDLER.getResult(rs, 1));
78+
}
79+
80+
@Override
81+
@Test
82+
public void shouldGetResultFromCallableStatement() throws Exception {
83+
when(cs.getArray(1)).thenReturn(mockArray);
84+
when(cs.wasNull()).thenReturn(false);
85+
String[] stringArray = new String[]{"a", "b"};
86+
when(mockArray.getArray()).thenReturn(stringArray);
87+
assertEquals(stringArray, TYPE_HANDLER.getResult(cs, 1));
88+
}
89+
90+
@Override
91+
@Test
92+
public void shouldGetResultNullFromCallableStatement() throws Exception {
93+
when(cs.getArray(1)).thenReturn(null);
94+
when(cs.wasNull()).thenReturn(true);
95+
assertNull(TYPE_HANDLER.getResult(cs, 1));
96+
}
97+
98+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
20+
import static org.hamcrest.core.Is.is;
21+
import static org.hamcrest.core.IsNull.nullValue;
22+
import static org.junit.Assert.assertThat;
23+
import static org.mockito.Mockito.verify;
24+
import static org.mockito.Mockito.when;
25+
26+
public class ByteObjectArrayTypeHandlerTest extends BaseTypeHandlerTest {
27+
28+
private static final TypeHandler<Byte[]> TYPE_HANDLER = new ByteObjectArrayTypeHandler();
29+
30+
@Override
31+
@Test
32+
public void shouldSetParameter() throws Exception {
33+
TYPE_HANDLER.setParameter(ps, 1, new Byte[] { 1, 2, 3 }, null);
34+
verify(ps).setBytes(1, new byte[] { 1, 2, 3 });
35+
}
36+
37+
@Override
38+
@Test
39+
public void shouldGetResultFromResultSetByName() throws Exception {
40+
byte[] byteArray = new byte[]{1, 2};
41+
when(rs.getBytes("column")).thenReturn(byteArray);
42+
when(rs.wasNull()).thenReturn(false);
43+
assertThat(TYPE_HANDLER.getResult(rs, "column"), is(new Byte[]{1, 2}));
44+
}
45+
46+
@Override
47+
@Test
48+
public void shouldGetResultNullFromResultSetByName() throws Exception {
49+
when(rs.getBlob("column")).thenReturn(null);
50+
when(rs.wasNull()).thenReturn(true);
51+
assertThat(TYPE_HANDLER.getResult(rs, "column"), nullValue());
52+
}
53+
54+
@Override
55+
@Test
56+
public void shouldGetResultFromResultSetByPosition() throws Exception {
57+
byte[] byteArray = new byte[]{1, 2};
58+
when(rs.getBytes(1)).thenReturn(byteArray);
59+
when(rs.wasNull()).thenReturn(false);
60+
assertThat(TYPE_HANDLER.getResult(rs, 1), is(new Byte[]{1, 2}));
61+
}
62+
63+
@Override
64+
@Test
65+
public void shouldGetResultNullFromResultSetByPosition() throws Exception {
66+
when(rs.getBlob(1)).thenReturn(null);
67+
when(rs.wasNull()).thenReturn(true);
68+
assertThat(TYPE_HANDLER.getResult(rs, 1), nullValue());
69+
}
70+
71+
@Override
72+
@Test
73+
public void shouldGetResultFromCallableStatement() throws Exception {
74+
byte[] byteArray = new byte[]{1, 2};
75+
when(cs.getBytes(1)).thenReturn(byteArray);
76+
when(cs.wasNull()).thenReturn(false);
77+
assertThat(TYPE_HANDLER.getResult(cs, 1), is(new Byte[]{1, 2}));
78+
}
79+
80+
@Override
81+
@Test
82+
public void shouldGetResultNullFromCallableStatement() throws Exception {
83+
when(cs.getBlob(1)).thenReturn(null);
84+
when(cs.wasNull()).thenReturn(true);
85+
assertThat(TYPE_HANDLER.getResult(cs, 1), nullValue());
86+
}
87+
88+
}

0 commit comments

Comments
 (0)