1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.configuration.event;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  
22  import org.apache.commons.configuration.AbstractConfiguration;
23  import org.apache.commons.configuration.HierarchicalConfiguration;
24  import org.apache.commons.configuration.SubnodeConfiguration;
25  import org.apache.commons.configuration.tree.DefaultConfigurationNode;
26  
27  /***
28   * Test class for the events generated by hierarchical configurations.
29   *
30   * @version $Id: TestHierarchicalConfigurationEvents.java 531254 2007-04-22 18:54:57Z oheger $
31   */
32  public class TestHierarchicalConfigurationEvents extends
33          AbstractTestConfigurationEvents
34  {
35      protected AbstractConfiguration createConfiguration()
36      {
37          return new HierarchicalConfiguration();
38      }
39  
40      /***
41       * Tests events generated by the clearTree() method.
42       */
43      public void testClearTreeEvent()
44      {
45          HierarchicalConfiguration hc = (HierarchicalConfiguration) config;
46          String key = EXIST_PROPERTY.substring(0, EXIST_PROPERTY.indexOf('.'));
47          Collection nodes = hc.getExpressionEngine()
48                  .query(hc.getRootNode(), key);
49          hc.clearTree(key);
50          l.checkEvent(HierarchicalConfiguration.EVENT_CLEAR_TREE, key, null,
51                  true);
52          l.checkEvent(HierarchicalConfiguration.EVENT_CLEAR_TREE, key, nodes,
53                  false);
54          l.done();
55      }
56  
57      /***
58       * Tests events generated by the addNodes() method.
59       */
60      public void testAddNodesEvent()
61      {
62          HierarchicalConfiguration hc = (HierarchicalConfiguration) config;
63          Collection nodes = new ArrayList(1);
64          nodes.add(new DefaultConfigurationNode("a_key", TEST_PROPVALUE));
65          hc.addNodes(TEST_PROPNAME, nodes);
66          l.checkEvent(HierarchicalConfiguration.EVENT_ADD_NODES, TEST_PROPNAME,
67                  nodes, true);
68          l.checkEvent(HierarchicalConfiguration.EVENT_ADD_NODES, TEST_PROPNAME,
69                  nodes, false);
70          l.done();
71      }
72  
73      /***
74       * Tests events generated by addNodes() when the list of nodes is empty. In
75       * this case no events should be generated.
76       */
77      public void testAddNodesEmptyEvent()
78      {
79          ((HierarchicalConfiguration) config).addNodes(TEST_PROPNAME,
80                  new ArrayList());
81          l.done();
82      }
83  
84      /***
85       * Tests whether manipulations of a subnode configuration trigger correct
86       * events.
87       */
88      public void testSubnodeChangedEvent()
89      {
90          SubnodeConfiguration sub = ((HierarchicalConfiguration) config)
91                  .configurationAt(EXIST_PROPERTY);
92          sub.addProperty("newProp", "newValue");
93          checkSubnodeEvent(l
94                  .nextEvent(HierarchicalConfiguration.EVENT_SUBNODE_CHANGED),
95                  true);
96          checkSubnodeEvent(l
97                  .nextEvent(HierarchicalConfiguration.EVENT_SUBNODE_CHANGED),
98                  false);
99          l.done();
100     }
101 
102     /***
103      * Tests whether a received event contains a correct subnode event.
104      *
105      * @param event the event object
106      * @param before the expected before flag
107      */
108     private void checkSubnodeEvent(ConfigurationEvent event, boolean before)
109     {
110         assertEquals("Wrong before flag of nesting event", before, event
111                 .isBeforeUpdate());
112         assertTrue("No subnode event found in value",
113                 event.getPropertyValue() instanceof ConfigurationEvent);
114         ConfigurationEvent evSub = (ConfigurationEvent) event
115                 .getPropertyValue();
116         assertEquals("Wrong event type",
117                 HierarchicalConfiguration.EVENT_ADD_PROPERTY, evSub.getType());
118         assertEquals("Wrong property name", "newProp", evSub.getPropertyName());
119         assertEquals("Wrong property value", "newValue", evSub
120                 .getPropertyValue());
121         assertEquals("Wrong before flag", before, evSub.isBeforeUpdate());
122     }
123 }