Author: sebb
Date: Sun Apr 15 15:14:27 2007
New Revision: 529081

URL: http://svn.apache.org/viewvc?view=rev&rev=529081
Log:
Bug 42088 - Add XPath Assertion for booleans

Added:
    jakarta/jmeter/branches/rel-2-2/bin/testfiles/XPathAssertionTest.xml   
(with props)
    
jakarta/jmeter/branches/rel-2-2/test/src/org/apache/jmeter/assertions/XPathAssertionTest.java
   (with props)
Modified:
    
jakarta/jmeter/branches/rel-2-2/src/components/org/apache/jmeter/assertions/XPathAssertion.java
    jakarta/jmeter/branches/rel-2-2/xdocs/changes.xml
    jakarta/jmeter/branches/rel-2-2/xdocs/usermanual/component_reference.xml

Added: jakarta/jmeter/branches/rel-2-2/bin/testfiles/XPathAssertionTest.xml
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/branches/rel-2-2/bin/testfiles/XPathAssertionTest.xml?view=auto&rev=529081
==============================================================================
--- jakarta/jmeter/branches/rel-2-2/bin/testfiles/XPathAssertionTest.xml (added)
+++ jakarta/jmeter/branches/rel-2-2/bin/testfiles/XPathAssertionTest.xml Sun 
Apr 15 15:14:27 2007
@@ -0,0 +1,5 @@
+<response>
+ <code>1</code>
+ <error>Print Error</error>
+ <error>Network Error</error>
+</response>

Propchange: jakarta/jmeter/branches/rel-2-2/bin/testfiles/XPathAssertionTest.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/jmeter/branches/rel-2-2/bin/testfiles/XPathAssertionTest.xml
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: 
jakarta/jmeter/branches/rel-2-2/src/components/org/apache/jmeter/assertions/XPathAssertion.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/branches/rel-2-2/src/components/org/apache/jmeter/assertions/XPathAssertion.java?view=diff&rev=529081&r1=529080&r2=529081
==============================================================================
--- 
jakarta/jmeter/branches/rel-2-2/src/components/org/apache/jmeter/assertions/XPathAssertion.java
 (original)
+++ 
jakarta/jmeter/branches/rel-2-2/src/components/org/apache/jmeter/assertions/XPathAssertion.java
 Sun Apr 15 15:14:27 2007
@@ -33,6 +33,7 @@
 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.NodeList;
 import org.xml.sax.SAXException;
@@ -114,18 +115,39 @@
 
                NodeList nodeList = null;
 
