1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 }