unit tests for composite event listener.
authorErik Brakkee <erik@brakkee.org>
Sun, 24 Jul 2011 18:23:13 +0000 (20:23 +0200)
committerErik Brakkee <erik@brakkee.org>
Sun, 24 Jul 2011 18:23:13 +0000 (20:23 +0200)
listener/src/main/java/org/wamblee/xmlrouter/listener/CompositeEventListener.java
listener/src/test/java/org/wamblee/xmlrouter/listener/CompositeEventListenerTest.java [new file with mode: 0644]

index be6c92a06bd07397dde310e11ff65d6d3e334503..15432eaca8f53c9702231a998e110410e929c865 100644 (file)
@@ -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<EventListener> 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 (file)
index 0000000..c268779
--- /dev/null
@@ -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> DESTINATION_ID = new Id<Destination>(
+        12);
+    private static final Id<DOMSource> EVENT_ID = new Id<DOMSource>(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<Transformation> getTransformations() {
+        return new ArrayList<Transformation>();
+    }
+
+    @Test
+    public void testMultipleListeners() {
+        EventListener listener1 = mock(EventListener.class);
+        EventListener listener2 = mock(EventListener.class);
+        composite.addListener(listener1);
+        composite.addListener(listener2);
+
+        invokeAndVerifyListenerInvoked(listener1, listener2);
+    }
+}