X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Ftest%2FEventTracker.java;h=38b24666bf7a760f1b9c1e572b60f7e5269e83d5;hb=e84a037d2ee2d2d2e8d991128f83978f2e98201c;hp=fce6b23f080ca7694f24a022dac9d70b7790feb3;hpb=32a62ca2c752e33a7873ac868a7a1f289caedcd4;p=utils diff --git a/support/general/src/test/java/org/wamblee/test/EventTracker.java b/support/general/src/test/java/org/wamblee/test/EventTracker.java index fce6b23f..38b24666 100644 --- a/support/general/src/test/java/org/wamblee/test/EventTracker.java +++ b/support/general/src/test/java/org/wamblee/test/EventTracker.java @@ -1,5 +1,5 @@ /* - * 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. @@ -20,15 +20,13 @@ 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; +import java.util.logging.Logger; /** * 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 - * using {@link #eventOccurred(Event)}. Test code inspects the events sent by a - * thread using {@link #isEventSent(Thread, Event)}. + * using {@link #eventOccurred(Object)}. Test code inspects the events sent by a + * thread using {@link #isEventSent(Thread, Object)}. * * A record is kept of every event which is sent. Therefore, the occurrence of a * new event does not erase a previously sent event. @@ -37,24 +35,28 @@ import org.apache.commons.logging.LogFactory; * 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 { - - private static final Log LOG = LogFactory.getLog(EventTracker.class); + private static final Logger LOG = Logger.getLogger(EventTracker.class + .getName()); /** * Map of Thread object to a list of events. */ - private Map> _events; + private Map> events; /** * Constructs the event tracker. * */ public EventTracker() { - _events = new HashMap>(); + clear(); + } + + public synchronized void clear() { + events = new HashMap>(); } /** @@ -65,13 +67,16 @@ public class EventTracker { */ public synchronized void eventOccurred(Event aEvent) { LOG.info("Event '" + aEvent + "' sent."); + Thread current = Thread.currentThread(); - List events = _events.get(current); - if (events == null) { - events = new ArrayList(); - _events.put(current, events); + List eventList = events.get(current); + + if (eventList == null) { + eventList = new ArrayList(); + events.put(current, eventList); } - events.add(aEvent); + + eventList.add(aEvent); } /** @@ -84,11 +89,13 @@ public class EventTracker { * @return Whether or not the event was sent. */ public synchronized boolean isEventSent(Thread aThread, Event aEvent) { - List events = _events.get(aThread); - if (events == null) { + List eventList = events.get(aThread); + + if (eventList == null) { return false; } - return events.contains(aEvent); + + return eventList.contains(aEvent); } /** @@ -100,16 +107,17 @@ public class EventTracker { * events were sent. */ public synchronized List getEvents(Thread aThread) { - List events = _events.get(aThread); - if (events == null) { - events = Collections.emptyList(); + List 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. @@ -117,14 +125,32 @@ public class EventTracker { */ public synchronized int getEventCount(Event aEvent) { int count = 0; - for (Thread thread : _events.keySet()) { - List events = _events.get(thread); - for (Event event : events) { + + for (Thread thread : events.keySet()) { + List 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; } }