jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pmoua...@apache.org
Subject svn commit: r1860789 - in /jmeter/trunk: src/components/org/apache/jmeter/assertions/ test/src/org/apache/jmeter/assertions/ test/src/org/apache/jmeter/extractor/ test/src/org/apache/jmeter/util/
Date Fri, 07 Jun 2019 20:29:00 GMT
Author: pmouawad
Date: Fri Jun  7 20:29:00 2019
New Revision: 1860789

URL: http://svn.apache.org/viewvc?rev=1860789&view=rev
Log:
Bug 63480 - XPathAssertion and XPathAssertion2: Cover input coming from variable

Contributed by UbikLoadPack Team
Bugzilla Id: 63480

Added:
    jmeter/trunk/test/src/org/apache/jmeter/extractor/TestXPath2Extractor.java   (with props)
Modified:
    jmeter/trunk/src/components/org/apache/jmeter/assertions/XPath2Assertion.java
    jmeter/trunk/test/src/org/apache/jmeter/assertions/XPath2AssertionTest.java
    jmeter/trunk/test/src/org/apache/jmeter/assertions/XPathAssertionTest.java
    jmeter/trunk/test/src/org/apache/jmeter/util/XPathUtilTest.java

Modified: jmeter/trunk/src/components/org/apache/jmeter/assertions/XPath2Assertion.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/components/org/apache/jmeter/assertions/XPath2Assertion.java?rev=1860789&r1=1860788&r2=1860789&view=diff
==============================================================================
--- jmeter/trunk/src/components/org/apache/jmeter/assertions/XPath2Assertion.java (original)
+++ jmeter/trunk/src/components/org/apache/jmeter/assertions/XPath2Assertion.java Fri Jun
 7 20:29:00 2019
@@ -18,6 +18,7 @@
 package org.apache.jmeter.assertions;
 
 import java.io.Serializable;
+import java.util.concurrent.CompletionException;
 
 import org.apache.commons.lang3.StringUtils;
 import org.apache.jmeter.samplers.SampleResult;
