From: Erik Brakkee Date: Sun, 24 Jul 2011 18:23:13 +0000 (+0200) Subject: unit tests for composite event listener. X-Git-Tag: PAX_EXAM_MOCKITO_ISSUE~40 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=bf43ef9863e3d5827eac1a4ce0fafafaadd92a08;p=xmlrouter unit tests for composite event listener. --- 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 be6c92a..15432ea 100644 --- a/listener/src/main/java/org/wamblee/xmlrouter/listener/CompositeEventListener.java +++ b/listener/src/main/java/org/wamblee/xmlrouter/listener/CompositeEventListener.java @@ -46,7 +46,7 @@ public class CompositeEventListener implements EventListener { * Logger */ @WriteLock - public void addLogger(EventListener aLogger) { + public void addListener(EventListener aLogger) { loggers.add(aLogger); } @@ -58,7 +58,7 @@ public class CompositeEventListener implements EventListener { * @return True iff the logger was removed. */ @WriteLock - public boolean removeLogger(EventListener aLogger) { + public boolean removeListener(EventListener aLogger) { Iterator logger = loggers.iterator(); while (logger.hasNext()) { EventListener value = logger.next(); diff --git a/listener/src/test/java/org/wamblee/xmlrouter/listener/CompositeEventListenerTest.java b/listener/src/test/java/org/wamblee/xmlrouter/listener/CompositeEventListenerTest.java new file mode 100644 index 0000000..c268779 --- /dev/null +++ b/listener/src/test/java/org/wamblee/xmlrouter/listener/CompositeEventListenerTest.java @@ -0,0 +1,128 @@ +/* + * 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 static junit.framework.Assert.*; +import static org.mockito.Matchers.*; +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; +import org.wamblee.xmlrouter.common.Id; +import org.wamblee.xmlrouter.config.Transformation; + +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; + + @Before + public void setUp() { + composite = new CompositeEventListener(); + source = mock(DOMSource.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)); + } + + @Test + public void testOneListener() { + EventListener listener = mock(EventListener.class); + composite.addListener(listener); + + invokeAndVerifyListenerInvoked(listener); + } + + private void invokeAndVerifyListenerInvoked(EventListener... listeners) { + invokeDelivered(); + + for (EventListener listener : listeners) { + checkInvokeDelivered(listener); + verifyNoMoreInteractions(listener); + reset(listener); + } + + invokeNotDelivered(); + for (EventListener listener : listeners) { + verify(listener).notDelivered(eq(DOCTYPE), eq(EVENT_ID), + same(source)); + verifyNoMoreInteractions(listener); + reset(listener); + } + } + + private void invokeDelivered() { + composite.delivered(DOCTYPE, EVENT_ID, source, getTransformations(), + DESTINATION_ID, DESTINATION_NAME, true); + } + + private void invokeNotDelivered() { + composite.notDelivered(DOCTYPE, EVENT_ID, 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)); + } + + @Test + public void testRemoveListener() { + EventListener listener1 = mock(EventListener.class); + composite.addListener(listener1); + + EventListener listener2 = mock(EventListener.class); + assertFalse(composite.removeListener(listener2)); + + invokeAndVerifyListenerInvoked(listener1); + + assertTrue(composite.removeListener(listener1)); + invokeDelivered(); + verifyNoMoreInteractions(listener1); + invokeNotDelivered(); + verifyNoMoreInteractions(listener1); + } + + private ArrayList getTransformations() { + return new ArrayList(); + } + + @Test + public void testMultipleListeners() { + EventListener listener1 = mock(EventListener.class); + EventListener listener2 = mock(EventListener.class); + composite.addListener(listener1); + composite.addListener(listener2); + + invokeAndVerifyListenerInvoked(listener1, listener2); + } +}