001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.configuration.event;
018    
019    
020    import org.apache.commons.configuration.AbstractConfiguration;
021    import org.junit.Before;
022    import org.junit.Test;
023    
024    /**
025     * Base class for testing events generated by configuration classes derived from
026     * AbstractConfiguration. This class implements a couple of tests related to
027     * event generation. Concrete sub classes only have to implement the
028     * {@code createConfiguration()} method for creating an instance of a
029     * specific configuration class. Because tests for detail events depend on a
030     * concrete implementation an exact sequence of events cannot be checked.
031     * Instead the corresponding test methods check whether the enclosing events
032     * (not the detail events) are of the expected type.
033     *
034     * @version $Id: AbstractTestConfigurationEvents.java 1225648 2011-12-29 20:55:07Z oheger $
035     */
036    public abstract class AbstractTestConfigurationEvents
037    {
038        /** Constant for a test property name. */
039        static final String TEST_PROPNAME = "event.test";
040    
041        /** Constant for a test property value. */
042        static final String TEST_PROPVALUE = "a value";
043    
044        /** Constant for an existing property. */
045        static final String EXIST_PROPERTY = "event.property";
046    
047        /** The configuration to be tested. */
048        protected AbstractConfiguration config;
049    
050        /** A test event listener. */
051        protected ConfigurationListenerTestImpl l;
052    
053        @Before
054        public void setUp() throws Exception
055        {
056            config = createConfiguration();
057            config.addProperty(EXIST_PROPERTY, "existing value");
058            l = new ConfigurationListenerTestImpl(config);
059            config.addConfigurationListener(l);
060        }
061    
062        /**
063         * Creates the configuration instance to be tested.
064         *
065         * @return the configuration instance under test
066         */
067        protected abstract AbstractConfiguration createConfiguration();
068    
069        /**
070         * Tests events generated by addProperty().
071         */
072        @Test
073        public void testAddPropertyEvent()
074        {
075            config.addProperty(TEST_PROPNAME, TEST_PROPVALUE);
076            l.checkEvent(AbstractConfiguration.EVENT_ADD_PROPERTY, TEST_PROPNAME,
077                    TEST_PROPVALUE, true);
078            l.checkEvent(AbstractConfiguration.EVENT_ADD_PROPERTY, TEST_PROPNAME,
079                    TEST_PROPVALUE, false);
080            l.done();
081        }
082    
083        /**
084         * Tests events generated by addProperty() when detail events are enabled.
085         */
086        @Test
087        public void testAddPropertyEventWithDetails()
088        {
089            config.setDetailEvents(true);
090            config.addProperty(TEST_PROPNAME, TEST_PROPVALUE);
091            l.checkEventCount(2);
092            l.checkEvent(AbstractConfiguration.EVENT_ADD_PROPERTY, TEST_PROPNAME,
093                    TEST_PROPVALUE, true);
094            l.skipToLast(AbstractConfiguration.EVENT_ADD_PROPERTY);
095            l.checkEvent(AbstractConfiguration.EVENT_ADD_PROPERTY, TEST_PROPNAME,
096                    TEST_PROPVALUE, false);
097            l.done();
098        }
099    
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    }