jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pmoua...@apache.org
Subject svn commit: r1204254 - in /jmeter/trunk/src: components/org/apache/jmeter/assertions/ components/org/apache/jmeter/assertions/gui/ components/org/apache/jmeter/extractor/ core/org/apache/jmeter/util/ functions/org/apache/jmeter/functions/
Date Sun, 20 Nov 2011 20:55:09 GMT
Author: pmouawad
Date: Sun Nov 20 20:55:08 2011
New Revision: 1204254

URL: http://svn.apache.org/viewvc?rev=1204254&view=rev
Log:
Bug 43294 - XPath Extractor namespace problems
First part: Put in XPathUtil everything related to XPath computations

Modified:
    jmeter/trunk/src/components/org/apache/jmeter/assertions/XPathAssertion.java
    jmeter/trunk/src/components/org/apache/jmeter/assertions/gui/XPathPanel.java
    jmeter/trunk/src/components/org/apache/jmeter/extractor/XPathExtractor.java
    jmeter/trunk/src/core/org/apache/jmeter/util/XPathUtil.java
    jmeter/trunk/src/functions/org/apache/jmeter/functions/XPathFileContainer.java

Modified: jmeter/trunk/src/components/org/apache/jmeter/assertions/XPathAssertion.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/components/org/apache/jmeter/assertions/XPathAssertion.java?rev=1204254&r1=1204253&r2=1204254&view=diff
==============================================================================
--- jmeter/trunk/src/components/org/apache/jmeter/assertions/XPathAssertion.java (original)
+++ jmeter/trunk/src/components/org/apache/jmeter/assertions/XPathAssertion.java Sun Nov 20
20:55:08 2011
@@ -23,7 +23,6 @@ import java.io.IOException;
 import java.io.Serializable;
 
 import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.transform.TransformerException;
 
 import org.apache.jmeter.samplers.SampleResult;
 import org.apache.jmeter.testelement.AbstractTestElement;
@@ -34,10 +33,7 @@ import org.apache.jmeter.util.XPathUtil;
 import org.apache.jorphan.logging.LoggingManager;
 import org.apache.jorphan.util.JOrphanUtils;
 import org.apache.log.Logger;
-import org.apache.xpath.XPathAPI;
-import org.apache.xpath.objects.XObject;
 import org.w3c.dom.Document;
