Coverage Report - kg.apc.jmeter.functions.Base64Encode
 
Classes in this File Line Coverage Branch Coverage Complexity
Base64Encode
100%
19/19
50%
3/6
1.75
 
 1  
 package kg.apc.jmeter.functions;
 2  
 
 3  
 import org.apache.commons.codec.binary.Base64;
 4  
 import org.apache.jmeter.engine.util.CompoundVariable;
 5  
 import org.apache.jmeter.functions.AbstractFunction;
 6  
 import org.apache.jmeter.functions.InvalidVariableException;
 7  
 import org.apache.jmeter.samplers.SampleResult;
 8  
 import org.apache.jmeter.samplers.Sampler;
 9  
 import org.apache.jmeter.threads.JMeterVariables;
 10  
 
 11  
 import java.util.Arrays;
 12  
 import java.util.Collection;
 13  
 import java.util.LinkedList;
 14  
 import java.util.List;
 15  
 
 16  3
 public class Base64Encode extends AbstractFunction {
 17  
 
 18  1
     private static final List<String> desc = new LinkedList<String>();
 19  
     private static final String KEY = "__base64Encode";
 20  
 
 21  
     // Number of parameters expected - used to reject invalid calls
 22  
     private static final int MIN_PARAMETER_COUNT = 1;
 23  
     private static final int MAX_PARAMETER_COUNT = 2;
 24  
 
 25  
     static {
 26  1
         desc.add("Base64 string to be encoded");
 27  1
         desc.add("Name of variable in which to store the result (optional)");
 28  1
     }
 29  
 
 30  
     private CompoundVariable[] values;
 31  
 
 32  
     @Override
 33  
     public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
 34  
             throws InvalidVariableException {
 35  1
         String sourceString = values[0].execute();
 36  
 
 37  1
         String decodedValue = new String(Base64.encodeBase64(sourceString.getBytes()));
 38  1
         if (values.length > 1) {
 39  1
             String variableName = values[1].execute();
 40  1
             if (variableName.length() > 0) {// Allow for empty name
 41  1
                 final JMeterVariables variables = getVariables();
 42  1
                 if (variables != null) {
 43  1
                     variables.put(variableName, decodedValue);
 44  
                 }
 45  
             }
 46  
         }
 47  1
         return decodedValue;
 48  
     }
 49  
 
 50  
     @Override
 51  
     public synchronized void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
 52  1
         checkParameterCount(parameters, MIN_PARAMETER_COUNT, MAX_PARAMETER_COUNT);
 53  1
         values = parameters.toArray(new CompoundVariable[parameters.size()]);
 54  1
     }
 55  
 
 56  
     @Override
 57  
     public String getReferenceKey() {
 58  1
         return KEY;
 59  
     }
 60  
 
 61  
     @Override
 62  
     public List<String> getArgumentDesc() {
 63  1
         return desc;
 64  
     }
 65  
 }