1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.configuration.event;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21
22 import java.util.ArrayList;
23 import java.util.Collection;
24
25 import org.apache.commons.configuration.AbstractConfiguration;
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.apache.commons.configuration.tree.DefaultConfigurationNode;
30 import org.junit.Test;
31
32
33
34
35
36
37 public class TestHierarchicalConfigurationEvents extends
38 AbstractTestConfigurationEvents
39 {
40 @Override
41 protected AbstractConfiguration createConfiguration()
42 {
43 return new HierarchicalConfiguration();
44 }
45
46
47
48
49 @Test
50 public void testClearTreeEvent()
51 {
52 HierarchicalConfiguration hc = (HierarchicalConfiguration) config;
53 String key = EXIST_PROPERTY.substring(0, EXIST_PROPERTY.indexOf('.'));
54 Collection<ConfigurationNode> nodes = hc.getExpressionEngine()
55 .query(hc.getRootNode(), key);
56 hc.clearTree(key);
57 l.checkEvent(HierarchicalConfiguration.EVENT_CLEAR_TREE, key, null,
58 true);
59 l.checkEvent(HierarchicalConfiguration.EVENT_CLEAR_TREE, key, nodes,
60 false);
61 l.done();
62 }
63
64
65
66
67 @Test
68 public void testAddNodesEvent()
69 {
70 HierarchicalConfiguration hc = (HierarchicalConfiguration) config;
71 Collection<ConfigurationNode> nodes = new ArrayList<ConfigurationNode>(1);
72 nodes.add(new DefaultConfigurationNode("a_key", TEST_PROPVALUE));
73 hc.addNodes(TEST_PROPNAME, nodes);
74 l.checkEvent(HierarchicalConfiguration.EVENT_ADD_NODES, TEST_PROPNAME,
75 nodes, true);
76 l.checkEvent(HierarchicalConfiguration.EVENT_ADD_NODES, TEST_PROPNAME,
77 nodes, false);
78 l.done();
79 }
80
81
82
83
84
85 @Test
86 public void testAddNodesEmptyEvent()
87 {
88 ((HierarchicalConfiguration) config).addNodes(TEST_PROPNAME,
89 new ArrayList<ConfigurationNode>());
90 l.done();
91 }
92
93
94
95
96
97 @Test
98 public void testSubnodeChangedEvent()
99 {
100 SubnodeConfiguration sub = ((HierarchicalConfiguration) config)
101 .configurationAt(EXIST_PROPERTY);
102 sub.addProperty("newProp", "newValue");
103 checkSubnodeEvent(l
104 .nextEvent(HierarchicalConfiguration.EVENT_SUBNODE_CHANGED),
105 true);
106 checkSubnodeEvent(l
107 .nextEvent(HierarchicalConfiguration.EVENT_SUBNODE_CHANGED),
108 false);
109 l.done();
110 }
111
112
113
114
115
116
117
118 private void checkSubnodeEvent(ConfigurationEvent event, boolean before)
119 {
120 assertEquals("Wrong before flag of nesting event", before, event
121 .isBeforeUpdate());
122 assertTrue("No subnode event found in value",
123 event.getPropertyValue() instanceof ConfigurationEvent);
124 ConfigurationEvent evSub = (ConfigurationEvent) event
125 .getPropertyValue();
126 assertEquals("Wrong event type",
127 HierarchicalConfiguration.EVENT_ADD_PROPERTY, evSub.getType());
128 assertEquals("Wrong property name", "newProp", evSub.getPropertyName());
129 assertEquals("Wrong property value", "newValue", evSub
130 .getPropertyValue());
131 assertEquals("Wrong before flag", before, evSub.isBeforeUpdate());
132 }
133 }