2 * Copyright 2006 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 org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.List;
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)}.
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.
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)}.
42 * @author Erik Brakkee
44 public class EventTracker<Event> {
45 private static final Log LOG = LogFactory.getLog(EventTracker.class);
48 * Map of Thread object to a list of events.
50 private Map<Thread, List<Event>> events;
53 * Constructs the event tracker.
56 public EventTracker() {
61 events = new HashMap<Thread, List<Event>>();
65 * Called by a thread to inform that an event has occurred.
68 * Event that was sent.
70 public synchronized void eventOccurred(Event aEvent) {
71 LOG.info("Event '" + aEvent + "' sent.");
73 Thread current = Thread.currentThread();
74 List<Event> eventList = events.get(current);
76 if (eventList == null) {
77 eventList = new ArrayList<Event>();
78 events.put(current, eventList);
81 eventList.add(aEvent);
85 * Checks if a specific event has happened in a specific thread.
91 * @return Whether or not the event was sent.
93 public synchronized boolean isEventSent(Thread aThread, Event aEvent) {
94 List<Event> eventList = events.get(aThread);
96 if (eventList == null) {
100 return eventList.contains(aEvent);
104 * Gets the events for a thread in the order they were sent
107 * Thread to get events for.
108 * @return Events that were sent. A zero-sized array is returned if no
111 public synchronized List<Event> getEvents(Thread aThread) {
112 List<Event> eventList = events.get(aThread);
114 if (eventList == null) {
115 eventList = Collections.emptyList();
118 return Collections.unmodifiableList(eventList);
122 * Gets the number of times an event was sent summed up
127 * @return Number of times it was reached.
129 public synchronized int getEventCount(Event aEvent) {
132 for (Thread thread : events.keySet()) {
133 List<Event> eventList = events.get(thread);
135 for (Event event : eventList) {
136 if (event.equals(aEvent)) {
146 * Gets the total event count over all threads.
149 public synchronized int getEventCount() {
152 for (Thread thread : events.keySet()) {
153 count += events.get(thread).size();