event listener is now used by the xml router and the publish method of the gateway is
[xmlrouter] / impl / src / test / java / org / wamblee / xmlrouter / impl / XMLRouterTest.java
index c4a73babb823ecfa6d1ad0bc32faa92e6c559522..47eb21a9d7226708946e0f0da4b4cb5a62f33a31 100644 (file)
@@ -1,19 +1,38 @@
+/*
+ * 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.impl;
 
-import static junit.framework.Assert.*;
 import static org.mockito.Matchers.*;
 import static org.mockito.Mockito.*;
 
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.logging.Level;
 
 import javax.xml.transform.dom.DOMSource;
 
 import org.junit.Before;
 import org.junit.Test;
+import org.wamblee.general.SystemClock;
 import org.wamblee.xmlrouter.common.Id;
 import org.wamblee.xmlrouter.config.DocumentType;
 import org.wamblee.xmlrouter.config.Transformation;
+import org.wamblee.xmlrouter.listener.EventInfo;
+import org.wamblee.xmlrouter.listener.EventListener;
+import org.wamblee.xmlrouter.listener.LoggingEventListener;
 import org.wamblee.xmlrouter.subscribe.Destination;
 
 public class XMLRouterTest {
@@ -52,10 +71,13 @@ public class XMLRouterTest {
 
     private Destination destinationSpy;
     private Id<Destination> destinationId;
+    private EventListener listener;
 
     @Before
     public void setUp() {
-        router = new XMLRouter();
+        EventListener logListener = new LoggingEventListener(Level.INFO);
+        listener = spy(logListener);
+        router = new XMLRouter(new SystemClock(), listener);
         source1 = mock(DOMSource.class);
         source2 = mock(DOMSource.class);
         source3 = mock(DOMSource.class);
@@ -67,7 +89,8 @@ public class XMLRouterTest {
         destinationSpy = spy(destination);
 
         destinationId = router.registerDestination(destinationSpy);
-        assertFalse(router.publish("any", source1));
+        router.publish("any", source1);
+        verify(listener).notDelivered(any(EventInfo.class));
     }
 
     @Test
@@ -75,16 +98,24 @@ public class XMLRouterTest {
         destinationSpy = registerDestination(true, "any");
         registerDocumentType("any");
 
-        assertTrue(router.publish("any", source1));
+        router.publish("any", source1);
+        verify(listener).delivered(any(EventInfo.class),
+            anyListOf(Transformation.class), anyLong(), anyString(), eq(true));
         verify(destinationSpy).receive(same(source1));
 
         // Unregister the destination.
         router.unregisterDestination(destinationId);
-        reset(destinationSpy);
-        assertFalse(router.publish("any", source2));
+        resetMocks();
+        router.publish("any", source2);
+        verify(listener).notDelivered(any(EventInfo.class));
         verifyNoMoreInteractions(destinationSpy);
     }
 
+    private void resetMocks() {
+        reset(destinationSpy);
+        reset(listener);
+    }
+
     private void registerDocumentType(String aType) {
         DocumentType type = mock(DocumentType.class);
         when(type.isInstance(any(DOMSource.class))).thenReturn(true);
@@ -112,7 +143,8 @@ public class XMLRouterTest {
         destinationSpy = registerDestination(true);
         registerDocumentType("any");
 
-        assertFalse(router.publish("any", source1));
+        router.publish("any", source1);
+        verify(listener).notDelivered(any(EventInfo.class));
         verify(destinationSpy, never()).receive(any(DOMSource.class));
     }
 
@@ -124,7 +156,8 @@ public class XMLRouterTest {
         doThrow(new RuntimeException()).when(destinationSpy).receive(
             any(DOMSource.class));
 
-        assertFalse(router.publish("any", source1));
+        router.publish("any", source1);
+        verify(listener).notDelivered(any(EventInfo.class));
         verify(destinationSpy).receive(same(source1));
     }
 
@@ -136,8 +169,12 @@ public class XMLRouterTest {
         Id<Destination> destinationId2 = router
             .registerDestination(destinationSpy2);
 
-        assertTrue(router.publish("any", source1));
+        router.publish("any", source1);
+        verify(listener).delivered(any(EventInfo.class),
+            anyListOf(Transformation.class), anyLong(), anyString(), eq(true));
+
         verify(destinationSpy2).receive(same(source1));
+
     }
 
     @Test
@@ -148,7 +185,8 @@ public class XMLRouterTest {
         doThrow(new RuntimeException()).when(destinationSpy)
             .chooseFromTargetTypes((Collection<String>) anyObject());
 
-        assertFalse(router.publish("any", source1));
+        router.publish("any", source1);
+        verify(listener).notDelivered(any(EventInfo.class));
         verify(destinationSpy, never()).receive(same(source1));
     }
 
@@ -160,7 +198,10 @@ public class XMLRouterTest {
         Destination destinationSpy2 = spy(destination2);
         Id<Destination> destinationId2 = router
             .registerDestination(destinationSpy2);
-        assertTrue(router.publish("any", source1));
+        router.publish("any", source1);
+        verify(listener).delivered(any(EventInfo.class),
+            anyListOf(Transformation.class), anyLong(), anyString(), eq(true));
+
         verify(destinationSpy, never()).receive(same(source1));
         verify(destinationSpy2).receive(same(source1));
     }
@@ -175,7 +216,8 @@ public class XMLRouterTest {
                 .chooseFromTargetTypes((Collection<String>) anyObject()))
             .thenReturn(null);
 
-        assertFalse(router.publish("any", source1));
+        router.publish("any", source1);
+        verify(listener).notDelivered(any(EventInfo.class));
         verify(destinationSpy, never()).receive(same(source1));
     }
 
@@ -187,7 +229,10 @@ public class XMLRouterTest {
         Destination destinationSpy2 = spy(destination2);
         Id<Destination> destinationId2 = router
             .registerDestination(destinationSpy2);
-        assertTrue(router.publish("any", source1));
+        router.publish("any", source1);
+        verify(listener).delivered(any(EventInfo.class),
+            anyListOf(Transformation.class), anyLong(), anyString(), eq(true));
+
         verify(destinationSpy, never()).receive(same(source1));
         verify(destinationSpy2).receive(same(source1));
     }
@@ -196,6 +241,7 @@ public class XMLRouterTest {
     public void testOneTransformationOneDestination() {
         registerDocumentType("any");
         Transformation transformation = mock(Transformation.class);
+        when(transformation.getName()).thenReturn("trans");
         when(transformation.getFromType()).thenReturn("any");
         when(transformation.getToType()).thenReturn("bla");
         when(transformation.transform(same(source1))).thenReturn(source2);
@@ -209,19 +255,23 @@ public class XMLRouterTest {
         router.registerDestination(destination);
 
         when(destination.receive(any(DOMSource.class))).thenReturn(true);
-        assertTrue(router.publish("bla", source1));
+        router.publish("bla", source1);
+        verify(listener).delivered(any(EventInfo.class),
+            anyListOf(Transformation.class), anyLong(), anyString(), eq(true));
 
         verify(transformation).transform(source1);
         verify(destination).receive(same(source2));
 
         // now the same when the destination rejects the event.
         when(destination.receive(any(DOMSource.class))).thenReturn(false);
-        assertFalse(router.publish("bla", source1));
+        router.publish("bla", source1);
+        verify(listener).notDelivered(any(EventInfo.class));
     }
 
     private Transformation createTransformation(String aFrom, String aTo,
         DOMSource aSource, DOMSource aTarget) {
         Transformation transformation = mock(Transformation.class);
+        when(transformation.getName()).thenReturn("trans");
         when(transformation.getFromType()).thenReturn(aFrom);
         when(transformation.getToType()).thenReturn(aTo);
         when(transformation.transform(same(aSource))).thenReturn(aTarget);
@@ -242,7 +292,8 @@ public class XMLRouterTest {
             .thenReturn(Arrays.asList("bla"));
         router.registerDestination(destination);
 
-        assertFalse(router.publish("bla", source1));
+        router.publish("bla", source1);
+        verify(listener).notDelivered(any(EventInfo.class));
 
         verify(transformation).transform(source1);
         verify(destination, never()).receive(any(DOMSource.class));
@@ -257,12 +308,15 @@ public class XMLRouterTest {
             .thenReturn(Arrays.asList("bla", "bla2"));
 
         reset(transformation);
+        when(transformation.getName()).thenReturn("trans");
         when(transformation.getFromType()).thenReturn("any");
         when(transformation.getToType()).thenReturn("bla");
         when(transformation.transform(same(source1))).thenReturn(null);
 
         when(destination.receive(any(DOMSource.class))).thenReturn(true);
-        assertTrue(router.publish("bla", source1));
+        router.publish("bla", source1);
+        verify(listener).delivered(any(EventInfo.class),
+            anyListOf(Transformation.class), anyLong(), anyString(), eq(true));
 
         verify(transformation).transform(source1);
         verify(transformation2).transform(source1);
@@ -277,7 +331,9 @@ public class XMLRouterTest {
         Destination dest2 = registerDestination(true, "any");
         registerDocumentType("any");
 
-        assertTrue(router.publish("source", source1));
+        router.publish("source", source1);
+        verify(listener, times(2)).delivered(any(EventInfo.class),
+            anyListOf(Transformation.class), anyLong(), anyString(), eq(true));
 
         verify(dest1).receive(same(source1));
         verify(dest2).receive(same(source1));
@@ -292,7 +348,9 @@ public class XMLRouterTest {
             source1, source2);
         router.addTransformation(transformation);
 
-        assertTrue(router.publish("source", source1));
+        router.publish("source", source1);
+        verify(listener, times(2)).delivered(any(EventInfo.class),
+            anyListOf(Transformation.class), anyLong(), anyString(), eq(true));
 
         verify(dest).receive(same(source1));
         verify(dest).receive(same(source2));
@@ -300,7 +358,7 @@ public class XMLRouterTest {
 
     @Test
     public void testMultipleTransformations() {
-        Destination dest = registerDestination(true, "any", "other");
+        Destination dest = registerDestination(true, "other");
         registerDocumentType("any", source1);
         registerDocumentType("other", source3);
 
@@ -311,8 +369,29 @@ public class XMLRouterTest {
             source2, source3);
         router.addTransformation(t2);
 
-        assertTrue(router.publish("source", source1));
+        router.publish("source", source1);
+        verify(listener).delivered(any(EventInfo.class),
+            anyListOf(Transformation.class), anyLong(), anyString(), eq(true));
 
         verify(dest).receive(same(source3));
     }
+
+    @Test
+    public void testDestinationGivesError() {
+        Destination destination = mock(Destination.class);
+        when(destination.getName()).thenReturn("name");
+        when(destination.chooseFromTargetTypes(anyCollectionOf(String.class)))
+            .thenReturn(Arrays.asList("any"));
+        doThrow(new RuntimeException("x")).when(destination).receive(
+            any(DOMSource.class));
+        router.registerDestination(destination);
+
+        registerDocumentType("any");
+
+        router.publish("source", source1);
+
+        verify(listener).delivered(any(EventInfo.class),
+            anyListOf(Transformation.class), anyLong(), anyString(), eq(false));
+
+    }
 }