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