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.assertNull;
21  import static org.junit.jupiter.api.Assertions.assertSame;
22  import static org.mockito.Mockito.mock;
23  
24  import java.util.Map;
25  
26  import org.apache.commons.configuration2.ConfigurationConsumer;
27  import org.apache.commons.configuration2.PropertiesConfiguration;
28  import org.apache.commons.configuration2.PropertiesConfigurationLayout;
29  import org.apache.commons.configuration2.beanutils.BeanHelper;
30  import org.apache.commons.configuration2.ex.ConfigurationException;
31  import org.junit.jupiter.api.BeforeEach;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Test class for {@code PropertiesBuilderParametersImpl}.
36   */
37  public class TestPropertiesBuilderParametersImpl {
38  
39      /** The parameters object to be tested. */
40      private PropertiesBuilderParametersImpl params;
41  
42      @BeforeEach
43      public void setUp() throws Exception {
44          params = new PropertiesBuilderParametersImpl();
45      }
46  
47      /**
48       * Tests whether properties can be set using BeanUtils.
49       */
50      @Test
51      void testBeanPropertiesAccess() throws Exception {
52          final PropertiesConfiguration.IOFactory factory = mock(PropertiesConfiguration.IOFactory.class);
53          BeanHelper.setProperty(params, "IOFactory", factory);
54          BeanHelper.setProperty(params, "throwExceptionOnMissing", Boolean.TRUE);
55          BeanHelper.setProperty(params, "fileName", "test.properties");
56          assertEquals("test.properties", params.getFileHandler().getFileName());
57          final Map<String, Object> paramsMap = params.getParameters();
58          assertEquals(Boolean.TRUE, paramsMap.get("throwExceptionOnMissing"));
59          assertSame(factory, params.getParameters().get("IOFactory"));
60      }
61  
62      /**
63       * Tests whether properties can be inherited.
64       */
65      @Test
66      void testInheritFrom() {
67          final PropertiesConfiguration.IOFactory factory = mock(PropertiesConfiguration.IOFactory.class);
68          @SuppressWarnings("unchecked")
69          final ConfigurationConsumer<ConfigurationException> includeListener = mock(ConfigurationConsumer.class);
70          params.setIOFactory(factory).setIncludeListener(includeListener).setIncludesAllowed(false).setLayout(new PropertiesConfigurationLayout())
71              .setThrowExceptionOnMissing(true);
72          final PropertiesBuilderParametersImpl params2 = new PropertiesBuilderParametersImpl();
73  
74          params2.inheritFrom(params.getParameters());
75          final Map<String, Object> parameters = params2.getParameters();
76          assertEquals(Boolean.TRUE, parameters.get("throwExceptionOnMissing"));
77          assertEquals(includeListener, parameters.get("includeListener"));
78          assertEquals(factory, parameters.get("IOFactory"));
79          assertEquals(Boolean.FALSE, parameters.get("includesAllowed"));
80          assertNull(parameters.get("layout"));
81      }
82  
83      /**
84       * Tests whether the include listener can be set.
85       */
86      @Test
87      void testSetIncludeListener() {
88          @SuppressWarnings("unchecked")
89          final ConfigurationConsumer<ConfigurationException> includeListener = mock(ConfigurationConsumer.class);
90          assertSame(params, params.setIncludeListener(includeListener));
91          assertSame(includeListener, params.getParameters().get("includeListener"));
92      }
93  
94      /**
95       * Tests whether the IncludeListener property can be correctly set.
96       */
97      @Test
98      void testSetIncludeListenerProperty() throws ConfigurationException {
99          final ConfigurationConsumer<ConfigurationException> includeListener = PropertiesConfiguration.DEFAULT_INCLUDE_LISTENER;
100         final ConfigurationBuilder<PropertiesConfiguration> builder = new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class)
101             .configure(params.setIncludeListener(includeListener));
102 
103         final PropertiesConfiguration config = builder.getConfiguration();
104         assertEquals(includeListener, config.getIncludeListener());
105     }
106 
107     /**
108      * Tests whether the includesAllowed property can be set.
109      */
110     @Test
111     void testSetIncludesAllowed() {
112         assertSame(params, params.setIncludesAllowed(true));
113         assertEquals(Boolean.TRUE, params.getParameters().get("includesAllowed"));
114     }
115 
116     /**
117      * Tests whether the IO factory can be set.
118      */
119     @Test
120     void testSetIOFactory() {
121         final PropertiesConfiguration.IOFactory factory = mock(PropertiesConfiguration.IOFactory.class);
122         assertSame(params, params.setIOFactory(factory));
123         assertSame(factory, params.getParameters().get("IOFactory"));
124     }
125 
126     /**
127      * Tests whether the IOFactory property can be correctly set. This test is related to CONFIGURATION-648.
128      */
129     @Test
130     void testSetIOFactoryProperty() throws ConfigurationException {
131         final PropertiesConfiguration.IOFactory factory = new PropertiesConfiguration.DefaultIOFactory();
132         final ConfigurationBuilder<PropertiesConfiguration> builder = new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class)
133             .configure(params.setIOFactory(factory));
134 
135         final PropertiesConfiguration config = builder.getConfiguration();
136         assertEquals(factory, config.getIOFactory());
137     }
138 
139     /**
140      * Tests whether the layout object can be set.
141      */
142     @Test
143     void testSetLayout() {
144         final PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout();
145         assertSame(params, params.setLayout(layout));
146         assertSame(layout, params.getParameters().get("layout"));
147     }
148 }