1 18 19 package org.apache.jmeter.functions; 20 21 import java.io.Serializable ; 22 import java.util.Collection ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 26 import org.apache.jmeter.engine.util.CompoundVariable; 27 import org.apache.jmeter.samplers.SampleResult; 28 import org.apache.jmeter.samplers.Sampler; 29 import org.apache.jmeter.threads.JMeterVariables; 30 import org.apache.jmeter.util.JMeterUtils; 31 import org.apache.jorphan.logging.LoggingManager; 32 import org.apache.log.Logger; 33 34 47 public class SplitFunction extends AbstractFunction implements Serializable 48 { 49 private static Logger log = LoggingManager.getLoggerForClass(); 50 51 private static final List desc = new LinkedList (); 52 private static final String KEY = "__split"; 53 54 private static final int MIN_PARAMETER_COUNT = 2; 56 private static final int MAX_PARAMETER_COUNT = 3; 57 static { 58 desc.add("String to split"); 59 desc.add("Variable name"); 60 desc.add("Split character (omit for ',')"); 61 } 62 63 private Object [] values; 64 65 public SplitFunction() 66 { 67 } 68 69 public Object clone() 70 { 71 return new SplitFunction(); 72 } 73 74 public synchronized String execute( 75 SampleResult previousResult, 76 Sampler currentSampler) 77 throws InvalidVariableException 78 { 79 JMeterVariables vars = getVariables(); 80 81 String stringToSplit = ((CompoundVariable) values[0]).execute(); 82 String varNamePrefix = ((CompoundVariable) values[1]).execute(); 83 String splitString = ","; 84 85 if (values.length > 2){ splitString = ((CompoundVariable) values[2]).execute(); 87 } 88 String parts[] = JMeterUtils.split(stringToSplit,splitString,"?"); 89 90 vars.put(varNamePrefix, stringToSplit); 91 vars.put(varNamePrefix+"_n", ""+parts.length); 92 for (int i = 1; i <= parts.length ;i++){ 93 vars.put(varNamePrefix+"_"+i,parts[i-1]); 94 } 95 return stringToSplit; 96 97 } 98 99 public void setParameters(Collection parameters) 100 throws InvalidVariableException 101 { 102 103 values = parameters.toArray(); 104 105 if ((values.length < MIN_PARAMETER_COUNT) 106 || (values.length > MAX_PARAMETER_COUNT)) 107 { 108 throw new InvalidVariableException( 109 "Parameter Count not between " 110 + MIN_PARAMETER_COUNT 111 + " & " 112 + MAX_PARAMETER_COUNT); 113 } 114 115 } 116 117 public String getReferenceKey() 118 { 119 return KEY; 120 } 121 122 public List getArgumentDesc() 123 { 124 return desc; 125 } 126 127 } | Popular Tags |