X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FXMLRouterTest.java;h=c1776f475e8723e677530c2c0382f7e56890a23f;hb=f8027d76e1c3e517a8b80a3476f51adee845fc5b;hp=7da3de5f932119f0499bb00761f00fb199aa24fa;hpb=1e030ca3ecfaf7a3c179978c297a6aa6b31aead3;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 7da3de5..c1776f4 100644 --- a/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java +++ b/impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java @@ -20,7 +20,7 @@ import static org.mockito.Mockito.*; import java.util.Arrays; import java.util.Collection; -import java.util.concurrent.atomic.AtomicLong; +import java.util.UUID; import java.util.logging.Level; import javax.xml.transform.dom.DOMSource; @@ -31,6 +31,7 @@ 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.RouterConfig; import org.wamblee.xmlrouter.config.Transformation; import org.wamblee.xmlrouter.listener.EventInfo; import org.wamblee.xmlrouter.listener.EventListener; @@ -79,9 +80,9 @@ public class XMLRouterTest { @Before public void setUp() { - routerConfig = new SingleRouterConfig(new AtomicLong(1L)); - config = new XMLRouterConfigurationImpl(routerConfig, - new TransformationPaths()); + routerConfig = new SingleRouterConfig(new Id( + "routerconfig")); + config = new XMLRouterConfigurationImpl(routerConfig); EventListener logListener = new LoggingEventListener(Level.INFO); listener = spy(logListener); router = new XMLRouter(new SystemClock(), config, listener); @@ -102,7 +103,7 @@ public class XMLRouterTest { @Test public void testMisBehavingDocumentType() { - DocumentType type = mock(DocumentType.class); + DocumentType type = mockDocument("docid"); doThrow(new RuntimeException("x")).when(type).isInstance( any(DOMSource.class)); routerConfig.documentTypeConfig().add(type); @@ -111,10 +112,16 @@ public class XMLRouterTest { // no exception should occur. } + private DocumentType mockDocument(String docid) { + DocumentType type = mock(DocumentType.class); + when(type.getId()).thenReturn(new Id(docid)); + return type; + } + @Test public void testMisBehavingFilter() { registerDocumentType("any"); - Filter filter = mock(Filter.class); + Filter filter = mockFilter("filterid"); doThrow(new RuntimeException("x")).when(filter).isAllowed(anyString(), any(DOMSource.class)); routerConfig.filterConfig().add(filter); @@ -123,6 +130,12 @@ public class XMLRouterTest { // no exception should occur. } + private Filter mockFilter(String filterId) { + Filter filter = mock(Filter.class); + when(filter.getId()).thenReturn(new Id(filterId)); + return filter; + } + @Test public void testOneDestinationNoTransformationSuccess() { destinationSpy = registerDestination(true, "any"); @@ -130,7 +143,7 @@ public class XMLRouterTest { router.publish("any", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(destinationSpy).receive(same(source1)); // Unregister the destination. @@ -147,17 +160,18 @@ public class XMLRouterTest { } private void registerDocumentType(String aType) { - DocumentType type = mock(DocumentType.class); + DocumentType type = mockDocument(UUID.randomUUID().toString()); when(type.isInstance(any(DOMSource.class))).thenReturn(true); when(type.getName()).thenReturn(aType); - Id typeId = routerConfig.documentTypeConfig().add(type); + 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 = routerConfig.documentTypeConfig().add(type); + when(type.getId()).thenReturn(new Id(aType)); + routerConfig.documentTypeConfig().add(type); } private Destination registerDestination(boolean aResult, String... types) { @@ -201,7 +215,7 @@ public class XMLRouterTest { router.publish("any", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(destinationSpy2).receive(same(source1)); @@ -230,7 +244,7 @@ public class XMLRouterTest { .registerDestination(destinationSpy2); router.publish("any", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(destinationSpy, never()).receive(same(source1)); verify(destinationSpy2).receive(same(source1)); @@ -261,7 +275,7 @@ public class XMLRouterTest { .registerDestination(destinationSpy2); router.publish("any", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(destinationSpy, never()).receive(same(source1)); verify(destinationSpy2).receive(same(source1)); @@ -271,11 +285,13 @@ public class XMLRouterTest { public void testOneTransformationOneDestination() { registerDocumentType("any"); Transformation transformation = mock(Transformation.class); - when(transformation.getName()).thenReturn("trans"); + when(transformation.getId()) + .thenReturn(new Id("trans")); when(transformation.getFromType()).thenReturn("any"); when(transformation.getToType()).thenReturn("bla"); when(transformation.transform(same(source1))).thenReturn(source2); routerConfig.transformationConfig().add(transformation); + config.setRouterConfig(routerConfig); Destination destination = mock(Destination.class); when( @@ -287,7 +303,7 @@ public class XMLRouterTest { when(destination.receive(any(DOMSource.class))).thenReturn(true); router.publish("bla", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(transformation).transform(source1); verify(destination).receive(same(source2)); @@ -302,7 +318,8 @@ public class XMLRouterTest { public void testMisbehavingTransformationOneDestination() { registerDocumentType("any"); Transformation transformation = mock(Transformation.class); - when(transformation.getName()).thenReturn("trans"); + when(transformation.getId()) + .thenReturn(new Id("trans")); when(transformation.getFromType()).thenReturn("any"); when(transformation.getToType()).thenReturn("bla"); doThrow(new RuntimeException("x")).when(transformation).transform( @@ -324,7 +341,8 @@ public class XMLRouterTest { private Transformation createTransformation(String aFrom, String aTo, DOMSource aSource, DOMSource aTarget) { Transformation transformation = mock(Transformation.class); - when(transformation.getName()).thenReturn("trans"); + when(transformation.getId()).thenReturn( + new Id(UUID.randomUUID().toString())); when(transformation.getFromType()).thenReturn(aFrom); when(transformation.getToType()).thenReturn(aTo); when(transformation.transform(same(aSource))).thenReturn(aTarget); @@ -338,7 +356,7 @@ public class XMLRouterTest { source1, null); routerConfig.transformationConfig().add(transformation); - + config.setRouterConfig(routerConfig); Destination destination = mock(Destination.class); when( destination.chooseFromTargetTypes((Collection) anyObject())) @@ -356,12 +374,14 @@ public class XMLRouterTest { source1, source2); routerConfig.transformationConfig().add(transformation2); + config.setRouterConfig(routerConfig); when( destination.chooseFromTargetTypes((Collection) anyObject())) .thenReturn(Arrays.asList("bla", "bla2")); reset(transformation); - when(transformation.getName()).thenReturn("trans"); + when(transformation.getId()) + .thenReturn(new Id("trans")); when(transformation.getFromType()).thenReturn("any"); when(transformation.getToType()).thenReturn("bla"); when(transformation.transform(same(source1))).thenReturn(null); @@ -369,7 +389,7 @@ public class XMLRouterTest { when(destination.receive(any(DOMSource.class))).thenReturn(true); router.publish("bla", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(transformation).transform(source1); verify(transformation2).transform(source1); @@ -386,7 +406,7 @@ public class XMLRouterTest { router.publish("source", source1); verify(listener, times(2)).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(dest1).receive(same(source1)); verify(dest2).receive(same(source1)); @@ -400,10 +420,11 @@ public class XMLRouterTest { Transformation transformation = createTransformation("any", "other", source1, source2); routerConfig.transformationConfig().add(transformation); + config.setRouterConfig(routerConfig); router.publish("source", source1); verify(listener, times(2)).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(dest).receive(same(source1)); verify(dest).receive(same(source2)); @@ -421,10 +442,11 @@ public class XMLRouterTest { Transformation t2 = createTransformation("intermediate", "other", source2, source3); routerConfig.transformationConfig().add(t2); + config.setRouterConfig(routerConfig); router.publish("source", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(true)); + anyListOf(Transformation.class), anyString(), eq(true)); verify(dest).receive(same(source3)); } @@ -444,7 +466,7 @@ public class XMLRouterTest { router.publish("source", source1); verify(listener).delivered(any(EventInfo.class), - anyListOf(Transformation.class), anyLong(), anyString(), eq(false)); + anyListOf(Transformation.class), anyString(), eq(false)); } }