X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FXMLRouterTest.java;h=c6b1aa0b8f65227b08b40a34442632632052e311;hb=9dbc2844950b55ae552fe74840954ea71b754c7a;hp=2746253de58653f31b04a21d63ef990f89680b44;hpb=e52385618670b54a5c6a4f2fbfab381bef43a905;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 2746253..c6b1aa0 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,10 @@ import static org.mockito.Mockito.*; import java.util.Arrays; import java.util.Collection; +import java.util.UUID; +import java.util.logging.Handler; import java.util.logging.Level; +import java.util.logging.Logger; import javax.xml.transform.dom.DOMSource; @@ -30,7 +33,6 @@ 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,8 +81,11 @@ public class XMLRouterTest { @Before public void setUp() { - routerConfig = new SingleRouterConfig(new Id( - "routerconfig")); + + Logger logger = Logger.getLogger(XMLRouter.class.getName()); + setLogLevel(logger, Level.FINEST); + + routerConfig = new SingleRouterConfig("routerconfig"); config = new XMLRouterConfigurationImpl(routerConfig); EventListener logListener = new LoggingEventListener(Level.INFO); listener = spy(logListener); @@ -90,6 +95,17 @@ public class XMLRouterTest { source3 = mock(DOMSource.class); } + private void setLogLevel(Logger aLogger, Level aLevel) { + aLogger.setLevel(aLevel); + for (Handler handler : aLogger.getHandlers()) { + handler.setLevel(Level.FINEST); + } + Logger parent = aLogger.getParent(); + if (parent != null) { + setLogLevel(parent, aLevel); + } + } + @Test public void testNoInputDocumentsRegistered() { Destination destination = new MyDestination(true, Arrays.asList("any")); @@ -102,7 +118,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 +127,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 +145,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"); @@ -147,7 +175,7 @@ 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); routerConfig.documentTypeConfig().add(type); @@ -157,6 +185,7 @@ public class XMLRouterTest { DocumentType type = mock(DocumentType.class); when(type.isInstance(same(aSource))).thenReturn(true); when(type.getName()).thenReturn(aType); + when(type.getId()).thenReturn(new Id(aType)); routerConfig.documentTypeConfig().add(type); } @@ -300,6 +329,41 @@ public class XMLRouterTest { verify(listener).notDelivered(any(EventInfo.class)); } + @Test + public void testOneTransformationOneDestinationFilterRejectsDestinationDocType() { + registerDocumentType("any"); + + Transformation transformation = mock(Transformation.class); + 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); + + Filter filter = mock(Filter.class); + when(filter.getId()).thenReturn(new Id("f")); + when(filter.isAllowed(anyString(), same(source2))).thenReturn(false); + when(filter.isAllowed(anyString(), same(source1))).thenReturn(true); + routerConfig.filterConfig().add(filter); + + config.setRouterConfig(routerConfig); + + 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)); + + verify(transformation).transform(source1); + verify(destination, never()).receive(any(DOMSource.class)); + } + @Test public void testMisbehavingTransformationOneDestination() { registerDocumentType("any"); @@ -327,8 +391,8 @@ public class XMLRouterTest { private Transformation createTransformation(String aFrom, String aTo, DOMSource aSource, DOMSource aTarget) { Transformation transformation = mock(Transformation.class); - when(transformation.getId()) - .thenReturn(new Id("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); @@ -437,6 +501,32 @@ public class XMLRouterTest { verify(dest).receive(same(source3)); } + @Test + public void testMultipleTransformationsFilterRejectsIntermediateDocument() { + Destination dest = registerDestination(true, "other"); + registerDocumentType("any", source1); + registerDocumentType("other", source3); + + Transformation t1 = createTransformation("any", "intermediate", + source1, source2); + routerConfig.transformationConfig().add(t1); + Transformation t2 = createTransformation("intermediate", "other", + source2, source3); + routerConfig.transformationConfig().add(t2); + config.setRouterConfig(routerConfig); + + Filter filter = mock(Filter.class); + when(filter.getId()).thenReturn(new Id("f")); + when(filter.isAllowed(anyString(), same(source1))).thenReturn(true); + when(filter.isAllowed(anyString(), same(source2))).thenReturn(false); + when(filter.isAllowed(anyString(), same(source3))).thenReturn(true); + routerConfig.filterConfig().add(filter); + + router.publish("source", source1); + verify(listener).notDelivered(any(EventInfo.class)); + verify(dest, never()).receive(any(DOMSource.class)); + } + @Test public void testDestinationGivesError() { Destination destination = mock(Destination.class);