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 org.apache.commons.configuration2.AbstractConfiguration;
20  import org.junit.jupiter.api.BeforeEach;
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * Base class for testing events generated by configuration classes derived from AbstractConfiguration. This class
25   * implements a couple of tests related to event generation. Concrete sub classes only have to implement the
26   * {@code createConfiguration()} method for creating an instance of a specific configuration class. Because tests for
27   * detail events depend on a concrete implementation an exact sequence of events cannot be checked. Instead the
28   * corresponding test methods check whether the enclosing events (not the detail events) are of the expected type.
29   */
30  public abstract class AbstractTestConfigurationEvents {
31  
32      /** Constant for a test property name. */
33      static final String TEST_PROPNAME = "event.test";
34  
35      /** Constant for a test property value. */
36      static final String TEST_PROPVALUE = "a value";
37  
38      /** Constant for an existing property. */
39      static final String EXIST_PROPERTY = "event.property";
40  
41      /** The configuration to be tested. */
42      protected AbstractConfiguration config;
43  
44      /** A test event listener. */
45      protected EventListenerTestImpl listener;
46  
47      /**
48       * Creates the configuration instance to be tested.
49       *
50       * @return the configuration instance under test
51       */
52      protected abstract AbstractConfiguration createConfiguration();
53  
54      @BeforeEach
55      public void setUp() throws Exception {
56          config = createConfiguration();
57          config.addProperty(EXIST_PROPERTY, "existing value");
58          listener = new EventListenerTestImpl(config);
59          config.addEventListener(ConfigurationEvent.ANY, listener);
60      }
61  
62      /**
63       * Tests events generated by addProperty().
64       */
65      @Test
66      void testAddPropertyEvent() {
67          config.addProperty(TEST_PROPNAME, TEST_PROPVALUE);
68          listener.checkEvent(ConfigurationEvent.ADD_PROPERTY, TEST_PROPNAME, TEST_PROPVALUE, true);
69          listener.checkEvent(ConfigurationEvent.ADD_PROPERTY, TEST_PROPNAME, TEST_PROPVALUE, false);
70          listener.done();
71      }
72  
73      /**
74       * Tests events generated by addProperty() when detail events are enabled.
75       */
76      @Test
77      void testAddPropertyEventWithDetails() {
78          config.setDetailEvents(true);
79          config.addProperty(TEST_PROPNAME, TEST_PROPVALUE);
80          listener.checkEventCount(2);
81          listener.checkEvent(ConfigurationEvent.ADD_PROPERTY, TEST_PROPNAME, TEST_PROPVALUE, true);
82          listener.skipToLast(ConfigurationEvent.ADD_PROPERTY);
83          listener.checkEvent(ConfigurationEvent.ADD_PROPERTY, TEST_PROPNAME, TEST_PROPVALUE, false);
84          listener.done();
85      }
86  
87      /**
88       * Tests the events generated by the clear() method.
89       */
90      @Test
91      void testClearEvent() {
92          config.clear();
93          listener.checkEvent(ConfigurationEvent.CLEAR, null, null, true);
94          listener.checkEvent(ConfigurationEvent.CLEAR, null, null, false);
95          listener.done();
96      }
97  
98      /**
99       * Tests the events generated by the clear method when detail events are enabled.
100      */
101     @Test
102     void testClearEventWithDetails() {
103         config.setDetailEvents(true);
104         config.clear();
105         listener.checkEventCount(2);
106         listener.checkEvent(ConfigurationEvent.CLEAR, null, null, true);
107         listener.skipToLast(ConfigurationEvent.CLEAR);
108         listener.checkEvent(ConfigurationEvent.CLEAR, null, null, false);
109         listener.done();
110     }
111 
112     /**
113      * Tests events generated by clearProperty().
114      */
115     @Test
116     void testClearPropertyEvent() {
117         config.clearProperty(EXIST_PROPERTY);
118         listener.checkEvent(ConfigurationEvent.CLEAR_PROPERTY, EXIST_PROPERTY, null, true);
119         listener.checkEvent(ConfigurationEvent.CLEAR_PROPERTY, EXIST_PROPERTY, null, false);
120         listener.done();
121     }
122 
123     /**
124      * Tests events generated by clearProperty() when detail events are enabled.
125      */
126     @Test
127     void testClearPropertyEventWithDetails() {
128         config.setDetailEvents(true);
129         config.clearProperty(EXIST_PROPERTY);
130         listener.checkEventCount(2);
131         listener.checkEvent(ConfigurationEvent.CLEAR_PROPERTY, EXIST_PROPERTY, null, true);
132         listener.skipToLast(ConfigurationEvent.CLEAR_PROPERTY);
133         listener.checkEvent(ConfigurationEvent.CLEAR_PROPERTY, EXIST_PROPERTY, null, false);
134         listener.done();
135     }
136 
137     /**
138      * Tests events generated by setProperty().
139      */
140     @Test
141     void testSetPropertyEvent() {
142         config.setProperty(EXIST_PROPERTY, TEST_PROPVALUE);
143         listener.checkEvent(ConfigurationEvent.SET_PROPERTY, EXIST_PROPERTY, TEST_PROPVALUE, true);
144         listener.checkEvent(ConfigurationEvent.SET_PROPERTY, EXIST_PROPERTY, TEST_PROPVALUE, false);
145         listener.done();
146     }
147 
148     /**
149      * Tests events generated by setProperty() when detail events are enabled.
150      */
151     @Test
152     void testSetPropertyEventWithDetails() {
153         config.setDetailEvents(true);
154         config.setProperty(EXIST_PROPERTY, TEST_PROPVALUE);
155         listener.checkEventCount(2);
156         listener.checkEvent(ConfigurationEvent.SET_PROPERTY, EXIST_PROPERTY, TEST_PROPVALUE, true);
157         listener.skipToLast(ConfigurationEvent.SET_PROPERTY);
158         listener.checkEvent(ConfigurationEvent.SET_PROPERTY, EXIST_PROPERTY, TEST_PROPVALUE, false);
159         listener.done();
160     }
161 }