Verified that filtering of types created by transformations also works.
authorErik Brakkee <erik@brakkee.org>
Thu, 11 Aug 2011 20:03:07 +0000 (22:03 +0200)
committerErik Brakkee <erik@brakkee.org>
Thu, 11 Aug 2011 20:03:07 +0000 (22:03 +0200)
impl/src/main/java/org/wamblee/xmlrouter/impl/XMLRouter.java
impl/src/test/java/org/wamblee/xmlrouter/impl/XMLRouterTest.java

index 09bbdc1dc50cc78c89e1900b879360c1d32e1dab..9c0f2d4937e8ad5ff068da5d97aec56c09efabf5 100644 (file)
@@ -41,8 +41,6 @@ import org.wamblee.xmlrouter.publish.Gateway;
 import org.wamblee.xmlrouter.subscribe.Destination;
 import org.wamblee.xmlrouter.subscribe.DestinationRegistry;
 
-// TODO check intermediate types during transformation based on filters. 
-
 /**
  * The XML Router.
  * 
@@ -97,7 +95,7 @@ public class XMLRouter implements Gateway, DestinationRegistry {
                     LOGGER
                         .log(
                             Level.FINE,
-                            "Event ''0}'' from source {1} removed because of filters.",
+                            "Event ''{0}'' from source ''{1}'' removed because of filters.",
                             new Object[] { doc, aSource });
                 }
             }
index c1776f475e8723e677530c2c0382f7e56890a23f..c4375c70331044e221015ac3108dbc61b4fbf526 100644 (file)
@@ -21,7 +21,9 @@ 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;
 
@@ -80,6 +82,10 @@ public class XMLRouterTest {
 
     @Before
     public void setUp() {
+
+        Logger logger = Logger.getLogger(XMLRouter.class.getName());
+        setLogLevel(logger, Level.FINEST);
+
         routerConfig = new SingleRouterConfig(new Id<RouterConfig>(
             "routerconfig"));
         config = new XMLRouterConfigurationImpl(routerConfig);
@@ -91,6 +97,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"));
@@ -314,6 +331,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<Transformation>("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<Filter>("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<String>) 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");
@@ -451,6 +503,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<Filter>("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);