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.combined;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertNotSame;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertSame;
24  import static org.mockito.Mockito.mock;
25  
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import org.apache.commons.configuration2.beanutils.BeanHelper;
30  import org.apache.commons.configuration2.builder.BuilderParameters;
31  import org.apache.commons.configuration2.builder.FileBasedBuilderParametersImpl;
32  import org.junit.jupiter.api.BeforeEach;
33  import org.junit.jupiter.api.Test;
34  
35  /**
36   * Test class for {@code MultiFileBuilderParametersImpl}.
37   */
38  public class TestMultiFileBuilderParametersImpl {
39  
40      /** The parameters object to be tested. */
41      private MultiFileBuilderParametersImpl params;
42  
43      @BeforeEach
44      public void setUp() throws Exception {
45          params = new MultiFileBuilderParametersImpl();
46      }
47  
48      /**
49       * Tests whether bean property access is possible.
50       */
51      @Test
52      void testBeanProperties() throws Exception {
53          final BuilderParameters bp = mock(BuilderParameters.class);
54          final String pattern = "testPattern";
55          BeanHelper.setProperty(params, "filePattern", pattern);
56          BeanHelper.setProperty(params, "managedBuilderParameters", bp);
57          BeanHelper.setProperty(params, "throwExceptionOnMissing", Boolean.TRUE);
58          final Map<String, Object> map = params.getParameters();
59          assertEquals(Boolean.TRUE, map.get("throwExceptionOnMissing"));
60          assertSame(params, MultiFileBuilderParametersImpl.fromParameters(map));
61          assertEquals(pattern, params.getFilePattern());
62          assertSame(bp, params.getManagedBuilderParameters());
63      }
64  
65      /**
66       * Tests extended cloning functionality.
67       */
68      @Test
69      void testClone() {
70          final FileBasedBuilderParametersImpl managedParams = new FileBasedBuilderParametersImpl();
71          managedParams.setFileName("test.xml");
72          params.setManagedBuilderParameters(managedParams);
73          params.setFilePattern("somePattern");
74          final MultiFileBuilderParametersImpl clone = params.clone();
75          assertEquals(params.getFilePattern(), clone.getFilePattern());
76          assertNotSame(params.getManagedBuilderParameters(), clone.getManagedBuilderParameters());
77          assertEquals(managedParams.getFileHandler().getFileName(),
78                  ((FileBasedBuilderParametersImpl) clone.getManagedBuilderParameters()).getFileHandler().getFileName());
79      }
80  
81      /**
82       * Tests whether an instance can be obtained from a parameters map.
83       */
84      @Test
85      void testFromParametersFound() {
86          final Map<String, Object> map = params.getParameters();
87          assertSame(params, MultiFileBuilderParametersImpl.fromParameters(map, true));
88      }
89  
90      /**
91       * Tests whether a new instance is created if the parameters map does not contain one.
92       */
93      @Test
94      void testFromParametersNewInstance() {
95          params = MultiFileBuilderParametersImpl.fromParameters(new HashMap<>(), true);
96          assertNotNull(params);
97      }
98  
99      /**
100      * Tests whether an instance can be obtained from a map if it cannot be found.
101      */
102     @Test
103     void testFromParatersNotFound() {
104         assertNull(MultiFileBuilderParametersImpl.fromParameters(new HashMap<>()));
105     }
106 
107     /**
108      * Tests whether a file pattern can be set.
109      */
110     @Test
111     void testSetFilePattern() {
112         final String pattern = "somePattern";
113         assertSame(params, params.setFilePattern(pattern));
114         assertEquals(pattern, params.getFilePattern());
115     }
116 
117     /**
118      * Tests whether parameters for managed configurations can be set.
119      */
120     @Test
121     void testSetManagedBuilderParameters() {
122         final BuilderParameters bp = mock(BuilderParameters.class);
123         assertSame(params, params.setManagedBuilderParameters(bp));
124         assertSame(bp, params.getManagedBuilderParameters());
125     }
126 }