View Javadoc

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 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   * Test class for the events generated by hierarchical configurations.
34   *
35   * @version $Id: TestHierarchicalConfigurationEvents.java 1225648 2011-12-29 20:55:07Z oheger $
36   */
37  public class TestHierarchicalConfigurationEvents extends
38          AbstractTestConfigurationEvents
39  {
40      @Override
41      protected AbstractConfiguration createConfiguration()
42      {
43          return new HierarchicalConfiguration();
44      }
45  
46      /**
47       * Tests events generated by the clearTree() method.
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       * Tests events generated by the addNodes() method.
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       * Tests events generated by addNodes() when the list of nodes is empty. In
83       * this case no events should be generated.
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       * Tests whether manipulations of a subnode configuration trigger correct
95       * events.
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      * Tests whether a received event contains a correct subnode event.
114      *
115      * @param event the event object
116      * @param before the expected before flag
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 }