Search in sources :

Example 31 with CompoundVariable

use of org.apache.jmeter.engine.util.CompoundVariable in project jmeter by apache.

the class TestRegexFunction method testComma.

@Test
public void testComma() throws Exception {
    params = new LinkedList<>();
    params.add(new CompoundVariable("<value,? field=\"(pinposition\\d+)\">(\\d+)</value>"));
    params.add(new CompoundVariable("$1$"));
    params.add(new CompoundVariable("3"));
    variable.setParameters(params);
    String match = variable.execute(result, null);
    assertEquals("pinposition3", match);
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) Test(org.junit.Test)

Example 32 with CompoundVariable

use of org.apache.jmeter.engine.util.CompoundVariable in project jmeter by apache.

the class TestRegexFunction method testVariableExtractionFromVariable6.

@Test
public void testVariableExtractionFromVariable6() throws Exception {
    params = new LinkedList<>();
    params.add(new CompoundVariable("(\\d+)\\s+(\\w+)"));
    // template
    params.add(new CompoundVariable("$2$$2$"));
    // match number
    params.add(new CompoundVariable("1"));
    // ALL separator
    params.add(new CompoundVariable("-"));
    params.add(new CompoundVariable("default"));
    params.add(new CompoundVariable("OUTVAR"));
    params.add(new CompoundVariable(INPUT_VARIABLE_NAME));
    variable.setParameters(params);
    String match = variable.execute(result, null);
    assertEquals("1", vars.getObject("OUTVAR_matchNr"));
    assertEquals("timestimes", match);
    assertEquals("timestimes", vars.getObject("OUTVAR"));
    assertEquals("123 times", vars.getObject("OUTVAR_g0"));
    assertEquals("123", vars.getObject("OUTVAR_g1"));
    assertEquals("times", vars.getObject("OUTVAR_g2"));
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) Test(org.junit.Test)

Example 33 with CompoundVariable

use of org.apache.jmeter.engine.util.CompoundVariable 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 34 with CompoundVariable

use of org.apache.jmeter.engine.util.CompoundVariable in project jmeter by apache.

the class SplitFunction method execute.

/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    JMeterVariables vars = getVariables();
    String stringToSplit = ((CompoundVariable) values[0]).execute();
    String varNamePrefix = ((CompoundVariable) values[1]).execute().trim();
    String splitString = ",";
    if (values.length > 2) {
        // Split string provided
        splitString = ((CompoundVariable) values[2]).execute();
    }
    if (log.isDebugEnabled()) {
        log.debug("Split " + stringToSplit + " using " + splitString + " into " + varNamePrefix);
    }
    // $NON-NLS-1$
    String[] parts = JOrphanUtils.split(stringToSplit, splitString, "?");
    vars.put(varNamePrefix, stringToSplit);
    // $NON-NLS-1$ 
    vars.put(varNamePrefix + "_n", Integer.toString(parts.length));
    for (int i = 1; i <= parts.length; i++) {
        if (log.isDebugEnabled()) {
            log.debug(parts[i - 1]);
        }
        // $NON-NLS-1$
        vars.put(varNamePrefix + "_" + i, parts[i - 1]);
    }
    vars.remove(varNamePrefix + "_" + (parts.length + 1));
    return stringToSplit;
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) JMeterVariables(org.apache.jmeter.threads.JMeterVariables)

Example 35 with CompoundVariable

use of org.apache.jmeter.engine.util.CompoundVariable in project jmeter by apache.

the class EvalVarFunction method execute.

/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    String variableName = ((CompoundVariable) values[0]).execute();
    final JMeterVariables vars = getVariables();
    if (vars == null) {
        log.error("Variables have not yet been defined");
        return "**ERROR - see log file**";
    }
    String variableValue = vars.get(variableName);
    CompoundVariable cv = new CompoundVariable(variableValue);
    return cv.execute();
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) JMeterVariables(org.apache.jmeter.threads.JMeterVariables)

Aggregations

CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)103 Test (org.junit.Test)77 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)16 LinkedList (java.util.LinkedList)9 JMeterContext (org.apache.jmeter.threads.JMeterContext)7 File (java.io.File)4 IOException (java.io.IOException)3 LocalDateTime (java.time.LocalDateTime)3 HashMap (java.util.HashMap)2 ReplaceStringWithFunctions (org.apache.jmeter.engine.util.ReplaceStringWithFunctions)2 InvalidVariableException (org.apache.jmeter.functions.InvalidVariableException)2 TestSampler (org.apache.jmeter.junit.stubs.TestSampler)2 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)2 StringProperty (org.apache.jmeter.testelement.property.StringProperty)2 JMeterStopThreadException (org.apache.jorphan.util.JMeterStopThreadException)2 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 DecimalFormat (java.text.DecimalFormat)1 LocalDate (java.time.LocalDate)1 ArrayList (java.util.ArrayList)1