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.assertThrows;
21  import static org.mockito.Mockito.mock;
22  
23  import java.util.Map;
24  
25  import org.apache.commons.configuration2.ex.ConfigurationRuntimeException;
26  import org.apache.commons.configuration2.tree.ExpressionEngine;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Test class for {@code CopyObjectDefaultHandler}.
31   */
32  public class TestCopyObjectDefaultHandler {
33  
34      /**
35       * Tests whether a base type can be initialized with default values. Unknown properties should silently be ignored.
36       */
37      @Test
38      void testInitializeDefaultsBaseType() {
39          final Long refresh = 50000L;
40          final XMLBuilderParametersImpl paramsXml = new XMLBuilderParametersImpl();
41          paramsXml.setValidating(true).setExpressionEngine(mock(ExpressionEngine.class)).setReloadingRefreshDelay(refresh);
42          final CopyObjectDefaultHandler handler = new CopyObjectDefaultHandler(paramsXml);
43          final FileBasedBuilderParametersImpl paramsFb = new FileBasedBuilderParametersImpl();
44          handler.initializeDefaults(paramsFb);
45          assertEquals(refresh, paramsFb.getReloadingRefreshDelay());
46      }
47  
48      /**
49       * Tests whether exceptions during copying are re-thrown as runtime exceptions.
50       */
51      @Test
52      void testInitializeDefaultsException() {
53          final ExpressionEngine engine = mock(ExpressionEngine.class);
54          final XMLBuilderParametersImpl source = new XMLBuilderParametersImpl();
55          source.setExpressionEngine(engine);
56          final XMLBuilderParametersImpl dest = new XMLBuilderParametersImpl() {
57              @Override
58              public HierarchicalBuilderParametersImpl setExpressionEngine(final ExpressionEngine engine) {
59                  throw new ConfigurationRuntimeException("Test exception");
60              }
61          };
62  
63          final CopyObjectDefaultHandler handler = new CopyObjectDefaultHandler(source);
64          assertThrows(ConfigurationRuntimeException.class, () -> handler.initializeDefaults(dest));
65      }
66  
67      /**
68       * Tests whether default values can be copied onto an object of the same type.
69       */
70      @Test
71      void testInitializeDefaultsSameType() {
72          final Long refresh = 50000L;
73          final FileBasedBuilderParametersImpl source = new FileBasedBuilderParametersImpl();
74          source.setReloadingRefreshDelay(refresh).setThrowExceptionOnMissing(true);
75          final CopyObjectDefaultHandler handler = new CopyObjectDefaultHandler(source);
76          final FileBasedBuilderParametersImpl copy = new FileBasedBuilderParametersImpl();
77          handler.initializeDefaults(copy);
78          final Map<String, Object> map = copy.getParameters();
79          assertEquals(Boolean.TRUE, map.get("throwExceptionOnMissing"));
80          assertEquals(refresh, copy.getReloadingRefreshDelay());
81      }
82  
83      /**
84       * Tries to create an instance without a source object.
85       */
86      @Test
87      void testInitNull() {
88          assertThrows(IllegalArgumentException.class, () -> new CopyObjectDefaultHandler(null));
89      }
90  }