Search in sources :

Example 1 with MalformedCachePatternException

use of org.apache.oro.text.MalformedCachePatternException in project jmeter by apache.

the class RegexFunction method execute.

/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    //$NON-NLS-1$
    String valueIndex = "";
    //$NON-NLS-1$
    String defaultValue = "";
    //$NON-NLS-1$ 
    String between = "";
    //$NON-NLS-1$
    String name = "";
    //$NON-NLS-1$
    String inputVariable = "";
    Pattern searchPattern;
    Object[] tmplt;
    try {
        searchPattern = JMeterUtils.getPatternCache().getPattern(((CompoundVariable) values[0]).execute(), Perl5Compiler.READ_ONLY_MASK);
        tmplt = generateTemplate(((CompoundVariable) values[1]).execute());
        if (values.length > 2) {
            valueIndex = ((CompoundVariable) values[2]).execute();
        }
        if (valueIndex.length() == 0) {
            //$NON-NLS-1$
            valueIndex = "1";
        }
        if (values.length > 3) {
            between = ((CompoundVariable) values[3]).execute();
        }
        if (values.length > 4) {
            String dv = ((CompoundVariable) values[4]).execute();
            if (dv.length() != 0) {
                defaultValue = dv;
            }
        }
        if (values.length > 5) {
            name = ((CompoundVariable) values[5]).execute();
        }
        if (values.length > 6) {
            inputVariable = ((CompoundVariable) values[6]).execute();
        }
    } catch (MalformedCachePatternException e) {
        log.error("Malformed cache pattern:" + values[0], e);
        throw new InvalidVariableException("Malformed cache pattern:" + values[0], e);
    }
    // Relatively expensive operation, so do it once
    JMeterVariables vars = getVariables();
    if (vars == null) {
        // Can happen if called during test closedown
        return defaultValue;
    }
    if (name.length() > 0) {
        vars.put(name, defaultValue);
    }
    String textToMatch = null;
    if (inputVariable.length() > 0) {
        textToMatch = vars.get(inputVariable);
    } else if (previousResult != null) {
        textToMatch = previousResult.getResponseDataAsString();
    }
    if (textToMatch == null || textToMatch.length() == 0) {
        return defaultValue;
    }
    List<MatchResult> collectAllMatches = new ArrayList<>();
    try {
        PatternMatcher matcher = JMeterUtils.getMatcher();
        PatternMatcherInput input = new PatternMatcherInput(textToMatch);
        while (matcher.contains(input, searchPattern)) {
            MatchResult match = matcher.getMatch();
            if (match != null) {
                collectAllMatches.add(match);
            }
        }
    } finally {
        if (name.length() > 0) {
            //$NON-NLS-1$
            vars.put(name + "_matchNr", Integer.toString(collectAllMatches.size()));
        }
    }
    if (collectAllMatches.isEmpty()) {
        return defaultValue;
    }
    if (valueIndex.equals(ALL)) {
        StringBuilder value = new StringBuilder();
        Iterator<MatchResult> it = collectAllMatches.iterator();
        boolean first = true;
        while (it.hasNext()) {
            if (!first) {
                value.append(between);
            } else {
                first = false;
            }
            value.append(generateResult(it.next(), name, tmplt, vars));
        }
        return value.toString();
    } else if (valueIndex.equals(RAND)) {
        MatchResult result = collectAllMatches.get(ThreadLocalRandom.current().nextInt(collectAllMatches.size()));
        return generateResult(result, name, tmplt, vars);
    } else {
        try {
            int index = Integer.parseInt(valueIndex) - 1;
            if (index >= collectAllMatches.size()) {
                return defaultValue;
            }
            MatchResult result = collectAllMatches.get(index);
            return generateResult(result, name, tmplt, vars);
        } catch (NumberFormatException e) {
            float ratio = Float.parseFloat(valueIndex);
            MatchResult result = collectAllMatches.get((int) (collectAllMatches.size() * ratio + .5) - 1);
            return generateResult(result, name, tmplt, vars);
        }
    }
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) Pattern(org.apache.oro.text.regex.Pattern) MalformedCachePatternException(org.apache.oro.text.MalformedCachePatternException) ArrayList(java.util.ArrayList) MatchResult(org.apache.oro.text.regex.MatchResult) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) PatternMatcherInput(org.apache.oro.text.regex.PatternMatcherInput) PatternMatcher(org.apache.oro.text.regex.PatternMatcher)

