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