From 3a727feb243925ae05b7e53aaaaf3d54eb988985 Mon Sep 17 00:00:00 2001 From: erik Date: Sun, 26 Mar 2006 21:15:04 +0000 Subject: [PATCH] --- .../test/org/wamblee/test/EventTracker.java | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 support/test/org/wamblee/test/EventTracker.java diff --git a/support/test/org/wamblee/test/EventTracker.java b/support/test/org/wamblee/test/EventTracker.java new file mode 100644 index 00000000..e49e671c --- /dev/null +++ b/support/test/org/wamblee/test/EventTracker.java @@ -0,0 +1,125 @@ +/* + * Copyright 2006 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * 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; +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 + * using {@link #eventOccurred(String)}. Test code inspects the events sent by + * a thread using {@link #isEventSent(Thread, String)}. + * + * A record is kept of every event which is sent. Therefore, the occurence of a + * new event does not erase a previously sent event. + * + * @param 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)}. + */ +public class EventTracker { + + private static final Log LOG = LogFactory.getLog(EventTracker.class); + + /** + * Map of Thread object to a list of events. + */ + private Map> _events; + + /** + * Constructs the event tracker. + * + */ + public EventTracker() { + _events = new HashMap>(); + } + + /** + * Called by a thread to inform that an event has occurred. + * + * @param aEvent + * Event that was sent. + */ + 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); + } + events.add(aEvent); + } + + /** + * Checks if a specific event has happened in a specific thread. + * + * @param aThread + * Thread to check. + * @param aEvent + * Event to check for. + * @return Whether or not the event was sent. + */ + public synchronized boolean isEventSent(Thread aThread, Event aEvent) { + List events = _events.get(aThread); + if (events == null) { + return false; + } + return events.contains(aEvent); + } + + /** + * Gets the events for a thread in the order they were sent + * + * @param aThread + * Thread to get events for. + * @return Events that were sent. A zero-sized array is returned if no + * events were sent. + */ + public synchronized List getEvents(Thread aThread) { + List events = _events.get(aThread); + if (events == null) { + events = Collections.emptyList(); + } + return Collections.unmodifiableList(events); + } + + /** + * Gets the number of times an event was sent. + * + * @param aEvent + * Event to check. + * @return Number of times it was reached. + */ + public synchronized int getEventCount(Event aEvent) { + int count = 0; + for (Thread thread : _events.keySet()) { + List events = _events.get(thread); + for (Event event : events) { + if (event.equals(aEvent)) { + count++; + } + } + } + return count; + } +} -- 2.31.1