Example 2 with MalformedCachePatternException

use of org.apache.oro.text.MalformedCachePatternException in project jmeter by apache.

the class HTTPSamplerBase method downloadPageResources.

/**
     * Download the resources of an HTML page.
     *
     * @param pRes
     *            result of the initial request - must contain an HTML response
     * @param container
     *            for storing the results, if any
     * @param frameDepth
     *            Depth of this target in the frame structure. Used only to
     *            prevent infinite recursion.
     * @return res if no resources exist, otherwise the "Container" result with one subsample per request issued
     */
protected HTTPSampleResult downloadPageResources(final HTTPSampleResult pRes, final HTTPSampleResult container, final int frameDepth) {
    HTTPSampleResult res = pRes;
    Iterator<URL> urls = null;
    try {
        final byte[] responseData = res.getResponseData();
        if (responseData.length > 0) {
            // Bug 39205
            final LinkExtractorParser parser = getParser(res);
            if (parser != null) {
                String userAgent = getUserAgent(res);
                urls = parser.getEmbeddedResourceURLs(userAgent, responseData, res.getURL(), res.getDataEncodingWithDefault());
            }
        }
    } catch (LinkExtractorParseException e) {
        // Don't break the world just because this failed:
        res.addSubResult(errorResult(e, new HTTPSampleResult(res)));
        setParentSampleSuccess(res, false);
    }
    HTTPSampleResult lContainer = container;
    // Iterate through the URLs and download each image:
    if (urls != null && urls.hasNext()) {
        if (lContainer == null) {
            lContainer = new HTTPSampleResult(res);
            lContainer.addRawSubResult(res);
        }
        res = lContainer;
        // Get the URL matcher
        String re = getEmbeddedUrlRE();
        Perl5Matcher localMatcher = null;
        Pattern pattern = null;
        if (re.length() > 0) {
            try {
                pattern = JMeterUtils.getPattern(re);
                // don't fetch unless pattern compiles
                localMatcher = JMeterUtils.getMatcher();
            } catch (MalformedCachePatternException e) {
                // NOSONAR
                log.warn("Ignoring embedded URL match string: " + e.getMessage());
            }
        }
        // For concurrent get resources
        final List<Callable<AsynSamplerResultHolder>> list = new ArrayList<>();
        // init with default value
        int maxConcurrentDownloads = CONCURRENT_POOL_SIZE;
        boolean isConcurrentDwn = isConcurrentDwn();
        if (isConcurrentDwn) {
            try {
                maxConcurrentDownloads = Integer.parseInt(getConcurrentPool());
            } catch (NumberFormatException nfe) {
                log.warn(// $NON-NLS-1$
                "Concurrent download resources selected, " + // $NON-NLS-1$
                "but pool size value is bad. Use default value");
            }
            // no need to use another thread, do the sample on the current thread
            if (maxConcurrentDownloads == 1) {
                log.warn("Number of parallel downloads set to 1, (sampler name=" + getName() + ")");
                isConcurrentDwn = false;
            }
        }
        while (urls.hasNext()) {
            // See catch clause below
            Object binURL = urls.next();
            try {
                URL url = (URL) binURL;
                if (url == null) {
                    log.warn("Null URL detected (should not happen)");
                } else {
                    try {
                        url = escapeIllegalURLCharacters(url);
                    } catch (Exception e) {
                        // NOSONAR
                        res.addSubResult(errorResult(new Exception(url.toString() + " is not a correct URI", e), new HTTPSampleResult(res)));
                        setParentSampleSuccess(res, false);
                        continue;
                    }
                    // I don't think localMatcher can be null here, but check just in case
                    if (pattern != null && localMatcher != null && !localMatcher.matches(url.toString(), pattern)) {
                        // we have a pattern and the URL does not match, so skip it
                        continue;
                    }
                    try {
                        url = url.toURI().normalize().toURL();
                    } catch (MalformedURLException | URISyntaxException e) {
                        res.addSubResult(errorResult(new Exception(url.toString() + " URI can not be normalized", e), new HTTPSampleResult(res)));
                        setParentSampleSuccess(res, false);
                        continue;
                    }
                    if (isConcurrentDwn) {
                        // if concurrent download emb. resources, add to a list for async gets later
                        list.add(new ASyncSample(url, HTTPConstants.GET, false, frameDepth + 1, getCookieManager(), this));
                    } else {
                        // default: serial download embedded resources
                        HTTPSampleResult binRes = sample(url, HTTPConstants.GET, false, frameDepth + 1);
                        res.addSubResult(binRes);
                        setParentSampleSuccess(res, res.isSuccessful() && (binRes == null || binRes.isSuccessful()));
                    }
                }
            } catch (ClassCastException e) {
                // NOSONAR
                res.addSubResult(errorResult(new Exception(binURL + " is not a correct URI", e), new HTTPSampleResult(res)));
                setParentSampleSuccess(res, false);
            }
        }
        // IF for download concurrent embedded resources
        if (isConcurrentDwn && !list.isEmpty()) {
            ResourcesDownloader resourcesDownloader = ResourcesDownloader.getInstance();
            try {
                // sample all resources
                final List<Future<AsynSamplerResultHolder>> retExec = resourcesDownloader.invokeAllAndAwaitTermination(maxConcurrentDownloads, list);
                CookieManager cookieManager = getCookieManager();
                // add result to main sampleResult
                for (Future<AsynSamplerResultHolder> future : retExec) {
                    // this call will not block as the futures return by invokeAllAndAwaitTermination
                    //   are either done or cancelled
                    AsynSamplerResultHolder binRes = future.get();
                    if (cookieManager != null) {
                        CollectionProperty cookies = binRes.getCookies();
                        for (JMeterProperty jMeterProperty : cookies) {
                            Cookie cookie = (Cookie) jMeterProperty.getObjectValue();
                            cookieManager.add(cookie);
                        }
                    }
                    res.addSubResult(binRes.getResult());
                    setParentSampleSuccess(res, res.isSuccessful() && (binRes.getResult() != null ? binRes.getResult().isSuccessful() : true));
                }
            } catch (InterruptedException ie) {
                // $NON-NLS-1$
                log.warn("Interrupted fetching embedded resources", ie);
                Thread.currentThread().interrupt();
            } catch (ExecutionException ee) {
                // $NON-NLS-1$
                log.warn("Execution issue when fetching embedded resources", ee);
            }
        }
    }
    return res;
}
Also used : MalformedURLException(java.net.MalformedURLException) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) ArrayList(java.util.ArrayList) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) URISyntaxException(java.net.URISyntaxException) URL(java.net.URL) Callable(java.util.concurrent.Callable) LinkExtractorParser(org.apache.jmeter.protocol.http.parser.LinkExtractorParser) ExecutionException(java.util.concurrent.ExecutionException) AsynSamplerResultHolder(org.apache.jmeter.protocol.http.sampler.ResourcesDownloader.AsynSamplerResultHolder) LinkExtractorParseException(org.apache.jmeter.protocol.http.parser.LinkExtractorParseException) CookieManager(org.apache.jmeter.protocol.http.control.CookieManager) Cookie(org.apache.jmeter.protocol.http.control.Cookie) Pattern(org.apache.oro.text.regex.Pattern) CollectionProperty(org.apache.jmeter.testelement.property.CollectionProperty) MalformedCachePatternException(org.apache.oro.text.MalformedCachePatternException) URISyntaxException(java.net.URISyntaxException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedCachePatternException(org.apache.oro.text.MalformedCachePatternException) MalformedURLException(java.net.MalformedURLException) LinkExtractorParseException(org.apache.jmeter.protocol.http.parser.LinkExtractorParseException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Future(java.util.concurrent.Future)

Example 3 with MalformedCachePatternException

use of org.apache.oro.text.MalformedCachePatternException in project jmeter by apache.

the class RegexpHTMLParser method getEmbeddedResourceURLs.

/**
     * {@inheritDoc}
     */
@Override
public Iterator<URL> getEmbeddedResourceURLs(String userAgent, byte[] html, URL baseUrl, URLCollection urls, String encoding) throws HTMLParseException {
    Pattern pattern = null;
    Perl5Matcher matcher = null;
    try {
        matcher = JMeterUtils.getMatcher();
        PatternMatcherInput input = localInput.get();
        // TODO: find a way to avoid the cost of creating a String here --
        // probably a new PatternMatcherInput working on a byte[] would do
        // better.
        input.setInput(new String(html, encoding));
        pattern = JMeterUtils.getPatternCache().getPattern(REGEXP, Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.SINGLELINE_MASK | Perl5Compiler.READ_ONLY_MASK);
        while (matcher.contains(input, pattern)) {
            MatchResult match = matcher.getMatch();
            String s;
            if (log.isDebugEnabled()) {
                log.debug("match groups " + match.groups() + " " + match.toString());
            }
            // Check for a BASE HREF:
            for (int g = 1; g <= NUM_BASE_GROUPS && g <= match.groups(); g++) {
                s = match.group(g);
                if (s != null) {
                    if (log.isDebugEnabled()) {
                        log.debug("new baseUrl: " + s + " - " + baseUrl.toString());
                    }
                    try {
                        baseUrl = ConversionUtils.makeRelativeURL(baseUrl, s);
                    } catch (MalformedURLException e) {
                        // Maybe it isn't: Ignore the exception.
                        if (log.isDebugEnabled()) {
                            log.debug("Can't build base URL from RL " + s + " in page " + baseUrl, e);
                        }
                    }
                }
            }
            for (int g = NUM_BASE_GROUPS + 1; g <= match.groups(); g++) {
                s = match.group(g);
                if (s != null) {
                    if (log.isDebugEnabled()) {
                        log.debug("group " + g + " - " + match.group(g));
                    }
                    urls.addURL(s, baseUrl);
                }
            }
        }
        return urls.iterator();
    } catch (UnsupportedEncodingException | MalformedCachePatternException e) {
        throw new HTMLParseException(e.getMessage(), e);
    } finally {
        JMeterUtils.clearMatcherMemory(matcher, pattern);
    }
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) MalformedURLException(java.net.MalformedURLException) PatternMatcherInput(org.apache.oro.text.regex.PatternMatcherInput) MalformedCachePatternException(org.apache.oro.text.MalformedCachePatternException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MatchResult(org.apache.oro.text.regex.MatchResult)

Example 4 with MalformedCachePatternException

use of org.apache.oro.text.MalformedCachePatternException 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);
    }
    // The string to check (Url or data)
    String toCheck;
    // What are we testing against?
    if (isScopeVariable()) {
        toCheck = getThreadContext().getVariables().get(getVariableName());
    } else if (isTestFieldResponseData()) {
        // (bug25052)
        toCheck = response.getResponseDataAsString();
    } else if (isTestFieldResponseDataAsDocument()) {
        toCheck = Document.getTextFromDocument(response.getResponseData());
    } else if (isTestFieldResponseCode()) {
        toCheck = response.getResponseCode();
    } else if (isTestFieldResponseMessage()) {
        toCheck = response.getResponseMessage();
    } else if (isTestFieldRequestHeaders()) {
        toCheck = response.getRequestHeaders();
    } else if (isTestFieldResponseHeaders()) {
        toCheck = response.getResponseHeaders();
    } else {
        // Assume it is the URL
        toCheck = "";
        final URL url = response.getURL();
        if (url != null) {
            toCheck = url.toString();
        }
    }
    result.setFailure(false);
    result.setError(false);
    boolean notTest = (NOT & getTestType()) > 0;
    boolean orTest = (OR & getTestType()) > 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();
    }
    boolean pass = true;
    boolean hasTrue = false;
    ArrayList<String> allCheckMessage = new ArrayList<>();
    try {
        // Get the Matcher for this thread
        Perl5Matcher localMatcher = JMeterUtils.getMatcher();
        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);
            }
            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);
                    result.setFailureMessage(getFailText(stringPattern, toCheck));
                    break;
                }
                log.debug("Passed: {}", stringPattern);
            }
        }
        if (orTest && !hasTrue) {
            StringBuilder errorMsg = new StringBuilder();
            for (String tmp : allCheckMessage) {
                errorMsg.append(tmp).append('\t');
            }
            result.setFailure(true);
            result.setFailureMessage(errorMsg.toString());
        }
    } 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) URL(java.net.URL)

Example 5 with MalformedCachePatternException

use of org.apache.oro.text.MalformedCachePatternException in project jmeter by apache.

the class RegexExtractor method process.

/**
     * Parses the response data using regular expressions and saving the results
     * into variables for use later in the test.
     *
     * @see org.apache.jmeter.processor.PostProcessor#process()
     */
@Override
public void process() {
    initTemplate();
    JMeterContext context = getThreadContext();
    SampleResult previousResult = context.getPreviousResult();
    if (previousResult == null) {
        return;
    }
    log.debug("RegexExtractor processing result");
    // Fetch some variables
    JMeterVariables vars = context.getVariables();
    String refName = getRefName();
    int matchNumber = getMatchNumber();
    final String defaultValue = getDefaultValue();
    if (defaultValue.length() > 0 || isEmptyDefaultValue()) {
        // Only replace default if it is provided or empty default value is explicitly requested
        vars.put(refName, defaultValue);
    }
    Perl5Matcher matcher = JMeterUtils.getMatcher();
    String regex = getRegex();
    Pattern pattern = null;
    try {
        pattern = JMeterUtils.getPatternCache().getPattern(regex, Perl5Compiler.READ_ONLY_MASK);
        List<MatchResult> matches = processMatches(pattern, regex, previousResult, matchNumber, vars);
        int prevCount = 0;
        String prevString = vars.get(refName + REF_MATCH_NR);
        if (prevString != null) {
            // ensure old value is not left defined
            vars.remove(refName + REF_MATCH_NR);
            try {
                prevCount = Integer.parseInt(prevString);
            } catch (NumberFormatException nfe) {
                log.warn("Could not parse number: '{}'", prevString);
            }
        }
        // Number of refName_n variable sets to keep
        int matchCount = 0;
        try {
            MatchResult match;
            if (matchNumber >= 0) {
                // Original match behaviour
                match = getCorrectMatch(matches, matchNumber);
                if (match != null) {
                    vars.put(refName, generateResult(match));
                    saveGroups(vars, refName, match);
                } else {
                    // refname has already been set to the default (if present)
                    removeGroups(vars, refName);
                }
            } else // < 0 means we save all the matches
            {
                // remove any single matches
                removeGroups(vars, refName);
                matchCount = matches.size();
                // Save the count
                vars.put(refName + REF_MATCH_NR, Integer.toString(matchCount));
                for (int i = 1; i <= matchCount; i++) {
                    match = getCorrectMatch(matches, i);
                    if (match != null) {
                        final String refName_n = new StringBuilder(refName).append(UNDERSCORE).append(i).toString();
                        vars.put(refName_n, generateResult(match));
                        saveGroups(vars, refName_n, match);
                    }
                }
            }
            // Remove any left-over variables
            for (int i = matchCount + 1; i <= prevCount; i++) {
                final String refName_n = new StringBuilder(refName).append(UNDERSCORE).append(i).toString();
                vars.remove(refName_n);
                removeGroups(vars, refName_n);
            }
        } catch (RuntimeException e) {
            log.warn("Error while generating result");
        }
    } catch (MalformedCachePatternException e) {
        log.error("Error in pattern: '{}'", regex);
    } finally {
        JMeterUtils.clearMatcherMemory(matcher, pattern);
    }
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) MalformedCachePatternException(org.apache.oro.text.MalformedCachePatternException) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MatchResult(org.apache.oro.text.regex.MatchResult) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) SampleResult(org.apache.jmeter.samplers.SampleResult)

Aggregations

MalformedCachePatternException (org.apache.oro.text.MalformedCachePatternException)6 Pattern (org.apache.oro.text.regex.Pattern)6 Perl5Matcher (org.apache.oro.text.regex.Perl5Matcher)5 MatchResult (org.apache.oro.text.regex.MatchResult)4 ArrayList (java.util.ArrayList)3 PatternMatcherInput (org.apache.oro.text.regex.PatternMatcherInput)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)2 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)2 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 LinkedList (java.util.LinkedList)1 Callable (java.util.concurrent.Callable)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)1 Cookie (org.apache.jmeter.protocol.http.control.Cookie)1