📄 responseassertion.java
字号:
}
public boolean getAssumeSuccess() {
return getPropertyAsBoolean(ASSUME_SUCCESS, false);
}
public void setAssumeSuccess(boolean b) {
setProperty(ASSUME_SUCCESS, b);
}
/**
* Make sure the response satisfies the specified assertion requirements.
*
* @param response
* an instance of SampleResult
* @return an instance of AssertionResult
*/
AssertionResult evaluateResponse(SampleResult response) {
boolean pass = true;
boolean not = (NOT & getTestType()) > 0;
AssertionResult result = new AssertionResult(getName());
String toCheck = ""; // The string to check (Url or data)
if (getAssumeSuccess()) {
response.setSuccessful(true);// Allow testing of failure codes
}
// What are we testing against?
if (isTestFieldResponseData()) {
toCheck = response.getResponseDataAsString(); // (bug25052)
} else if (isTestFieldResponseCode()) {
toCheck = response.getResponseCode();
} else if (isTestFieldResponseMessage()) {
toCheck = response.getResponseMessage();
} else if (isTestFieldResponseHeaders()) {
toCheck = response.getResponseHeaders();
} else { // Assume it is the URL
toCheck = response.getSamplerData(); // TODO - is this where the URL is stored?
if (toCheck == null)
toCheck = "";
}
if (toCheck.length() == 0) {
return result.setResultForNull();
}
result.setFailure(false);
result.setError(false);
boolean contains = isContainsType(); // do it once outside loop
boolean equals = isEqualsType();
boolean debugEnabled = log.isDebugEnabled();
if (debugEnabled){
log.debug("Type:" + (contains?"Contains":"Match") + (not? "(not)": ""));
}
try {
// Get the Matcher for this thread
Perl5Matcher localMatcher = JMeterUtils.getMatcher();
PropertyIterator iter = getTestStrings().iterator();
while (iter.hasNext()) {
String stringPattern = iter.next().getStringValue();
Pattern pattern = JMeterUtils.getPatternCache().getPattern(stringPattern, Perl5Compiler.READ_ONLY_MASK);
boolean found;
if (contains) {
found = localMatcher.contains(toCheck, pattern);
} else if (equals) {
found = toCheck.equals(stringPattern);
} else {
found = localMatcher.matches(toCheck, pattern);
}
pass = not ? !found : found;
if (!pass) {
if (debugEnabled){log.debug("Failed: "+pattern);}
result.setFailure(true);
result.setFailureMessage(getFailText(stringPattern,toCheck));
break;
}
if (debugEnabled){log.debug("Passed: "+pattern);}
}
} catch (MalformedCachePatternException e) {
result.setError(true);
result.setFailure(false);
result.setFailureMessage("Bad test configuration " + e);
}
return result;
}
/**
* Generate the failure reason from the TestType
*
* @param stringPattern
* @return the message for the assertion report
*/
// TODO strings should be resources (but currently must not contain commas or the CSV file will be broken)
private String getFailText(String stringPattern, String toCheck) {
StringBuffer sb = new StringBuffer(200);
sb.append("Test failed: ");
if (isTestFieldResponseData()) {
sb.append("text");
} else if (isTestFieldResponseCode()) {
sb.append("code");
} else if (isTestFieldResponseMessage()) {
sb.append("message");
} else if (isTestFieldResponseHeaders()) {
sb.append("headers");
} else // Assume it is the URL
{
sb.append("URL");
}
switch (getTestType()) {
case CONTAINS:
sb.append(" expected to contain ");
break;
case NOT | CONTAINS:
sb.append(" expected not to contain ");
break;
case MATCH:
sb.append(" expected to match ");
break;
case NOT | MATCH:
sb.append(" expected not to match ");
break;
case EQUALS:
sb.append(" expected to equal ");
break;
case NOT | EQUALS:
sb.append(" expected not to equal ");
break;
default:// should never happen...
sb.append(" expected something using ");
}
sb.append("/");
if (isEqualsType()){
sb.append(equalsComparisonText(toCheck, stringPattern));
} else {
sb.append(stringPattern);
}
sb.append("/");
return sb.toString();
}
private static String trunc(final boolean right, final String str)
{
if (str.length() <= EQUALS_SECTION_DIFF_LEN)
return str;
else if (right)
return str.substring(0, EQUALS_SECTION_DIFF_LEN) + EQUALS_DIFF_TRUNC;
else
return EQUALS_DIFF_TRUNC + str.substring(str.length() - EQUALS_SECTION_DIFF_LEN, str.length());
}
/**
* Returns some helpful logging text to determine where equality between two strings
* is broken, with one pointer working from the front of the strings and another working
* backwards from the end.
*
* @param received String received from sampler.
* @param comparison String specified for "equals" response assertion.
* @return Two lines of text separated by newlines, and then forward and backward pointers
* denoting first position of difference.
*/
private static StringBuffer equalsComparisonText(final String received, final String comparison)
{
final StringBuffer text;
int firstDiff;
int lastRecDiff = -1;
int lastCompDiff = -1;
final int recLength = received.length();
final int compLength = comparison.length();
final int minLength = Math.min(recLength, compLength);
final String startingEqSeq;
String recDeltaSeq = "";
String compDeltaSeq = "";
String endingEqSeq = "";
final StringBuffer pad;
text = new StringBuffer(Math.max(recLength, compLength) * 2);
for (firstDiff = 0; firstDiff < minLength; firstDiff++)
if (received.charAt(firstDiff) != comparison.charAt(firstDiff))
break;
if (firstDiff == 0)
startingEqSeq = "";
else
startingEqSeq = trunc(false, received.substring(0, firstDiff));
lastRecDiff = recLength - 1;
lastCompDiff = compLength - 1;
while ((lastRecDiff > firstDiff) && (lastCompDiff > firstDiff)
&& received.charAt(lastRecDiff) == comparison.charAt(lastCompDiff))
{
lastRecDiff--;
lastCompDiff--;
}
endingEqSeq = trunc(true, received.substring(lastRecDiff + 1, recLength));
if (endingEqSeq.length() == 0)
{
recDeltaSeq = trunc(true, received.substring(firstDiff, recLength));
compDeltaSeq = trunc(true, comparison.substring(firstDiff, compLength));
}
else
{
recDeltaSeq = trunc(true, received.substring(firstDiff, lastRecDiff + 1));
compDeltaSeq = trunc(true, comparison.substring(firstDiff, lastCompDiff + 1));
}
pad = new StringBuffer(Math.abs(recDeltaSeq.length() - compDeltaSeq.length()));
for (int i = 0; i < pad.capacity(); i++)
pad.append(' ');
if (recDeltaSeq.length() > compDeltaSeq.length())
compDeltaSeq += pad.toString();
else
recDeltaSeq += pad.toString();
text.append("\n\n");
text.append(RECEIVED_STR);
text.append(startingEqSeq);
text.append(DIFF_DELTA_START);
text.append(recDeltaSeq);
text.append(DIFF_DELTA_END);
text.append(endingEqSeq);
text.append("\n\n");
text.append(COMPARISON_STR);
text.append(startingEqSeq);
text.append(DIFF_DELTA_START);
text.append(compDeltaSeq);
text.append(DIFF_DELTA_END);
text.append(endingEqSeq);
text.append("\n\n");
return text;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -