1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration.beanutils;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
22 import static org.junit.Assert.assertTrue;
23
24 import java.util.Map;
25
26 import org.apache.commons.configuration.HierarchicalConfiguration;
27 import org.apache.commons.configuration.SubnodeConfiguration;
28 import org.apache.commons.configuration.tree.ConfigurationNode;
29 import org.junit.Test;
30
31
32
33
34
35
36
37
38
39
40 public class TestXMLBeanDeclaration
41 {
42
43 static final String[] TEST_PROPS =
44 { "firstName", "lastName", "department", "age", "hobby"};
45
46
47 static final String[] TEST_VALUES =
48 { "John", "Smith", "Engineering", "42", "TV"};
49
50
51 static final String[] COMPLEX_PROPS =
52 { "address", "car"};
53
54
55 static final String[] COMPLEX_CLASSES =
56 { "org.apache.commons.configuration.test.AddressTest",
57 "org.apache.commons.configuration.test.CarTest"};
58
59
60 static final String[][] COMPLEX_ATTRIBUTES =
61 {
62 { "street", "zip", "city", "country"},
63 { "brand", "color"}};
64
65
66 static final String[][] COMPLEX_VALUES =
67 {
68 { "Baker Street", "12354", "London", "UK"},
69 { "Bentley", "silver"}};
70
71
72 static final String KEY = "myBean";
73
74
75 static final String VARS = "variables.";
76
77
78 XMLBeanDeclaration decl;
79
80
81
82
83
84 @Test(expected = IllegalArgumentException.class)
85 public void testInitFromNullNode()
86 {
87 decl = new XMLBeanDeclaration(new HierarchicalConfiguration().configurationAt(null),
88 (ConfigurationNode) null);
89 }
90
91
92
93
94
95 @Test(expected = IllegalArgumentException.class)
96 public void testInitFromNullConfiguration()
97 {
98 decl = new XMLBeanDeclaration((HierarchicalConfiguration) null);
99 }
100
101
102
103
104
105 @Test(expected = IllegalArgumentException.class)
106 public void testInitFromNullConfigurationAndKey()
107 {
108 decl = new XMLBeanDeclaration(null, KEY);
109 }
110
111
112
113
114
115 @Test(expected = IllegalArgumentException.class)
116 public void testInitFromNullConfigurationAndNode()
117 {
118 decl = new XMLBeanDeclaration(null, new HierarchicalConfiguration()
119 .getRoot());
120 }
121
122
123
124
125 @Test
126 public void testGetBeanClassName()
127 {
128 HierarchicalConfiguration config = new HierarchicalConfiguration();
129 config.addProperty(KEY + "[@config-class]", getClass().getName());
130 decl = new XMLBeanDeclaration(config, KEY);
131 assertEquals("Wrong class name", getClass().getName(), decl
132 .getBeanClassName());
133 }
134
135
136
137
138 @Test
139 public void testGetBeanClassNameUndefined()
140 {
141 decl = new XMLBeanDeclaration(new HierarchicalConfiguration());
142 assertNull(decl.getBeanClassName());
143 }
144
145
146
147
148 @Test
149 public void testGetBeanFactoryName()
150 {
151 HierarchicalConfiguration config = new HierarchicalConfiguration();
152 config.addProperty(KEY + "[@config-factory]", "myFactory");
153 decl = new XMLBeanDeclaration(config, KEY);
154 assertEquals("Wrong factory name", "myFactory", decl
155 .getBeanFactoryName());
156 }
157
158
159
160
161 @Test
162 public void testGetBeanFactoryNameUndefined()
163 {
164 decl = new XMLBeanDeclaration(new HierarchicalConfiguration());
165 assertNull(decl.getBeanFactoryName());
166 }
167
168
169
170
171 @Test
172 public void testGetBeanFactoryParameter()
173 {
174 HierarchicalConfiguration config = new HierarchicalConfiguration();
175 config
176 .addProperty(KEY + "[@config-factoryParam]",
177 "myFactoryParameter");
178 decl = new XMLBeanDeclaration(config, KEY);
179 assertEquals("Wrong factory parameter", "myFactoryParameter", decl
180 .getBeanFactoryParameter());
181 }
182
183
184
185
186 @Test
187 public void testGetBeanFactoryParameterUndefined()
188 {
189 decl = new XMLBeanDeclaration(new HierarchicalConfiguration());
190 assertNull(decl.getBeanFactoryParameter());
191 }
192
193
194
195
196
197 @Test
198 public void testGetBeanProperties()
199 {
200 HierarchicalConfiguration config = new HierarchicalConfiguration();
201 setupBeanDeclaration(config, KEY, TEST_PROPS, TEST_VALUES);
202 decl = new XMLBeanDeclaration(config, KEY);
203 checkProperties(decl, TEST_PROPS, TEST_VALUES);
204 }
205
206
207
208
209
210 @Test
211 public void testGetBeanPropertiesWithReservedAttributes()
212 {
213 HierarchicalConfiguration config = new HierarchicalConfiguration();
214 setupBeanDeclaration(config, KEY, TEST_PROPS, TEST_VALUES);
215 config.addProperty(KEY + "[@config-testattr]", "yes");
216 config.addProperty(KEY + "[@config-anothertest]", "this, too");
217 decl = new XMLBeanDeclaration(config, KEY);
218 checkProperties(decl, TEST_PROPS, TEST_VALUES);
219 }
220
221
222
223
224 @Test
225 public void testGetBeanPropertiesEmpty()
226 {
227 decl = new XMLBeanDeclaration(new HierarchicalConfiguration());
228 Map<String, Object> props = decl.getBeanProperties();
229 assertTrue("Properties found", props == null || props.isEmpty());
230 }
231
232
233
234
235
236 private HierarchicalConfiguration prepareNestedBeanDeclarations()
237 {
238 HierarchicalConfiguration config = new HierarchicalConfiguration();
239 setupBeanDeclaration(config, KEY, TEST_PROPS, TEST_VALUES);
240 for (int i = 0; i < COMPLEX_PROPS.length; i++)
241 {
242 setupBeanDeclaration(config, KEY + '.' + COMPLEX_PROPS[i],
243 COMPLEX_ATTRIBUTES[i], COMPLEX_VALUES[i]);
244 config.addProperty(
245 KEY + '.' + COMPLEX_PROPS[i] + "[@config-class]",
246 COMPLEX_CLASSES[i]);
247 }
248 return config;
249 }
250
251
252
253
254 @Test
255 public void testGetNestedBeanDeclarations()
256 {
257 HierarchicalConfiguration config = prepareNestedBeanDeclarations();
258 decl = new XMLBeanDeclaration(config, KEY);
259 checkProperties(decl, TEST_PROPS, TEST_VALUES);
260
261 Map<String, Object> nested = decl.getNestedBeanDeclarations();
262 assertEquals("Wrong number of nested declarations",
263 COMPLEX_PROPS.length, nested.size());
264 for (int i = 0; i < COMPLEX_PROPS.length; i++)
265 {
266 XMLBeanDeclaration d = (XMLBeanDeclaration) nested
267 .get(COMPLEX_PROPS[i]);
268 assertNotNull("No declaration found for " + COMPLEX_PROPS[i], d);
269 checkProperties(d, COMPLEX_ATTRIBUTES[i], COMPLEX_VALUES[i]);
270 assertEquals("Wrong bean class", COMPLEX_CLASSES[i], d
271 .getBeanClassName());
272 }
273 }
274
275
276
277
278
279 @Test
280 public void testGetNestedBeanDeclarationsFactoryMethod()
281 {
282 HierarchicalConfiguration config = prepareNestedBeanDeclarations();
283 decl = new XMLBeanDeclaration(config, KEY)
284 {
285 @Override
286 protected BeanDeclaration createBeanDeclaration(
287 ConfigurationNode node)
288 {
289 return new XMLBeanDeclarationTestImpl(getConfiguration()
290 .configurationAt(node.getName()), node);
291 }
292 };
293 Map<String, Object> nested = decl.getNestedBeanDeclarations();
294 for (int i = 0; i < COMPLEX_PROPS.length; i++)
295 {
296 Object d = nested.get(COMPLEX_PROPS[i]);
297 assertTrue("Wrong class for bean declaration: " + d,
298 d instanceof XMLBeanDeclarationTestImpl);
299 }
300 }
301
302
303
304
305 @Test
306 public void testGetNestedBeanDeclarationsEmpty()
307 {
308 HierarchicalConfiguration config = new HierarchicalConfiguration();
309 setupBeanDeclaration(config, KEY, TEST_PROPS, TEST_VALUES);
310 decl = new XMLBeanDeclaration(config, KEY);
311 Map<String, Object> nested = decl.getNestedBeanDeclarations();
312 assertTrue("Found nested declarations", nested == null
313 || nested.isEmpty());
314 }
315
316
317
318
319 @Test
320 public void testGetInterpolatedBeanProperties()
321 {
322 HierarchicalConfiguration config = new HierarchicalConfiguration();
323 String[] varValues = new String[TEST_PROPS.length];
324 for(int i = 0; i < TEST_PROPS.length; i++)
325 {
326 varValues[i] = "${" + VARS + TEST_PROPS[i] + "}";
327 config.addProperty(VARS + TEST_PROPS[i], TEST_VALUES[i]);
328 }
329 setupBeanDeclaration(config, KEY, TEST_PROPS, varValues);
330 decl = new XMLBeanDeclaration(config, KEY);
331 checkProperties(decl, TEST_PROPS, TEST_VALUES);
332 }
333
334
335
336
337
338 @Test(expected = IllegalArgumentException.class)
339 public void testInitFromUndefinedKey()
340 {
341 HierarchicalConfiguration config = new HierarchicalConfiguration();
342 setupBeanDeclaration(config, KEY, TEST_PROPS, TEST_VALUES);
343 decl = new XMLBeanDeclaration(config, "undefined_key");
344 }
345
346
347
348
349
350
351
352 @Test
353 public void testInitFromUndefinedKeyOptional()
354 {
355 HierarchicalConfiguration config = new HierarchicalConfiguration();
356 setupBeanDeclaration(config, KEY, TEST_PROPS, TEST_VALUES);
357 decl = new XMLBeanDeclaration(config, "undefined_key", true);
358 assertNull("Found a bean class", decl.getBeanClassName());
359 }
360
361
362
363
364
365 @Test(expected = IllegalArgumentException.class)
366 public void testInitFromMultiValueKey()
367 {
368 HierarchicalConfiguration config = new HierarchicalConfiguration();
369 config.addProperty(KEY, "myFirstKey");
370 config.addProperty(KEY, "mySecondKey");
371 decl = new XMLBeanDeclaration(config, KEY);
372 }
373
374
375
376
377
378
379
380
381
382
383 private void setupBeanDeclaration(HierarchicalConfiguration config,
384 String key, String[] names, String[] values)
385 {
386 for (int i = 0; i < names.length; i++)
387 {
388 config.addProperty(key + "[@" + names[i] + "]", values[i]);
389 }
390 }
391
392
393
394
395
396
397
398
399 private void checkProperties(BeanDeclaration beanDecl, String[] names,
400 String[] values)
401 {
402 Map<String, Object> props = beanDecl.getBeanProperties();
403 assertEquals("Wrong number of properties", names.length, props.size());
404 for (int i = 0; i < names.length; i++)
405 {
406 assertTrue("Property " + names[i] + " not contained", props
407 .containsKey(names[i]));
408 assertEquals("Wrong value for property " + names[i], values[i],
409 props.get(names[i]));
410 }
411 }
412
413
414
415
416
417 private static class XMLBeanDeclarationTestImpl extends XMLBeanDeclaration
418 {
419 public XMLBeanDeclarationTestImpl(SubnodeConfiguration config,
420 ConfigurationNode node)
421 {
422 super(config, node);
423 }
424 }
425 }