Coverage Report - kg.apc.jmeter.functions.MD5
 
Classes in this File Line Coverage Branch Coverage Complexity
MD5
90%
20/22
50%
2/4
1.8
 
 1  
 package kg.apc.jmeter.functions;
 2  
 
 3  
 import org.apache.jmeter.engine.util.CompoundVariable;
 4  
 import org.apache.jmeter.functions.AbstractFunction;
 5  
 import org.apache.jmeter.functions.InvalidVariableException;
 6  
 import org.apache.jmeter.samplers.SampleResult;
 7  
 import org.apache.jmeter.samplers.Sampler;
 8  
 import org.apache.jmeter.threads.JMeterVariables;
 9  
 import org.apache.jorphan.util.JOrphanUtils;
 10  
 
 11  
 import java.security.MessageDigest;
 12  
 import java.security.NoSuchAlgorithmException;
 13  
 import java.util.Collection;
 14  
 import java.util.LinkedList;
 15  
 import java.util.List;
 16  
 
 17  
 public class MD5 extends AbstractFunction {
 18  
 
 19  1
     private static final List<String> desc = new LinkedList<String>();
 20  
     private static final String KEY = "__MD5";
 21  
 
 22  
     static {
 23  1
         desc.add("String to calculate MD5 hash");
 24  1
         desc.add("Name of variable in which to store the result (optional)");
 25  1
     }
 26  
 
 27  
     private Object[] values;
 28  
 
 29  
     /**
 30  
      * No-arg constructor.
 31  
      */
 32  4
     public MD5() {
 33  4
     }
 34  
 
 35  
     /**
 36  
      * {@inheritDoc}
 37  
      */
 38  
     @Override
 39  
     public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
 40  
             throws InvalidVariableException {
 41  1
         JMeterVariables vars = getVariables();
 42  1
         String str = ((CompoundVariable) values[0]).execute();
 43  
         MessageDigest digest;
 44  
         try {
 45  1
             digest = MessageDigest.getInstance("md5");
 46  0
         } catch (NoSuchAlgorithmException ex) {
 47  0
             return "Error creating digest: " + ex;
 48  1
         }
 49  
 
 50  1
         String res = JOrphanUtils.baToHexString(digest.digest(str.getBytes()));
 51  
 
 52  1
         if (vars != null && values.length > 1) {
 53  1
             String varName = ((CompoundVariable) values[1]).execute().trim();
 54  1
             vars.put(varName, res);
 55  
         }
 56  
 
 57  1
         return res;
 58  
     }
 59  
 
 60  
     /**
 61  
      * {@inheritDoc}
 62  
      */
 63  
     @Override
 64  
     public synchronized void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
 65  2
         checkMinParameterCount(parameters, 1);
 66  2
         values = parameters.toArray();
 67  2
     }
 68  
 
 69  
     /**
 70  
      * {@inheritDoc}
 71  
      */
 72  
     @Override
 73  
     public String getReferenceKey() {
 74  1
         return KEY;
 75  
     }
 76  
 
 77  
     /**
 78  
      * {@inheritDoc}
 79  
      */
 80  
     @Override
 81  
     public List<String> getArgumentDesc() {
 82  1
         return desc;
 83  
     }
 84  
 }