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