X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FXMLRouterTest.java;h=eeb05aa3452707091410f9c65d3e9531a4c48f3d;hb=d96c59e2c9e5b15c4ce2023ac93d70b4c0ddf568;hp=50fa4f8d0694b4c0cf670915ed6f1785d8493de1;hpb=7ace7a8cf3173112717904aa825a7481dd0804e8;p=xmlrouter 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..eeb05aa 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java @@ -12,23 +12,29 @@ * 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.concurrent.atomic.AtomicLong; +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.Filter; 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 { @@ -60,6 +66,7 @@ public class XMLRouterTest { } } + private ExtendedRouterConfig routerConfig; private XMLRouter router; private DOMSource source1; private DOMSource source2; @@ -67,10 +74,14 @@ public class XMLRouterTest { private Destination destinationSpy; private Id destinationId; + private EventListener listener; @Before public void setUp() { - router = new XMLRouter(); + routerConfig = new SingleRouterConfig(new AtomicLong(1L)); + EventListener logListener = new LoggingEventListener(Level.INFO); + listener = spy(logListener); + router = new XMLRouter(new SystemClock(), routerConfig, listener); source1 = mock(DOMSource.class); source2 = mock(DOMSource.class); source3 = mock(DOMSource.class); @@ -82,7 +93,31 @@ 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 + public void testMisBehavingDocumentType() { + DocumentType type = mock(DocumentType.class); + doThrow(new RuntimeException("x")).when(type).isInstance( + any(DOMSource.class)); + routerConfig.documentTypeConfig().add(type); + router.publish("xx", mock(DOMSource.class)); + verify(listener).notDelivered(any(EventInfo.class)); + // no exception should occur. + } + + @Test + public void testMisBehavingFilter() { + registerDocumentType("any"); + Filter filter = mock(Filter.class); + doThrow(new RuntimeException("x")).when(filter).isAllowed(anyString(), + any(DOMSource.class)); + routerConfig.filterConfig().add(filter); + router.publish("xx", mock(DOMSource.class)); + verify(listener).notDelivered(any(EventInfo.class)); + // no exception should occur. } @Test @@ -90,28 +125,36 @@ 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); when(type.getName()).thenReturn(aType); - Id typeId = router.addDocumentType(type); + Id typeId = routerConfig.documentTypeConfig().add(type); } private void registerDocumentType(String aType, DOMSource aSource) { DocumentType type = mock(DocumentType.class); when(type.isInstance(same(aSource))).thenReturn(true); when(type.getName()).thenReturn(aType); - Id typeId = router.addDocumentType(type); + Id typeId = routerConfig.documentTypeConfig().add(type); } private Destination registerDestination(boolean aResult, String... types) { @@ -127,7 +170,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)); } @@ -139,7 +183,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)); } @@ -151,8 +196,12 @@ public class XMLRouterTest { Id 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 @@ -163,7 +212,8 @@ public class XMLRouterTest { doThrow(new RuntimeException()).when(destinationSpy) .chooseFromTargetTypes((Collection) anyObject()); - assertFalse(router.publish("any", source1)); + router.publish("any", source1); + verify(listener).notDelivered(any(EventInfo.class)); verify(destinationSpy, never()).receive(same(source1)); } @@ -175,7 +225,10 @@ public class XMLRouterTest { Destination destinationSpy2 = spy(destination2); Id 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)); } @@ -190,7 +243,8 @@ public class XMLRouterTest { .chooseFromTargetTypes((Collection) anyObject())) .thenReturn(null); - assertFalse(router.publish("any", source1)); + router.publish("any", source1); + verify(listener).notDelivered(any(EventInfo.class)); verify(destinationSpy, never()).receive(same(source1)); } @@ -202,7 +256,10 @@ public class XMLRouterTest { Destination destinationSpy2 = spy(destination2); Id 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)); } @@ -211,10 +268,11 @@ 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); - router.addTransformation(transformation); + routerConfig.transformationConfig().add(transformation); Destination destination = mock(Destination.class); when( @@ -224,19 +282,46 @@ 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)); + } + + @Test + public void testMisbehavingTransformationOneDestination() { + registerDocumentType("any"); + Transformation transformation = mock(Transformation.class); + when(transformation.getName()).thenReturn("trans"); + when(transformation.getFromType()).thenReturn("any"); + when(transformation.getToType()).thenReturn("bla"); + doThrow(new RuntimeException("x")).when(transformation).transform( + same(source1)); + routerConfig.transformationConfig().add(transformation); + + Destination destination = mock(Destination.class); + when( + destination.chooseFromTargetTypes((Collection) anyObject())) + .thenReturn(Arrays.asList("bla")); + + router.registerDestination(destination); + + when(destination.receive(any(DOMSource.class))).thenReturn(true); + 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); @@ -249,7 +334,7 @@ public class XMLRouterTest { Transformation transformation = createTransformation("any", "bla", source1, null); - router.addTransformation(transformation); + routerConfig.transformationConfig().add(transformation); Destination destination = mock(Destination.class); when( @@ -257,7 +342,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)); @@ -266,18 +352,21 @@ public class XMLRouterTest { Transformation transformation2 = createTransformation("any", "bla2", source1, source2); - router.addTransformation(transformation2); + routerConfig.transformationConfig().add(transformation2); when( destination.chooseFromTargetTypes((Collection) anyObject())) .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); @@ -292,7 +381,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)); @@ -305,9 +396,11 @@ public class XMLRouterTest { registerDocumentType("other", source2); Transformation transformation = createTransformation("any", "other", source1, source2); - router.addTransformation(transformation); + routerConfig.transformationConfig().add(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)); @@ -315,19 +408,40 @@ public class XMLRouterTest { @Test public void testMultipleTransformations() { - Destination dest = registerDestination(true, "any", "other"); + Destination dest = registerDestination(true, "other"); registerDocumentType("any", source1); registerDocumentType("other", source3); Transformation t1 = createTransformation("any", "intermediate", source1, source2); - router.addTransformation(t1); + routerConfig.transformationConfig().add(t1); Transformation t2 = createTransformation("intermediate", "other", source2, source3); - router.addTransformation(t2); + routerConfig.transformationConfig().add(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)); + + } }