mirror of https://github.com/apache/jmeter.git
DigestEncodeFunction: Reduce code complexity (#516)
- remove if statements - remove constant UTF_8 - remove new String
This commit is contained in:
parent
d1a5856646
commit
4ff6596a9f
|
|
@ -33,6 +33,7 @@ import org.apache.jmeter.samplers.Sampler;
|
|||
import org.apache.jmeter.util.JMeterUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* Digest Encode Function that provides computing of different SHA-XXX, can
|
||||
|
|
@ -47,7 +48,6 @@ import org.slf4j.LoggerFactory;
|
|||
public class DigestEncodeFunction extends AbstractFunction {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(DigestEncodeFunction.class);
|
||||
private static final String UTF_8 = "UTF-8";
|
||||
|
||||
/**
|
||||
* The algorithm names in this section can be specified when generating an
|
||||
|
|
@ -74,19 +74,16 @@ public class DigestEncodeFunction extends AbstractFunction {
|
|||
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
|
||||
String digestAlgorithm = values[0].execute();
|
||||
String stringToEncode = values[1].execute();
|
||||
String salt = null;
|
||||
if (values.length > 2) {
|
||||
salt = values[2].execute();
|
||||
}
|
||||
String salt = values.length > 2 ? values[2].execute() : null;
|
||||
String encodedString = null;
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance(digestAlgorithm);
|
||||
md.update(stringToEncode.getBytes(UTF_8));
|
||||
md.update(stringToEncode.getBytes(StandardCharsets.UTF_8));
|
||||
if (StringUtils.isNotEmpty(salt)) {
|
||||
md.update(salt.getBytes(UTF_8));
|
||||
md.update(salt.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
byte[] bytes = md.digest();
|
||||
encodedString = uppercase(new String(Hex.encodeHex(bytes)), values, 3);
|
||||
encodedString = uppercase(Hex.encodeHexString(bytes), values, 3);
|
||||
addVariableValue(encodedString, values, 4);
|
||||
} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
|
||||
log.error("Error calling {} function with value {}, digest algorithm {}, salt {}, ", KEY, stringToEncode,
|
||||
|
|
@ -103,12 +100,9 @@ public class DigestEncodeFunction extends AbstractFunction {
|
|||
* @return
|
||||
*/
|
||||
private String uppercase(String encodedString, CompoundVariable[] values, int index) {
|
||||
if (values.length > index) {
|
||||
String shouldUpperCase = values[index].execute();
|
||||
boolean shouldDoUpperCase = Boolean.TRUE.toString().equalsIgnoreCase(shouldUpperCase);
|
||||
if (shouldDoUpperCase) {
|
||||
return encodedString.toUpperCase();
|
||||
}
|
||||
String shouldUpperCase = values.length > index ? values[index].execute() : null;
|
||||
if (Boolean.parseBoolean(shouldUpperCase)) {
|
||||
return encodedString.toUpperCase();
|
||||
}
|
||||
return encodedString;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue