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