    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;
    18
    19  import junit.framework.Assert;
    20
    21  import org.apache.commons.configuration.event.ConfigurationErrorEvent;
    22  import org.apache.commons.configuration.event.ConfigurationErrorListener;
    23
    24  /***
    25   * An implementation of the <code>ConfigurationErrorListener</code> interface
    26   * that can be used in unit tests. This implementation just records received
    27   * events and allows to test whether expected errors occurred.
    28   *
    29   * @author Oliver Heger
    30   * @version $Id: ConfigurationErrorListenerImpl.java 558330 2007-07-21 15:26:19Z oheger $
    31   */
    32  public class ConfigurationErrorListenerImpl implements
    33          ConfigurationErrorListener
    34  {
    35      /*** Stores the last received error event. */
    36      private ConfigurationErrorEvent event;
    37
    38      /*** Stores the number of calls to configurationError(). */
    39      private int errorCount;
    40
    41      /***
    42       * An error event is received. Updates the internal counter and stores the
    43       * event.
    44       *
    45       * @param event the error event
    46       */
    47      public void configurationError(ConfigurationErrorEvent event)
    48      {
    49          this.event = event;
    50          errorCount++;
    51      }
    52
    53      /***
    54       * Returns the last received error event.
    55       *
    56       * @return the last error event (may be <b>null</b>)
    57       */
    58      public ConfigurationErrorEvent getLastEvent()
    59      {
    60          return event;
    61      }
    62
    63      /***
    64       * Returns the number of received error events.
    65       *
    66       * @return the number of error events
    67       */
    68      public int getErrorCount()
    69      {
    70          return errorCount;
    71      }
    72
    73      /***
    74       * Checks whether no error event was received.
    75       */
    76      public void verify()
    77      {
    78          Assert.assertEquals("Error events received", 0, errorCount);
    79      }
    80
    81      /***
    82       * Checks whether an expected error event was received. This is a
    83       * convenience method for checking whether exactly one event of a certain
    84       * type was received.
    85       *
    86       * @param type the type of the event
    87       * @param propName the name of the property
    88       * @param propValue the value of the property
    89       */
    90      public void verify(int type, String propName, Object propValue)
    91      {
    92          Assert.assertEquals("Wrong number of error events", 1, errorCount);
    93          Assert.assertEquals("Wrong event type", type, event.getType());
    94          Assert.assertTrue("Wrong property name", (propName == null) ? event
    95                  .getPropertyName() == null : propName.equals(event
    96                  .getPropertyName()));
    97          Assert.assertTrue("Wrong property value", (propValue == null) ? event
    98                  .getPropertyValue() == null : propValue.equals(event
    99                  .getPropertyValue()));
    100     }
    101 }