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