1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
31
32 public class TestCopyObjectDefaultHandler {
33
34
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
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
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
84
85 @Test
86 public void testInitNull() {
87 assertThrows(IllegalArgumentException.class, () -> new CopyObjectDefaultHandler(null));
88 }
89 }