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