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;
018    
019    import static org.junit.Assert.assertEquals;
020    import static org.junit.Assert.assertTrue;
021    
022    import org.apache.commons.configuration.event.ConfigurationErrorEvent;
023    import org.apache.commons.configuration.event.ConfigurationErrorListener;
024    
025    /**
026     * An implementation of the {@code ConfigurationErrorListener} interface
027     * that can be used in unit tests. This implementation just records received
028     * events and allows to test whether expected errors occurred.
029     *
030     * @author <a
031     * href="http://commons.apache.org/configuration/team-list.html">Commons
032     * Configuration team</a>
033     * @version $Id: ConfigurationErrorListenerImpl.java 1222446 2011-12-22 20:57:32Z oheger $
034     */
035    public class ConfigurationErrorListenerImpl implements
036            ConfigurationErrorListener
037    {
038        /** Stores the last received error event. */
039        private ConfigurationErrorEvent event;
040    
041        /** Stores the number of calls to configurationError(). */
042        private int errorCount;
043    
044        /**
045         * An error event is received. Updates the internal counter and stores the
046         * event.
047         *
048         * @param event the error event
049         */
050        public void configurationError(ConfigurationErrorEvent event)
051        {
052            this.event = event;
053            errorCount++;
054        }
055    
056        /**
057         * Returns the last received error event.
058         *
059         * @return the last error event (may be <b>null</b>)
060         */
061        public ConfigurationErrorEvent getLastEvent()
062        {
063            return event;
064        }
065    
066        /**
067         * Returns the number of received error events.
068         *
069         * @return the number of error events
070         */
071        public int getErrorCount()
072        {
073            return errorCount;
074        }
075    
076        /**
077         * Checks whether no error event was received.
078         */
079        public void verify()
080        {
081            assertEquals("Error events received", 0, errorCount);
082        }
083    
084        /**
085         * Checks whether an expected error event was received. This is a
086         * convenience method for checking whether exactly one event of a certain
087         * type was received.
088         *
089         * @param type the type of the event
090         * @param propName the name of the property
091         * @param propValue the value of the property
092         */
093        public void verify(int type, String propName, Object propValue)
094        {
095            assertEquals("Wrong number of error events", 1, errorCount);
096            assertEquals("Wrong event type", type, event.getType());
097            assertTrue("Wrong property name", (propName == null) ? event
098                    .getPropertyName() == null : propName.equals(event
099                    .getPropertyName()));
100            assertTrue("Wrong property value", (propValue == null) ? event
101                    .getPropertyValue() == null : propValue.equals(event
102                    .getPropertyValue()));
103        }
104    }