+               final String pathString = getXPathString();
                try {
-                       nodeList = XPathAPI.selectNodeList(doc, 
getXPathString());
+                       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 
StringBuffer("TransformerException: ").append(e.getMessage()).toString());
+                       result.setFailureMessage(
+                                       new StringBuffer("TransformerException: 
")
+                                       .append(e.getMessage())
+                                       .append(" for:")
+                                       .append(pathString)
+                                       .toString());
                        return result;
                }
 
                if (nodeList == null || nodeList.getLength() == 0) {
-                       log.debug(new StringBuffer("nodeList null no match  
").append(getXPathString()).toString());
+                       log.debug(new StringBuffer("nodeList null no match  
").append(pathString).toString());
                        result.setFailure(!isNegated());
-                       result.setFailureMessage("No Nodes Matched " + 
getXPathString());
+                       result.setFailureMessage("No Nodes Matched " + 
pathString);
                        return result;
                }
                log.debug("nodeList length " + nodeList.getLength());

Added: 
jakarta/jmeter/branches/rel-2-2/test/src/org/apache/jmeter/assertions/XPathAssertionTest.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/branches/rel-2-2/test/src/org/apache/jmeter/assertions/XPathAssertionTest.java?view=auto&rev=529081
==============================================================================
--- 
jakarta/jmeter/branches/rel-2-2/test/src/org/apache/jmeter/assertions/XPathAssertionTest.java
 (added)
+++ 
jakarta/jmeter/branches/rel-2-2/test/src/org/apache/jmeter/assertions/XPathAssertionTest.java
 Sun Apr 15 15:14:27 2007
@@ -0,0 +1,188 @@
+/*
+ * 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.jmeter.assertions;
+
+import java.io.BufferedInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.FileInputStream;
+import java.io.IOException;
+
+import org.apache.jmeter.junit.JMeterTestCase;
+import org.apache.jmeter.samplers.SampleResult;
+import org.apache.jmeter.threads.JMeterContext;
+import org.apache.jmeter.threads.JMeterContextService;
+import org.apache.jmeter.threads.JMeterVariables;
+
+public class XPathAssertionTest extends JMeterTestCase {
+
+       private XPathAssertion assertion;
+
+       private SampleResult result;
+
+       private JMeterVariables vars;
+
+       private JMeterContext jmctx;
+
+       public XPathAssertionTest(String arg0) {
+               super(arg0);
+       }
+
+       protected void setUp() throws Exception {
+               super.setUp();
+               jmctx = JMeterContextService.getContext();
+               assertion = new XPathAssertion();
+               assertion.setThreadContext(jmctx);// This would be done by the 
run
+                                                                               
        // command
+               result = new SampleResult();
+               
result.setResponseData(readFile("testfiles/XPathAssertionTest.xml"));
+               vars = new JMeterVariables();
+               jmctx.setVariables(vars);
+               //jmctx.setPreviousResult(result);
+               //testLog.setPriority(org.apache.log.Priority.DEBUG);
+       }
+
+       private ByteArrayOutputStream readBA(String name) throws IOException {
+               BufferedInputStream bis = new BufferedInputStream(new 
FileInputStream(findTestFile(name)));
+               ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
+               int len = 0;
+               byte[] data = new byte[512];
+               while ((len = bis.read(data)) >= 0) {
+                       baos.write(data, 0, len);
+               }
+               bis.close();
+               return baos;
+       }
+
+       private byte[] readFile(String name) throws IOException {
+               return readBA(name).toByteArray();
+       }
+
+       public void testAssertionOK() throws Exception {
+               assertion.setXPathString("/");
+               AssertionResult res = assertion.getResult(result);
+               testLog.debug("isError() " + res.isError() + " isFailure() " + 
res.isFailure());
+               testLog.debug("failure message: " + res.getFailureMessage());
+               assertFalse("Should not be an error", res.isError());
+               assertFalse("Should not be a failure", res.isFailure());
+       }
+
+       public void testAssertionFail() throws Exception {
+               assertion.setXPathString("//x");
+               AssertionResult res = assertion.getResult(result);
+               testLog.debug("isError() " + res.isError() + " isFailure() " + 
res.isFailure());
+               testLog.debug("failure message: " + res.getFailureMessage());
+               assertFalse("Should not be an error", res.isError());
+               assertTrue("Should be a failure",res.isFailure());
+       }
+
+       public void testAssertionPath1() throws Exception {
+               assertion.setXPathString("//*[code=1]");
+               AssertionResult res = assertion.getResult(result);
+               testLog.debug("isError() " + res.isError() + " isFailure() " + 
res.isFailure());
+               testLog.debug("failure message: " + res.getFailureMessage());
+               assertFalse("Should not be an error", res.isError());
+               assertFalse("Should not be a failure",res.isFailure());
+       }
+
+       public void testAssertionPath2() throws Exception {
+               assertion.setXPathString("//*[code=2]"); // Not present
+               AssertionResult res = assertion.getResult(result);
+               testLog.debug("isError() " + res.isError() + " isFailure() " + 
res.isFailure());
+               testLog.debug("failure message: " + res.getFailureMessage());
+               assertFalse("Should not be an error", res.isError());
+               assertTrue("Should be a failure",res.isFailure());
+       }
+
+       public void testAssertionBool1() throws Exception {
+               assertion.setXPathString("count(//error)=2");
+               AssertionResult res = assertion.getResult(result);
+               testLog.debug("isError() " + res.isError() + " isFailure() " + 
res.isFailure());
+               testLog.debug("failure message: " + res.getFailureMessage());
+               assertFalse("Should not be an error", res.isError());
+               assertFalse("Should not be a failure",res.isFailure());
+       }
+
+       public void testAssertionBool2() throws Exception {
+               assertion.setXPathString("count(//*[code=1])=1");
+               AssertionResult res = assertion.getResult(result);
+               testLog.debug("isError() " + res.isError() + " isFailure() " + 
res.isFailure());
+               testLog.debug("failure message: " + res.getFailureMessage());
+               assertFalse("Should not be an error", res.isError());
+               assertFalse("Should not be a failure",res.isFailure());
+       }
+
+       public void testAssertionBool3() throws Exception {
+               assertion.setXPathString("count(//error)=1"); // wrong
+               AssertionResult res = assertion.getResult(result);
+               testLog.debug("isError() " + res.isError() + " isFailure() " + 
res.isFailure());
+               testLog.debug("failure message: " + res.getFailureMessage());
+               assertFalse("Should not be an error", res.isError());
+               assertTrue("Should be a failure",res.isFailure());
+       }
+
+       public void testAssertionBool4() throws Exception {
+               assertion.setXPathString("count(//*[code=2])=1"); //Wrong
+               AssertionResult res = assertion.getResult(result);
+               testLog.debug("isError() " + res.isError() + " isFailure() " + 
res.isFailure());
+               testLog.debug("failure message: " + res.getFailureMessage());
+               assertFalse("Should not be an error", res.isError());
+               assertTrue("Should be a failure",res.isFailure());
+       }
+
+       public void testAssertionNumber() throws Exception {
+               assertion.setXPathString("count(//error)");// not yet handled
+               AssertionResult res = assertion.getResult(result);
+               testLog.debug("isError() " + res.isError() + " isFailure() " + 
res.isFailure());
+               testLog.debug("failure message: " + res.getFailureMessage());
+               assertFalse("Should not be an error", res.isError());
+               assertTrue("Should be a failure",res.isFailure());
+       }
+
+       public void testAssertionNoResult() throws Exception {
+               // result.setResponseData - not set
+               result = new SampleResult();
+               AssertionResult res = assertion.getResult(result);
+               testLog.debug("isError() " + res.isError() + " isFailure() " + 
res.isFailure());
+               testLog.debug("failure message: " + res.getFailureMessage());
+               assertEquals(AssertionResult.RESPONSE_WAS_NULL, 
res.getFailureMessage());
+               assertFalse("Should not be an error", res.isError());
+               assertTrue("Should be a failure",res.isFailure());
+       }
+
+       public void testAssertionEmptyResult() throws Exception {
+               result.setResponseData("".getBytes());
+               AssertionResult res = assertion.getResult(result);
+               testLog.debug("isError() " + res.isError() + " isFailure() " + 
res.isFailure());
+               testLog.debug("failure message: " + res.getFailureMessage());
+               assertEquals(AssertionResult.RESPONSE_WAS_NULL, 
res.getFailureMessage());
+               assertFalse("Should not be an error", res.isError());
+               assertTrue("Should be a failure",res.isFailure());
+       }
+
+       public void testAssertionBlankResult() throws Exception {
+               result.setResponseData(" ".getBytes());
+               AssertionResult res = assertion.getResult(result);
+               testLog.debug("isError() " + res.isError() + " isFailure() " + 
res.isFailure());
+               testLog.debug("failure message: " + res.getFailureMessage());
+               assertTrue(res.getFailureMessage().indexOf("Premature end of 
file") > 0);
+               assertTrue("Should be an error",res.isError());
+               assertFalse("Should not be a failure", res.isFailure());
+       }
+
+}

Propchange: 
jakarta/jmeter/branches/rel-2-2/test/src/org/apache/jmeter/assertions/XPathAssertionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jakarta/jmeter/branches/rel-2-2/test/src/org/apache/jmeter/assertions/XPathAssertionTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: jakarta/jmeter/branches/rel-2-2/xdocs/changes.xml
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/branches/rel-2-2/xdocs/changes.xml?view=diff&rev=529081&r1=529080&r2=529081
==============================================================================
--- jakarta/jmeter/branches/rel-2-2/xdocs/changes.xml (original)
+++ jakarta/jmeter/branches/rel-2-2/xdocs/changes.xml Sun Apr 15 15:14:27 2007
@@ -131,6 +131,7 @@
 <li>Bug 40825 - Add JDBC prepared statement support</li>
 <li>Bug 27780 - (patch 19936) create multipart/form-data HTTP request without 
uploading file</li>
 <li>Bug 42098 - Use specified encoding for parameter values in HTTP GET</li>
+<li>Bug 42088 - Add XPath Assertion for booleans</li>
 </ul>
 
 <h4>Non-functional improvements:</h4>

Modified: 
jakarta/jmeter/branches/rel-2-2/xdocs/usermanual/component_reference.xml
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/branches/rel-2-2/xdocs/usermanual/component_reference.xml?view=diff&rev=529081&r1=529080&r2=529081
==============================================================================
--- jakarta/jmeter/branches/rel-2-2/xdocs/usermanual/component_reference.xml 
(original)
+++ jakarta/jmeter/branches/rel-2-2/xdocs/usermanual/component_reference.xml 
Sun Apr 15 15:14:27 2007
@@ -2530,7 +2530,8 @@
 <description><p>The XPath Assertion tests a document for well formedness, has 
the option
 of validating against a DTD, or putting the document through JTidy and testing 
for an
 XPath.  If that XPath exists, the Assertion is true.  Using "/" will match any 
well-formed
-document, and is the default XPath Expression.
+document, and is the default XPath Expression. 
+The assertion also supports boolean expressions, such as "count(//*error)=2".
 See <a href="http://www.w3.org/TR/xpath";>http://www.w3.org/TR/xpath</a> for 
more information
 on XPath.
 </p></description>



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to