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