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