1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.dbutils;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.ArgumentMatchers.eq;
23 import static org.mockito.Mockito.times;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26
27 import java.sql.CallableStatement;
28 import java.sql.Types;
29
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 import org.mockito.junit.MockitoJUnitRunner;
35
36 @RunWith(MockitoJUnitRunner.class)
37 public class OutParameterTest {
38 private static final int INDEX = 2;
39 private static final int VALUE = 42;
40
41 @Mock CallableStatement stmt;
42
43 OutParameter<Number> parameter;
44
45 @Before
46 public void setUp() throws Exception {
47 parameter = new OutParameter<>(Types.INTEGER, Number.class);
48 }
49
50 @Test
51 public void testRegister() throws Exception {
52 parameter.register(stmt, INDEX);
53 verify(stmt, times(1)).registerOutParameter(INDEX, Types.INTEGER);
54 verify(stmt, times(0)).setObject(eq(INDEX), any(Number.class));
55
56 parameter.setValue(VALUE);
57 parameter.register(stmt, INDEX);
58 verify(stmt, times(2)).registerOutParameter(INDEX, Types.INTEGER);
59 verify(stmt, times(1)).setObject(INDEX, VALUE);
60 }
61
62 @Test
63 public void testRegisterAlternateConstructor() throws Exception {
64 parameter = new OutParameter<>(Types.INTEGER, Number.class, VALUE);
65 parameter.register(stmt, INDEX);
66 verify(stmt, times(1)).registerOutParameter(INDEX, Types.INTEGER);
67 verify(stmt, times(1)).setObject(INDEX, VALUE);
68 }
69
70 @Test
71 public void testSetValue() throws Exception {
72 when(stmt.getObject(INDEX)).thenReturn(VALUE);
73
74 parameter.setValue(stmt, INDEX);
75
76 assertEquals(VALUE, parameter.getValue());
77 }
78
79 }