use of org.apache.oro.text.regex.Perl5Matcher in project jmeter by apache.
the class RenderAsRegexp method processOroRegex.
private String processOroRegex(String textToParse) {
Perl5Matcher matcher = new Perl5Matcher();
PatternMatcherInput input = new PatternMatcherInput(textToParse);
PatternCacheLRU pcLRU = new PatternCacheLRU();
Pattern pattern;
try {
pattern = pcLRU.getPattern(regexpField.getText(), Perl5Compiler.READ_ONLY_MASK);
} catch (MalformedCachePatternException e) {
return e.toString();
}
List<MatchResult> matches = new ArrayList<>();
while (matcher.contains(input, pattern)) {
matches.add(matcher.getMatch());
}
// Construct a multi-line string with all matches
StringBuilder sb = new StringBuilder();
final int size = matches.size();
sb.append("Match count: ").append(size).append("\n");
for (int j = 0; j < size; j++) {
MatchResult mr = matches.get(j);
final int groups = mr.groups();
for (int i = 0; i < groups; i++) {
sb.append("Match[").append(j + 1).append("][").append(i).append("]=").append(mr.group(i)).append("\n");
}
}
return sb.toString();
}
use of org.apache.oro.text.regex.Perl5Matcher in project jmeter by apache.
the class HttpMirrorThread method getRequestHeaderValueWithOroRegex.
private static String getRequestHeaderValueWithOroRegex(String requestHeaders, String headerName) {
Perl5Matcher localMatcher = JMeterUtils.getMatcher();
// We use multi-line mask so can prefix the line with ^
// $NON-NLS-1$ $NON-NLS-2$
String expression = "^" + headerName + ":\\s+([^\\r\\n]+)";
Pattern pattern = JMeterUtils.getPattern(expression, Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.MULTILINE_MASK);
if (localMatcher.contains(requestHeaders, pattern)) {
// The value is in the first group, group 0 is the whole match
return localMatcher.getMatch().group(1);
}
return null;
}
use of org.apache.oro.text.regex.Perl5Matcher in project jmeter by apache.
the class MultipartUrlConfig method getHeaderValueWithOroRegex.
private static String getHeaderValueWithOroRegex(String multiPart, String regularExpression) {
Perl5Matcher localMatcher = JMeterUtils.getMatcher();
Pattern pattern = JMeterUtils.getPattern(regularExpression, Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.MULTILINE_MASK);
if (localMatcher.contains(multiPart, pattern)) {
return localMatcher.getMatch().group(1).trim();
}
return null;
}
use of org.apache.oro.text.regex.Perl5Matcher in project jmeter by apache.
the class ResponseAssertion method evaluateResponse.
/**
* Make sure the response satisfies the specified assertion requirements.
*
* @param response an instance of SampleResult
* @return an instance of AssertionResult
*/
private AssertionResult evaluateResponse(SampleResult response) {
AssertionResult result = new AssertionResult(getName());
if (getAssumeSuccess()) {
// Allow testing of failure codes
response.setSuccessful(true);
}
String toCheck = getStringToCheck(response);
result.setFailure(false);
result.setError(false);
int testType = getTestType();
boolean notTest = (NOT & testType) > 0;
boolean orTest = (OR & testType) > 0;
// do it once outside loop
boolean contains = isContainsType();
boolean equals = isEqualsType();
boolean substring = isSubstringType();
log.debug("Test Type Info: contains={}, notTest={}, orTest={}", contains, notTest, orTest);
if (StringUtils.isEmpty(toCheck)) {
if (notTest) {
// Not should always succeed against an empty result
return result;
}
if (log.isDebugEnabled()) {
log.debug("Not checking empty response field in: {}", response.getSampleLabel());
}
return result.setResultForNull();
}
try {
// Get the Matcher for this thread
Perl5Matcher localMatcher = JMeterUtils.getMatcher();
boolean hasTrue = false;
List<String> allCheckMessage = new ArrayList<>();
for (JMeterProperty jMeterProperty : getTestStrings()) {
String stringPattern = jMeterProperty.getStringValue();
boolean found;
if (contains) {
if (USE_JAVA_REGEX) {
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 {
// this is the old `matches` part which means `isMatchType()` is true
if (USE_JAVA_REGEX) {
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) {
if (!pass) {
log.debug("Failed: {}", stringPattern);
allCheckMessage.add(getFailText(stringPattern, toCheck));
} else {
hasTrue = true;
break;
}
} else {
if (!pass) {
log.debug("Failed: {}", stringPattern);
result.setFailure(true);
String customMsg = getCustomFailureMessage();
if (StringUtils.isEmpty(customMsg)) {
result.setFailureMessage(getFailText(stringPattern, toCheck));
} else {
result.setFailureMessage(customMsg);
}
break;
}
log.debug("Passed: {}", stringPattern);
}
}
if (orTest && !hasTrue) {
result.setFailure(true);
String customMsg = getCustomFailureMessage();
if (StringUtils.isEmpty(customMsg)) {
result.setFailureMessage(allCheckMessage.stream().collect(Collectors.joining("\t", "", "\t")));
} else {
result.setFailureMessage(customMsg);
}
}
} catch (MalformedCachePatternException | PatternSyntaxException e) {
result.setError(true);
result.setFailure(false);
result.setFailureMessage("Bad test configuration " + e);
}
return result;
}
use of org.apache.oro.text.regex.Perl5Matcher in project jmeter by apache.
the class HTTPSamplerBase method generateMatcherPredicate.
private Predicate<URL> generateMatcherPredicate(String regex, String explanation, boolean defaultAnswer) {
if (StringUtils.isEmpty(regex)) {
return s -> defaultAnswer;
}
if (USE_JAVA_REGEX) {
try {
java.util.regex.Pattern pattern = JMeterUtils.compilePattern(regex);
return s -> pattern.matcher(s.toString()).matches();
} catch (PatternSyntaxException e) {
log.warn("Ignoring embedded URL {} string: {}", explanation, e.getMessage());
return s -> defaultAnswer;
}
}
try {
Pattern pattern = JMeterUtils.getPattern(regex);
Perl5Matcher matcher = JMeterUtils.getMatcher();
return s -> matcher.matches(s.toString(), pattern);
} catch (MalformedCachePatternException e) {
// NOSONAR
log.warn("Ignoring embedded URL {} string: {}", explanation, e.getMessage());
return s -> defaultAnswer;
}
}
Aggregations