1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration2.builder.combined;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21 import static org.junit.jupiter.api.Assertions.assertNull;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import org.apache.commons.beanutils.DynaClass;
30 import org.apache.commons.beanutils.LazyDynaBean;
31 import org.apache.commons.beanutils.PropertyUtils;
32 import org.apache.commons.configuration2.builder.BasicBuilderParameters;
33 import org.apache.commons.configuration2.builder.FileBasedBuilderParametersImpl;
34 import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
35 import org.apache.commons.configuration2.convert.ListDelimiterHandler;
36 import org.junit.jupiter.api.Test;
37
38
39
40
41 public class TestMultiWrapDynaBean {
42
43
44
45
46 public static class WrappedBeanTestImpl {
47
48
49 private final Map<String, String> mapProperties = new HashMap<>();
50
51
52 private final int[] indexedValues = new int[8];
53
54
55 private String text;
56
57 public int getIndexedProperty(final int idx) {
58 return indexedValues[idx];
59 }
60
61 public String getMapProperty(final String key) {
62 return mapProperties.get(key);
63 }
64
65 public String getText() {
66 return text;
67 }
68
69 public void setIndexedProperty(final int idx, final int value) {
70 indexedValues[idx] = value;
71 }
72
73 public void setMapProperty(final String key, final String value) {
74 mapProperties.put(key, value);
75 }
76
77 public void setText(final String text) {
78 this.text = text;
79 }
80 }
81
82
83 private static final String MAPPED_PROPERTY = "testMappedProperty";
84
85
86 private BasicBuilderParameters params;
87
88
89 private WrappedBeanTestImpl wrapBean;
90
91
92 private LazyDynaBean wrapDynaBean;
93
94
95
96
97
98
99
100 private MultiWrapDynaBean createBean(final boolean withDynaBean) {
101 params = new BasicBuilderParameters();
102 wrapBean = new WrappedBeanTestImpl();
103 final Collection<Object> beans = new ArrayList<>();
104 beans.add(params);
105 beans.add(wrapBean);
106 if (withDynaBean) {
107 wrapDynaBean = new LazyDynaBean();
108 wrapDynaBean.set(MAPPED_PROPERTY, "someKey", "somValue");
109 beans.add(wrapDynaBean);
110 }
111 return new MultiWrapDynaBean(beans);
112 }
113
114
115
116
117 @Test
118 void testContains() {
119 final MultiWrapDynaBean bean = createBean(false);
120 assertThrows(UnsupportedOperationException.class, () -> bean.contains(MAPPED_PROPERTY, "someKey"));
121 }
122
123
124
125
126 @Test
127 void testGetDynaClass() {
128 final DynaClass cls = createBean(false).getDynaClass();
129 assertNotNull(cls.getDynaProperty("throwExceptionOnMissing"));
130 assertNotNull(cls.getDynaProperty("text"));
131 }
132
133
134
135
136 @Test
137 void testGetDynaClassName() {
138 assertNull(createBean(false).getDynaClass().getName());
139 }
140
141
142
143
144 @Test
145 void testGetDynaClassNewInstance() {
146 final DynaClass dynaClass = createBean(false).getDynaClass();
147 assertThrows(UnsupportedOperationException.class, dynaClass::newInstance);
148 }
149
150
151
152
153 @Test
154 void testGetIndexedProperty() throws Exception {
155 final MultiWrapDynaBean bean = createBean(false);
156 wrapBean.setIndexedProperty(3, 20121117);
157 assertEquals(20121117, PropertyUtils.getIndexedProperty(bean, "indexedProperty", 3));
158 }
159
160
161
162
163 @Test
164 void testGetMappedProperty() throws Exception {
165 final MultiWrapDynaBean bean = createBean(true);
166 final String key = "testKey";
167 final String value = "Hello World";
168 wrapDynaBean.set(MAPPED_PROPERTY, key, value);
169 assertEquals(value, PropertyUtils.getMappedProperty(bean, MAPPED_PROPERTY, key));
170 }
171
172
173
174
175 @Test
176 void testGetPropertyUnknown() {
177 final MultiWrapDynaBean bean = createBean(false);
178 assertThrows(IllegalArgumentException.class, () -> bean.get("unknown property"));
179 }
180
181
182
183
184 @Test
185 void testGetSimpleProperty() throws Exception {
186 final MultiWrapDynaBean bean = createBean(false);
187 final String text = "testText";
188 wrapBean.setText(text);
189 assertEquals(text, PropertyUtils.getProperty(bean, "text"));
190 }
191
192
193
194
195 @Test
196 void testOrderOfProperties() throws Exception {
197 final Collection<Object> beans = new ArrayList<>();
198 params = new BasicBuilderParameters();
199 beans.add(params);
200 beans.add(new FileBasedBuilderParametersImpl());
201 for (int i = 0; i < 32; i++) {
202 beans.add(new BasicBuilderParameters());
203 }
204 final MultiWrapDynaBean bean = new MultiWrapDynaBean(beans);
205 final ListDelimiterHandler listHandler = new DefaultListDelimiterHandler('+');
206 PropertyUtils.setProperty(bean, "throwExceptionOnMissing", Boolean.TRUE);
207 PropertyUtils.setProperty(bean, "listDelimiterHandler", listHandler);
208 final Map<String, Object> map = params.getParameters();
209 assertEquals(Boolean.TRUE, map.get("throwExceptionOnMissing"));
210 assertEquals(listHandler, map.get("listDelimiterHandler"));
211 }
212
213
214
215
216 @Test
217 void testRemove() {
218 final MultiWrapDynaBean bean = createBean(false);
219 assertThrows(UnsupportedOperationException.class, () -> bean.remove(MAPPED_PROPERTY, "someKey"));
220 }
221
222
223
224
225 @Test
226 void testSetIndexedProperty() throws Exception {
227 PropertyUtils.setIndexedProperty(createBean(false), "indexedProperty", 1, 42);
228 assertEquals(42, wrapBean.getIndexedProperty(1));
229 }
230
231
232
233
234 @Test
235 void testSetMappedProperty() throws Exception {
236 final MultiWrapDynaBean bean = createBean(true);
237 final String key = "testKey";
238 final String text = "Hello World";
239 PropertyUtils.setMappedProperty(bean, MAPPED_PROPERTY, key, text);
240 assertEquals(text, wrapDynaBean.get(MAPPED_PROPERTY, key));
241 }
242
243
244
245
246 @Test
247 void testSetSimpleProperty() throws Exception {
248 PropertyUtils.setProperty(createBean(false), "throwExceptionOnMissing", Boolean.TRUE);
249 assertEquals(Boolean.TRUE, params.getParameters().get("throwExceptionOnMissing"));
250 }
251 }