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.tree.DefaultConfigurationNode;
25  
26  /***
27   * Test class for the events generated by hierarchical configurations.
28   *
29   * @version $Id: TestHierarchicalConfigurationEvents.java 439648 2006-09-02 20:42:10Z oheger $
30   */
31  public class TestHierarchicalConfigurationEvents extends
32          AbstractTestConfigurationEvents
33  {
34      protected AbstractConfiguration createConfiguration()
35      {
36          return new HierarchicalConfiguration();
37      }
38  
39      /***
40       * Tests events generated by the clearTree() method.
41       */
42      public void testClearTreeEvent()
43      {
44          HierarchicalConfiguration hc = (HierarchicalConfiguration) config;
45          String key = EXIST_PROPERTY.substring(0, EXIST_PROPERTY.indexOf('.'));
46          Collection nodes = hc.getExpressionEngine()
47                  .query(hc.getRootNode(), key);
48          hc.clearTree(key);
49          l.checkEvent(HierarchicalConfiguration.EVENT_CLEAR_TREE, key, null,
50                  true);
51          l.checkEvent(HierarchicalConfiguration.EVENT_CLEAR_TREE, key, nodes,
52                  false);
53          l.done();
54      }
55  
56      /***
57       * Tests events generated by the addNodes() method.
58       */
59      public void testAddNodesEvent()
60      {
61          HierarchicalConfiguration hc = (HierarchicalConfiguration) config;
62          Collection nodes = new ArrayList(1);
63          nodes.add(new DefaultConfigurationNode("a_key", TEST_PROPVALUE));
64          hc.addNodes(TEST_PROPNAME, nodes);
65          l.checkEvent(HierarchicalConfiguration.EVENT_ADD_NODES, TEST_PROPNAME,
66                  nodes, true);
67          l.checkEvent(HierarchicalConfiguration.EVENT_ADD_NODES, TEST_PROPNAME,
68                  nodes, false);
69          l.done();
70      }
71  
72      /***
73       * Tests events generated by addNodes() when the list of nodes is empty. In
74       * this case no events should be generated.
75       */
76      public void testAddNodesEmptyEvent()
77      {
78          ((HierarchicalConfiguration) config).addNodes(TEST_PROPNAME,
79                  new ArrayList());
80          l.done();
81      }
82  }