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.configuration2.event;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertInstanceOf;
21  
22  import java.util.ArrayList;
23  import java.util.Collection;
24  
25  import org.apache.commons.configuration2.AbstractConfiguration;
26  import org.apache.commons.configuration2.BaseHierarchicalConfiguration;
27  import org.apache.commons.configuration2.HierarchicalConfiguration;
28  import org.apache.commons.configuration2.tree.ImmutableNode;
29  import org.apache.commons.configuration2.tree.NodeHandler;
30  import org.apache.commons.configuration2.tree.NodeStructureHelper;
31  import org.apache.commons.configuration2.tree.QueryResult;
32  import org.junit.jupiter.api.Test;
33  
34  /**
35   * Test class for the events generated by hierarchical configurations.
36   */
37  public class TestHierarchicalConfigurationEvents extends AbstractTestConfigurationEvents {
38      /**
39       * Tests whether a received event contains a correct subnode event.
40       *
41       * @param event the event object
42       * @param before the expected before flag
43       */
44      private void checkSubnodeEvent(final ConfigurationEvent event, final boolean before) {
45          assertEquals(before, event.isBeforeUpdate());
46          final ConfigurationEvent evSub = assertInstanceOf(ConfigurationEvent.class, event.getPropertyValue());
47          assertEquals(ConfigurationEvent.ADD_PROPERTY, evSub.getEventType());
48          assertEquals("newProp", evSub.getPropertyName());
49          assertEquals("newValue", evSub.getPropertyValue());
50          assertEquals(before, evSub.isBeforeUpdate());
51      }
52  
53      @Override
54      protected AbstractConfiguration createConfiguration() {
55          return new BaseHierarchicalConfiguration();
56      }
57  
58      /**
59       * Tests events generated by addNodes() when the list of nodes is empty. In this case no events should be generated.
60       */
61      @Test
62      public void testAddNodesEmptyEvent() {
63          ((BaseHierarchicalConfiguration) config).addNodes(TEST_PROPNAME, new ArrayList<>());
64          listener.done();
65      }
66  
67      /**
68       * Tests events generated by the addNodes() method.
69       */
70      @Test
71      public void testAddNodesEvent() {
72          final BaseHierarchicalConfiguration hc = (BaseHierarchicalConfiguration) config;
73          final Collection<ImmutableNode> nodes = new ArrayList<>(1);
74          nodes.add(NodeStructureHelper.createNode("a_key", TEST_PROPVALUE));
75          hc.addNodes(TEST_PROPNAME, nodes);
76          listener.checkEvent(ConfigurationEvent.ADD_NODES, TEST_PROPNAME, nodes, true);
77          listener.checkEvent(ConfigurationEvent.ADD_NODES, TEST_PROPNAME, nodes, false);
78          listener.done();
79      }
80  
81      /**
82       * Tests events generated by the clearTree() method.
83       */
84      @Test
85      public void testClearTreeEvent() {
86          final BaseHierarchicalConfiguration hc = (BaseHierarchicalConfiguration) config;
87          final String key = EXIST_PROPERTY.substring(0, EXIST_PROPERTY.indexOf('.'));
88          final NodeHandler<ImmutableNode> nodeHandler = hc.getNodeModel().getNodeHandler();
89          final Collection<QueryResult<ImmutableNode>> nodes = hc.getExpressionEngine().query(nodeHandler.getRootNode(), key, nodeHandler);
90          hc.clearTree(key);
91          listener.checkEvent(ConfigurationEvent.CLEAR_TREE, key, null, true);
92          listener.checkEvent(ConfigurationEvent.CLEAR_TREE, key, nodes, false);
93          listener.done();
94      }
95  
96      /**
97       * Tests whether manipulations of a connected sub configuration trigger correct events.
98       */
99      @Test
100     public void testSubConfigurationChangedEventConnected() {
101         final HierarchicalConfiguration<ImmutableNode> sub = ((BaseHierarchicalConfiguration) config).configurationAt(EXIST_PROPERTY, true);
102         sub.addProperty("newProp", "newValue");
103         checkSubnodeEvent(listener.nextEvent(ConfigurationEvent.SUBNODE_CHANGED), true);
104         checkSubnodeEvent(listener.nextEvent(ConfigurationEvent.SUBNODE_CHANGED), false);
105         listener.done();
106     }
107 
108     /**
109      * Tests that no events are generated for a disconnected sub configuration.
110      */
111     @Test
112     public void testSubConfigurationChangedEventNotConnected() {
113         final HierarchicalConfiguration<ImmutableNode> sub = ((BaseHierarchicalConfiguration) config).configurationAt(EXIST_PROPERTY);
114         sub.addProperty("newProp", "newValue");
115         listener.done();
116     }
117 }