-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add fail option for regex extractor #6256
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
import java.util.regex.PatternSyntaxException; | ||
|
||
import org.apache.commons.text.StringEscapeUtils; | ||
import org.apache.jmeter.assertions.AssertionResult; | ||
import org.apache.jmeter.processor.PostProcessor; | ||
import org.apache.jmeter.samplers.SampleResult; | ||
import org.apache.jmeter.testelement.AbstractScopedTestElement; | ||
|
@@ -57,7 +58,7 @@ public class RegexExtractor extends AbstractScopedTestElement implements PostPro | |
* These are passed to the setUseField() method | ||
* | ||
* Do not change these values! | ||
*/ | ||
*/ | ||
public static final String USE_HDRS = "true"; // $NON-NLS-1$ | ||
public static final String USE_REQUEST_HDRS = "request_headers"; // $NON-NLS-1$ | ||
public static final String USE_BODY = "false"; // $NON-NLS-1$ | ||
|
@@ -71,6 +72,8 @@ public class RegexExtractor extends AbstractScopedTestElement implements PostPro | |
|
||
private static final String UNDERSCORE = "_"; // $NON-NLS-1$ | ||
|
||
private static final String FAILIFNOTFOUND = "RegexExtractor.fail_if_not_found"; | ||
vlsi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private static final boolean USE_JAVA_REGEX = !JMeterUtils.getPropDefault( | ||
"jmeter.regex.engine", "oro").equalsIgnoreCase("oro"); | ||
|
||
|
@@ -126,6 +129,9 @@ private void extractWithOroRegex(SampleResult previousResult, JMeterVariables va | |
try { | ||
pattern = JMeterUtils.getPatternCache().getPattern(regex, Perl5Compiler.READ_ONLY_MASK); | ||
List<MatchResult> matches = processMatches(pattern, regex, previousResult, matchNumber, vars); | ||
if(matches.isEmpty() && isFailIfNotFound()){ | ||
failResult(previousResult); | ||
} | ||
int prevCount = 0; | ||
String prevString = vars.get(refName + REF_MATCH_NR); | ||
if (prevString != null) { | ||
|
@@ -178,6 +184,14 @@ private void extractWithOroRegex(SampleResult previousResult, JMeterVariables va | |
} | ||
} | ||
|
||
private void failResult(SampleResult previousResult){ | ||
previousResult.setSuccessful(false); | ||
AssertionResult res = new AssertionResult(getName()); | ||
res.setFailure(true); | ||
res.setFailureMessage("Pattern not found: " + getRegex()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest including the "source" that was checked against the regexp. In other words, something like `Pattern ... not found in ...<response body | ... >" |
||
previousResult.addAssertionResult(res); | ||
} | ||
|
||
private void extractWithJavaRegex(SampleResult previousResult, JMeterVariables vars, String refName, int matchNumber) { | ||
String regex = getRegex(); | ||
java.util.regex.Pattern pattern = null; | ||
|
@@ -244,8 +258,8 @@ private String getInputString(SampleResult result) { | |
: useBodyAsDocument() ? Document.getTextFromDocument(result.getResponseData()) | ||
: result.getResponseDataAsString() // Bug 36898 | ||
; | ||
log.debug("Input = '{}'", inputString); | ||
return inputString; | ||
log.debug("Input = '{}'", inputString); | ||
return inputString; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please refrain from making changes that are not a part of the PR |
||
} | ||
|
||
private List<MatchResult> processMatches(Pattern pattern, String regex, SampleResult result, int matchNumber, JMeterVariables vars) { | ||
|
@@ -472,7 +486,7 @@ private void initTemplate() { | |
PatternMatcher matcher = JMeterUtils.getMatcher(); | ||
Pattern templatePattern = JMeterUtils.getPatternCache().getPattern("\\$(\\d+)\\$" // $NON-NLS-1$ | ||
, Perl5Compiler.READ_ONLY_MASK | ||
| Perl5Compiler.SINGLELINE_MASK); | ||
| Perl5Compiler.SINGLELINE_MASK); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please refrain from making changes that are not a part of the PR |
||
if (log.isDebugEnabled()) { | ||
log.debug("Pattern = '{}', template = '{}'", templatePattern.getPattern(), rawTemplate); | ||
} | ||
|
@@ -688,4 +702,14 @@ public boolean useMessage() { | |
public void setUseField(String actionCommand) { | ||
set(getSchema().getMatchTarget(), actionCommand); | ||
} | ||
|
||
public void setFailIfNotFound(Boolean isFailing){ | ||
log.warn("Setting fail as " + isFailing); | ||
setProperty(FAILIFNOTFOUND,isFailing); | ||
} | ||
|
||
public boolean isFailIfNotFound(){ | ||
return getPropertyAsBoolean(FAILIFNOTFOUND); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
import javax.swing.ButtonGroup; | ||
import javax.swing.JCheckBox; | ||
import javax.swing.JComponent; | ||
import javax.swing.JLabel; | ||
import javax.swing.JPanel; | ||
import javax.swing.JRadioButton; | ||
|
||
|
@@ -52,6 +53,7 @@ public class RegexExtractorGui extends AbstractPostProcessorGui { | |
private JLabeledTextField defaultField; | ||
private JLabeledTextField matchNumberField; | ||
private JLabeledTextField refNameField; | ||
private JLabel failResultField; | ||
private JRadioButton useBody; | ||
private JRadioButton useUnescapedBody; | ||
private JRadioButton useBodyAsDocument; | ||
|
@@ -62,6 +64,7 @@ public class RegexExtractorGui extends AbstractPostProcessorGui { | |
private JRadioButton useMessage; | ||
private ButtonGroup group; | ||
private JCheckBox emptyDefaultValue; | ||
private JCheckBox failResult; | ||
|
||
public RegexExtractorGui() { | ||
super(); | ||
|
@@ -93,6 +96,7 @@ public void configure(TestElement el) { | |
emptyDefaultValue.setSelected(re.isEmptyDefaultValue()); | ||
matchNumberField.setText(re.getMatchNumberAsString()); | ||
refNameField.setText(re.getRefName()); | ||
failResult.setSelected(re.isFailIfNotFound()); | ||
} | ||
} | ||
|
||
|
@@ -124,6 +128,7 @@ public void modifyTestElement(TestElement extractor) { | |
regex.setDefaultValue(defaultField.getText()); | ||
regex.setDefaultEmptyValue(emptyDefaultValue.isSelected()); | ||
regex.setMatchNumber(matchNumberField.getText()); | ||
regex.setFailIfNotFound(failResult.isSelected()); | ||
} | ||
} | ||
|
||
|
@@ -142,6 +147,7 @@ public void clearGui() { | |
emptyDefaultValue.setSelected(false); | ||
refNameField.setText(""); //$NON-NLS-1$ | ||
matchNumberField.setText(""); //$NON-NLS-1$ | ||
failResult.setSelected(false); | ||
} | ||
|
||
private void init() { // WARNING: called from ctor so must not be overridden (i.e. must be private or final) | ||
|
@@ -208,6 +214,8 @@ private JPanel makeParameterPanel() { | |
templateField = new JLabeledTextField(JMeterUtils.getResString("template_field")); //$NON-NLS-1$ | ||
refNameField = new JLabeledTextField(JMeterUtils.getResString("ref_name_field")); //$NON-NLS-1$ | ||
matchNumberField = new JLabeledTextField(JMeterUtils.getResString("match_num_field")); //$NON-NLS-1$ | ||
failResultField = new JLabel(JMeterUtils.getResString("fail_if_not_matched_field")); | ||
failResult = new JCheckBox(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. emptyDefaultValue is also a JCheckBox(), so what would a JEditableCheckbox do better? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm using JLabel + JCheckBox here because it's nicer to have the label in the same row as the others and the checkbox in the column of the input fields There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JEditableCheckbox would allow using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, although the JEditableCheckbox is a nice feature to paramaterize scripts, I don't think it's a good usecase on this specific checkbox. I don't see a reason to parameterize a specific regex extractor. The other checkboxes on the Regex Extractor aren't an editable checkbox as well. |
||
|
||
JPanel panel = new JPanel(new GridBagLayout()); | ||
GridBagConstraints gbc = new GridBagConstraints(); | ||
|
@@ -220,6 +228,8 @@ private JPanel makeParameterPanel() { | |
resetContraints(gbc); | ||
addField(panel, matchNumberField, gbc); | ||
resetContraints(gbc); | ||
addField(panel, failResultField, failResult, gbc); | ||
resetContraints(gbc); | ||
gbc.weighty = 1; | ||
|
||
defaultField = new JLabeledTextField(JMeterUtils.getResString("default_value_field")); //$NON-NLS-1$ | ||
|
@@ -253,6 +263,14 @@ private static void addField(JPanel panel, JLabeledTextField field, GridBagConst | |
panel.add(item.get(1), gbc.clone()); | ||
} | ||
|
||
private static void addField(JPanel panel, JLabel label, JCheckBox checkBox, GridBagConstraints gbc) { | ||
panel.add(label, gbc.clone()); | ||
gbc.gridx++; | ||
gbc.weightx = 1; | ||
gbc.fill = GridBagConstraints.HORIZONTAL; | ||
panel.add(checkBox, gbc.clone()); | ||
} | ||
|
||
// Next line | ||
private static void resetContraints(GridBagConstraints gbc) { | ||
gbc.gridx = 0; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please refrain from making changes that are not a part of the PR