From: Erik Brakkee Date: Sun, 24 Jul 2011 18:47:11 +0000 (+0200) Subject: introduction of EventInfo. X-Git-Tag: PAX_EXAM_MOCKITO_ISSUE~39 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;ds=sidebyside;h=3994d4a35e7404908fc17beac75479c1a72fa915;hp=bf43ef9863e3d5827eac1a4ce0fafafaadd92a08;p=xmlrouter introduction of EventInfo. --- diff --git a/impl/src/main/java/org/wamblee/xmlrouter/impl/XMLRouter.java b/impl/src/main/java/org/wamblee/xmlrouter/impl/XMLRouter.java index 679c7e3..e166d75 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/XMLRouter.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/XMLRouter.java @@ -35,6 +35,7 @@ import org.wamblee.xmlrouter.config.Config; import org.wamblee.xmlrouter.config.DocumentType; import org.wamblee.xmlrouter.config.Filter; import org.wamblee.xmlrouter.config.Transformation; +import org.wamblee.xmlrouter.listener.EventListener; import org.wamblee.xmlrouter.publish.Gateway; import org.wamblee.xmlrouter.subscribe.Destination; import org.wamblee.xmlrouter.subscribe.DestinationRegistry; @@ -46,13 +47,15 @@ public class XMLRouter implements Config, Gateway, DestinationRegistry { private static final Logger LOGGER = Logger.getLogger(XMLRouter.class .getName()); + private EventListener listener; private AtomicInteger sequenceNumbers; private Map documentTypes; private Transformations transformations; private Map filters; private Map destinations; - public XMLRouter() { + public XMLRouter(EventListener aListener) { + listener = aListener; sequenceNumbers = new AtomicInteger(1); documentTypes = new LinkedHashMap(); transformations = new Transformations(); diff --git a/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java b/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java index 50fa4f8..2d73fad 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java @@ -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.xmlrouter.impl; import static junit.framework.Assert.*; @@ -21,6 +21,7 @@ import static org.mockito.Mockito.*; import java.util.Arrays; import java.util.Collection; +import java.util.logging.Level; import javax.xml.transform.dom.DOMSource; @@ -29,6 +30,8 @@ import org.junit.Test; import org.wamblee.xmlrouter.common.Id; import org.wamblee.xmlrouter.config.DocumentType; import org.wamblee.xmlrouter.config.Transformation; +import org.wamblee.xmlrouter.listener.EventListener; +import org.wamblee.xmlrouter.listener.LoggingEventListener; import org.wamblee.xmlrouter.subscribe.Destination; public class XMLRouterTest { @@ -67,10 +70,13 @@ public class XMLRouterTest { private Destination destinationSpy; private Id destinationId; + private EventListener listener; @Before public void setUp() { - router = new XMLRouter(); + EventListener logListener = new LoggingEventListener(Level.INFO); + listener = spy(logListener); + router = new XMLRouter(listener); source1 = mock(DOMSource.class); source2 = mock(DOMSource.class); source3 = mock(DOMSource.class); diff --git a/listener/src/main/java/org/wamblee/xmlrouter/listener/CompositeEventListener.java b/listener/src/main/java/org/wamblee/xmlrouter/listener/CompositeEventListener.java index 15432ea..22943db 100644 --- a/listener/src/main/java/org/wamblee/xmlrouter/listener/CompositeEventListener.java +++ b/listener/src/main/java/org/wamblee/xmlrouter/listener/CompositeEventListener.java @@ -22,7 +22,6 @@ import java.util.logging.Level; import java.util.logging.Logger; import javax.print.attribute.standard.Destination; -import javax.xml.transform.dom.DOMSource; import org.wamblee.concurrency.ReadLock; import org.wamblee.concurrency.WriteLock; @@ -72,14 +71,13 @@ public class CompositeEventListener implements EventListener { @Override @ReadLock - public void delivered(String aDocumentType, Id aEventId, - DOMSource aEvent, List aSequence, + public void delivered(EventInfo aInfo, List aSequence, Id aDestination, String aDestinationName, boolean aSuccessFlag) { for (EventListener logger : loggers) { try { - logger.delivered(aDocumentType, aEventId, aEvent, aSequence, - aDestination, aDestinationName, aSuccessFlag); + logger.delivered(aInfo, aSequence, aDestination, + aDestinationName, aSuccessFlag); } catch (Exception e) { LOGGER.log(Level.WARNING, "Logger threw exception", e); } @@ -88,11 +86,10 @@ public class CompositeEventListener implements EventListener { @Override @ReadLock - public void notDelivered(String aDocumentType, Id aEventId, - DOMSource aEvent) { + public void notDelivered(EventInfo aInfo) { for (EventListener logger : loggers) { try { - logger.notDelivered(aDocumentType, aEventId, aEvent); + logger.notDelivered(aInfo); } catch (Exception e) { LOGGER.log(Level.WARNING, "Logger threw exception", e); } diff --git a/listener/src/main/java/org/wamblee/xmlrouter/listener/EventInfo.java b/listener/src/main/java/org/wamblee/xmlrouter/listener/EventInfo.java new file mode 100644 index 0000000..1c4e48e --- /dev/null +++ b/listener/src/main/java/org/wamblee/xmlrouter/listener/EventInfo.java @@ -0,0 +1,82 @@ +/* + * Copyright 2005-2011 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.xmlrouter.listener; + +import javax.xml.transform.dom.DOMSource; + +import org.wamblee.xml.XMLDocument; +import org.wamblee.xmlrouter.common.Id; + +/** + * Combines information about the event. + * + * @author Erik Brakkee + * + */ +public class EventInfo { + + private long time; + private String source; + private Id id; + private String type; + private DOMSource event; + + public EventInfo(long aTime, String aSource, Id aId, + String aType, DOMSource aEvent) { + time = aTime; + source = aSource; + id = aId; + type = aType; + event = aEvent; + } + + public long getTime() { + return time; + } + + public String getSource() { + return source; + } + + public Id getId() { + return id; + } + + public String getType() { + return type; + } + + public void setType(String aType) { + type = aType; + } + + public DOMSource getEvent() { + return event; + } + + @Override + public String toString() { + StringBuffer buf = new StringBuffer(); + buf.append("Event("); + buf.append("time " + time); + buf.append(", source " + source); + buf.append(", id " + id); + buf.append(", type " + type); + buf.append(", event " + new XMLDocument(event).print(true)); + buf.append(")"); + return buf.toString(); + } +} diff --git a/listener/src/main/java/org/wamblee/xmlrouter/listener/EventListener.java b/listener/src/main/java/org/wamblee/xmlrouter/listener/EventListener.java index a7a5b5a..ee03b7b 100644 --- a/listener/src/main/java/org/wamblee/xmlrouter/listener/EventListener.java +++ b/listener/src/main/java/org/wamblee/xmlrouter/listener/EventListener.java @@ -18,7 +18,6 @@ package org.wamblee.xmlrouter.listener; import java.util.List; import javax.print.attribute.standard.Destination; -import javax.xml.transform.dom.DOMSource; import org.wamblee.xmlrouter.common.Id; import org.wamblee.xmlrouter.config.Transformation; @@ -34,12 +33,8 @@ public interface EventListener { /** * Called when an event is delivered to a destination. * - * @param aDocumentType - * Document type. - * @param aEventId - * Unique id for the original event. - * @param aEvent - * The event. + * @param aInfo + * Event information. * @param aSequence * Sequence of transformations performed. * @param aDestination @@ -49,19 +44,15 @@ public interface EventListener { * @param aSuccessFlag * Whether or not event delivery succeeded. */ - void delivered(String aDocumentType, Id aEventId, - DOMSource aEvent, List aSequence, + void delivered(EventInfo aInfo, List aSequence, Id aDestination, String aDestinationName, boolean aSuccessFlag); /** * Called when an event could not be delivered to any destination. * - * @param aEventId - * Unique id for the original event. - * @param aEvent - * Event. + * @param aInfo + * Event info. */ - void notDelivered(String aDocumentType, Id aEventId, - DOMSource aEvent); + void notDelivered(EventInfo aInfo); } diff --git a/listener/src/main/java/org/wamblee/xmlrouter/listener/LoggingEventListener.java b/listener/src/main/java/org/wamblee/xmlrouter/listener/LoggingEventListener.java index f623b13..189f763 100644 --- a/listener/src/main/java/org/wamblee/xmlrouter/listener/LoggingEventListener.java +++ b/listener/src/main/java/org/wamblee/xmlrouter/listener/LoggingEventListener.java @@ -20,9 +20,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import javax.print.attribute.standard.Destination; -import javax.xml.transform.dom.DOMSource; -import org.wamblee.xml.XMLDocument; import org.wamblee.xmlrouter.common.Id; import org.wamblee.xmlrouter.config.Transformation; @@ -43,14 +41,11 @@ public class LoggingEventListener implements EventListener { } @Override - public void delivered(String aDocType, Id aEventId, - DOMSource aEvent, List aSequence, + public void delivered(EventInfo aEvent, List aSequence, Id aDestination, String aDestinationName, boolean aSuccessFlag) { if (LOGGER.isLoggable(level)) { - LOGGER.log(level, "event delivered: document type '" + aDocType + - "', eventId " + aEventId + ", event '" + - new XMLDocument(aEvent).print(true) + "', sequence '" + + LOGGER.log(level, "event delivered: " + aEvent + ", sequence '" + getSequenceString(aSequence) + "', destionationId " + aDestination + ", destinationName '" + aDestinationName + "'"); } @@ -70,12 +65,9 @@ public class LoggingEventListener implements EventListener { } @Override - public void notDelivered(String aDocumentType, Id aEventId, - DOMSource aEvent) { + public void notDelivered(EventInfo aInfo) { if (LOGGER.isLoggable(level)) { - LOGGER.log(level, "event not delivered: document type '" + - aDocumentType + "', eventId " + aEventId + ", event '" + - new XMLDocument(aEvent).print(true) + "'"); + LOGGER.log(level, "event not delivered: " + aInfo); } } } diff --git a/listener/src/test/java/org/wamblee/xmlrouter/listener/CompositeEventListenerTest.java b/listener/src/test/java/org/wamblee/xmlrouter/listener/CompositeEventListenerTest.java index c268779..fd94c43 100644 --- a/listener/src/test/java/org/wamblee/xmlrouter/listener/CompositeEventListenerTest.java +++ b/listener/src/test/java/org/wamblee/xmlrouter/listener/CompositeEventListenerTest.java @@ -22,7 +22,6 @@ import static org.mockito.Mockito.*; import java.util.ArrayList; import javax.print.attribute.standard.Destination; -import javax.xml.transform.dom.DOMSource; import org.junit.Before; import org.junit.Test; @@ -34,24 +33,22 @@ public class CompositeEventListenerTest { private static final String DESTINATION_NAME = "dest"; private static final Id DESTINATION_ID = new Id( 12); - private static final Id EVENT_ID = new Id(1); - private static final String DOCTYPE = "doc"; private CompositeEventListener composite; - private DOMSource source; + private EventInfo source; @Before public void setUp() { composite = new CompositeEventListener(); - source = mock(DOMSource.class); + source = mock(EventInfo.class); } @Test public void testNoListeners() { // verify no exceptions occur. - composite.delivered(DOCTYPE, EVENT_ID, mock(DOMSource.class), - getTransformations(), DESTINATION_ID, DESTINATION_NAME, true); - composite.notDelivered(DOCTYPE, EVENT_ID, mock(DOMSource.class)); + composite.delivered(mock(EventInfo.class), getTransformations(), + DESTINATION_ID, DESTINATION_NAME, true); + composite.notDelivered(mock(EventInfo.class)); } @Test @@ -73,26 +70,24 @@ public class CompositeEventListenerTest { invokeNotDelivered(); for (EventListener listener : listeners) { - verify(listener).notDelivered(eq(DOCTYPE), eq(EVENT_ID), - same(source)); + verify(listener).notDelivered(same(source)); verifyNoMoreInteractions(listener); reset(listener); } } private void invokeDelivered() { - composite.delivered(DOCTYPE, EVENT_ID, source, getTransformations(), - DESTINATION_ID, DESTINATION_NAME, true); + composite.delivered(source, getTransformations(), DESTINATION_ID, + DESTINATION_NAME, true); } private void invokeNotDelivered() { - composite.notDelivered(DOCTYPE, EVENT_ID, source); + composite.notDelivered(source); } private void checkInvokeDelivered(EventListener listener) { - verify(listener).delivered(eq(DOCTYPE), eq(EVENT_ID), same(source), - eq(getTransformations()), eq(DESTINATION_ID), eq(DESTINATION_NAME), - eq(true)); + verify(listener).delivered(same(source), eq(getTransformations()), + eq(DESTINATION_ID), eq(DESTINATION_NAME), eq(true)); } @Test