Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package some.jmeter.functions;
- /*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
- 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 ReplaceSJP 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 ReplaceSJP() {
- }
- /** {@inheritDoc} */
- @Override
- public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
- {
- String string = ((CompoundVariable) values[0]).execute();
- String pattern = null;
- String value = null;
- int length = values.length;
- //initializing all parameter&value pairs and replacing it in target string
- for(int i = 1; i < length; 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;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment