Skip to content
Permalink
Browse files
Add a switch to decide which regex engine should be used
  • Loading branch information
FSchumacher committed Mar 5, 2022
1 parent cc792fb commit a9f512732fc3bfa7c75ce711c09dcdb79baa1d5d
Showing 1 changed file with 26 additions and 7 deletions.
@@ -21,6 +21,7 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
@@ -94,6 +95,8 @@ public class ResponseAssertion extends AbstractScopedAssertion implements Serial
private static final String DIFF_DELTA_END
= JMeterUtils.getPropDefault("assertion.equals_diff_delta_end", "]]]");

private boolean useJavaRegex = JMeterUtils.getPropDefault("jmeter.use_java_regex", false);

public ResponseAssertion() {
setProperty(new CollectionProperty(TEST_STRINGS, new ArrayList<String>()));
}
@@ -326,19 +329,27 @@ private AssertionResult evaluateResponse(SampleResult response) {
List<String> allCheckMessage = new ArrayList<>();
for (JMeterProperty jMeterProperty : getTestStrings()) {
String stringPattern = jMeterProperty.getStringValue();
Pattern pattern = null;
if (contains || matches) {
pattern = JMeterUtils.getPatternCache().getPattern(stringPattern, Perl5Compiler.READ_ONLY_MASK);
}
boolean found;
if (contains) {
found = localMatcher.contains(toCheck, pattern);
if (useJavaRegex) {
found = containsWithJavaRegex(toCheck, stringPattern);
} else {
Pattern pattern = JMeterUtils.getPatternCache()
.getPattern(stringPattern, Perl5Compiler.READ_ONLY_MASK);
found = localMatcher.contains(toCheck, pattern);
}
} else if (equals) {
found = toCheck.equals(stringPattern);
} else if (substring) {
found = toCheck.contains(stringPattern);
} else {
found = localMatcher.matches(toCheck, pattern);
if (useJavaRegex) {
found = matchesWithJavaRegex(toCheck, stringPattern);
} else {
Pattern pattern = JMeterUtils.getPatternCache()
.getPattern(stringPattern, Perl5Compiler.READ_ONLY_MASK);
found = localMatcher.matches(toCheck, pattern);
}
}
boolean pass = notTest ? !found : found;
if (orTest) {
@@ -375,14 +386,22 @@ private AssertionResult evaluateResponse(SampleResult response) {
result.setFailureMessage(customMsg);
}
}
} catch (MalformedCachePatternException e) {
} catch (MalformedCachePatternException | PatternSyntaxException e) {
result.setError(true);
result.setFailure(false);
result.setFailureMessage("Bad test configuration " + e);
}
return result;
}

private static boolean matchesWithJavaRegex(String toCheck, String stringPattern) {
return java.util.regex.Pattern.compile(stringPattern).matcher(toCheck).matches();
}

private static boolean containsWithJavaRegex(String toCheck, String stringPattern) {
return java.util.regex.Pattern.compile(stringPattern).matcher(toCheck).find();
}

private String getStringToCheck(SampleResult response) {
String toCheck; // The string to check (Url or data)
// What are we testing against?

0 comments on commit a9f5127

Please sign in to comment.