Search in sources :

Example 66 with Pattern

use of org.apache.oro.text.regex.Pattern in project jmeter by apache.

the class JMESPathAssertion method isEquals.

private boolean isEquals(ObjectMapper mapper, JsonNode jsonNode) throws JsonProcessingException {
    String str = objectToString(mapper, jsonNode);
    if (isUseRegex()) {
        Pattern pattern = JMeterUtils.getPatternCache().getPattern(getExpectedValue());
        return JMeterUtils.getMatcher().matches(str, pattern);
    } else {
        String expectedValueString = getExpectedValue();
        // we did in the old days
        if (str.equals(expectedValueString)) {
            return true;
        }
        // now try harder and compare it as an JSON object
        JsonNode expected = OBJECT_MAPPER.readValue(expectedValueString, JsonNode.class);
        return Objects.equals(expected, jsonNode);
    }
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 67 with Pattern

use of org.apache.oro.text.regex.Pattern in project jmeter by apache.

the class HttpMirrorThread method getRequestHeaderValue.

private static String getRequestHeaderValue(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);
    } else {
        return null;
    }
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher)

Example 68 with Pattern

use of org.apache.oro.text.regex.Pattern 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();
    boolean matches = isMatchType();
    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();
            Pattern pattern = null;
            if (contains || matches) {
                pattern = JMeterUtils.getPatternCache().getPattern(stringPattern, Perl5Compiler.READ_ONLY_MASK);
            }
            boolean found;
            if (contains) {
                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);
            }
            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 e) {
        result.setError(true);
        result.setFailure(false);
        result.setFailureMessage("Bad test configuration " + e);
    }
    return result;
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) MalformedCachePatternException(org.apache.oro.text.MalformedCachePatternException) ArrayList(java.util.ArrayList) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher)

Example 69 with Pattern

use of org.apache.oro.text.regex.Pattern in project jmeter by apache.

the class TestHTTPSamplersAgainstHttpMirrorServer method getPositionOfBody.

private int getPositionOfBody(String stringToCheck) {
    Perl5Matcher localMatcher = JMeterUtils.getMatcher();
    // The headers and body are divided by a blank line
    String regularExpression = "^.$";
    Pattern pattern = JMeterUtils.getPattern(regularExpression, Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.MULTILINE_MASK);
    PatternMatcherInput input = new PatternMatcherInput(stringToCheck);
    while (localMatcher.contains(input, pattern)) {
        MatchResult match = localMatcher.getMatch();
        return match.beginOffset(0);
    }
    // No divider was found
    return -1;
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) PatternMatcherInput(org.apache.oro.text.regex.PatternMatcherInput) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MatchResult(org.apache.oro.text.regex.MatchResult)

Example 70 with Pattern

use of org.apache.oro.text.regex.Pattern in project jmeter by apache.

the class RenderAsRegexp method process.

private String process(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();
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) PatternMatcherInput(org.apache.oro.text.regex.PatternMatcherInput) MalformedCachePatternException(org.apache.oro.text.MalformedCachePatternException) ArrayList(java.util.ArrayList) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MatchResult(org.apache.oro.text.regex.MatchResult) PatternCacheLRU(org.apache.oro.text.PatternCacheLRU)

Aggregations

Pattern (org.apache.oro.text.regex.Pattern)70 Perl5Matcher (org.apache.oro.text.regex.Perl5Matcher)48 MalformedPatternException (org.apache.oro.text.regex.MalformedPatternException)27 Perl5Compiler (org.apache.oro.text.regex.Perl5Compiler)24 MatchResult (org.apache.oro.text.regex.MatchResult)17 PatternMatcherInput (org.apache.oro.text.regex.PatternMatcherInput)14 ArrayList (java.util.ArrayList)12 PatternMatcher (org.apache.oro.text.regex.PatternMatcher)9 PatternCompiler (org.apache.oro.text.regex.PatternCompiler)7 IOException (java.io.IOException)6 MalformedCachePatternException (org.apache.oro.text.MalformedCachePatternException)6 MalformedURLException (java.net.MalformedURLException)4 BufferedReader (java.io.BufferedReader)3 FileInputStream (java.io.FileInputStream)3 InputStreamReader (java.io.InputStreamReader)3 Perl5Substitution (org.apache.oro.text.regex.Perl5Substitution)3 EOFException (java.io.EOFException)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2