View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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  }