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.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertSame;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.util.Collections;
25  import java.util.HashSet;
26  import java.util.Set;
27  
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * A test class which tests whether the types of basic configuration events are correctly defined.
32   */
33  public class TestConfigurationEventTypes {
34      /**
35       * Helper method for checking the relevant properties of an error event type.
36       *
37       * @param type the type to be checked
38       */
39      private void checkErrorEvent(final EventType<ConfigurationErrorEvent> type) {
40          assertSame(ConfigurationErrorEvent.ANY, type.getSuperType(), "Wrong super type for " + type);
41      }
42  
43      /**
44       * Helper method for checking the relevant properties of a given event type representing a hierarchical update event.
45       *
46       * @param eventType the event type to check
47       */
48      private void checkHierarchicalEvent(final EventType<ConfigurationEvent> eventType) {
49          assertSame(ConfigurationEvent.ANY_HIERARCHICAL, eventType.getSuperType(), "Wrong super type for " + eventType);
50      }
51  
52      /**
53       * Helper method for checking the relevant properties of a given event type representing a configuration update event.
54       *
55       * @param eventType the event type to check
56       */
57      private void checkUpdateEvent(final EventType<ConfigurationEvent> eventType) {
58          assertSame(ConfigurationEvent.ANY, eventType.getSuperType(), "Wrong super type for " + eventType);
59      }
60  
61      /**
62       * Tests the event type for an add nodes operation.
63       */
64      @Test
65      public void testAddNodesEventType() {
66          checkHierarchicalEvent(ConfigurationEvent.ADD_NODES);
67      }
68  
69      /**
70       * Tests the event type for adding a property.
71       */
72      @Test
73      public void testAddPropertyEventType() {
74          checkUpdateEvent(ConfigurationEvent.ADD_PROPERTY);
75      }
76  
77      /**
78       * Tests the common base event type for error events.
79       */
80      @Test
81      public void testBaseErrorEventType() {
82          assertEquals(Event.ANY, ConfigurationErrorEvent.ANY.getSuperType());
83      }
84  
85      /**
86       * Tests the event type for clearing a whole configuration.
87       */
88      @Test
89      public void testClearEventType() {
90          checkUpdateEvent(ConfigurationEvent.CLEAR);
91      }
92  
93      /**
94       * Tests the event type for clearing a property.
95       */
96      @Test
97      public void testClearPropertyEventType() {
98          checkUpdateEvent(ConfigurationEvent.CLEAR_PROPERTY);
99      }
100 
101     /**
102      * Tests the event type for a clear tree operation.
103      */
104     @Test
105     public void testClearTreeEventType() {
106         checkHierarchicalEvent(ConfigurationEvent.CLEAR_TREE);
107     }
108 
109     /**
110      * Tests the base event type for configuration events.
111      */
112     @Test
113     public void testConfigurationEventType() {
114         assertSame(Event.ANY, ConfigurationEvent.ANY.getSuperType());
115     }
116 
117     /**
118      * Tests whether the set of super event types for the base type can be obtained.
119      */
120     @Test
121     public void testFetchSuperEventTypesForBaseType() {
122         final Set<EventType<?>> superTypes = EventType.fetchSuperEventTypes(Event.ANY);
123         assertEquals(Collections.singleton(Event.ANY), superTypes);
124     }
125 
126     /**
127      * Tests whether the set of super event types for null input can be obtained.
128      */
129     @Test
130     public void testFetchSuperEventTypesNull() {
131         final Set<EventType<?>> superTypes = EventType.fetchSuperEventTypes(null);
132         assertTrue(superTypes.isEmpty());
133     }
134 
135     /**
136      * Tests whether the super event types of a specific type can be retrieved.
137      */
138     @Test
139     public void testFetchSuperEventTypesOfType() {
140         final Set<EventType<?>> superTypes = EventType.fetchSuperEventTypes(ConfigurationEvent.ADD_NODES);
141         final Set<EventType<? extends Event>> expected = new HashSet<>();
142         expected.add(ConfigurationEvent.ADD_NODES);
143         expected.add(ConfigurationEvent.ANY_HIERARCHICAL);
144         expected.add(ConfigurationEvent.ANY);
145         expected.add(Event.ANY);
146         assertEquals(expected, superTypes);
147     }
148 
149     /**
150      * Tests the common base event type for hierarchical update events.
151      */
152     @Test
153     public void testHierarchicalEventType() {
154         checkUpdateEvent(ConfigurationEvent.ANY_HIERARCHICAL);
155     }
156 
157     /**
158      * Tests isInstanceOf() if the base type is null.
159      */
160     @Test
161     public void testIsInstanceOfBaseNull() {
162         assertFalse(EventType.isInstanceOf(ConfigurationEvent.ANY, null));
163     }
164 
165     /**
166      * Tests isInstanceOf() if the derived type is null.
167      */
168     @Test
169     public void testIsInstanceOfDerivedNull() {
170         assertFalse(EventType.isInstanceOf(null, Event.ANY));
171     }
172 
173     /**
174      * Tests isInstanceOf() if there is no instanceof relationship.
175      */
176     @Test
177     public void testIsInstanceOfFalse() {
178         assertFalse(EventType.isInstanceOf(ConfigurationErrorEvent.READ, ConfigurationEvent.ANY));
179     }
180 
181     /**
182      * Tests isInstanceOf() if the expected result is true.
183      */
184     @Test
185     public void testIsInstanceOfTrue() {
186         assertTrue(EventType.isInstanceOf(ConfigurationEvent.ADD_NODES, ConfigurationEvent.ANY_HIERARCHICAL));
187         assertTrue(EventType.isInstanceOf(ConfigurationEvent.ADD_NODES, ConfigurationEvent.ANY));
188         assertTrue(EventType.isInstanceOf(ConfigurationEvent.ADD_NODES, Event.ANY));
189         assertTrue(EventType.isInstanceOf(ConfigurationEvent.ADD_NODES, ConfigurationEvent.ADD_NODES));
190     }
191 
192     /**
193      * Tests the event type indicating a read error.
194      */
195     @Test
196     public void testReadErrorEventType() {
197         checkErrorEvent(ConfigurationErrorEvent.READ);
198     }
199 
200     /**
201      * Tests the event type for setting a property.
202      */
203     @Test
204     public void testSetPropertyEventType() {
205         checkUpdateEvent(ConfigurationEvent.SET_PROPERTY);
206     }
207 
208     /**
209      * Tests the event type indicating a change on a sub configuration.
210      */
211     @Test
212     public void testSubnodeChangedEventType() {
213         checkHierarchicalEvent(ConfigurationEvent.SUBNODE_CHANGED);
214     }
215 
216     /**
217      * Tests the event type indicating a write error.
218      */
219     @Test
220     public void testWriteErrorEventType() {
221         checkErrorEvent(ConfigurationErrorEvent.WRITE);
222     }
223 }