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 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      /** Constant for a test property name. */
32      static final String TEST_PROPNAME = "event.test";
33  
34      /** Constant for a test property value. */
35      static final String TEST_PROPVALUE = "a value";
36  
37      /** Constant for an existing property. */
38      static final String EXIST_PROPERTY = "event.property";
39  
40      /** The configuration to be tested. */
41      protected AbstractConfiguration config;
42  
43      /** A test event listener. */
44      protected EventListenerTestImpl listener;
45  
46      /**
47       * Creates the configuration instance to be tested.
48       *
49       * @return the configuration instance under test
50       */
51      protected abstract AbstractConfiguration createConfiguration();
52  
53      @BeforeEach
54      public void setUp() throws Exception {
55          config = createConfiguration();
56          config.addProperty(EXIST_PROPERTY, "existing value");
57          listener = new EventListenerTestImpl(config);
58          config.addEventListener(ConfigurationEvent.ANY, listener);
59      }
60  
61      /**
62       * Tests events generated by addProperty().
63       */
64      @Test
65      public void testAddPropertyEvent() {
66          config.addProperty(TEST_PROPNAME, TEST_PROPVALUE);
67          listener.checkEvent(ConfigurationEvent.ADD_PROPERTY, TEST_PROPNAME, TEST_PROPVALUE, true);
68          listener.checkEvent(ConfigurationEvent.ADD_PROPERTY, TEST_PROPNAME, TEST_PROPVALUE, false);
69          listener.done();
70      }
71  
72      /**
73       * Tests events generated by addProperty() when detail events are enabled.
74       */
75      @Test
76      public void testAddPropertyEventWithDetails() {
77          config.setDetailEvents(true);
78          config.addProperty(TEST_PROPNAME, TEST_PROPVALUE);
79          listener.checkEventCount(2);
80          listener.checkEvent(ConfigurationEvent.ADD_PROPERTY, TEST_PROPNAME, TEST_PROPVALUE, true);
81          listener.skipToLast(ConfigurationEvent.ADD_PROPERTY);
82          listener.checkEvent(ConfigurationEvent.ADD_PROPERTY, TEST_PROPNAME, TEST_PROPVALUE, false);
83          listener.done();
84      }
85  
86      /**
87       * Tests the events generated by the clear() method.
88       */
89      @Test
90      public void testClearEvent() {
91          config.clear();
92          listener.checkEvent(ConfigurationEvent.CLEAR, null, null, true);
93          listener.checkEvent(ConfigurationEvent.CLEAR, null, null, false);
94          listener.done();
95      }
96  
97      /**
98       * Tests the events generated by the clear method when detail events are enabled.
99       */
100     @Test
101     public void testClearEventWithDetails() {
102         config.setDetailEvents(true);
103         config.clear();
104         listener.checkEventCount(2);
105         listener.checkEvent(ConfigurationEvent.CLEAR, null, null, true);
106         listener.skipToLast(ConfigurationEvent.CLEAR);
107         listener.checkEvent(ConfigurationEvent.CLEAR, null, null, false);
108         listener.done();
109     }
110 
111     /**
112      * Tests events generated by clearProperty().
113      */
114     @Test
115     public void testClearPropertyEvent() {
116         config.clearProperty(EXIST_PROPERTY);
117         listener.checkEvent(ConfigurationEvent.CLEAR_PROPERTY, EXIST_PROPERTY, null, true);
118         listener.checkEvent(ConfigurationEvent.CLEAR_PROPERTY, EXIST_PROPERTY, null, false);
119         listener.done();
120     }
121 
122     /**
123      * Tests events generated by clearProperty() when detail events are enabled.
124      */
125     @Test
126     public void testClearPropertyEventWithDetails() {
127         config.setDetailEvents(true);
128         config.clearProperty(EXIST_PROPERTY);
129         listener.checkEventCount(2);
130         listener.checkEvent(ConfigurationEvent.CLEAR_PROPERTY, EXIST_PROPERTY, null, true);
131         listener.skipToLast(ConfigurationEvent.CLEAR_PROPERTY);
132         listener.checkEvent(ConfigurationEvent.CLEAR_PROPERTY, EXIST_PROPERTY, null, false);
133         listener.done();
134     }
135 
136     /**
137      * Tests events generated by setProperty().
138      */
139     @Test
140     public void testSetPropertyEvent() {
141         config.setProperty(EXIST_PROPERTY, TEST_PROPVALUE);
142         listener.checkEvent(ConfigurationEvent.SET_PROPERTY, EXIST_PROPERTY, TEST_PROPVALUE, true);
143         listener.checkEvent(ConfigurationEvent.SET_PROPERTY, EXIST_PROPERTY, TEST_PROPVALUE, false);
144         listener.done();
145     }
146 
147     /**
148      * Tests events generated by setProperty() when detail events are enabled.
149      */
150     @Test
151     public void testSetPropertyEventWithDetails() {
152         config.setDetailEvents(true);
153         config.setProperty(EXIST_PROPERTY, TEST_PROPVALUE);
154         listener.checkEventCount(2);
155         listener.checkEvent(ConfigurationEvent.SET_PROPERTY, EXIST_PROPERTY, TEST_PROPVALUE, true);
156         listener.skipToLast(ConfigurationEvent.SET_PROPERTY);
157         listener.checkEvent(ConfigurationEvent.SET_PROPERTY, EXIST_PROPERTY, TEST_PROPVALUE, false);
158         listener.done();
159     }
160 }