BAEL-5179: Introduction to HexFormat in Java 17 (#11387)
This commit is contained in:
parent
0356a2f6da
commit
683f3199bb
@ -0,0 +1,23 @@
|
|||||||
|
package com.baeldung.hexformat;
|
||||||
|
|
||||||
|
import java.util.HexFormat;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
class ByteHexadecimalConversionUnitTest {
|
||||||
|
|
||||||
|
private HexFormat hexFormat = HexFormat.of();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenInitialisedHexFormat_whenHexStringIsPassed_thenByteArrayRepresentationIsReturned() {
|
||||||
|
byte[] hexBytes = hexFormat.parseHex("ABCDEF0123456789");
|
||||||
|
assertArrayEquals(new byte[] { -85, -51, -17, 1, 35, 69, 103, -119 }, hexBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenInitialisedHexFormat_whenByteArrayIsPassed_thenHexStringRepresentationIsReturned() {
|
||||||
|
String bytesAsString = hexFormat.formatHex(new byte[] { -85, -51, -17, 1, 35, 69, 103, -119});
|
||||||
|
assertEquals("abcdef0123456789", bytesAsString);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.hexformat;
|
||||||
|
|
||||||
|
import java.util.HexFormat;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
public class PrimitiveTypeHexadecimalConversionUnitTest {
|
||||||
|
|
||||||
|
private HexFormat hexFormat = HexFormat.of();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenInitialisedHexFormat_whenPrimitiveByteIsPassed_thenHexStringRepresentationIsReturned() {
|
||||||
|
String fromByte = hexFormat.toHexDigits((byte)64);
|
||||||
|
assertEquals("40", fromByte);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenInitialisedHexFormat_whenPrimitiveLongIsPassed_thenHexStringRepresentationIsReturned() {
|
||||||
|
String fromLong = hexFormat.toHexDigits(1234_5678_9012_3456L);
|
||||||
|
assertEquals("000462d53c8abac0", fromLong);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package com.baeldung.hexformat;
|
||||||
|
|
||||||
|
import java.util.HexFormat;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
public class StringFormattingUnitTest {
|
||||||
|
|
||||||
|
private HexFormat hexFormat = HexFormat.of().withPrefix("[").withSuffix("]").withDelimiter(", ");
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInitialisedHexFormatWithFormattedStringOptions_whenByteArrayIsPassed_thenHexStringRepresentationFormattedCorrectlyIsReturned() {
|
||||||
|
assertEquals("[48], [0c], [11]", hexFormat.formatHex(new byte[] {72, 12, 17}));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.baeldung.hexformat;
|
||||||
|
|
||||||
|
import java.util.HexFormat;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
public class UppercaseLowercaseOutputUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInitialisedHexFormat_whenByteArrayIsPassed_thenLowerCaseHexStringRepresentationIsReturned() {
|
||||||
|
HexFormat hexFormat = HexFormat.of();
|
||||||
|
String bytesAsString = hexFormat.formatHex(new byte[] { -85, -51, -17, 1, 35, 69, 103, -119});
|
||||||
|
assertTrue(isLowerCase(bytesAsString));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInitialisedHexFormatWithUpperCaseOption_whenByteArrayIsPassed_thenLowerCaseHexStringRepresentationIsReturned() {
|
||||||
|
HexFormat hexFormat = HexFormat.of().withUpperCase();
|
||||||
|
String bytesAsString = hexFormat.formatHex(new byte[] { -85, -51, -17, 1, 35, 69, 103, -119});
|
||||||
|
assertTrue(isUpperCase(bytesAsString));
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isLowerCase(String str) {
|
||||||
|
char[] charArray = str.toCharArray();
|
||||||
|
for (int i=0; i < charArray.length; i++) {
|
||||||
|
if (Character.isUpperCase(charArray[i]))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isUpperCase(String str) {
|
||||||
|
char[] charArray = str.toCharArray();
|
||||||
|
for (int i=0; i < charArray.length; i++) {
|
||||||
|
if (Character.isLowerCase(charArray[i]))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user