Use HexFormat to print out byte[] properties 35/100935/5
authorivan.martiniak <ivan.martiniak@pantheon.tech>
Tue, 3 May 2022 11:32:21 +0000 (13:32 +0200)
committerIvan Martiniak <ivan.martiniak@pantheon.tech>
Thu, 5 May 2022 08:46:45 +0000 (08:46 +0000)
HexFormat.of().formatHex(byte[] arr)
 - transform byte array to hex formated string,

For comparison array formated with Arrays.toString()
[-32,79,-48,32,-22,58,105,16,-94,-40,8,0,43,48,48,-99],
with formatHex() it is string "e04fd020ea3a6910a2d808002b30309d".

The main difference is that array formatted with formatHex()
does not contain signed values, what was a condition
in the task description.

The test was required to be changed as well because it was
dependent on Arrays.toString() method.

JIRA: MDSAL-692
Change-Id: Ica9e399d61d54ac19369d3b2a9f60c9690b44128
Signed-off-by: ivan.martiniak <ivan.martiniak@pantheon.tech>
binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/CompilationTest.java
binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/CodeHelpers.java

index f3d640aa8dda14d476f6f399c6db447eee1113a3..8b36aa85060b68da256cd93eaf8a0c6a827d0e22 100644 (file)
@@ -32,6 +32,7 @@ import java.net.URLClassLoader;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.HexFormat;
 import java.util.List;
 import java.util.stream.Collectors;
 import org.junit.Test;
@@ -417,7 +418,7 @@ public class CompilationTest extends BaseCompilationTest {
         final List<Range<Integer>> lengthConstraints = new ArrayList<>();
         lengthConstraints.add(Range.closed(1, 10));
         byte[] arg = new byte[] {};
-        String expectedMsg = String.format("Invalid length: %s, expected: %s.", Arrays.toString(arg),
+        String expectedMsg = String.format("Invalid length: %s, expected: %s.", HexFormat.of().formatHex(arg),
             lengthConstraints);
         CompilationTestUtils.assertContainsRestrictionCheck(builderObj, method, expectedMsg, arg);
 
index 75e625fc1f11b33e97fc8461d540a9e1fe7e3112..e12cb35a88b4a4883a9072f7c19842ee99b05c53 100644 (file)
@@ -18,6 +18,7 @@ import com.google.common.collect.ImmutableMap;
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.HexFormat;
 import java.util.List;
 import java.util.Map;
 import java.util.NoSuchElementException;
@@ -117,8 +118,7 @@ public final class CodeHelpers {
      */
     public static void appendValue(final ToStringHelper helper, final String name, final byte[] value) {
         if (value != null) {
-            // FIXME: MDSAL-692: use hex-encoding instead
-            helper.add(name, Arrays.toString(value));
+            helper.add(name, HexFormat.of().formatHex(value));
         }
     }
 
@@ -214,8 +214,7 @@ public final class CodeHelpers {
      * @throws IllegalArgumentException always
      */
     public static void throwInvalidLength(final String expected, final byte[] actual) {
-        // FIXME: MDSAL-692: use hex-encoding instead
-        throwInvalidLength(expected, Arrays.toString(actual));
+        throwInvalidLength(expected, HexFormat.of().formatHex(actual));
     }
 
     /**