2 * Copyright 2005-2010 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.test;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.List;
23 import java.util.logging.Logger;
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)}.
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.
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)}.
39 * @author Erik Brakkee
41 public class EventTracker<Event> {
42 private static final Logger LOG = Logger.getLogger(EventTracker.class
46 * Map of Thread object to a list of events.
48 private Map<Thread, List<Event>> events;
51 * Constructs the event tracker.
54 public EventTracker() {
58 public synchronized void clear() {
59 events = new HashMap<Thread, List<Event>>();
63 * Called by a thread to inform that an event has occurred.
66 * Event that was sent.
68 public synchronized void eventOccurred(Event aEvent) {
69 LOG.info("Event '" + aEvent + "' sent.");
71 Thread current = Thread.currentThread();
72 List<Event> eventList = events.get(current);
74 if (eventList == null) {
75 eventList = new ArrayList<Event>();
76 events.put(current, eventList);
79 eventList.add(aEvent);
83 * Checks if a specific event has happened in a specific thread.
89 * @return Whether or not the event was sent.
91 public synchronized boolean isEventSent(Thread aThread, Event aEvent) {
92 List<Event> eventList = events.get(aThread);
94 if (eventList == null) {
98 return eventList.contains(aEvent);
102 * Gets the events for a thread in the order they were sent
105 * Thread to get events for.
106 * @return Events that were sent. A zero-sized array is returned if no
109 public synchronized List<Event> getEvents(Thread aThread) {
110 List<Event> eventList = events.get(aThread);
112 if (eventList == null) {
113 eventList = Collections.emptyList();
116 return Collections.unmodifiableList(eventList);
120 * Gets the number of times an event was sent summed up over all threads.
124 * @return Number of times it was reached.
126 public synchronized int getEventCount(Event aEvent) {
129 for (Thread thread : events.keySet()) {
130 List<Event> eventList = events.get(thread);
132 for (Event event : eventList) {
133 if (event.equals(aEvent)) {
143 * Gets the total event count over all threads.
147 public synchronized int getEventCount() {
150 for (Thread thread : events.keySet()) {
151 count += events.get(thread).size();