Author: hiranya
Date: Sat Sep 11 13:27:49 2010
New Revision: 996144

URL: http://svn.apache.org/viewvc?rev=996144&view=rev
Log:
Test cases for the two new text retriever implementations


Modified:
    
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/evaluators/source/SOAPEnvelopeTextRetriever.java
    
synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/evaluators/source/SourceTextRetrieverTest.java

Modified: 
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/evaluators/source/SOAPEnvelopeTextRetriever.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/evaluators/source/SOAPEnvelopeTextRetriever.java?rev=996144&r1=996143&r2=996144&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/evaluators/source/SOAPEnvelopeTextRetriever.java
 (original)
+++ 
synapse/trunk/java/modules/commons/src/main/java/org/apache/synapse/commons/evaluators/source/SOAPEnvelopeTextRetriever.java
 Sat Sep 11 13:27:49 2010
@@ -34,6 +34,7 @@ import java.util.List;
 public class SOAPEnvelopeTextRetriever implements SourceTextRetriever {
 
     private String xpath;
+    private AXIOMXPath compiledXPath;
 
     public SOAPEnvelopeTextRetriever(String xpath) {
         this.xpath = xpath;
@@ -41,44 +42,46 @@ public class SOAPEnvelopeTextRetriever i
 
     public String getSourceText(EvaluatorContext context) throws 
EvaluatorException {
         SOAPEnvelope envelope = context.getMessageContext().getEnvelope();
+        Object result;
+        
         try {
-            AXIOMXPath axiomXPath = new AXIOMXPath(xpath);
-            Object result = axiomXPath.evaluate(envelope);
+            if (compiledXPath == null) {
+                compiledXPath = new AXIOMXPath(xpath);
+            }
+            result = compiledXPath.evaluate(envelope);
+        } catch (JaxenException e) {
+            throw new EvaluatorException("Error while parsing the XPath 
expression: " + xpath, e);
+        }
 
-            if (result instanceof List) {
-                List list = (List) result;
-                if (list.size() == 1 && list.get(0) == null) {
-                    return null;
-                }
+        if (result instanceof List) {
+            List list = (List) result;
+            if (list.size() == 1 && list.get(0) == null) {
+                return null;
+            }
 
-                StringBuffer textValue = new StringBuffer();
-                for (Object o : list) {
-                    if (o instanceof OMTextImpl) {
-                        textValue.append(((OMTextImpl) o).getText());
-
-                    } else if (o instanceof OMElementImpl) {
-                        String s = ((OMElementImpl) o).getText();
-                        if (s.trim().length() == 0) {
-                            s = o.toString();
-                        }
-                        textValue.append(s);
-
-                    } else if (o instanceof OMDocumentImpl) {
-                        textValue.append(((OMDocumentImpl) 
o).getOMDocumentElement().toString());
-                    } else if (o instanceof OMAttribute) {
-                        textValue.append(((OMAttribute) 
o).getAttributeValue());
+            StringBuffer textValue = new StringBuffer();
+            for (Object o : list) {
+                if (o instanceof OMTextImpl) {
+                    textValue.append(((OMTextImpl) o).getText());
+
+                } else if (o instanceof OMElementImpl) {
+                    String s = ((OMElementImpl) o).getText();
+                    if (s.trim().length() == 0) {
+                        s = o.toString();
                     }
-                }
-
-                return textValue.toString();
+                    textValue.append(s);
 
-            } else {
-                return result.toString();
+                } else if (o instanceof OMDocumentImpl) {
+                    textValue.append(((OMDocumentImpl) 
o).getOMDocumentElement().toString());
+                } else if (o instanceof OMAttribute) {
+                    textValue.append(((OMAttribute) o).getAttributeValue());
+                }
             }
 
+            return textValue.toString();
 
-        } catch (JaxenException e) {
-            throw new EvaluatorException("", e);
+        } else {
+            return result.toString();
         }
     }
 

Modified: 
synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/evaluators/source/SourceTextRetrieverTest.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/evaluators/source/SourceTextRetrieverTest.java?rev=996144&r1=996143&r2=996144&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/evaluators/source/SourceTextRetrieverTest.java
 (original)
+++ 
synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/evaluators/source/SourceTextRetrieverTest.java
 Sat Sep 11 13:27:49 2010
@@ -23,11 +23,20 @@ import junit.framework.TestCase;
 import org.apache.synapse.commons.evaluators.EvaluatorContext;
 import org.apache.synapse.commons.evaluators.EvaluatorException;
 import org.apache.synapse.commons.evaluators.EvaluatorConstants;
+import org.apache.axiom.soap.SOAPFactory;
+import org.apache.axiom.soap.SOAPEnvelope;
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.util.AXIOMUtil;
+import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.AxisFault;
 
+import javax.xml.stream.XMLStreamException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.Map;
 import java.util.HashMap;
+import java.util.Properties;
 
 public class SourceTextRetrieverTest extends TestCase {
 
@@ -82,4 +91,47 @@ public class SourceTextRetrieverTest ext
         assertNull(txtRtvr.getSourceText(context));
     }
 
+    public void testPropertyTextRetriver() throws EvaluatorException {
+        Properties props = new Properties();
+        props.setProperty("key1", "value1");
+        props.setProperty("key2", "value2");
+        EvaluatorContext context = new EvaluatorContext(null, null);
+        context.setProperties(props);
+
+        PropertyTextRetriever txtRtvr = new PropertyTextRetriever("key1");
+        assertEquals(props.getProperty("key1"), 
txtRtvr.getSourceText(context));
+
+        txtRtvr = new PropertyTextRetriever("key2");
+        assertEquals(props.getProperty("key2"), 
txtRtvr.getSourceText(context));
+
+        txtRtvr = new PropertyTextRetriever("bogusKey");
+        assertNull(txtRtvr.getSourceText(context));
+    }
+
+    public void testSOAPEnvelopeTextRetriever() throws Exception {
+        SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
+        SOAPEnvelope env = fac.getDefaultEnvelope();
+
+        OMElement payload = AXIOMUtil.stringToOM(fac,
+                "<getQuote 
id=\"attr\"><symbol>TEST</symbol><property>1</property>" +
+                        "<property>2</property></getQuote>");
+        env.getBody().addChild(payload);
+        MessageContext msgCtx = new MessageContext();
+        msgCtx.setEnvelope(env);
+        EvaluatorContext context = new EvaluatorContext(null, null);
+        context.setMessageContext(msgCtx);
+
+        SOAPEnvelopeTextRetriever txtRtvr = new 
SOAPEnvelopeTextRetriever("//symbol");
+        assertEquals("TEST", txtRtvr.getSourceText(context));
+
+        txtRtvr = new SOAPEnvelopeTextRetriever("//getQuote/@id");
+        assertEquals("attr", txtRtvr.getSourceText(context));
+
+        txtRtvr = new SOAPEnvelopeTextRetriever("//property[1]");
+        assertEquals("1", txtRtvr.getSourceText(context));
+
+        txtRtvr = new SOAPEnvelopeTextRetriever("//property[2]");
+        assertEquals("2", txtRtvr.getSourceText(context));
+    }
+
 }


Reply via email to