Search in sources :

Example 96 with Perl5Matcher

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();
}
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)

Example 97 with Perl5Matcher

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;
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher)

Example 98 with Perl5Matcher

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;
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher)

Example 99 with Perl5Matcher

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;
}
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) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 100 with Perl5Matcher

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;
    }
}
Also used : HTTPFileArgs(org.apache.jmeter.protocol.http.util.HTTPFileArgs) Arrays(java.util.Arrays) HeaderManager(org.apache.jmeter.protocol.http.control.HeaderManager) MetricUtils(org.apache.jmeter.report.utils.MetricUtils) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) StringProperty(org.apache.jmeter.testelement.property.StringProperty) AuthManager(org.apache.jmeter.protocol.http.control.AuthManager) LoggerFactory(org.slf4j.LoggerFactory) SampleResult(org.apache.jmeter.samplers.SampleResult) TestElement(org.apache.jmeter.testelement.TestElement) StringUtils(org.apache.commons.lang3.StringUtils) DirectAccessByteArrayOutputStream(org.apache.jmeter.protocol.http.util.DirectAccessByteArrayOutputStream) Future(java.util.concurrent.Future) BooleanProperty(org.apache.jmeter.testelement.property.BooleanProperty) Map(java.util.Map) AsynSamplerResultHolder(org.apache.jmeter.protocol.http.sampler.ResourcesDownloader.AsynSamplerResultHolder) TestStateListener(org.apache.jmeter.testelement.TestStateListener) TestElementProperty(org.apache.jmeter.testelement.property.TestElementProperty) Cookie(org.apache.jmeter.protocol.http.control.Cookie) PatternSyntaxException(java.util.regex.PatternSyntaxException) HTTPFileArg(org.apache.jmeter.protocol.http.util.HTTPFileArg) Predicate(java.util.function.Predicate) ConfigTestElement(org.apache.jmeter.config.ConfigTestElement) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Pattern(org.apache.oro.text.regex.Pattern) Set(java.util.Set) ConversionUtils(org.apache.jmeter.protocol.http.util.ConversionUtils) CollectionProperty(org.apache.jmeter.testelement.property.CollectionProperty) LoopIterationEvent(org.apache.jmeter.engine.event.LoopIterationEvent) IOUtils(org.apache.commons.io.IOUtils) EncoderCache(org.apache.jmeter.protocol.http.util.EncoderCache) List(java.util.List) AbstractSampler(org.apache.jmeter.samplers.AbstractSampler) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Argument(org.apache.jmeter.config.Argument) DNSCacheManager(org.apache.jmeter.protocol.http.control.DNSCacheManager) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HTTPConstants(org.apache.jmeter.protocol.http.util.HTTPConstants) JMeterUtils(org.apache.jmeter.util.JMeterUtils) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CookieManager(org.apache.jmeter.protocol.http.control.CookieManager) MalformedCachePatternException(org.apache.oro.text.MalformedCachePatternException) MessageDigest(java.security.MessageDigest) JOrphanUtils(org.apache.jorphan.util.JOrphanUtils) Callable(java.util.concurrent.Callable) HTTPConstantsInterface(org.apache.jmeter.protocol.http.util.HTTPConstantsInterface) ThreadListener(org.apache.jmeter.testelement.ThreadListener) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) JMeterContext(org.apache.jmeter.threads.JMeterContext) KeystoreConfig(org.apache.jmeter.config.KeystoreConfig) Entry(org.apache.jmeter.samplers.Entry) Arguments(org.apache.jmeter.config.Arguments) JMeterContextService(org.apache.jmeter.threads.JMeterContextService) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) CacheManager(org.apache.jmeter.protocol.http.control.CacheManager) OutputStream(java.io.OutputStream) PrintStream(java.io.PrintStream) BaseParser(org.apache.jmeter.protocol.http.parser.BaseParser) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) MalformedURLException(java.net.MalformedURLException) PropertyIterator(org.apache.jmeter.testelement.property.PropertyIterator) LinkExtractorParseException(org.apache.jmeter.protocol.http.parser.LinkExtractorParseException) IOException(java.io.IOException) HTTPArgument(org.apache.jmeter.protocol.http.util.HTTPArgument) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) LinkExtractorParser(org.apache.jmeter.protocol.http.parser.LinkExtractorParser) ExecutionException(java.util.concurrent.ExecutionException) Replaceable(org.apache.jmeter.gui.Replaceable) Closeable(java.io.Closeable) TestIterationListener(org.apache.jmeter.testelement.TestIterationListener) Collections(java.util.Collections) IntegerProperty(org.apache.jmeter.testelement.property.IntegerProperty) InputStream(java.io.InputStream) Pattern(org.apache.oro.text.regex.Pattern) MalformedCachePatternException(org.apache.oro.text.MalformedCachePatternException) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Aggregations

Perl5Matcher (org.apache.oro.text.regex.Perl5Matcher)102 Pattern (org.apache.oro.text.regex.Pattern)78 MatchResult (org.apache.oro.text.regex.MatchResult)39 Perl5Compiler (org.apache.oro.text.regex.Perl5Compiler)38 MalformedPatternException (org.apache.oro.text.regex.MalformedPatternException)37 PatternMatcher (org.apache.oro.text.regex.PatternMatcher)32 PatternMatcherInput (org.apache.oro.text.regex.PatternMatcherInput)22 PatternCompiler (org.apache.oro.text.regex.PatternCompiler)20 ArrayList (java.util.ArrayList)17 IOException (java.io.IOException)8 MalformedCachePatternException (org.apache.oro.text.MalformedCachePatternException)7 Perl5Substitution (org.apache.oro.text.regex.Perl5Substitution)7 MalformedURLException (java.net.MalformedURLException)5 PatternCacheLRU (org.apache.oro.text.PatternCacheLRU)5 PluginException (org.apache.wiki.api.exceptions.PluginException)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 NoSuchElementException (java.util.NoSuchElementException)3 SampleResult (org.apache.jmeter.samplers.SampleResult)3 BufferedReader (java.io.BufferedReader)2 EOFException (java.io.EOFException)2