You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@synapse.apache.org by hi...@apache.org on 2010/05/05 13:12:13 UTC

svn commit: r941247 - in /synapse/trunk/java/modules/core/src: main/java/org/apache/synapse/eventing/filters/TopicBasedEventFilter.java test/java/org/apache/synapse/eventing/filters/ test/java/org/apache/synapse/eventing/filters/EventFilterTest.java

Author: hiranya
Date: Wed May  5 11:12:12 2010
New Revision: 941247

URL: http://svn.apache.org/viewvc?rev=941247&view=rev
Log:
Event filter tests


Added:
    synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/eventing/filters/
    synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/eventing/filters/EventFilterTest.java
Modified:
    synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/eventing/filters/TopicBasedEventFilter.java

Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/eventing/filters/TopicBasedEventFilter.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/eventing/filters/TopicBasedEventFilter.java?rev=941247&r1=941246&r2=941247&view=diff
==============================================================================
--- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/eventing/filters/TopicBasedEventFilter.java (original)
+++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/eventing/filters/TopicBasedEventFilter.java Wed May  5 11:12:12 2010
@@ -28,7 +28,6 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.wso2.eventing.EventFilter;
 import org.wso2.eventing.Event;
-import org.wso2.eventing.exceptions.EventException;
 import org.jaxen.JaxenException;
 
 /**
@@ -39,7 +38,8 @@ public class TopicBasedEventFilter imple
     private AXIOMXPath sourceXpath;
     private String resultValue;
     private static final String FILTER_SEP = "/";
- private static final Log log = LogFactory.getLog(TopicBasedEventFilter.class);
+
+    private static final Log log = LogFactory.getLog(TopicBasedEventFilter.class);
 
     public String getResultValue() {
         return resultValue;
@@ -63,28 +63,25 @@ public class TopicBasedEventFilter imple
 
     public boolean match(Event<MessageContext> event) {
         MessageContext messageContext = event.getMessage();
-        String evaluatedValue=null;
+        String evaluatedValue = null;
         try {
-            OMElement topicNode = (OMElement) sourceXpath.selectSingleNode(messageContext.getEnvelope());
+            OMElement topicNode = (OMElement) sourceXpath.selectSingleNode(
+                    messageContext.getEnvelope());
             if (topicNode != null) {
                 evaluatedValue = topicNode.getText();
             }
         } catch (JaxenException e) {
             handleException("Error creating topic xpath",e);
-        }     
-        if (evaluatedValue!=null){
-        if (evaluatedValue.equals(resultValue)) {
-            return true;
-        } else if (evaluatedValue.startsWith((resultValue + FILTER_SEP).trim())) {
-            return true;
         }
+        if (evaluatedValue != null){
+            if (evaluatedValue.equals(resultValue)) {
+                return true;
+            } else if (evaluatedValue.startsWith((resultValue + FILTER_SEP).trim())) {
+                return true;
+            }
         }
         return false;
     }
-   private void handleException(String message) {
-        log.error(message);
-        throw new SynapseException(message);
-    }
 
     private void handleException(String message, Exception e) {
         log.error(message, e);

Added: synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/eventing/filters/EventFilterTest.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/eventing/filters/EventFilterTest.java?rev=941247&view=auto
==============================================================================
--- synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/eventing/filters/EventFilterTest.java (added)
+++ synapse/trunk/java/modules/core/src/test/java/org/apache/synapse/eventing/filters/EventFilterTest.java Wed May  5 11:12:12 2010
@@ -0,0 +1,71 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.apache.synapse.eventing.filters;
+
+import junit.framework.TestCase;
+import org.apache.axis2.context.MessageContext;
+import org.apache.synapse.mediators.TestUtils;
+import org.apache.synapse.util.xpath.SynapseXPath;
+import org.wso2.eventing.Event;
+
+public class EventFilterTest extends TestCase {
+
+    public void testTopicBasedEventFilter() {
+        String status = "snow";
+        try {
+            MessageContext msgCtx = TestUtils.getAxis2MessageContext("<weatherCondition>" +
+                    status + "</weatherCondition>", null).
+                    getAxis2MessageContext();
+            Event<MessageContext> event = new Event<MessageContext>();
+            event.setMessage(msgCtx);
+
+            TopicBasedEventFilter filter = new TopicBasedEventFilter();
+            filter.setResultValue(status);
+            filter.setSourceXpath(new SynapseXPath("//weatherCondition"));
+            assertTrue(filter.match(event));
+
+            filter.setResultValue("rain");
+            assertFalse(filter.match(event));            
+        } catch (Exception e) {
+            fail("Error while constructing the test message context: " + e.getMessage());
+        }
+    }
+
+    public void testXPathBasedEventFilter() {
+        String status = "snow";
+        try {
+            org.apache.synapse.MessageContext msgCtx =
+                    TestUtils.createLightweightSynapseMessageContext("<weatherCondition>" +
+                            status + "</weatherCondition>");
+
+            XPathBasedEventFilter filter = new XPathBasedEventFilter();
+            filter.setResultValue(status);
+            filter.setSourceXpath(new SynapseXPath("//weatherCondition"));
+            assertTrue(filter.isSatisfied(msgCtx));
+
+            filter.setResultValue("rain");
+            assertFalse(filter.isSatisfied(msgCtx));
+        } catch (Exception e) {
+            e.printStackTrace();
+            fail("Error while constructing the test message context: " + e.getMessage());
+        }
+    }
+    
+}