@@ -67,9 +68,10 @@ public class XPath2Assertion extends Abs
         try {
             XPathUtil.computeAssertionResultUsingSaxon(result, responseData, getXPathString(),
                     getNamespaces(),isNegated());
-        } catch (SaxonApiException e) { // NOSONAR We handle exception within result failur
message
+        } catch (CompletionException|SaxonApiException e) { // NOSONAR We handle exception
within result failure message
             result.setError(true);
-            result.setFailureMessage("SaxonApiException occured computing assertion with
XPath:" + getXPathString() + ", error:" + e.getMessage());
+            // CompletionException happens if caching fails
+            result.setFailureMessage("Exception occured computing assertion with XPath:"
+ getXPathString() + ", error:" + e.getMessage());
             return result;
         }
         return result;

Modified: jmeter/trunk/test/src/org/apache/jmeter/assertions/XPath2AssertionTest.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/assertions/XPath2AssertionTest.java?rev=1860789&r1=1860788&r2=1860789&view=diff
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/assertions/XPath2AssertionTest.java (original)
+++ jmeter/trunk/test/src/org/apache/jmeter/assertions/XPath2AssertionTest.java Fri Jun  7
20:29:00 2019
@@ -18,22 +18,42 @@
 
 package org.apache.jmeter.assertions;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
 import javax.xml.parsers.FactoryConfigurationError;
 
 import org.apache.jmeter.samplers.SampleResult;
+import org.apache.jmeter.threads.JMeterContext;
+import org.apache.jmeter.threads.JMeterContextService;
+import org.apache.jmeter.threads.JMeterVariables;
 import org.apache.jmeter.util.JMeterUtils;
+import org.junit.Before;
 import org.junit.Test;
 
 public class XPath2AssertionTest {
 
-    final String xmlDoc = JMeterUtils.getResourceFileAsText("XPathUtilTestXml.xml");
+    private final String xmlDoc = JMeterUtils.getResourceFileAsText("XPathUtilTestXml.xml");
+    private XPath2Assertion assertion;
+    private SampleResult response;
+    private JMeterVariables vars;
+    private JMeterContext jmctx;
+
+    @Before
+    public void setUp() throws Exception {
+        jmctx = JMeterContextService.getContext();
+        assertion = new  XPath2Assertion();
+        assertion.setThreadContext(jmctx);// This would be done by the run command
+        response = new SampleResult();
+        response.setResponseData(xmlDoc, "UTF-8");
+        vars = new JMeterVariables();
+        jmctx.setVariables(vars);
+        jmctx.setPreviousResult(response);
+    }
+
     @Test
     public void testXPath2AssertionPath1() throws FactoryConfigurationError {
-        XPath2Assertion assertion = new XPath2Assertion();
-        SampleResult response = new SampleResult();
         String namespaces = "age=http://www.w3.org/2003/01/geo/wgs84_pos#";
         String xPathQuery = "//Employees/Employee[1]/age:ag";
         assertion.setNamespaces(namespaces);
@@ -45,8 +65,6 @@ public class XPath2AssertionTest {
     }
     @Test
     public void testXPath2AssertionPath1Negated() throws FactoryConfigurationError {
-        XPath2Assertion assertion = new XPath2Assertion();
-        SampleResult response = new SampleResult();
         String namespaces = "age=http://www.w3.org/2003/01/geo/wgs84_pos#";
         String xPathQuery = "//Employees/Employee[1]/age:ag";
         assertion.setNamespaces(namespaces);
@@ -60,8 +78,6 @@ public class XPath2AssertionTest {
     }
     @Test
     public void testXPath2AssertionPath2() throws FactoryConfigurationError {
-       XPath2Assertion assertion = new XPath2Assertion();
-        SampleResult response = new SampleResult();
         String namespaces = "age=http://www.w3.org/2003/01/geo/wgs84#";
         String xPathQuery = "//Employees/Employee[1]/age:ag";
         assertion.setNamespaces(namespaces);
@@ -73,8 +89,6 @@ public class XPath2AssertionTest {
     }
     @Test
     public void testXPath2AssertionPath2Negated() throws FactoryConfigurationError {
-        XPath2Assertion assertion = new XPath2Assertion();
-        SampleResult response = new SampleResult();
         String namespaces = "age=http://www.w3.org/2003/01/geo/wgs84#";
         String xPathQuery = "//Employees/Employee[1]/age:ag";
         assertion.setNamespaces(namespaces);
@@ -84,13 +98,10 @@ public class XPath2AssertionTest {
         AssertionResult res = assertion.getResult(response);
         assertFalse("When xpath2 doesn't conform to xml, the result of assertion should be
true ", res.isFailure());
         assertFalse("When the format of xpath2 is right, assertion will run correctly ",res.isError());
-
     }
     
     @Test
     public void testXPath2AssertionBool1() throws FactoryConfigurationError {
-       XPath2Assertion assertion = new XPath2Assertion();
-        SampleResult response = new SampleResult();
         String namespaces = "age=http://www.w3.org/2003/01/geo/wgs84_pos#";
         String xPathQuery = "count(//Employee)=4";
         assertion.setNamespaces(namespaces);
@@ -102,8 +113,6 @@ public class XPath2AssertionTest {
     }
     @Test
     public void testXPath2AssertionBool1Negated() throws FactoryConfigurationError {
-        XPath2Assertion assertion = new XPath2Assertion();
-        SampleResult response = new SampleResult();
         String namespaces = "age=http://www.w3.org/2003/01/geo/wgs84_pos#";
         String xPathQuery = "count(//Employee)=4";
         assertion.setNamespaces(namespaces);
@@ -116,8 +125,6 @@ public class XPath2AssertionTest {
     }
     @Test
     public void testXPath2AssertionBool2() throws FactoryConfigurationError {
-        XPath2Assertion assertion = new XPath2Assertion();
-        SampleResult response = new SampleResult();
         String namespaces = "age=http://www.w3.org/2003/01/geo/wgs84_pos#";
         String xPathQuery = "count(//Employee)=3";  //Wrong
         assertion.setNamespaces(namespaces);
@@ -129,8 +136,6 @@ public class XPath2AssertionTest {
     }
     @Test
     public void testXPath2AssertionBool2Negated() throws FactoryConfigurationError {
-        XPath2Assertion assertion = new XPath2Assertion();
-        SampleResult response = new SampleResult();
         String namespaces = "age=http://www.w3.org/2003/01/geo/wgs84_pos#";
         String xPathQuery = "count(//Employee)=3";  //Wrong
         assertion.setNamespaces(namespaces);
@@ -141,6 +146,76 @@ public class XPath2AssertionTest {
         assertFalse("When xpath2 doesn't conforms to xml, the result of assertion should
be true ",res.isFailure());
         assertFalse("When the format of xpath2 is right, assertion will run correctly ",res.isError());
     }
-          
-
+      
+    @Test
+    public void testScope(){
+        assertion.setThreadContext(jmctx);// This would be done by the run command
+        SampleResult result = new SampleResult();
+        String data = "<html><head><title>testtitle</title></head></html>";
+        assertion.setScopeVariable("testScope");
+        vars.put("testScope", data);
+        jmctx.setVariables(vars);
+        jmctx.setPreviousResult(result);
+        assertion.setXPathString("/html/head/title");
+        AssertionResult res = assertion.getResult(result);
+        assertFalse("When xpath conforms to xml, the result of assertion "
+                + "should be true ",res.isFailure());
+        assertFalse(res.isError());            
+    }
+    @Test
+    public void testScopeFailure(){
+        assertion.setThreadContext(jmctx);// This would be done by the run command
+        SampleResult result = new SampleResult();
+        String data = "<html><head><title>testtitle</title></head></html>";
+        assertion.setScopeVariable("testScope");
+        vars.put("testScope", data);
+        jmctx.setVariables(vars);
+        jmctx.setPreviousResult(result);
+        assertion.setXPathString("/html/head/tit");
+        AssertionResult res = assertion.getResult(result);
+        assertTrue("When xpath doesn't conforms to xml, the result "
+                + "of assertion should be false ",res.isFailure());
+        assertFalse(res.isError());
+    }
+    @Test
+    public void testResponseDataIsEmpty(){
+        assertion.setThreadContext(jmctx);// This would be done by the run command
+        SampleResult result = new SampleResult();   
+        assertion.setScopeVariable("testScope");
+        vars.put("testScope", null);
+        jmctx.setVariables(vars);
+        jmctx.setPreviousResult(result);
+        assertion.setXPathString("/html/head/tit");
+        AssertionResult res = assertion.getResult(result);
+        assertTrue("When xpath doesn't conforms to xml, the result "
+                + "of assertion should be false ",res.isFailure());
+        assertFalse(res.isError());
+        assertEquals("When the response data is empty, the result of assertion should be
false",
+                "Response was null", res.getFailureMessage());
+    }
+    @Test
+    public void testBadXpathFormat() throws FactoryConfigurationError {
+        String namespaces = "age=http://www.w3.org/2003/01/geo/wgs84_pos#";
+        String xPathQuery = "///Employees/Employee[1]/age:ag";
+        assertion.setNamespaces(namespaces);
+        assertion.setXPathString(xPathQuery);
+        response.setResponseData(xmlDoc, "UTF-8");
+        AssertionResult res = assertion.getResult(response);
+        assertTrue("When format of xpath is wrong, the test should failed",res.isError());
+        assertTrue(res.getFailureMessage().contains("Exception occured computing assertion
with XPath"));
+    }
+    
+    @Test
+    public void testXPath2AssertionPathWithoutNamespace() throws FactoryConfigurationError
{
+        String data = "<html><head><title>testtitle</title></head></html>";
+        vars = new JMeterVariables();
+        jmctx.setVariables(vars);
+        jmctx.setPreviousResult(response);
+        String xPathQuery = "/html/head";
+        assertion.setXPathString(xPathQuery);
+        response.setResponseData(data, "UTF-8");
+        AssertionResult res = assertion.getResult(response);
+        assertFalse("When xpath2 conforms to xml, the result of assertion should be true
",res.isFailure());
+        assertFalse("When the format of xpath2 is right, assertion will run correctly ",res.isError());
+    }
 }

Modified: jmeter/trunk/test/src/org/apache/jmeter/assertions/XPathAssertionTest.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/assertions/XPathAssertionTest.java?rev=1860789&r1=1860788&r2=1860789&view=diff
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/assertions/XPathAssertionTest.java (original)
+++ jmeter/trunk/test/src/org/apache/jmeter/assertions/XPathAssertionTest.java Fri Jun  7
20:29:00 2019
@@ -409,5 +409,63 @@ public class XPathAssertionTest extends
         assertFalse(res.isError());
     }
     
+    @Test
+    public void testScope(){
+        String data = "<html><head><title>testtitle</title></head><body>"
+                + "<p><i><b>invalid tag nesting</i></b><hr></body></html>";
+        assertion.setScopeVariable("testScope");
+        vars = new JMeterVariables();
+        vars.put("testScope", data);
+        jmctx.setVariables(vars);
+        jmctx.setPreviousResult(result);
+        assertion.setXPathString("/html/head/title");
+        assertion.setValidating(true);
+        assertion.setTolerant(true);
+        AssertionResult res = assertion.getResult(result);
+        log.debug("failureMessage: {}", res.getFailureMessage());
+        assertFalse("When xpath conforms to xml, the result of assertion "
+                + "should be true ",res.isFailure());
+        assertFalse(res.isError());            
+    }
+    @Test
+    public void testScopeFailure(){
+        String data = "<html><head><title>testtitle</title></head><body>"
+                + "<p><i><b>invalid tag nesting</i></b><hr></body></html>";
+        assertion.setScopeVariable("testScope");
+        vars = new JMeterVariables();
+        vars.put("testScope", data);
+        jmctx.setVariables(vars);
+        jmctx.setPreviousResult(result);
+        assertion.setXPathString("/html/head/tit");
+        assertion.setValidating(true);
+        assertion.setTolerant(true);
+        AssertionResult res = assertion.getResult(result);
+        log.debug("failureMessage: {}", res.getFailureMessage());
+        assertTrue("When xpath doesn't conforms to xml, the result "
+                + "of assertion should be false ",res.isFailure());
+        assertFalse(res.isError());
+    }
 
+    @Test
+    public void testWrongXpathMethod() {
+        assertion.setXPathString("cou(//error)=1"); // wrong
+        assertion.setNegated(true);
+        AssertionResult res = assertion.getResult(result);
+        testLog.debug("isError() {} isFailure() {}", res.isError(), res.isFailure());
+        testLog.debug("failure message: {}", res.getFailureMessage());
+        assertTrue("Should not be an error", res.isError());
+        assertTrue("Un transformerException should be throw",
+                res.getFailureMessage().contains("TransformerException"));
+    }
+    @Test
+    public void testWithoutSuitableNamespaces() {
+        setAlternateResponseData();
+        assertion.setNamespace(true);
+        assertion.setXPathString("//b:row/value[@field = 'alias']");
+        AssertionResult res = assertion.getResult(jmctx.getPreviousResult());
+        log.debug(" res {}", res.isError());
+        log.debug(" failure {}", res.getFailureMessage());
+        assertTrue("When the user give namspaces, un transformerException should be throw",
+                res.getFailureMessage().contains("TransformerException"));
+    }
 }

Added: jmeter/trunk/test/src/org/apache/jmeter/extractor/TestXPath2Extractor.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/extractor/TestXPath2Extractor.java?rev=1860789&view=auto
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/extractor/TestXPath2Extractor.java (added)
+++ jmeter/trunk/test/src/org/apache/jmeter/extractor/TestXPath2Extractor.java Fri Jun  7
20:29:00 2019
@@ -0,0 +1,292 @@
+/*
+ * 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.extractor;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.io.UnsupportedEncodingException;
+
+import org.apache.jmeter.samplers.SampleResult;
+import org.apache.jmeter.threads.JMeterContext;
+import org.apache.jmeter.threads.JMeterContextService;
+import org.apache.jmeter.threads.JMeterVariables;
+import org.junit.Before;
+import org.junit.Test;
+
+public class TestXPath2Extractor {
+    private static final String VAL_NAME = "value";
+    private static final String VAL_NAME_NR = "value_matchNr";
+
+    private XPath2Extractor extractor;
+    private SampleResult result;
+    private String data;
+    private JMeterVariables vars;
+    private JMeterContext jmctx;
+
+    @Before
+    public void setUp() throws UnsupportedEncodingException {
+        jmctx = JMeterContextService.getContext();
+        extractor = new XPath2Extractor();
+        extractor.setThreadContext(jmctx);// This would be done by the run
+                                          // command
+        extractor.setRefName(VAL_NAME);
+        extractor.setDefaultValue("Default");
+        result = new SampleResult();
+        data = "<book><preface title='Intro'>zero</preface><page>one</page><page>two</page><empty></empty><a><b></b></a></book>";
+        result.setResponseData(data.getBytes("UTF-8"));
+        vars = new JMeterVariables();
+        jmctx.setVariables(vars);
+        jmctx.setPreviousResult(result);
+    }
+
+    @Test
+    public void testAttributeExtraction() throws Exception {
+        extractor.setXPathQuery("/book/preface/@title");
+        extractor.process();
+        assertEquals("Intro", vars.get(VAL_NAME));
+        assertEquals("1", vars.get(VAL_NAME_NR));
+        assertEquals("Intro", vars.get(VAL_NAME + "_1"));
+        assertNull(vars.get(VAL_NAME + "_2"));
+
+        extractor.setXPathQuery("/book/preface[@title]");
+        extractor.process();
+        assertEquals("zero", vars.get(VAL_NAME));
+        assertEquals("1", vars.get(VAL_NAME_NR));
+        assertEquals("zero", vars.get(VAL_NAME + "_1"));
+        assertNull(vars.get(VAL_NAME + "_2"));
+
+        extractor.setXPathQuery("/book/preface[@title='Intro']");
+        extractor.process();
+        assertEquals("zero", vars.get(VAL_NAME));
+        assertEquals("1", vars.get(VAL_NAME_NR));
+        assertEquals("zero", vars.get(VAL_NAME + "_1"));
+        assertNull(vars.get(VAL_NAME + "_2"));
+
+        extractor.setXPathQuery("/book/preface[@title='xyz']");
+        extractor.process();
+        assertEquals("Default", vars.get(VAL_NAME));
+        assertEquals("0", vars.get(VAL_NAME_NR));
+        assertNull(vars.get(VAL_NAME + "_1"));
+    }
+
+    @Test
+    public void testVariableExtraction() throws Exception {
+        extractor.setXPathQuery("/book/preface");
+        extractor.process();
+        assertEquals("zero", vars.get(VAL_NAME));
+        assertEquals("1", vars.get(VAL_NAME_NR));
+        assertEquals("zero", vars.get(VAL_NAME + "_1"));
+        assertNull(vars.get(VAL_NAME + "_2"));
+
+        // Test match 1
+        extractor.setXPathQuery("/book/page");
+        extractor.setMatchNumber(1);
+        extractor.process();
+        assertEquals("one", vars.get(VAL_NAME));
+        assertEquals("1", vars.get(VAL_NAME_NR));
+        assertEquals("one", vars.get(VAL_NAME + "_1"));
+        assertNull(vars.get(VAL_NAME + "_2"));
+        assertNull(vars.get(VAL_NAME + "_3"));
+
+        // Test match 1 in String
+        extractor.setXPathQuery("/book/page");
+        extractor.setMatchNumber("1");
+        extractor.process();
+        assertEquals("one", vars.get(VAL_NAME));
+        assertEquals("1", vars.get(VAL_NAME_NR));
+        assertEquals("one", vars.get(VAL_NAME + "_1"));
+        assertNull(vars.get(VAL_NAME + "_2"));
+        assertNull(vars.get(VAL_NAME + "_3"));
+
+        // Test match 2
+        extractor.setXPathQuery("/book/page");
+        extractor.setMatchNumber(2);
+        extractor.process();
+        assertEquals("two", vars.get(VAL_NAME));
+        assertEquals("1", vars.get(VAL_NAME_NR));
+        assertEquals("two", vars.get(VAL_NAME + "_1"));
+        assertNull(vars.get(VAL_NAME + "_2"));
+        assertNull(vars.get(VAL_NAME + "_3"));
+
+        // Test more than one match
+        extractor.setMatchNumber(-1);
+        extractor.setXPathQuery("/book/page");
+        extractor.process();
+        assertEquals("2", vars.get(VAL_NAME_NR));
+        assertEquals("one", vars.get(VAL_NAME + "_1"));
+        assertEquals("one", vars.get(VAL_NAME));
+        assertEquals("two", vars.get(VAL_NAME + "_2"));
+        assertNull(vars.get(VAL_NAME + "_3"));
+
+        // Put back default value
+        extractor.setMatchNumber(-1);
+
+        extractor.setXPathQuery("/book/page[2]");
+        extractor.process();
+        assertEquals("two", vars.get(VAL_NAME));
+        assertEquals("1", vars.get(VAL_NAME_NR));
+        assertEquals("two", vars.get(VAL_NAME + "_1"));
+        assertNull(vars.get(VAL_NAME + "_2"));
+        assertNull(vars.get(VAL_NAME + "_3"));
+
+        extractor.setXPathQuery("/book/index");
+        extractor.process();
+        assertEquals("Default", vars.get(VAL_NAME));
+        assertEquals("0", vars.get(VAL_NAME_NR));
+        assertNull(vars.get(VAL_NAME + "_1"));
+
+        extractor.setMatchNumber(-1);
+        // Test fragment
+        extractor.setXPathQuery("/book/page[2]");
+        extractor.setFragment(true);
+        extractor.process();
+        assertEquals("<page>two</page>", vars.get(VAL_NAME));
+        // Now get its text
+        extractor.setXPathQuery("/book/page[2]/text()");
+        extractor.process();
+        assertEquals("two", vars.get(VAL_NAME));
+
+    }
+
+    //
+    @Test
+    public void testScope() {
+        extractor.setXPathQuery("/book/preface");
+        extractor.process();
+        assertEquals("zero", vars.get(VAL_NAME));
+        assertEquals("1", vars.get(VAL_NAME_NR));
+        assertEquals("zero", vars.get(VAL_NAME + "_1"));
+        assertNull(vars.get(VAL_NAME + "_2"));
+
+        extractor.setScopeChildren(); // There aren't any
+        extractor.process();
+        assertEquals("Default", vars.get(VAL_NAME));
+        assertEquals("0", vars.get(VAL_NAME_NR));
+        assertNull(vars.get(VAL_NAME + "_1"));
+
+        extractor.setScopeAll(); // same as Parent
+        extractor.process();
+        assertEquals("zero", vars.get(VAL_NAME));
+        assertEquals("1", vars.get(VAL_NAME_NR));
+        assertEquals("zero", vars.get(VAL_NAME + "_1"));
+        assertNull(vars.get(VAL_NAME + "_2"));
+
+        // Try to get data from subresult
+        result.sampleStart(); // Needed for addSubResult()
+        result.sampleEnd();
+        SampleResult subResult = new SampleResult();
+        subResult.sampleStart();
+        subResult.setResponseData(result.getResponseData());
+        subResult.sampleEnd();
+        result.addSubResult(subResult);
+
+        // Get data from both
+        extractor.setScopeAll();
+        extractor.process();
+        assertEquals("zero", vars.get(VAL_NAME));
+        assertEquals("2", vars.get(VAL_NAME_NR));
+        assertEquals("zero", vars.get(VAL_NAME + "_1"));
+        assertEquals("zero", vars.get(VAL_NAME + "_2"));
+        assertNull(vars.get(VAL_NAME + "_3"));
+
+        // get data from child
+        extractor.setScopeChildren();
+        extractor.process();
+        assertEquals("zero", vars.get(VAL_NAME));
+        assertEquals("1", vars.get(VAL_NAME_NR));
+        assertEquals("zero", vars.get(VAL_NAME + "_1"));
+        assertNull(vars.get(VAL_NAME + "_2"));
+
+        // get data from child
+        extractor.setScopeVariable("result");
+        result = new SampleResult();
+        vars.put("result", data);
+        extractor.process();
+        assertEquals("zero", vars.get(VAL_NAME));
+        assertEquals("1", vars.get(VAL_NAME_NR));
+        assertEquals("zero", vars.get(VAL_NAME + "_1"));
+        assertNull(vars.get(VAL_NAME + "_2"));
+
+        // get data from child
+        extractor.setScopeVariable("result");
+        result = new SampleResult();
+        vars.remove("result");
+        extractor.process();
+        assertEquals("Default", vars.get(VAL_NAME));
+        assertEquals("0", vars.get(VAL_NAME_NR));
+    }
+
+    @Test
+    public void testWithNamespace() throws Exception {
+        result.setResponseData(
+                "<age:ag xmlns:age=\"http://www.w3.org/wgs84_pos#\"><head><title>test</title></head></age:ag>",
null);
+        String namespaces = "age=http://www.w3.org/wgs84_pos#";
+        String xPathQuery = "/age:ag/head/title";
+        extractor.setXPathQuery(xPathQuery);
+        extractor.setNamespaces(namespaces);
+        extractor.process();
+        assertEquals("test", vars.get(VAL_NAME));
+        assertEquals("1", vars.get(VAL_NAME_NR));
+        assertEquals("test", vars.get(VAL_NAME + "_1"));
+        assertNull(vars.get(VAL_NAME + "_2"));
+    }
+
+    @Test
+    public void testWithNamespaces() throws Exception {
+        result.setResponseData(
+                "<age:ag xmlns:age=\"http://www.w3.org/wgs84_pos#\">"
+                        + "<hd:head xmlns:hd=\"http://www.w3.org/wgs85_pos#\"><title>test</title></hd:head></age:ag>",
+                null);
+        String namespaces = "age=http://www.w3.org/wgs84_pos#" + "\n" + "hd=http://www.w3.org/wgs85_pos#";
+        String xPathQuery = "/age:ag/hd:head/title";
+        extractor.setXPathQuery(xPathQuery);
+        extractor.setNamespaces(namespaces);
+        extractor.process();
+        assertEquals("test", vars.get(VAL_NAME));
+        assertEquals("1", vars.get(VAL_NAME_NR));
+        assertEquals("test", vars.get(VAL_NAME + "_1"));
+        assertNull(vars.get(VAL_NAME + "_2"));
+    }
+
+    @Test
+    public void testWithoutNamespace() throws Exception {
+        result.setResponseData(
+                "<age:ag xmlns:age=\"http://www.w3.org/wgs84_pos#\"><head><title>test</title></head></age:ag>",
null);
+        String xPathQuery = "/age:ag/head/title";
+        extractor.setXPathQuery(xPathQuery);
+        extractor.process();
+        assertEquals("Default", vars.get(VAL_NAME));
+        assertEquals("0", vars.get(VAL_NAME_NR));
+    }
+
+    @Test
+    public void testPreviousResultIsEmpty() throws Exception {
+        JMeterContext jmc = JMeterContextService.getContext();
+        extractor = new XPath2Extractor();
+        extractor.setThreadContext(jmctx);// This would be done by the run
+                                          // command
+        extractor.setRefName(VAL_NAME);
+        extractor.setDefaultValue("Default");
+        jmc.setPreviousResult(null);
+        extractor.setXPathQuery("/book/preface");
+        extractor.process();
+        assertEquals(null, vars.get(VAL_NAME));
+    }
+}

Propchange: jmeter/trunk/test/src/org/apache/jmeter/extractor/TestXPath2Extractor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jmeter/trunk/test/src/org/apache/jmeter/extractor/TestXPath2Extractor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jmeter/trunk/test/src/org/apache/jmeter/util/XPathUtilTest.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/util/XPathUtilTest.java?rev=1860789&r1=1860788&r2=1860789&view=diff
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/util/XPathUtilTest.java (original)
+++ jmeter/trunk/test/src/org/apache/jmeter/util/XPathUtilTest.java Fri Jun  7 20:29:00 2019
@@ -23,23 +23,27 @@ import static org.junit.Assert.assertFal
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
 import java.io.PrintStream;
 import java.io.StringReader;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
 
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.stream.FactoryConfigurationError;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.transform.TransformerException;
 import javax.xml.transform.stream.StreamSource;
 
-import org.apache.jmeter.assertions.XPath2Assertion;
+import org.apache.jmeter.assertions.AssertionResult;
 import org.apache.jmeter.assertions.gui.XPath2Panel;
 import org.hamcrest.CoreMatchers;
 import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
+import org.xml.sax.SAXException;
 
 import net.sf.saxon.s9api.Processor;
 import net.sf.saxon.s9api.SaxonApiException;
@@ -50,11 +54,10 @@ import net.sf.saxon.s9api.XdmItem;
 import net.sf.saxon.s9api.XdmValue;
 
 public class XPathUtilTest {
-    private static final Logger log = LoggerFactory.getLogger(XPathUtil.class);
-    final String lineSeparator = System.getProperty("line.separator");
+    private final String lineSeparator = System.getProperty("line.separator");
 
     final String xmlDoc = JMeterUtils.getResourceFileAsText("XPathUtilTestXml.xml");
-    private XPath2Assertion assertion;
+
     @Test
     public void testBug63033() throws SaxonApiException {
         Processor p = new Processor(false);
@@ -161,7 +164,6 @@ public class XPathUtilTest {
                         "")));
     }
     
-
     @Test()
     public void testFormatXmlInvalid() {
         PrintStream origErr = System.err;
@@ -172,18 +174,124 @@ public class XPathUtilTest {
                 .is(XPathUtil.formatXml("No well formed xml here")));
         System.setErr(origErr);
     }
-    
+
     @Test()
     public void testValidateXPath2() throws ParserConfigurationException {
-        Document testDoc=XPathUtil.makeDocumentBuilder(false, false, false, false).newDocument();
+        Document testDoc = XPathUtil.makeDocumentBuilder(false, false, false, false).newDocument();
         Element el = testDoc.createElement("root"); //$NON-NLS-1$
         testDoc.appendChild(el);
         String namespaces = "a=http://www.w3.org/2003/01/geo/wgs84_pos# b=http://www.w3.org/2003/01/geo/wgs85_pos#";
         String xPathQuery = "//Employees/b:Employee[1]/a:ag";
-        assertTrue("When the user give namspaces, the result of validation should be true",
XPath2Panel.validXPath(xPathQuery, false, namespaces));
-        namespaces ="a=http://www.w3.org/2003/01/geo/wgs84_pos#";
-        assertFalse("When the user doesn't give namspaces, the result of validation should
be false", XPath2Panel.validXPath(xPathQuery, false, namespaces));
-}
-    
+        assertTrue("When the user give namspaces, the result of validation should be true",
+                XPath2Panel.validXPath(xPathQuery, false, namespaces));
+        namespaces = "a=http://www.w3.org/2003/01/geo/wgs84_pos#";
+        assertFalse("When the user doesn't give namspaces, the result of validation should
be false",
+                XPath2Panel.validXPath(xPathQuery, false, namespaces));
+    }
+
+    @Test()
+    public void testMakeDocument() throws ParserConfigurationException, SAXException, IOException,
TidyException {
+        String responseData = "<book><page>one</page><page>two</page><empty></empty><a><b></b></a></book>";
+        Document testDoc = XPathUtil.makeDocument(
+                new ByteArrayInputStream(responseData.getBytes(StandardCharsets.UTF_8)),
false, false, false, false,
+                false, false, false, false, false);
+        AssertionResult res = new AssertionResult("test");
+        String xpathquery = "/book";
+        XPathUtil.computeAssertionResult(res, testDoc, xpathquery, false);
+        assertFalse("Should not be an error", res.isError());
+        assertFalse("Should not be a failure", res.isFailure());
+        xpathquery = "/book/error";
+        XPathUtil.computeAssertionResult(res, testDoc, xpathquery, false);
+        assertFalse("Should not be an error", res.isError());
+        assertTrue("Should be a failure", res.isFailure());
+        xpathquery = "count(/book/page)=2";
+        XPathUtil.computeAssertionResult(res, testDoc, xpathquery, false);
+        assertFalse("Should not be an error", res.isError());
+        assertFalse("Should not be a failure", res.isFailure());
+        xpathquery = "count(/book/page)=1";
+        XPathUtil.computeAssertionResult(res, testDoc, xpathquery, false);
+        assertFalse("Should not be an error", res.isError());
+        assertTrue("Should be a failure", res.isFailure());
+        xpathquery = "///book";
+        XPathUtil.computeAssertionResult(res, testDoc, xpathquery, false);
+        assertTrue("Should be an error", res.isError());
+    }
 
+    @Test()
+    public void testMakeDocumentIsnegated()
+            throws ParserConfigurationException, SAXException, IOException, TidyException
{
+        String responseData = "<book><preface>zero</preface><page>one</page><page>two</page><empty></empty><a><b></b></a></book>";
+        Document testDoc = XPathUtil.makeDocument(
+                new ByteArrayInputStream(responseData.getBytes(StandardCharsets.UTF_8)),
false, false, false, false,
+                false, false, false, false, false);
+        AssertionResult res = new AssertionResult("test");
+        String xpathquery = "/book/error";
+        XPathUtil.computeAssertionResult(res, testDoc, xpathquery, true);
+        assertFalse("Should not be an error", res.isError());
+        assertFalse("Should not be a failure", res.isFailure());
+        xpathquery = "/book/preface";
+        XPathUtil.computeAssertionResult(res, testDoc, xpathquery, true);
+        assertFalse("Should not be an error", res.isError());
+        assertTrue("Should be a failure", res.isFailure());
+        XPathUtil.computeAssertionResult(res, testDoc, xpathquery, true);
+        assertFalse("Should not be an error", res.isError());
+        assertTrue("Should be a failure", res.isFailure());
+        xpathquery = "count(/book/page)=1";
+        XPathUtil.computeAssertionResult(res, testDoc, xpathquery, true);
+        assertFalse("Should not be an error", res.isError());
+        assertFalse("Should not be a failure", res.isFailure());
+    }
+
+    @Test()
+    public void testGetNamespaces() throws XMLStreamException, FactoryConfigurationError
{
+        String responseData = "<age:ag xmlns:age=\"http://www.w3.org/wgs84_pos#\">\n"
+                + "<hd:head xmlns:hd=\"http://www.w3.org/wgs85_pos#\"><title>test</title></hd:head></age:ag>";
+        List<String[]> res = XPathUtil.getNamespaces(responseData);
+        assertEquals("age", res.get(0)[0]);
+        assertEquals("http://www.w3.org/wgs84_pos#", res.get(0)[1]);
+        assertEquals("hd", res.get(1)[0]);
+        assertEquals("http://www.w3.org/wgs85_pos#", res.get(1)[1]);
+    }
+    
+    @Test()
+    public void testComputeAssertionResultUsingSaxon() throws SaxonApiException, FactoryConfigurationError
{
+        //test xpath2 assertion 
+        AssertionResult res = new AssertionResult("test");
+        String responseData = "<book><page>one</page><page>two</page><empty></empty><a><b></b></a></book>";
+        String xpathquery = "/book";
+        XPathUtil.computeAssertionResultUsingSaxon(res, responseData, xpathquery, "", false);
+        assertFalse("Should not be an error", res.isError());
+        assertFalse("Should not be a failure", res.isFailure());
+        //test xpath2 assertion 
+        xpathquery = "/b";
+        XPathUtil.computeAssertionResultUsingSaxon(res, responseData, xpathquery, "", false);
+        assertFalse("Should not be an error", res.isError());
+        assertTrue("Should be a failure", res.isFailure());
+        //test xpath2 assertion boolean
+        xpathquery = "count(//page)=2";
+        XPathUtil.computeAssertionResultUsingSaxon(res, responseData, xpathquery, "", false);
+        assertFalse("Should not be an error", res.isError());
+        assertFalse("Should not be a failure", res.isFailure());
+        //test xpath2 assertion boolean
+        xpathquery = "count(//page)=3";
+        XPathUtil.computeAssertionResultUsingSaxon(res, responseData, xpathquery, "", false);
+        assertFalse("Should not be an error", res.isError());
+        assertTrue("Should be a failure", res.isFailure());
+    }
+    @Test()
+    public void testPutValuesForXPathInList() throws ParserConfigurationException, SAXException,
IOException, TidyException, TransformerException {
+        String responseData = "<book><page>one</page><page>two</page><empty></empty><a><b></b></a></book>";
+        Document testDoc = XPathUtil.makeDocument(
+                new ByteArrayInputStream(responseData.getBytes(StandardCharsets.UTF_8)),
false, false, false, false,
+                false, false, false, false, false);
+        String xpathquery = "/book/page";
+        List<String> matchs=new ArrayList<>();
+        XPathUtil.putValuesForXPathInList(testDoc, xpathquery, matchs, true);
+        assertEquals("<page>one</page>", matchs.get(0));
+        assertEquals("<page>two</page>", matchs.get(1));
+        matchs=new ArrayList<>();
+        XPathUtil.putValuesForXPathInList(testDoc, xpathquery, matchs, false); 
+        assertEquals("one", matchs.get(0));
+        assertEquals("two", matchs.get(1));
+    }
 }



Mime
View raw message