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