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
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
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
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
85
86 @Test
87 void testInitNull() {
88 assertThrows(IllegalArgumentException.class, () -> new CopyObjectDefaultHandler(null));
89 }
90 }