yugene

ReplaceSJP.java

Nov 9th, 2012
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.06 KB | None | 0 0
  1. package some.jmeter.functions;
  2. /*
  3.  * Licensed to the Apache Software Foundation (ASF) under one or more
  4.  * contributor license agreements.  See the NOTICE file distributed with
  5.  * this work for additional information regarding copyright ownership.
  6.  * The ASF licenses this file to You under the Apache License, Version 2.0
  7.  * (the "License"); you may not use this file except in compliance with
  8.  * the License.  You may obtain a copy of the License at
  9.  *
  10.  *   http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing, software
  13.  * distributed under the License is distributed on an "AS IS" BASIS,
  14.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15.  * See the License for the specific language governing permissions and
  16.  * limitations under the License.
  17.  *
  18.  */
  19.  
  20. import java.util.Collection;
  21. import java.util.LinkedList;
  22. import java.util.List;
  23. import org.apache.jmeter.engine.util.CompoundVariable;
  24. import org.apache.jmeter.functions.AbstractFunction;
  25. import org.apache.jmeter.functions.InvalidVariableException;
  26. import org.apache.jmeter.samplers.SampleResult;
  27. import org.apache.jmeter.samplers.Sampler;
  28.  
  29. /**
  30.  * Parameterize Function to replace in string array of patterns with value.
  31.  *
  32.  * Parameters:
  33.  * - string
  34.  * - pattern
  35.  * - value
  36.  */
  37.  
  38. public class ReplaceSJP extends AbstractFunction {
  39.  
  40.     private static final List<String> desc = new LinkedList<String>();
  41.  
  42.     private static final String KEY = "__Replace";//$NON-NLS-1$
  43.  
  44.     static final String ERR_IND = "**ERR**";//$NON-NLS-1$
  45.  
  46.     private Object[] values;
  47.  
  48.     static {
  49.         desc.add("String where replace");
  50.         desc.add("Pattern for replace");
  51.         desc.add("Value for replace");
  52.     }
  53.  
  54.     private static final int MIN_PARAM_COUNT = 3;
  55.  
  56.     private static final int MAX_PARAM_COUNT = 101;
  57.  
  58.     public ReplaceSJP() {
  59.     }
  60.  
  61.     /** {@inheritDoc} */
  62.     @Override
  63.     public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
  64.              {
  65.         String string = ((CompoundVariable) values[0]).execute();
  66.         String pattern = null;
  67.         String value = null;
  68.         int length = values.length;
  69.         //initializing all parameter&value pairs and replacing it in target string
  70.         for(int i = 1; i < length; i = i + 2) {
  71.             int j = i + 1;
  72.             pattern = ((CompoundVariable) values[i]).execute();
  73.             value = ((CompoundVariable) values[j]).execute();
  74.             string = string.replaceAll(pattern, value);
  75.         }
  76.         return string;
  77.     }
  78.  
  79.  
  80.     /** {@inheritDoc}
  81.      * @throws InvalidVariableException */
  82.     @Override
  83.     public synchronized void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
  84.         checkParameterCount(parameters, MIN_PARAM_COUNT, MAX_PARAM_COUNT);
  85.         values = parameters.toArray();
  86.     }
  87.  
  88.     /** {@inheritDoc} */
  89.     @Override
  90.     public String getReferenceKey() {
  91.         return KEY;
  92.     }
  93.  
  94.     /** {@inheritDoc} */
  95.     public List<String> getArgumentDesc() {
  96.         return desc;
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment