Advertisement
mnaufaldillah

SplitFunction Jmeter

Oct 23rd, 2021
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.35 KB | None | 0 0
  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to you under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  * http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17.  
  18. package org.apache.jmeter.functions;
  19.  
  20.  
  21. import java.util.ArrayList;
  22. import java.util.Collection;
  23. import java.util.List;
  24.  
  25. import org.apache.jmeter.engine.util.CompoundVariable;
  26. import org.apache.jmeter.samplers.SampleResult;
  27. import org.apache.jmeter.samplers.Sampler;
  28. import org.apache.jmeter.threads.JMeterVariables;
  29. import org.apache.jmeter.util.JMeterUtils;
  30. import org.apache.jorphan.util.JOrphanUtils;
  31. import org.slf4j.Logger;
  32. import org.slf4j.LoggerFactory;
  33.  
  34. // @see org.apache.jmeter.functions.PackageTest for unit tests
  35.  
  36. /**
  37.  * Function to split a string into variables
  38.  * <p>
  39.  * Parameters:
  40.  * <ul>
  41.  * <li>String to split</li>
  42.  * <li>Variable name prefix</li>
  43.  * <li>String to split on (optional, default is comma)</li>
  44.  * </ul>
  45.  * <p>
  46.  * Returns: the input string
  47.  * </p>
  48.  * Also sets the variables:
  49.  * <ul>
  50.  * <li>VARNAME - the input string</li>
  51.  * <li>VARNAME_n - number of fields found</li>
  52.  * <li>VARNAME_1..n - fields</li>
  53.  * </ul>
  54.  * @since 2.0.2
  55.  */
  56. public class SplitFunction extends AbstractFunction {
  57.     private static final Logger log = LoggerFactory.getLogger(SplitFunction.class);
  58.  
  59.     private static final List<String> desc = new ArrayList<>();
  60.  
  61.     private static final String KEY = "__split";// $NON-NLS-1$
  62.  
  63.     // Number of parameters expected - used to reject invalid calls
  64.     private static final int MIN_PARAMETER_COUNT = 2;
  65.  
  66.     private static final int MAX_PARAMETER_COUNT = 3;
  67.     static {
  68.         desc.add(JMeterUtils.getResString("split_function_string"));   //$NON-NLS-1$
  69.         desc.add(JMeterUtils.getResString("function_name_param"));     //$NON-NLS-1$
  70.         desc.add(JMeterUtils.getResString("split_function_separator"));//$NON-NLS-1$
  71.     }
  72.  
  73.     private Object[] values;
  74.  
  75.     public SplitFunction() {
  76.         super();
  77.     }
  78.  
  79.     /** {@inheritDoc} */
  80.     @Override
  81.     public String execute(SampleResult previousResult, Sampler currentSampler)
  82.             throws InvalidVariableException {
  83.         JMeterVariables vars = getVariables();
  84.  
  85.         String stringToSplit = ((CompoundVariable) values[0]).execute();
  86.         String varNamePrefix = ((CompoundVariable) values[1]).execute().trim();
  87.         String splitString = ",";
  88.  
  89.         if (values.length > 2) { // Split string provided
  90.             String newSplitString = ((CompoundVariable) values[2]).execute();
  91.             splitString = newSplitString.length() > 0 ? newSplitString : splitString;
  92.         }
  93.         log.debug("Split {} using {} into {}", stringToSplit, splitString, varNamePrefix);
  94.         String[] parts = JOrphanUtils.split(stringToSplit, splitString, "?");// $NON-NLS-1$
  95.  
  96.         vars.put(varNamePrefix, stringToSplit);
  97.         vars.put(varNamePrefix + "_n", Integer.toString(parts.length));// $NON-NLS-1$
  98.         for (int i = 1; i <= parts.length; i++) {
  99.             if (log.isDebugEnabled()){
  100.                 log.debug(parts[i-1]);
  101.             }
  102.             vars.put(varNamePrefix + "_" + i, parts[i - 1]);// $NON-NLS-1$
  103.         }
  104.         vars.remove(varNamePrefix + "_" + (parts.length+1));
  105.         return stringToSplit;
  106.  
  107.     }
  108.  
  109.     /** {@inheritDoc} */
  110.     @Override
  111.     public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
  112.         checkParameterCount(parameters, MIN_PARAMETER_COUNT, MAX_PARAMETER_COUNT);
  113.         values = parameters.toArray();
  114.     }
  115.  
  116.     /** {@inheritDoc} */
  117.     @Override
  118.     public String getReferenceKey() {
  119.         return KEY;
  120.     }
  121.  
  122.     /** {@inheritDoc} */
  123.     @Override
  124.     public List<String> getArgumentDesc() {
  125.         return desc;
  126.     }
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement