(no commit message)
[utils] / support / general / src / test / java / org / wamblee / test / EventTracker.java
1 /*
2  * Copyright 2005-2010 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 import java.util.logging.Logger;
24
25 /**
26  * Tracks the occurence of certain events in a test environment. Threads in a
27  * test environment tell the event tracker of the occurrence of certain events
28  * using {@link #eventOccurred(Event)}. Test code inspects the events sent by a
29  * thread using {@link #isEventSent(Thread, Event)}.
30  * 
31  * A record is kept of every event which is sent. Therefore, the occurrence of a
32  * new event does not erase a previously sent event.
33  * 
34  * @param <Event>
35  *            Type of event sent from test code. Usually String will be
36  *            sufficient. The event type must provide a sensible implementation
37  *            of {@link java.lang.Object#equals(java.lang.Object)}.
38  * 
39  * @author Erik Brakkee
40  */
41 public class EventTracker<Event> {
42     private static final Logger LOG = Logger.getLogger(EventTracker.class
43         .getName());
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         clear();
56     }
57
58     public synchronized void clear() {
59         events = new HashMap<Thread, List<Event>>();
60     }
61
62     /**
63      * Called by a thread to inform that an event has occurred.
64      * 
65      * @param aEvent
66      *            Event that was sent.
67      */
68     public synchronized void eventOccurred(Event aEvent) {
69         LOG.info("Event '" + aEvent + "' sent.");
70
71         Thread current = Thread.currentThread();
72         List<Event> eventList = events.get(current);
73
74         if (eventList == null) {
75             eventList = new ArrayList<Event>();
76             events.put(current, eventList);
77         }
78
79         eventList.add(aEvent);
80     }
81
82     /**
83      * Checks if a specific event has happened in a specific thread.
84      * 
85      * @param aThread
86      *            Thread to check.
87      * @param aEvent
88      *            Event to check for.
89      * @return Whether or not the event was sent.
90      */
91     public synchronized boolean isEventSent(Thread aThread, Event aEvent) {
92         List<Event> eventList = events.get(aThread);
93
94         if (eventList == null) {
95             return false;
96         }
97
98         return eventList.contains(aEvent);
99     }
100
101     /**
102      * Gets the events for a thread in the order they were sent
103      * 
104      * @param aThread
105      *            Thread to get events for.
106      * @return Events that were sent. A zero-sized array is returned if no
107      *         events were sent.
108      */
109     public synchronized List<Event> getEvents(Thread aThread) {
110         List<Event> eventList = events.get(aThread);
111
112         if (eventList == null) {
113             eventList = Collections.emptyList();
114         }
115
116         return Collections.unmodifiableList(eventList);
117     }
118
119     /**
120      * Gets the number of times an event was sent summed up over all threads.
121      * 
122      * @param aEvent
123      *            Event to check.
124      * @return Number of times it was reached.
125      */
126     public synchronized int getEventCount(Event aEvent) {
127         int count = 0;
128
129         for (Thread thread : events.keySet()) {
130             List<Event> eventList = events.get(thread);
131
132             for (Event event : eventList) {
133                 if (event.equals(aEvent)) {
134                     count++;
135                 }
136             }
137         }
138
139         return count;
140     }
141
142     /**
143      * Gets the total event count over all threads.
144      * 
145      * @return
146      */
147     public synchronized int getEventCount() {
148         int count = 0;
149
150         for (Thread thread : events.keySet()) {
151             count += events.get(thread).size();
152         }
153
154         return count;
155     }
156 }