📄 responseassertion.java
字号:
/*
* 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.Serializable;
import java.util.ArrayList;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.testelement.AbstractTestElement;
import org.apache.jmeter.testelement.property.CollectionProperty;
import org.apache.jmeter.testelement.property.IntegerProperty;
import org.apache.jmeter.testelement.property.JMeterProperty;
import org.apache.jmeter.testelement.property.NullProperty;
import org.apache.jmeter.testelement.property.PropertyIterator;
import org.apache.jmeter.testelement.property.StringProperty;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
import org.apache.oro.text.MalformedCachePatternException;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
// @see org.apache.jmeter.assertions.PackageTest for unit tests
/**
* Test element to handle Response Assertions, @see AssertionGui
*/
public class ResponseAssertion extends AbstractTestElement implements Serializable, Assertion {
private static final Logger log = LoggingManager.getLoggerForClass();
private final static String TEST_FIELD = "Assertion.test_field"; // $NON-NLS-1$
// Values for TEST_FIELD
// N.B. we cannot change the text value as it is in test plans
private final static String SAMPLE_URL = "Assertion.sample_label"; // $NON-NLS-1$
private final static String RESPONSE_DATA = "Assertion.response_data"; // $NON-NLS-1$
private final static String RESPONSE_CODE = "Assertion.response_code"; // $NON-NLS-1$
private final static String RESPONSE_MESSAGE = "Assertion.response_message"; // $NON-NLS-1$
private final static String RESPONSE_HEADERS = "Assertion.response_headers"; // $NON-NLS-1$
private final static String ASSUME_SUCCESS = "Assertion.assume_success"; // $NON-NLS-1$
private final static String TEST_STRINGS = "Asserion.test_strings"; // $NON-NLS-1$
private final static String TEST_TYPE = "Assertion.test_type"; // $NON-NLS-1$
/*
* Mask values for TEST_TYPE TODO: remove either MATCH or CONTAINS - they
* are mutually exckusive
*/
private final static int MATCH = 1 << 0;
final static int CONTAINS = 1 << 1;
private final static int NOT = 1 << 2;
private final static int EQUALS = 1 << 3;
private static final int EQUALS_SECTION_DIFF_LEN
= JMeterUtils.getPropDefault("assertion.equals_section_diff_len", 100);
/** Signifies truncated text in diff display. */
private static final String EQUALS_DIFF_TRUNC = "...";
private static final String RECEIVED_STR = "****** received : ";
private static final String COMPARISON_STR = "****** comparison: ";
private static final String DIFF_DELTA_START
= JMeterUtils.getPropDefault("assertion.equals_diff_delta_start", "[[[");
private static final String DIFF_DELTA_END
= JMeterUtils.getPropDefault("assertion.equals_diff_delta_end", "]]]");
public ResponseAssertion() {
setProperty(new CollectionProperty(TEST_STRINGS, new ArrayList()));
}
public void clear() {
super.clear();
setProperty(new CollectionProperty(TEST_STRINGS, new ArrayList()));
}
private void setTestField(String testField) {
setProperty(TEST_FIELD, testField);
}
public void setTestFieldURL(){
setTestField(SAMPLE_URL);
}
public void setTestFieldResponseCode(){
setTestField(RESPONSE_CODE);
}
public void setTestFieldResponseData(){
setTestField(RESPONSE_DATA);
}
public void setTestFieldResponseMessage(){
setTestField(RESPONSE_MESSAGE);
}
public void setTestFieldResponseHeaders(){
setTestField(RESPONSE_HEADERS);
}
public boolean isTestFieldURL(){
return SAMPLE_URL.equals(getTestField());
}
public boolean isTestFieldResponseCode(){
return RESPONSE_CODE.equals(getTestField());
}
public boolean isTestFieldResponseData(){
return RESPONSE_DATA.equals(getTestField());
}
public boolean isTestFieldResponseMessage(){
return RESPONSE_MESSAGE.equals(getTestField());
}
public boolean isTestFieldResponseHeaders(){
return RESPONSE_HEADERS.equals(getTestField());
}
private void setTestType(int testType) {
setProperty(new IntegerProperty(TEST_TYPE, testType));
}
public void addTestString(String testString) {
getTestStrings().addProperty(new StringProperty(String.valueOf(testString.hashCode()), testString));
}
public void clearTestStrings() {
getTestStrings().clear();
}
public AssertionResult getResult(SampleResult response) {
AssertionResult result;
// None of the other Assertions check the response status, so remove
// this check
// for the time being, at least...
// if (!response.isSuccessful())
// {
// result = new AssertionResult();
// result.setError(true);
// byte [] ba = response.getResponseData();
// result.setFailureMessage(
// ba == null ? "Unknown Error (responseData is empty)" : new String(ba)
// );
// return result;
// }
result = evaluateResponse(response);
return result;
}
/***************************************************************************
* !ToDoo (Method description)
*
* @return !ToDo (Return description)
**************************************************************************/
public String getTestField() {
return getPropertyAsString(TEST_FIELD);
}
/***************************************************************************
* !ToDoo (Method description)
*
* @return !ToDo (Return description)
**************************************************************************/
public int getTestType() {
JMeterProperty type = getProperty(TEST_TYPE);
if (type instanceof NullProperty) {
return CONTAINS;
}
return type.getIntValue();
}
/***************************************************************************
* !ToDoo (Method description)
*
* @return !ToDo (Return description)
**************************************************************************/
public CollectionProperty getTestStrings() {
return (CollectionProperty) getProperty(TEST_STRINGS);
}
public boolean isEqualsType() {
return (getTestType() & EQUALS) > 0;
}
public boolean isContainsType() {
return (getTestType() & CONTAINS) > 0;
}
public boolean isMatchType() {
return (getTestType() & MATCH) > 0;
}
public boolean isNotType() {
return (getTestType() & NOT) > 0;
}
public void setToContainsType() {
setTestType((getTestType() | CONTAINS) & ~(MATCH | EQUALS));
}
public void setToMatchType() {
setTestType((getTestType() | MATCH) & ~(CONTAINS | EQUALS));
}
public void setToEqualsType() {
setTestType((getTestType() | EQUALS) & ~(MATCH | CONTAINS));
}
public void setToNotType() {
setTestType((getTestType() | NOT));
}
public void unsetNotType() {
setTestType(getTestType() & ~NOT);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -