fce6b23f080ca7694f24a022dac9d70b7790feb3
[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 java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
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
45     private static final Log LOG = LogFactory.getLog(EventTracker.class);
46
47     /**
48      * Map of Thread object to a list of events.
49      */
50     private Map<Thread, List<Event>> _events;
51
52     /**
53      * Constructs the event tracker.
54      * 
55      */
56     public EventTracker() {
57         _events = new HashMap<Thread, List<Event>>();
58     }
59
60     /**
61      * Called by a thread to inform that an event has occurred.
62      * 
63      * @param aEvent
64      *            Event that was sent.
65      */
66     public synchronized void eventOccurred(Event aEvent) {
67         LOG.info("Event '" + aEvent + "' sent.");
68         Thread current = Thread.currentThread();
69         List<Event> events = _events.get(current);
70         if (events == null) {
71             events = new ArrayList<Event>();
72             _events.put(current, events);
73         }
74         events.add(aEvent);
75     }
76
77     /**
78      * Checks if a specific event has happened in a specific thread.
79      * 
80      * @param aThread
81      *            Thread to check.
82      * @param aEvent
83      *            Event to check for.
84      * @return Whether or not the event was sent.
85      */
86     public synchronized boolean isEventSent(Thread aThread, Event aEvent) {
87         List<Event> events = _events.get(aThread);
88         if (events == null) {
89             return false;
90         }
91         return events.contains(aEvent);
92     }
93
94     /**
95      * Gets the events for a thread in the order they were sent
96      * 
97      * @param aThread
98      *            Thread to get events for.
99      * @return Events that were sent. A zero-sized array is returned if no
100      *         events were sent.
101      */
102     public synchronized List<Event> getEvents(Thread aThread) {
103         List<Event> events = _events.get(aThread);
104         if (events == null) {
105             events = Collections.emptyList();
106         }
107         return Collections.unmodifiableList(events);
108     }
109
110     /**
111      * Gets the number of times an event was sent summed up
112      * over all threads. 
113      * 
114      * @param aEvent
115      *            Event to check.
116      * @return Number of times it was reached.
117      */
118     public synchronized int getEventCount(Event aEvent) {
119         int count = 0;
120         for (Thread thread : _events.keySet()) {
121             List<Event> events = _events.get(thread);
122             for (Event event : events) {
123                 if (event.equals(aEvent)) {
124                     count++;
125                 }
126             }
127         }
128         return count;
129     }
130 }