-import org.w3c.dom.NodeList;
 import org.xml.sax.SAXException;
 
 /**
@@ -50,8 +46,6 @@ public class XPathAssertion extends Abst
 
     private static final long serialVersionUID = 240L;
 
-    // private static XPathAPI xpath = null;
-
     //+ JMX file attributes
     private static final String XPATH_KEY         = "XPath.xpath"; // $NON-NLS-1$
     private static final String WHITESPACE_KEY    = "XPath.whitespace"; // $NON-NLS-1$
@@ -123,58 +117,7 @@ public class XPathAssertion extends Abst
             result.setFailureMessage("Document is null, probably not parsable");
             return result;
         }
-
-        NodeList nodeList = null;
-
-        final String pathString = getXPathString();
-        try {
-            XObject xObject = XPathAPI.eval(doc, pathString);
-            switch (xObject.getType()) {
-                case XObject.CLASS_NODESET:
-                    nodeList = xObject.nodelist();
-                    break;
-                case XObject.CLASS_BOOLEAN:
-                    if (!xObject.bool()){
-                        result.setFailure(!isNegated());
-                        result.setFailureMessage("No Nodes Matched " + pathString);
-                    }
-                    return result;
-                default:
-                    result.setFailure(true);
-                    result.setFailureMessage("Cannot understand: " + pathString);
-                    return result;
-            }
-        } catch (TransformerException e) {
-            result.setError(true);
-            result.setFailureMessage(
-                    new StringBuilder("TransformerException: ")
-                    .append(e.getMessage())
-                    .append(" for:")
-                    .append(pathString)
-                    .toString());
-            return result;
-        }
-
-        if (nodeList == null || nodeList.getLength() == 0) {
-            if (log.isDebugEnabled()) {
-                log.debug(new StringBuilder("nodeList null no match  ").append(pathString).toString());
-            }
-            result.setFailure(!isNegated());
-            result.setFailureMessage("No Nodes Matched " + pathString);
-            return result;
-        }
-        if (log.isDebugEnabled()) {
-            log.debug("nodeList length " + nodeList.getLength());
-            if (!isNegated()) {
-                for (int i = 0; i < nodeList.getLength(); i++){
-                    log.debug(new StringBuilder("nodeList[").append(i).append("] ").append(nodeList.item(i)).toString());
-                }
-            }
-        }
-        result.setFailure(isNegated());
-        if (isNegated()) {
-            result.setFailureMessage("Specified XPath was found... Turn off negate if this
is not desired");
-        }
+        XPathUtil.computeAssertionResult(result, doc, getXPathString(), isNegated());
         return result;
     }
 

Modified: jmeter/trunk/src/components/org/apache/jmeter/assertions/gui/XPathPanel.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/components/org/apache/jmeter/assertions/gui/XPathPanel.java?rev=1204254&r1=1204253&r2=1204254&view=diff
==============================================================================
--- jmeter/trunk/src/components/org/apache/jmeter/assertions/gui/XPathPanel.java (original)
+++ jmeter/trunk/src/components/org/apache/jmeter/assertions/gui/XPathPanel.java Sun Nov 20
20:55:08 2011
@@ -34,7 +34,6 @@ import org.apache.jmeter.util.JMeterUtil
 import org.apache.jmeter.util.XPathUtil;
 import org.apache.jorphan.logging.LoggingManager;
 import org.apache.log.Logger;
-import org.apache.xpath.XPathAPI;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
@@ -190,16 +189,11 @@ public class XPathPanel extends JPanel {
                 testDoc.appendChild(el);
 
             }
-            if (XPathAPI.eval(testDoc, xpathString) == null) {
-                // We really should never get here
-                // because eval will throw an exception
-                // if xpath is invalid, but whatever, better
-                // safe
-                ret = "xpath eval was null";
-                log.warn(ret+" "+xpathString);
-                success = false;
-            }
-
+            XPathUtil.validateXPath(testDoc, xpathString);
+        } catch (IllegalArgumentException e) {
+        	log.warn(e.getLocalizedMessage());
+            success = false;
+            ret = e.getLocalizedMessage();
         } catch (ParserConfigurationException e) {
             success = false;
             ret = e.getLocalizedMessage();

Modified: jmeter/trunk/src/components/org/apache/jmeter/extractor/XPathExtractor.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/components/org/apache/jmeter/extractor/XPathExtractor.java?rev=1204254&r1=1204253&r2=1204254&view=diff
==============================================================================
--- jmeter/trunk/src/components/org/apache/jmeter/extractor/XPathExtractor.java (original)
+++ jmeter/trunk/src/components/org/apache/jmeter/extractor/XPathExtractor.java Sun Nov 20
20:55:08 2011
@@ -20,18 +20,12 @@ package org.apache.jmeter.extractor;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.Serializable;
-import java.io.StringWriter;
 import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.List;
 
 import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.transform.OutputKeys;
-import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.stream.StreamResult;
 
 import org.apache.jmeter.assertions.AssertionResult;
 import org.apache.jmeter.processor.PostProcessor;
@@ -46,12 +40,7 @@ import org.apache.jorphan.logging.Loggin
 import org.apache.jorphan.util.JMeterError;
 import org.apache.jorphan.util.JOrphanUtils;
 import org.apache.log.Logger;
-import org.apache.xpath.XPathAPI;
-import org.apache.xpath.objects.XObject;
 import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
 import org.xml.sax.SAXException;
 
 //@see org.apache.jmeter.extractor.TestXPathExtractor for unit tests
@@ -317,39 +306,7 @@ public class XPathExtractor extends Abst
      */
     private void getValuesForXPath(Document d,String query, List<String> matchStrings)
         throws TransformerException {
-        String val = null;
-        XObject xObject = XPathAPI.eval(d, query);
-        final int objectType = xObject.getType();
-        if (objectType == XObject.CLASS_NODESET) {
-            NodeList matches = xObject.nodelist();
-            int length = matches.getLength();
-            for (int i = 0 ; i < length; i++) {
-                Node match = matches.item(i);
-                if ( match instanceof Element){
-                    if (getFragment()){
-                        val = getValueForNode(match);
-                    } else {
-                        // elements have empty nodeValue, but we are usually interested in
their content
-                        final Node firstChild = match.getFirstChild();
-                        if (firstChild != null) {
-                            val = firstChild.getNodeValue();
-                        } else {
-                            val = match.getNodeValue(); // TODO is this correct?
-                        }
-                    }
-                } else {
-                   val = match.getNodeValue();
-                }
-                matchStrings.add(val);
-            }
-        } else if (objectType == XObject.CLASS_NULL
-                || objectType == XObject.CLASS_UNKNOWN
-                || objectType == XObject.CLASS_UNRESOLVEDVARIABLE) {
-            log.warn("Unexpected object type: "+xObject.getTypeString()+" returned for: "+getXPathQuery());
-        } else {
-            val = xObject.toString();
-            matchStrings.add(val);
-      }
+    	XPathUtil.putValuesForXPathInList(d, query, matchStrings, getFragment());
     }
 
     public void setWhitespace(boolean selected) {
@@ -375,18 +332,6 @@ public class XPathExtractor extends Abst
     public boolean isDownloadDTDs() {
         return getPropertyAsBoolean(DOWNLOAD_DTDS, false);
     }
-
-    private String getValueForNode(Node node) {
-        StringWriter sw = new StringWriter();
-        try {
-            Transformer t = TransformerFactory.newInstance().newTransformer();
-            t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
-            t.transform(new DOMSource(node), new StreamResult(sw));
-        } catch (TransformerException e) {
-            sw.write(e.getMessageAndLocation());
-        }
-        return sw.toString();
-    }
     
     /** 
      * {@inheritDoc}}

Modified: jmeter/trunk/src/core/org/apache/jmeter/util/XPathUtil.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/core/org/apache/jmeter/util/XPathUtil.java?rev=1204254&r1=1204253&r2=1204254&view=diff
==============================================================================
--- jmeter/trunk/src/core/org/apache/jmeter/util/XPathUtil.java (original)
+++ jmeter/trunk/src/core/org/apache/jmeter/util/XPathUtil.java Sun Nov 20 20:55:08 2011
@@ -24,14 +24,27 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.PrintWriter;
 import java.io.StringWriter;
+import java.util.List;
 
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
 
+import org.apache.jmeter.assertions.AssertionResult;
 import org.apache.jorphan.logging.LoggingManager;
 import org.apache.log.Logger;
+import org.apache.xpath.XPathAPI;
+import org.apache.xpath.objects.XObject;
 import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 import org.w3c.tidy.Tidy;
 import org.xml.sax.EntityResolver;
 import org.xml.sax.ErrorHandler;
@@ -245,4 +258,153 @@ public class XPathUtil {
             }
         }
     }
+
+    /**
+     * Extract NodeList using expression
+     * @param document {@link Document}
+     * @param xPathExpression XPath expression
+     * @return {@link NodeList}
+     * @throws TransformerException 
+     */
+	public static NodeList selectNodeList(Document document, String xPathExpression) throws
TransformerException {
+		return XPathAPI.selectNodeList(document, xPathExpression);
+	}
+
+	/**
+	 * Put in matchStrings results of evaluation
+	 * @param document XML document
+	 * @param xPathQuery XPath Query
+	 * @param matchStrings List<String> that will be filled
+	 * @param fragment return fragment
+	 * @throws TransformerException
+	 */
+	public static void putValuesForXPathInList(Document document, 
+			String xPathQuery,
+			List<String> matchStrings, boolean fragment) throws TransformerException {
+		String val = null;
+        XObject xObject = XPathAPI.eval(document, xPathQuery);
+        final int objectType = xObject.getType();
+        if (objectType == XObject.CLASS_NODESET) {
+            NodeList matches = xObject.nodelist();
+            int length = matches.getLength();
+            for (int i = 0 ; i < length; i++) {
+                Node match = matches.item(i);
+                if ( match instanceof Element){
+                    if (fragment){
+                        val = getValueForNode(match);
+                    } else {
+                        // elements have empty nodeValue, but we are usually interested in
their content
+                        final Node firstChild = match.getFirstChild();
+                        if (firstChild != null) {
+                            val = firstChild.getNodeValue();
+                        } else {
+                            val = match.getNodeValue(); // TODO is this correct?
+                        }
+                    }
+                } else {
+                   val = match.getNodeValue();
+                }
+                matchStrings.add(val);
+            }
+        } else if (objectType == XObject.CLASS_NULL
+                || objectType == XObject.CLASS_UNKNOWN
+                || objectType == XObject.CLASS_UNRESOLVEDVARIABLE) {
+            log.warn("Unexpected object type: "+xObject.getTypeString()+" returned for: "+xPathQuery);
+        } else {
+            val = xObject.toString();
+            matchStrings.add(val);
+      }
+	}
+	
+	/**
+	 * Return value for node
+	 * @param node Node
+	 * @return String
+	 */
+	private static String getValueForNode(Node node) {
+        StringWriter sw = new StringWriter();
+        try {
+            Transformer t = TransformerFactory.newInstance().newTransformer();
+            t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+            t.transform(new DOMSource(node), new StreamResult(sw));
+        } catch (TransformerException e) {
+            sw.write(e.getMessageAndLocation());
+        }
+        return sw.toString();
+    }
+
+	/**
+	 * Validate xpathString is a valid XPath expression
+	 * @param document XML Document
+	 * @param xpathString XPATH String
+	 * @throws TransformerException if expression fails to evaluate
+	 */
+	public static void validateXPath(Document document, String xpathString) throws TransformerException
{
+		if (XPathAPI.eval(document, xpathString) == null) {
+            // We really should never get here
+            // because eval will throw an exception
+            // if xpath is invalid, but whatever, better
+            // safe
+			throw new IllegalArgumentException("xpath eval of '" + xpathString + "' was null");
+        }
+	}
+	
+	/**
+	 * Fills result
+	 * @param result {@link AssertionResult}
+	 * @param doc XML Document
+	 * @param xPathExpression XPath expression
+	 * @param isNegated
+	 */
+	public static void computeAssertionResult(AssertionResult result,
+			Document doc, 
+			String xPathExpression,
+			boolean isNegated) {
+        try {
+            XObject xObject = XPathAPI.eval(doc, xPathExpression);
+            switch (xObject.getType()) {
+                case XObject.CLASS_NODESET:
+                	NodeList nodeList = xObject.nodelist();
+                    if (nodeList == null || nodeList.getLength() == 0) {
+                        if (log.isDebugEnabled()) {
+                            log.debug(new StringBuilder("nodeList null no match  ").append(xPathExpression).toString());
+                        }
+                        result.setFailure(!isNegated);
+                        result.setFailureMessage("No Nodes Matched " + xPathExpression);
+                        return;
+                    }
+                    if (log.isDebugEnabled()) {
+                        log.debug("nodeList length " + nodeList.getLength());
+                        if (!isNegated) {
+                            for (int i = 0; i < nodeList.getLength(); i++){
+                                log.debug(new StringBuilder("nodeList[").append(i).append("]
").append(nodeList.item(i)).toString());
+                            }
+                        }
+                    }
+                    result.setFailure(isNegated);
+                    if (isNegated) {
+                        result.setFailureMessage("Specified XPath was found... Turn off negate
if this is not desired");
+                    }
+                    return;
+                case XObject.CLASS_BOOLEAN:
+                    if (!xObject.bool()){
+                        result.setFailure(!isNegated);
+                        result.setFailureMessage("No Nodes Matched " + xPathExpression);
+                    }
+                    return;
+                default:
+                    result.setFailure(true);
+                    result.setFailureMessage("Cannot understand: " + xPathExpression);
+                    return;
+            }
+        } catch (TransformerException e) {
+            result.setError(true);
+            result.setFailureMessage(
+                    new StringBuilder("TransformerException: ")
+                    .append(e.getMessage())
+                    .append(" for:")
+                    .append(xPathExpression)
+                    .toString());
+        }
+	}
 }
\ No newline at end of file

Modified: jmeter/trunk/src/functions/org/apache/jmeter/functions/XPathFileContainer.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/functions/org/apache/jmeter/functions/XPathFileContainer.java?rev=1204254&r1=1204253&r2=1204254&view=diff
==============================================================================
--- jmeter/trunk/src/functions/org/apache/jmeter/functions/XPathFileContainer.java (original)
+++ jmeter/trunk/src/functions/org/apache/jmeter/functions/XPathFileContainer.java Sun Nov
20 20:55:08 2011
@@ -28,10 +28,10 @@ import javax.xml.parsers.DocumentBuilder
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.TransformerException;
 
+import org.apache.jmeter.util.XPathUtil;
 import org.apache.jorphan.logging.LoggingManager;
 import org.apache.jorphan.util.JOrphanUtils;
 import org.apache.log.Logger;
-import org.apache.xpath.XPathAPI;
 import org.w3c.dom.NodeList;
 import org.xml.sax.SAXException;
 
@@ -74,7 +74,7 @@ public class XPathFileContainer {
             DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
 
             fis = new FileInputStream(fileName);
-            nl = XPathAPI.selectNodeList(builder.parse(fis), xpath);
+            nl = XPathUtil.selectNodeList(builder.parse(fis), xpath);
             log.debug("found " + nl.getLength());
 
         } catch (FileNotFoundException e) {



Mime
View raw message