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.assertSame;
21  import static org.mockito.Mockito.mock;
22  
23  import java.util.Map;
24  
25  import org.apache.commons.configuration2.beanutils.BeanHelper;
26  import org.apache.commons.configuration2.tree.ExpressionEngine;
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Test class for {@code HierarchicalBuilderParametersImpl}.
32   */
33  public class TestHierarchicalBuilderParametersImpl {
34  
35      /** The parameters object to be tested. */
36      private HierarchicalBuilderParametersImpl params;
37  
38      @BeforeEach
39      public void setUp() throws Exception {
40          params = new HierarchicalBuilderParametersImpl();
41      }
42  
43      /**
44       * Tests whether properties can be set via BeanUtils.
45       */
46      @Test
47      void testBeanPropertiesAccess() throws Exception {
48          final ExpressionEngine engine = mock(ExpressionEngine.class);
49          BeanHelper.setProperty(params, "expressionEngine", engine);
50          BeanHelper.setProperty(params, "throwExceptionOnMissing", Boolean.TRUE);
51          final Map<String, Object> map = params.getParameters();
52          assertSame(engine, map.get("expressionEngine"));
53          assertEquals(Boolean.TRUE, map.get("throwExceptionOnMissing"));
54      }
55  
56      /**
57       * Tests whether inheritFrom() copies additional properties.
58       */
59      @Test
60      void testInheritFrom() {
61          final ExpressionEngine engine = mock(ExpressionEngine.class);
62          final HierarchicalBuilderParametersImpl params = new HierarchicalBuilderParametersImpl();
63          params.setExpressionEngine(engine);
64          params.setThrowExceptionOnMissing(true);
65          final HierarchicalBuilderParametersImpl params2 = new HierarchicalBuilderParametersImpl();
66  
67          params2.inheritFrom(params.getParameters());
68          final Map<String, Object> parameters = params2.getParameters();
69          assertEquals(Boolean.TRUE, parameters.get("throwExceptionOnMissing"));
70          assertEquals(engine, parameters.get("expressionEngine"));
71      }
72  
73      /**
74       * Tests whether the expression engine can be set.
75       */
76      @Test
77      void testSetExpressionEngine() {
78          final ExpressionEngine engine = mock(ExpressionEngine.class);
79          assertSame(params, params.setExpressionEngine(engine));
80          assertSame(engine, params.getParameters().get("expressionEngine"));
81      }
82  }