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