This is an automated email from the ASF dual-hosted git repository.
fschumacher pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jmeter.git
The following commit(s) were added to refs/heads/master by this push:
new cbcf41fc84 Restructure tests for XPathUtil
cbcf41fc84 is described below
commit cbcf41fc84fd637a5e063e8de88cc746fa884b06
Author: Felix Schumacher <[email protected]>
AuthorDate: Sat Jul 23 15:19:15 2022 +0200
Restructure tests for XPathUtil
Use a dummy printstream instead of null to ignore errors on System.err.
Use ParameterizedTests to make test methods more concise.
---
.../java/org/apache/jmeter/util/XPathUtilTest.java | 174 +++++++++------------
1 file changed, 76 insertions(+), 98 deletions(-)
diff --git a/src/core/src/test/java/org/apache/jmeter/util/XPathUtilTest.java
b/src/core/src/test/java/org/apache/jmeter/util/XPathUtilTest.java
index e3c1af6519..c258cba5d3 100644
--- a/src/core/src/test/java/org/apache/jmeter/util/XPathUtilTest.java
+++ b/src/core/src/test/java/org/apache/jmeter/util/XPathUtilTest.java
@@ -19,16 +19,16 @@ package org.apache.jmeter.util;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.IOException;
+import java.io.OutputStream;
import java.io.PrintStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
+import java.util.stream.Stream;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.FactoryConfigurationError;
@@ -39,6 +39,10 @@ import javax.xml.transform.stream.StreamSource;
import org.apache.jmeter.assertions.AssertionResult;
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.junit.jupiter.params.provider.MethodSource;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
@@ -115,29 +119,25 @@ public class XPathUtilTest {
assertEquals(0,matchStrings.size());
}
- @Test
- public void testnamespacesParse() {
- String namespaces = "donald=duck";
+ static Stream<Arguments> namespaceData() {
+ return Stream.of(
+ Arguments.of("donald=duck", "donald", "duck", 0),
+ Arguments.of("donald=duck\nmickey=mouse", "donald", "duck", 0),
+ Arguments.of("donald=duck\nmickey=mouse", "mickey", "mouse",
1),
+ Arguments.of("donald=duck\n\n\nmickey=mouse", "mickey",
"mouse", 1),
+ Arguments.of("donald=duck\n\n\nmickey=mouse", "donald",
"duck", 0),
+ Arguments.of("donald=duck\n \n \nmickey=mouse\n \n\n",
"donald", "duck", 0),
+ Arguments.of("donald=duck\n \n \nmickey=mouse\n \n\n",
"mickey", "mouse", 1),
+ Arguments.of(" \n \ndonald=duck\n \n \nmickey=mouse\n
\n\n", "donald", "duck", 0),
+ Arguments.of(" \n \ndonald=duck\n \n \nmickey=mouse\n
\n\n", "mickey", "mouse", 1)
+ );
+ }
+ @ParameterizedTest
+ @MethodSource("namespaceData")
+ public void testnamespacesParse(String namespaces, String key, String
value, int position) {
List<String[]> test = XPathUtil.namespacesParse(namespaces);
- assertEquals("donald",test.get(0)[0]);
- assertEquals("duck",test.get(0)[1]);
-
- namespaces = "donald=duck\nmickey=mouse";
- test = XPathUtil.namespacesParse(namespaces);
- assertEquals("donald",test.get(0)[0]);
- assertEquals("duck",test.get(0)[1]);
- assertEquals("mickey",test.get(1)[0]);
- assertEquals("mouse",test.get(1)[1]);
-
- namespaces = "donald=duck\n\n\nmickey=mouse";
- test = XPathUtil.namespacesParse(namespaces);
- assertEquals("mickey",test.get(1)[0]);
- assertEquals("mouse",test.get(1)[1]);
-
- namespaces = "geo=patate\n \n \n\nmickey=mouse\n\n \n";
- test = XPathUtil.namespacesParse(namespaces);
- assertEquals("mickey",test.get(1)[0]);
- assertEquals("mouse",test.get(1)[1]);
+ assertEquals(key,test.get(position)[0]);
+ assertEquals(value, test.get(position)[1]);
}
@Test
@@ -163,64 +163,44 @@ public class XPathUtilTest {
@Test
public void testFormatXmlInvalid() {
PrintStream origErr = System.err;
- // The parser will print an error, so let it go where we will not see
it
- System.setErr(null);
- assertThat("No well formed xml here", CoreMatchers
- .is(XPathUtil.formatXml("No well formed xml here")));
- System.setErr(origErr);
+ try {
+ // The parser will print an error, so let it go where we will not
see it
+ System.setErr(new PrintStream(new OutputStream() {
+ @Override
+ public void write(int b) throws IOException {
+ // ignore output
+ }
+ }));
+ assertThat("No well formed xml here", CoreMatchers
+ .is(XPathUtil.formatXml("No well formed xml here")));
+ } finally {
+ System.setErr(origErr);
+ }
}
- @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()
+ @ParameterizedTest
+ @CsvSource(value = {
+ "/book,false,false,false",
+ "/book/error,false,false,true",
+ "/book/error,true,false,false",
+ "/book/preface,true,false,true",
+ "count(/book/page)=2,false,false,false",
+ "count(/book/page)=2,true,false,true",
+ "count(/book/page)=1,false,false,true",
+ "count(/book/page)=1,true,false,false",
+ "///book,false,true,false"
+ })
+ public void testMakeDocument(String xpathquery, boolean isNegated, boolean
isError, boolean isFailure)
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,
+ 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());
+ XPathUtil.computeAssertionResult(res, testDoc, xpathquery, isNegated);
+ assertEquals("test isError", isError, res.isError());
+ assertEquals("test isFailure", isFailure, res.isFailure());
}
@Test
@@ -234,37 +214,35 @@ public class XPathUtilTest {
assertEquals("http://www.w3.org/wgs85_pos#", res.get(1)[1]);
}
- @Test
- public void testComputeAssertionResultUsingSaxon() throws
SaxonApiException, FactoryConfigurationError {
+ @ParameterizedTest
+ @CsvSource(value = {
+ "/book,false,false,false",
+ "/book,true,false,true",
+ "/b,false,false,true",
+ "/b,true,false,false",
+ "count(//page)=2,false,false,false",
+ "count(//page)=2,true,false,true",
+ "count(//page)=3,false,false,true",
+ "count(//page)=3,true,false,false"
+ })
+ public void testComputeAssertionResultUsingSaxon(String xpathquery,
boolean isNegated, boolean isError, boolean isFailure)
+ 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());
+ XPathUtil.computeAssertionResultUsingSaxon(res, responseData,
xpathquery, "", isNegated);
+ assertEquals(isError, res.isError());
+ assertEquals(isFailure, res.isFailure());
}
@Test
- public void testPutValuesForXPathInList() throws
ParserConfigurationException, SAXException, IOException, TidyException,
TransformerException {
+ 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,
+ 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<>();