https://issues.apache.org/bugzilla/show_bug.cgi?id=54066

          Priority: P2
            Bug ID: 54066
          Assignee: [email protected]
           Summary: New function __Replace() for replacing some pattern in
                    string
          Severity: enhancement
    Classification: Unclassified
                OS: All
          Reporter: [email protected]
          Hardware: All
            Status: NEW
           Version: 2.8
         Component: Main
           Product: JMeter

Hello!
I want ask, what you think about implementing new function for JMeter which
provide possibility for replacing of some pattern in a string?

I wrote a little example which implement that functionality, could you look at
it?

You can use it like:
______________________________________________________________________________________________________________________________________________________________________________
Example                                                                        
                                            |Result
_________________________________________________________________________________________________________________________|____________________________________________________
${__Replace(some_xml_big_data_with_#CustomerID#_and_another_else,#CustomerID#,3.15-9.20)}
                            |some_xml_big_data_with_3.15-9.20_and_another_else
_________________________________________________________________________________________________________________________|____________________________________________________
${__Replace(some_#FormatID#_big_data_with_#CustomerID#_and_another_else,#CustomerID#,3.15-9.20,#FormatID#,xml)}
            |some_xml_big_data_with_3.15-9.20_and_another_else
_________________________________________________________________________________________________________________________|____________________________________________________
${__Replace(123456,5,7,3,9)}                                                   
                                         |129476
_________________________________________________________________________________________________________________________|____________________________________________________
${__Replace(123456,[1-2],7,3,9)}                                               
                                         |779456
_________________________________________________________________________________________________________________________|____________________________________________________
${__Replace(123456789qwerty,[1-2],7,3,9,[w],word)}                             
                                       |779456789qworderty
_________________________________________________________________________________________________________________________|____________________________________________________
in User defined variables                                                      
                                         |
string = 123456789qwerty                                                       
                                         |
${__Replace(${string},[1-2],7,3,9,[w],word)}                                   
                                         |779456789qworderty
_________________________________________________________________________________________________________________________|____________________________________________________

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.functions.AbstractFunction;
import org.apache.jmeter.functions.InvalidVariableException;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;

/**
 * Parameterize Function to replace in string array of patterns with value.
 *
 * Parameters:
 * - string
 * - pattern
 * - value
 */

public class Replace extends AbstractFunction {

    private static final List<String> desc = new LinkedList<String>();

    private static final String KEY = "__Replace";//$NON-NLS-1$

    static final String ERR_IND = "**ERR**";//$NON-NLS-1$

    private Object[] values;

    static {
        desc.add("String where replace");
        desc.add("Pattern for replace");
        desc.add("Value for replace");
    }

    private static final int MIN_PARAM_COUNT = 3;

    private static final int MAX_PARAM_COUNT = 101;

    public Replace() {
    }

    /** {@inheritDoc} */
    @Override
    public synchronized String execute(SampleResult previousResult, Sampler
currentSampler)
             {
        String string = ((CompoundVariable) values[0]).execute();
        String pattern = null;
        String value = null;
        for(int i = 1; i < (values.length-1); i = i + 2) {
            int j = i + 1;
            pattern = ((CompoundVariable) values[i]).execute();
            value = ((CompoundVariable) values[j]).execute();
            string = string.replaceAll(pattern, value);
        }
        return string;
    }


    /** {@inheritDoc} 
     * @throws InvalidVariableException */
    @Override
    public synchronized void setParameters(Collection<CompoundVariable>
parameters) throws InvalidVariableException {
        checkParameterCount(parameters, MIN_PARAM_COUNT, MAX_PARAM_COUNT);
        values = parameters.toArray();
    }

    /** {@inheritDoc} */
    @Override
    public String getReferenceKey() {
        return KEY;
    }

    /** {@inheritDoc} */
    public List<String> getArgumentDesc() {
        return desc;
    }
}

-- 
You are receiving this mail because:
You are the assignee for the bug.

Reply via email to