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    *     https://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.configuration2.builder;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertSame;
21  import static org.mockito.Mockito.mock;
22  
23  import java.util.Map;
24  
25  import javax.sql.DataSource;
26  
27  import org.apache.commons.configuration2.beanutils.BeanHelper;
28  import org.junit.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Test class for {@code DatabaseBuilderParametersImpl}.
33   */
34  public class TestDatabaseBuilderParametersImpl {
35  
36      /** The parameters object to be tested. */
37      private DatabaseBuilderParametersImpl params;
38  
39      @BeforeEach
40      public void setUp() throws Exception {
41          params = new DatabaseBuilderParametersImpl();
42      }
43  
44      /**
45       * Tests whether properties can be set through BeanUtils.
46       */
47      @Test
48      void testBeanProperties() throws Exception {
49          BeanHelper.setProperty(params, "table", "testTable");
50          BeanHelper.setProperty(params, "autoCommit", Boolean.FALSE);
51          final Map<String, Object> map = params.getParameters();
52          assertEquals("testTable", map.get("table"));
53          assertEquals(Boolean.FALSE, map.get("autoCommit"));
54      }
55  
56      /**
57       * Tests whether the auto commit flag can be set.
58       */
59      @Test
60      void testSetAutoCommit() {
61          assertSame(params, params.setAutoCommit(true));
62          assertEquals(Boolean.TRUE, params.getParameters().get("autoCommit"));
63      }
64  
65      /**
66       * Tests whether the configuration name can be set.
67       */
68      @Test
69      void testSetConfigurationName() {
70          final String confName = "TestConfiguration";
71          assertSame(params, params.setConfigurationName(confName));
72          assertEquals(confName, params.getParameters().get("configurationName"));
73      }
74  
75      /**
76       * Tests whether the configuration name column can be set.
77       */
78      @Test
79      void testSetConfigurationNameColumn() {
80          final String colName = "CONFIG_COLUMN";
81          assertSame(params, params.setConfigurationNameColumn(colName));
82          assertEquals(colName, params.getParameters().get("configurationNameColumn"));
83      }
84  
85      /**
86       * Tests whether the data source property can be set.
87       */
88      @Test
89      void testSetDataSource() {
90          final DataSource src = mock(DataSource.class);
91          assertSame(params, params.setDataSource(src));
92          assertSame(src, params.getParameters().get("dataSource"));
93      }
94  
95      /**
96       * Tests whether the key column name can be set.
97       */
98      @Test
99      void testSetKeyColumn() {
100         final String colName = "KEY_COLUMN";
101         assertSame(params, params.setKeyColumn(colName));
102         assertEquals(colName, params.getParameters().get("keyColumn"));
103     }
104 
105     /**
106      * Tests whether the table name can be set.
107      */
108     @Test
109     void testSetTable() {
110         final String table = "TestTable";
111         assertSame(params, params.setTable(table));
112         assertEquals(table, params.getParameters().get("table"));
113     }
114 
115     /**
116      * Tests whether the value column name can be set.
117      */
118     @Test
119     void testSetValueColumn() {
120         final String colName = "VALUE_COLUMN";
121         assertSame(params, params.setValueColumn(colName));
122         assertEquals(colName, params.getParameters().get("valueColumn"));
123     }
124 }