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