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  package org.apache.commons.dbutils;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.time.Duration;
24  
25  import org.junit.Test;
26  
27  public class StatementConfigurationTest {
28      /**
29       * Test that a builder with all values set yields like values in the constructed configuration.
30       */
31      @Test
32      public void testBuilder() {
33          final StatementConfiguration.Builder builder = new StatementConfiguration.Builder()
34                  .fetchDirection(1)
35                  .fetchSize(2)
36                  .maxFieldSize(3)
37                  .maxRows(4)
38                  .queryTimeout(5);
39          final StatementConfiguration config = builder.build();
40  
41          assertTrue(config.isFetchDirectionSet());
42          assertEquals(Integer.valueOf(1), config.getFetchDirection());
43  
44          assertTrue(config.isFetchSizeSet());
45          assertEquals(Integer.valueOf(2), config.getFetchSize());
46  
47          assertTrue(config.isMaxFieldSizeSet());
48          assertEquals(Integer.valueOf(3), config.getMaxFieldSize());
49  
50          assertTrue(config.isMaxRowsSet());
51          assertEquals(Integer.valueOf(4), config.getMaxRows());
52  
53          assertTrue(config.isQueryTimeoutSet());
54          assertEquals(Integer.valueOf(5), config.getQueryTimeout());
55  
56          assertTrue(config.isQueryTimeoutSet());
57          assertEquals(Duration.ofSeconds(5), config.getQueryTimeoutDuration());
58          
59          final StatementConfiguration config2 = builder.queryTimeout(Duration.ofSeconds(3)).build();
60  
61          assertTrue(config2.isQueryTimeoutSet());
62          assertEquals(Integer.valueOf(3), config2.getQueryTimeout());
63  
64          assertTrue(config2.isQueryTimeoutSet());
65          assertEquals(Duration.ofSeconds(3), config2.getQueryTimeoutDuration());
66      }
67  
68      /**
69       * Test that the constructor of {@code StatementConfiguration} correctly sets all values.
70       */
71      @Test
72      public void testConstructor() {
73          final StatementConfiguration config = new StatementConfiguration(1, 2, 3, 4, 5);
74  
75          assertEquals(Integer.valueOf(1), config.getFetchDirection());
76          assertEquals(Integer.valueOf(2), config.getFetchSize());
77          assertEquals(Integer.valueOf(3), config.getMaxFieldSize());
78          assertEquals(Integer.valueOf(4), config.getMaxRows());
79          assertEquals(Integer.valueOf(5), config.getQueryTimeout());
80      }
81  
82      /**
83       * Test that an empty builder yields null values for all configuration settings.
84       */
85      @Test
86      public void testEmptyBuilder() {
87          final StatementConfiguration config = new StatementConfiguration.Builder().build();
88  
89          assertFalse(config.isFetchDirectionSet());
90          assertFalse(config.isFetchSizeSet());
91          assertFalse(config.isMaxFieldSizeSet());
92          assertFalse(config.isMaxRowsSet());
93          assertFalse(config.isQueryTimeoutSet());
94      }
95  }