/*
- * Copyright 2006 the original author or authors.
+ * Copyright 2005-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
- */
+ */
package org.wamblee.test;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
/**
* Tracks the occurence of certain events in a test environment. Threads in a
* test environment tell the event tracker of the occurrence of certain events
* Type of event sent from test code. Usually String will be
* sufficient. The event type must provide a sensible implementation
* of {@link java.lang.Object#equals(java.lang.Object)}.
- *
+ *
* @author Erik Brakkee
*/
public class EventTracker<Event> {
-
private static final Log LOG = LogFactory.getLog(EventTracker.class);
/**
* Map of Thread object to a list of events.
*/
- private Map<Thread, List<Event>> _events;
+ private Map<Thread, List<Event>> events;
/**
* Constructs the event tracker.
*
*/
public EventTracker() {
- _events = new HashMap<Thread, List<Event>>();
+ clear();
+ }
+
+ public synchronized void clear() {
+ events = new HashMap<Thread, List<Event>>();
}
/**
*/
public synchronized void eventOccurred(Event aEvent) {
LOG.info("Event '" + aEvent + "' sent.");
+
Thread current = Thread.currentThread();
- List<Event> events = _events.get(current);
- if (events == null) {
- events = new ArrayList<Event>();
- _events.put(current, events);
+ List<Event> eventList = events.get(current);
+
+ if (eventList == null) {
+ eventList = new ArrayList<Event>();
+ events.put(current, eventList);
}
- events.add(aEvent);
+
+ eventList.add(aEvent);
}
/**
* @return Whether or not the event was sent.
*/
public synchronized boolean isEventSent(Thread aThread, Event aEvent) {
- List<Event> events = _events.get(aThread);
- if (events == null) {
+ List<Event> eventList = events.get(aThread);
+
+ if (eventList == null) {
return false;
}
- return events.contains(aEvent);
+
+ return eventList.contains(aEvent);
}
/**
* events were sent.
*/
public synchronized List<Event> getEvents(Thread aThread) {
- List<Event> events = _events.get(aThread);
- if (events == null) {
- events = Collections.emptyList();
+ List<Event> eventList = events.get(aThread);
+
+ if (eventList == null) {
+ eventList = Collections.emptyList();
}
- return Collections.unmodifiableList(events);
+
+ return Collections.unmodifiableList(eventList);
}
/**
- * Gets the number of times an event was sent summed up
- * over all threads.
+ * Gets the number of times an event was sent summed up over all threads.
*
* @param aEvent
* Event to check.
*/
public synchronized int getEventCount(Event aEvent) {
int count = 0;
- for (Thread thread : _events.keySet()) {
- List<Event> events = _events.get(thread);
- for (Event event : events) {
+
+ for (Thread thread : events.keySet()) {
+ List<Event> eventList = events.get(thread);
+
+ for (Event event : eventList) {
if (event.equals(aEvent)) {
count++;
}
}
}
+
+ return count;
+ }
+
+ /**
+ * Gets the total event count over all threads.
+ *
+ * @return
+ */
+ public synchronized int getEventCount() {
+ int count = 0;
+
+ for (Thread thread : events.keySet()) {
+ count += events.get(thread).size();
+ }
+
return count;
}
}