-
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 2 commits
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$ | ||
|
@@ -126,6 +127,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 +182,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 +256,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 +484,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 +700,13 @@ public boolean useMessage() { | |
public void setUseField(String actionCommand) { | ||
set(getSchema().getMatchTarget(), actionCommand); | ||
} | ||
|
||
public void setFailIfNotFound(boolean isFailing) { | ||
set(getSchema().getFailIfNotFound(), isFailing); | ||
} | ||
|
||
public boolean isFailIfNotFound() { | ||
return get(getSchema().getFailIfNotFound()); | ||
} | ||
|
||
} |
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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,7 @@ | |
* {@code @Isolated}. | ||
*/ | ||
@Isolated | ||
public abstract class JMeterTestCase { | ||
public abstract class JMeterTestCase { | ||
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 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. Sorry, somehow it was updated by a plugin I guess and didn't bothered fixing it. Will undo this |
||
// Used by findTestFile | ||
private static final String filePrefix; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -382,6 +382,7 @@ public void GUIComponents2(GuiComponentHolder componentHolder) throws Exception | |
IGNORED_PROPERTIES.add(LoopControllerSchema.INSTANCE.getContinueForever()); | ||
IGNORED_PROPERTIES.add(RegexExtractorSchema.INSTANCE.getMatchTarget()); | ||
IGNORED_PROPERTIES.add(RegexExtractorSchema.INSTANCE.getDefaultIsEmpty()); | ||
IGNORED_PROPERTIES.add(RegexExtractorSchema.INSTANCE.getFailIfNotFound()); | ||
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 clarify why adding the property here? Ideally the list of "ignored properties" should be empty. I should add a clarification javadoc to 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 have no idea, if I don't add this line the build fails on the tests:
|
||
// TODO: support expressions? | ||
IGNORED_PROPERTIES.add(HTTPSamplerBaseSchema.INSTANCE.getIpSourceType()); | ||
IGNORED_PROPERTIES.add(HTTPSamplerBaseSchema.INSTANCE.getImplementation()); | ||
|
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