You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jmeter.apache.org by pm...@apache.org on 2019/10/06 20:11:34 UTC

[jmeter] branch master updated: DigestEncodeFunction: Reduce code complexity (#516)

This is an automated email from the ASF dual-hosted git repository.

pmouawad pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jmeter.git


The following commit(s) were added to refs/heads/master by this push:
     new 4ff6596  DigestEncodeFunction: Reduce code complexity (#516)
4ff6596 is described below

commit 4ff6596a9f2efbcc90ad5f5f70ca089d1b6eb3d1
Author: jmetertea <33...@users.noreply.github.com>
AuthorDate: Sun Oct 6 23:11:30 2019 +0300

    DigestEncodeFunction: Reduce code complexity (#516)
    
    - remove if statements
    - remove constant UTF_8
    - remove new String
---
 .../jmeter/functions/DigestEncodeFunction.java     | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)

diff --git a/src/functions/src/main/java/org/apache/jmeter/functions/DigestEncodeFunction.java b/src/functions/src/main/java/org/apache/jmeter/functions/DigestEncodeFunction.java
index 1f902d5..27cdccc 100644
--- a/src/functions/src/main/java/org/apache/jmeter/functions/DigestEncodeFunction.java
+++ b/src/functions/src/main/java/org/apache/jmeter/functions/DigestEncodeFunction.java
@@ -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;
     }