Removed DOCUMENT ME comments that were generated and applied source code
[utils] / support / general / src / test / java / org / wamblee / test / EventTracker.java
1 /*
2  * Copyright 2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.test;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 /**
28  * Tracks the occurence of certain events in a test environment. Threads in a
29  * test environment tell the event tracker of the occurrence of certain events
30  * using {@link #eventOccurred(Event)}. Test code inspects the events sent by a
31  * thread using {@link #isEventSent(Thread, Event)}.
32  * 
33  * A record is kept of every event which is sent. Therefore, the occurrence of a
34  * new event does not erase a previously sent event.
35  * 
36  * @param <Event>
37  *            Type of event sent from test code. Usually String will be
38  *            sufficient. The event type must provide a sensible implementation
39  *            of {@link java.lang.Object#equals(java.lang.Object)}.
40  * 
41  * @author Erik Brakkee
42  */
43 public class EventTracker<Event> {
44     private static final Log LOG = LogFactory.getLog(EventTracker.class);
45
46     /**
47      * Map of Thread object to a list of events.
48      */
49     private Map<Thread, List<Event>> events;
50
51     /**
52      * Constructs the event tracker.
53      * 
54      */
55     public EventTracker() {
56         clear();
57     }
58
59     public void clear() {
60         events = new HashMap<Thread, List<Event>>();
61     }
62
63     /**
64      * Called by a thread to inform that an event has occurred.
65      * 
66      * @param aEvent
67      *            Event that was sent.
68      */
69     public synchronized void eventOccurred(Event aEvent) {
70         LOG.info("Event '" + aEvent + "' sent.");
71
72         Thread current = Thread.currentThread();
73         List<Event> eventList = events.get(current);
74
75         if (eventList == null) {
76             eventList = new ArrayList<Event>();
77             events.put(current, eventList);
78         }
79
80         eventList.add(aEvent);
81     }
82
83     /**
84      * Checks if a specific event has happened in a specific thread.
85      * 
86      * @param aThread
87      *            Thread to check.
88      * @param aEvent
89      *            Event to check for.
90      * @return Whether or not the event was sent.
91      */
92     public synchronized boolean isEventSent(Thread aThread, Event aEvent) {
93         List<Event> eventList = events.get(aThread);
94
95         if (eventList == null) {
96             return false;
97         }
98
99         return eventList.contains(aEvent);
100     }
101
102     /**
103      * Gets the events for a thread in the order they were sent
104      * 
105      * @param aThread
106      *            Thread to get events for.
107      * @return Events that were sent. A zero-sized array is returned if no
108      *         events were sent.
109      */
110     public synchronized List<Event> getEvents(Thread aThread) {
111         List<Event> eventList = events.get(aThread);
112
113         if (eventList == null) {
114             eventList = Collections.emptyList();
115         }
116
117         return Collections.unmodifiableList(eventList);
118     }
119
120     /**
121      * Gets the number of times an event was sent summed up over all threads.
122      * 
123      * @param aEvent
124      *            Event to check.
125      * @return Number of times it was reached.
126      */
127     public synchronized int getEventCount(Event aEvent) {
128         int count = 0;
129
130         for (Thread thread : events.keySet()) {
131             List<Event> eventList = events.get(thread);
132
133             for (Event event : eventList) {
134                 if (event.equals(aEvent)) {
135                     count++;
136                 }
137             }
138         }
139
140         return count;
141     }
142
143     /**
144      * Gets the total event count over all threads.
145      * 
146      * @return
147      */
148     public synchronized int getEventCount() {
149         int count = 0;
150
151         for (Thread thread : events.keySet()) {
152             count += events.get(thread).size();
153         }
154
155         return count;
156     }
157 }