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.assertNull;
21 import static org.junit.jupiter.api.Assertions.assertSame;
22 import static org.mockito.Mockito.mock;
23
24 import java.util.Map;
25
26 import org.apache.commons.configuration2.ConfigurationConsumer;
27 import org.apache.commons.configuration2.PropertiesConfiguration;
28 import org.apache.commons.configuration2.PropertiesConfigurationLayout;
29 import org.apache.commons.configuration2.beanutils.BeanHelper;
30 import org.apache.commons.configuration2.ex.ConfigurationException;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33
34
35
36
37 public class TestPropertiesBuilderParametersImpl {
38
39
40 private PropertiesBuilderParametersImpl params;
41
42 @BeforeEach
43 public void setUp() throws Exception {
44 params = new PropertiesBuilderParametersImpl();
45 }
46
47
48
49
50 @Test
51 void testBeanPropertiesAccess() throws Exception {
52 final PropertiesConfiguration.IOFactory factory = mock(PropertiesConfiguration.IOFactory.class);
53 BeanHelper.setProperty(params, "IOFactory", factory);
54 BeanHelper.setProperty(params, "throwExceptionOnMissing", Boolean.TRUE);
55 BeanHelper.setProperty(params, "fileName", "test.properties");
56 assertEquals("test.properties", params.getFileHandler().getFileName());
57 final Map<String, Object> paramsMap = params.getParameters();
58 assertEquals(Boolean.TRUE, paramsMap.get("throwExceptionOnMissing"));
59 assertSame(factory, params.getParameters().get("IOFactory"));
60 }
61
62
63
64
65 @Test
66 void testInheritFrom() {
67 final PropertiesConfiguration.IOFactory factory = mock(PropertiesConfiguration.IOFactory.class);
68 @SuppressWarnings("unchecked")
69 final ConfigurationConsumer<ConfigurationException> includeListener = mock(ConfigurationConsumer.class);
70 params.setIOFactory(factory).setIncludeListener(includeListener).setIncludesAllowed(false).setLayout(new PropertiesConfigurationLayout())
71 .setThrowExceptionOnMissing(true);
72 final PropertiesBuilderParametersImpl params2 = new PropertiesBuilderParametersImpl();
73
74 params2.inheritFrom(params.getParameters());
75 final Map<String, Object> parameters = params2.getParameters();
76 assertEquals(Boolean.TRUE, parameters.get("throwExceptionOnMissing"));
77 assertEquals(includeListener, parameters.get("includeListener"));
78 assertEquals(factory, parameters.get("IOFactory"));
79 assertEquals(Boolean.FALSE, parameters.get("includesAllowed"));
80 assertNull(parameters.get("layout"));
81 }
82
83
84
85
86 @Test
87 void testSetIncludeListener() {
88 @SuppressWarnings("unchecked")
89 final ConfigurationConsumer<ConfigurationException> includeListener = mock(ConfigurationConsumer.class);
90 assertSame(params, params.setIncludeListener(includeListener));
91 assertSame(includeListener, params.getParameters().get("includeListener"));
92 }
93
94
95
96
97 @Test
98 void testSetIncludeListenerProperty() throws ConfigurationException {
99 final ConfigurationConsumer<ConfigurationException> includeListener = PropertiesConfiguration.DEFAULT_INCLUDE_LISTENER;
100 final ConfigurationBuilder<PropertiesConfiguration> builder = new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class)
101 .configure(params.setIncludeListener(includeListener));
102
103 final PropertiesConfiguration config = builder.getConfiguration();
104 assertEquals(includeListener, config.getIncludeListener());
105 }
106
107
108
109
110 @Test
111 void testSetIncludesAllowed() {
112 assertSame(params, params.setIncludesAllowed(true));
113 assertEquals(Boolean.TRUE, params.getParameters().get("includesAllowed"));
114 }
115
116
117
118
119 @Test
120 void testSetIOFactory() {
121 final PropertiesConfiguration.IOFactory factory = mock(PropertiesConfiguration.IOFactory.class);
122 assertSame(params, params.setIOFactory(factory));
123 assertSame(factory, params.getParameters().get("IOFactory"));
124 }
125
126
127
128
129 @Test
130 void testSetIOFactoryProperty() throws ConfigurationException {
131 final PropertiesConfiguration.IOFactory factory = new PropertiesConfiguration.DefaultIOFactory();
132 final ConfigurationBuilder<PropertiesConfiguration> builder = new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class)
133 .configure(params.setIOFactory(factory));
134
135 final PropertiesConfiguration config = builder.getConfiguration();
136 assertEquals(factory, config.getIOFactory());
137 }
138
139
140
141
142 @Test
143 void testSetLayout() {
144 final PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout();
145 assertSame(params, params.setLayout(layout));
146 assertSame(layout, params.getParameters().get("layout"));
147 }
148 }