|
| 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