use of org.apache.jmeter.util.TidyException in project jmeter by apache.
the class XPathAssertion method getResult.
/**
* Returns the result of the Assertion. Checks if the result is well-formed
* XML, and that the XPath expression is matched (or not, as the case may
* be)
*/
@Override
public AssertionResult getResult(SampleResult response) {
// no error as default
AssertionResult result = new AssertionResult(getName());
result.setFailure(false);
result.setFailureMessage("");
byte[] responseData = null;
Document doc = null;
try {
if (isScopeVariable()) {
String inputString = getThreadContext().getVariables().get(getVariableName());
if (!StringUtils.isEmpty(inputString)) {
responseData = inputString.getBytes(StandardCharsets.UTF_8);
}
} else {
responseData = response.getResponseData();
}
if (responseData == null || responseData.length == 0) {
return result.setResultForNull();
}
if (log.isDebugEnabled()) {
log.debug("Validation is set to {}, Whitespace is set to {}, Tolerant is set to {}", isValidating(), isWhitespace(), isTolerant());
}
boolean isXML = JOrphanUtils.isXML(responseData);
doc = XPathUtil.makeDocument(new ByteArrayInputStream(responseData), isValidating(), isWhitespace(), isNamespace(), isTolerant(), isQuiet(), showWarnings(), reportErrors(), isXML, isDownloadDTDs());
} catch (SAXException e) {
log.debug("Caught sax exception.", e);
result.setError(true);
result.setFailureMessage(new StringBuilder("SAXException: ").append(e.getMessage()).toString());
return result;
} catch (IOException e) {
log.warn("Cannot parse result content.", e);
result.setError(true);
result.setFailureMessage(new StringBuilder("IOException: ").append(e.getMessage()).toString());
return result;
} catch (ParserConfigurationException e) {
log.warn("Cannot parse result content.", e);
result.setError(true);
result.setFailureMessage(new StringBuilder("ParserConfigurationException: ").append(e.getMessage()).toString());
return result;
} catch (TidyException e) {
result.setError(true);
result.setFailureMessage(e.getMessage());
return result;
}
if (doc == null || doc.getDocumentElement() == null) {
result.setError(true);
result.setFailureMessage("Document is null, probably not parsable");
return result;
}
XPathUtil.computeAssertionResult(result, doc, getXPathString(), isNegated());
return result;
}
use of org.apache.jmeter.util.TidyException in project jmeter by apache.
the class XPathExtractor method process.
/**
* Do the job - extract value from (X)HTML response using XPath Query.
* Return value as variable defined by REFNAME. Returns DEFAULT value
* if not found.
*/
@Override
public void process() {
JMeterContext context = getThreadContext();
final SampleResult previousResult = context.getPreviousResult();
if (previousResult == null) {
return;
}
JMeterVariables vars = context.getVariables();
String refName = getRefName();
vars.put(refName, getDefaultValue());
final String matchNR = concat(refName, REF_MATCH_NR);
// number of previous matches
int prevCount = 0;
try {
prevCount = Integer.parseInt(vars.get(matchNR));
} catch (NumberFormatException e) {
// ignored
}
// In case parse fails // $NON-NLS-1$
vars.put(matchNR, "0");
// In case parse fails // $NON-NLS-1$
vars.remove(concat(refName, "1"));
int matchNumber = getMatchNumber();
List<String> matches = new ArrayList<>();
try {
if (isScopeVariable()) {
String inputString = vars.get(getVariableName());
if (inputString != null) {
if (inputString.length() > 0) {
Document d = parseResponse(inputString);
getValuesForXPath(d, getXPathQuery(), matches, matchNumber);
}
} else {
if (log.isWarnEnabled()) {
log.warn("No variable '{}' found to process by XPathExtractor '{}', skipping processing", getVariableName(), getName());
}
}
} else {
List<SampleResult> samples = getSampleList(previousResult);
for (SampleResult res : samples) {
Document d = parseResponse(res.getResponseDataAsString());
getValuesForXPath(d, getXPathQuery(), matches, matchNumber);
}
}
final int matchCount = matches.size();
vars.put(matchNR, String.valueOf(matchCount));
if (matchCount > 0) {
String value = matches.get(0);
if (value != null) {
vars.put(refName, value);
}
for (int i = 0; i < matchCount; i++) {
value = matches.get(i);
if (value != null) {
vars.put(concat(refName, i + 1), matches.get(i));
}
}
}
// Just in case
vars.remove(concat(refName, matchCount + 1));
// Clear any other remaining variables
for (int i = matchCount + 2; i <= prevCount; i++) {
vars.remove(concat(refName, i));
}
} catch (IOException e) {
// e.g. DTD not reachable
log.error("IOException on ({})", getXPathQuery(), e);
AssertionResult ass = new AssertionResult(getName());
ass.setError(true);
ass.setFailureMessage(new StringBuilder("IOException: ").append(e.getLocalizedMessage()).toString());
previousResult.addAssertionResult(ass);
previousResult.setSuccessful(false);
} catch (ParserConfigurationException e) {
// Should not happen
final String errrorMessage = "ParserConfigurationException while processing (" + getXPathQuery() + ")";
log.error(errrorMessage, e);
throw new JMeterError(errrorMessage, e);
} catch (SAXException e) {
// Can happen for bad input document
if (log.isWarnEnabled()) {
log.warn("SAXException while processing ({}). {}", getXPathQuery(), e.getLocalizedMessage());
}
// Should this also fail the sample?
addAssertionFailure(previousResult, e, false);
} catch (TransformerException e) {
// Can happen for incorrect XPath expression
if (log.isWarnEnabled()) {
log.warn("TransformerException while processing ({}). {}", getXPathQuery(), e.getLocalizedMessage());
}
addAssertionFailure(previousResult, e, false);
} catch (TidyException e) {
// Will already have been logged by XPathUtil
// fail the sample
addAssertionFailure(previousResult, e, true);
}
}
Aggregations