X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Ftest%2FEventTracker.java;h=c2b18f6d0c4ded723a6f0a7ee0622edf1fc748d1;hb=e72743b6a9fac5a99b842f92b1687fba65ef3210;hp=9e9c472ddfe65d0e87cc0db45365328d71c3a961;hpb=0d8d8f24656e585ee75558cfd6a4c661f8f14985;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 9e9c472d..c2b18f6d 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. @@ -12,7 +12,7 @@ * 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 java.util.ArrayList; @@ -20,9 +20,7 @@ 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 @@ -37,12 +35,11 @@ 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. @@ -57,9 +54,9 @@ public class EventTracker { clear(); } - public void clear() { - events = new HashMap>(); - } + public synchronized void clear() { + events = new HashMap>(); + } /** * Called by a thread to inform that an event has occurred. @@ -69,12 +66,15 @@ public class EventTracker { */ public synchronized void eventOccurred(Event aEvent) { LOG.info("Event '" + aEvent + "' sent."); + Thread current = Thread.currentThread(); List eventList = events.get(current); + if (eventList == null) { eventList = new ArrayList(); events.put(current, eventList); } + eventList.add(aEvent); } @@ -89,9 +89,11 @@ public class EventTracker { */ public synchronized boolean isEventSent(Thread aThread, Event aEvent) { List eventList = events.get(aThread); + if (eventList == null) { return false; } + return eventList.contains(aEvent); } @@ -105,15 +107,16 @@ public class EventTracker { */ public synchronized List getEvents(Thread aThread) { List eventList = events.get(aThread); + if (eventList == null) { eventList = Collections.emptyList(); } + 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. @@ -121,26 +124,32 @@ public class EventTracker { */ public synchronized int getEventCount(Event aEvent) { int count = 0; + 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. + * 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; + public synchronized int getEventCount() { + int count = 0; + + for (Thread thread : events.keySet()) { + count += events.get(thread).size(); + } + + return count; } }