Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introducing bytes method for AbstractStringAssert #3232

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@

import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static org.assertj.core.error.ShouldBeNumeric.shouldBeNumeric;
import static org.assertj.core.error.ShouldBeNumeric.NumericType.BYTE;
import static org.assertj.core.error.ShouldBeNumeric.NumericType.DOUBLE;
import static org.assertj.core.error.ShouldBeNumeric.NumericType.FLOAT;
import static org.assertj.core.error.ShouldBeNumeric.NumericType.INTEGER;
import static org.assertj.core.error.ShouldBeNumeric.NumericType.LONG;
import static org.assertj.core.error.ShouldBeNumeric.NumericType.SHORT;
import static org.assertj.core.error.ShouldBeNumeric.shouldBeNumeric;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Base64;
import java.util.Comparator;

Expand Down Expand Up @@ -459,6 +461,67 @@ public AbstractByteAssert<?> asByte() {
}
}

/**
* Encodes the actual value as byte array using the platform's default charset, the encoded byte array becoming the new value under test.
* <p>
* Examples:
* <pre><code class='java'> assertThat("abc").bytes().isEqualTo(new byte[] {'a', 'b', 'c'});
* assertThat("").bytes().isEqualTo(new byte[0]())); </code></pre>
*
* @throws AssertionError actual string is {@code null}.
* @return a new {@link AbstractByteArrayAssert} instance whose value under test is the result of the parse.
*
* @since 3.25.0
*/
public AbstractByteArrayAssert<?> bytes() {
isNotNull();
return InstanceOfAssertFactories.BYTE_ARRAY.createAssert(actual.getBytes()).withAssertionState(myself);
}

/**
* Encodes the actual value as byte array using a specific {@link Charset}, the encoded byte array becoming the new value under test.
* <p>
* Examples:
* <pre><code class='java'> assertThat("abc").bytes(StandardCharsets.US_ASCII).isEqualTo("abc".getBytes(StandardCharsets.US_ASCII));
* assertThat("").bytes(StandardCharsets.US_ASCII).isEqualTo(new byte[0]())); </code></pre>
*
* @param charset the Charset to be used to encode the string.
* @throws NullPointerException if charset parameter is {@code null}.
* @throws AssertionError actual string is {@code null}.
* @return a new {@link AbstractByteArrayAssert} instance whose value under test is the result of the parse.
*
* @since 3.25.0
*/
public AbstractByteArrayAssert<?> bytes(Charset charset) {
isNotNull();
byte[] bytes = actual.getBytes(requireNonNull(charset, "The charset must not be null"));
return InstanceOfAssertFactories.BYTE_ARRAY.createAssert(bytes).withAssertionState(myself);
}

/**
* Encodes the actual value as byte array using a specific {@link Charset}, the encoded byte array becoming the new value under test.
* <p>
* Examples:
* <pre><code class='java'> assertThat("abc").bytes(StandardCharsets.US_ASCII).isEqualTo("abc".getBytes(StandardCharsets.US_ASCII));
* assertThat("").bytes(StandardCharsets.US_ASCII).isEqualTo(new byte[0]())); </code></pre>
*
* @param charsetName the Charset to be used to encode the string.
* @throws NullPointerException if named charset parameter is {@code null}.
* @throws AssertionError if the actual string is {@code null} or if the named charset parameter is not supported.
* @return a new {@link AbstractByteArrayAssert} instance whose value under test is the result of the parse.
*
* @since 3.25.0
*/
public AbstractByteArrayAssert<?> bytes(String charsetName) {
isNotNull();
try {
byte[] bytes = actual.getBytes(requireNonNull(charsetName, "The charsetName must not be null"));
return InstanceOfAssertFactories.BYTE_ARRAY.createAssert(bytes).withAssertionState(myself);
} catch (UnsupportedEncodingException e) {
throw failures.failure(String.format("%s is not a supported Charset", charsetName));
}
}

/**
* Parses the actual value as short, using radix 10, the parsed short becoming the new value under test.
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2023 the original author or authors.
*/
package org.assertj.core.api.string_;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.assertj.core.api.StringAssert;
import org.assertj.core.api.StringAssertBaseTest;
import org.junit.jupiter.api.Test;

/**
* @author Emanuel Trandafir
*/
class StringAssert_bytes_Test extends StringAssertBaseTest {

@Override
protected StringAssert invoke_api_method() {
// Tested below.
return null;
}

@Override
protected void verify_internal_effects() {
// Verify disabled as the bytes() cast have no internal effect.
}

@Override
public void should_return_this() {
// Test disabled as the assertion does not return this.
}

@Test
void should_encode_string_to_byte_array_for_valid_input() {
assertThat("abc").bytes().isEqualTo(new byte[] { 'a', 'b', 'c' });
}

@Test
void should_not_throw_exception_for_empty_string() {
assertThat("").bytes().isEqualTo(new byte[0]);
}

@Test
void should_throw_assertion_error_for_null_input() {
// WHEN
AssertionError assertionError = expectAssertionError(assertThat((String) null)::bytes);
// THEN
then(assertionError).hasStackTraceContaining("Expecting actual not to be null");
}

@Test
void should_encode_string_to_byte_array_for_specific_charset_and_valid_input() {
assertThat("abc").bytes(StandardCharsets.US_ASCII).isEqualTo("abc".getBytes(StandardCharsets.US_ASCII));
}

@Test
void should_not_throw_exception_for_specific_charset_and_empty_string() {
assertThat("").bytes(StandardCharsets.US_ASCII).isEqualTo(new byte[] {});
}

@Test
void should_throw_assertion_error_for_specific_charset_and_null_input() {
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat((String) null).bytes(StandardCharsets.US_ASCII));
// THEN
then(assertionError).hasStackTraceContaining("Expecting actual not to be null");
}

@Test
void should_throw_exception_for_null_charset() {
assertThatNullPointerException().isThrownBy(() -> assertThat("abc").bytes((Charset) null))
.withMessage("The charset must not be null");
}

@Test
void should_encode_string_to_byte_array_for_specific_charset_name_and_valid_input() {
assertThat("abc").bytes("UTF-8").isEqualTo("abc".getBytes(StandardCharsets.UTF_8));
}

@Test
void should_not_throw_exception_for_specific_charset_name_and_empty_string() {
assertThat("").bytes("UTF-8").isEqualTo(new byte[] {});
}

@Test
void should_throw_assertion_error_for_specific_charset_name_and_null_input() {
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat((String) null).bytes("UTF-8"));
// THEN
then(assertionError).hasStackTraceContaining("Expecting actual not to be null");
}

@Test
void should_throw_exception_for_null_charset_name() {
assertThatNullPointerException().isThrownBy(() -> assertThat("abc").bytes((String) null))
.withMessage("The charsetName must not be null");
}

@Test
void should_throw_AssertionError_for_invalid_charset_name() {
// WHEN
AssertionError assertionError = expectAssertionError(() -> assertThat("abc").bytes("UNSUPPORTED_CHARSET"));
// THEN
then(assertionError).hasMessage("UNSUPPORTED_CHARSET is not a supported Charset");
}

}