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 java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.List;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
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(String)}. Test code inspects the events sent by
31 * a thread using {@link #isEventSent(Thread, String)}.
33 * A record is kept of every event which is sent. Therefore, the occurence of a
34 * new event does not erase a previously sent event.
36 * @param <Event> Type of event sent from test code. Usually String will be sufficient.
37 * The event type must provide a sensible implementation of {@link java.lang.Object#equals(java.lang.Object)}.
39 public class EventTracker<Event> {
41 private static final Log LOG = LogFactory.getLog(EventTracker.class);
44 * Map of Thread object to a list of events.
46 private Map<Thread, List<Event>> _events;
49 * Constructs the event tracker.
52 public EventTracker() {
53 _events = new HashMap<Thread, List<Event>>();
57 * Called by a thread to inform that an event has occurred.
60 * Event that was sent.
62 public synchronized void eventOccurred(Event aEvent) {
63 LOG.info("Event '" + aEvent + "' sent.");
64 Thread current = Thread.currentThread();
65 List<Event> events = _events.get(current);
67 events = new ArrayList<Event>();
68 _events.put(current, events);
74 * Checks if a specific event has happened in a specific thread.
80 * @return Whether or not the event was sent.
82 public synchronized boolean isEventSent(Thread aThread, Event aEvent) {
83 List<Event> events = _events.get(aThread);
87 return events.contains(aEvent);
91 * Gets the events for a thread in the order they were sent
94 * Thread to get events for.
95 * @return Events that were sent. A zero-sized array is returned if no
98 public synchronized List<Event> getEvents(Thread aThread) {
99 List<Event> events = _events.get(aThread);
100 if (events == null) {
101 events = Collections.emptyList();
103 return Collections.unmodifiableList(events);
107 * Gets the number of times an event was sent.
111 * @return Number of times it was reached.
113 public synchronized int getEventCount(Event aEvent) {
115 for (Thread thread : _events.keySet()) {
116 List<Event> events = _events.get(thread);
117 for (Event event : events) {
118 if (event.equals(aEvent)) {