Skip to content

Commit ccff783

Browse files
AndreasMagerjoel-costigliola
authored andcommitted
Add asHexString() to AbstractByteArrayAssert.
Fixes assertj#1839
1 parent a639940 commit ccff783

File tree

4 files changed

+137
-1
lines changed

4 files changed

+137
-1
lines changed

src/main/java/org/assertj/core/api/AbstractByteArrayAssert.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
*/
1313
package org.assertj.core.api;
1414

15+
import static org.assertj.core.api.Assertions.assertThat;
16+
import static org.assertj.core.util.Hexadecimals.toHexString;
17+
1518
import java.util.Comparator;
1619

1720
import org.assertj.core.data.Index;
@@ -910,4 +913,30 @@ public SELF containsAnyOf(int... values) {
910913
return myself;
911914
}
912915

916+
/**
917+
* Converts the actual byte array under test to an hexadecimal String and returns assertions for the computed String allowing String specific assertions from this call.
918+
* <p>
919+
* The Hex String representation is in upper case.
920+
* <p>
921+
* Example :
922+
* <pre><code class='java'> byte[] bytes = new byte[] { -1, 0, 1 };
923+
*
924+
* // assertions will pass
925+
* assertThat(bytes).asHexString()
926+
* .startsWith("FF")
927+
* .isEqualTo("FF0001");
928+
*
929+
* // assertion will fail
930+
* assertThat(bytes).asHexString()
931+
* .isEqualTo("FF0000");</code></pre>
932+
*
933+
* @return a String assertion object
934+
*
935+
* @since 3.16.0
936+
*/
937+
@CheckReturnValue
938+
public AbstractStringAssert<?> asHexString() {
939+
objects.assertNotNull(info, actual);
940+
return assertThat(toHexString(actual));
941+
}
913942
}

src/main/java/org/assertj/core/api/SoftProxies.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class SoftProxies {
4141

4242
private static final Junction<MethodDescription> METHODS_CHANGING_THE_OBJECT_UNDER_TEST = methodsNamed("asInstanceOf").or(named("asList"))
4343
.or(named("asString"))
44+
.or(named("asHexString"))
4445
.or(named("decodedAsBase64"))
4546
.or(named("extracting"))
4647
.or(named("extractingByKey"))

src/main/java/org/assertj/core/util/Hexadecimals.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ public static String byteToHexString(byte b) {
2424
return new String(new char[] { HEX_ARRAY[v >>> 4], HEX_ARRAY[v & 0x0F] });
2525
}
2626

27-
private Hexadecimals() {
27+
public static String toHexString(byte... bytes) {
28+
StringBuilder stringBuilder = new StringBuilder();
29+
for (byte b : bytes)
30+
stringBuilder.append(byteToHexString(b));
2831

32+
return stringBuilder.toString();
2933
}
3034

35+
private Hexadecimals() {}
3136
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2020 the original author or authors.
12+
*/
13+
package org.assertj.core.api.bytearray;
14+
15+
import static org.assertj.core.api.Assertions.assertThat;
16+
import static org.assertj.core.api.Assertions.assertThatCode;
17+
import static org.assertj.core.api.Assumptions.assumeThat;
18+
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;
19+
import static org.assertj.core.util.AssertionsUtil.expectAssumptionViolatedException;
20+
21+
import org.assertj.core.api.SoftAssertions;
22+
import org.assertj.core.error.AssertJMultipleFailuresError;
23+
import org.junit.jupiter.api.DisplayName;
24+
import org.junit.jupiter.api.Test;
25+
import org.opentest4j.AssertionFailedError;
26+
27+
/**
28+
* Tests for <code>{@link org.assertj.core.api.ByteArrayAssert#asHexString()}</code>.
29+
*/
30+
@DisplayName("ByteArrayAssert asHexString")
31+
public class ByteArrayAssert_asHexString_Test {
32+
33+
private static final byte[] BYTES = new byte[] { -1, 0, 1 };
34+
35+
@Test
36+
public void should_pass() {
37+
// GIVEN
38+
// WHEN / THEN
39+
assertThat(BYTES).asHexString()
40+
.startsWith("FF")
41+
.isEqualTo("FF0001");
42+
}
43+
44+
@Test
45+
public void should_fail_if_actual_does_not_match() {
46+
// GIVEN
47+
byte[] actual = new byte[] { -1, 0, 1 };
48+
// WHEN
49+
AssertionError assertionError = expectAssertionError(() -> assertThat(actual).asHexString().isEqualTo("010203"));
50+
// THEN
51+
assertThat(assertionError).hasMessageContainingAll("Expecting:",
52+
"<\"FF0001\">",
53+
"to be equal to:",
54+
"<\"010203\">",
55+
"but was not.")
56+
.isExactlyInstanceOf(AssertionFailedError.class);
57+
}
58+
59+
@Test
60+
public void should_pass_with_soft_assertions() {
61+
// GIVEN
62+
SoftAssertions softly = new SoftAssertions();
63+
// WHEN / THEN
64+
softly.assertThat(BYTES).asHexString().isEqualTo("FF0001");
65+
softly.assertAll();
66+
}
67+
68+
@Test
69+
public void should_fail_with_soft_assertions_capturing_all_errors() {
70+
// GIVEN
71+
SoftAssertions softly = new SoftAssertions();
72+
// WHEN
73+
softly.assertThat(BYTES)
74+
.asHexString()
75+
.isEqualTo("010203")
76+
.isBlank();
77+
AssertionError assertionError = expectAssertionError(softly::assertAll);
78+
// THEN
79+
assertThat(assertionError).hasMessageContainingAll("Multiple Failures (2 failures)",
80+
"-- failure 1 --",
81+
"Expecting:",
82+
"<\"FF0001\">",
83+
"to be equal to:",
84+
"<\"010203\">",
85+
"but was not.",
86+
"-- failure 2 --",
87+
"Expecting blank but was:<\"FF0001\">")
88+
.isExactlyInstanceOf(AssertJMultipleFailuresError.class);
89+
}
90+
91+
@Test
92+
public void should_ignore_test_when_assumption_for_internally_created_hex_string_assertion_fails() {
93+
expectAssumptionViolatedException(() -> assumeThat(BYTES).asHexString().isEqualTo("other"));
94+
}
95+
96+
@Test
97+
public void should_run_test_when_assumption_for_internally_created_string_passes() {
98+
assertThatCode(() -> assumeThat(BYTES).asHexString().startsWith("FF")).doesNotThrowAnyException();
99+
}
100+
101+
}

0 commit comments

Comments
 (0)