Top Description Fields Constructors Methods
org.assertj.core.api

public abstract Class AbstractInputStreamAssert<SELF extends AbstractInputStreamAssert<SELF, ACTUAL>, ACTUAL extends InputStream>

extends AbstractAssert<SELF extends AbstractInputStreamAssert<SELF, ACTUAL>, ACTUAL extends InputStream>
Class Inheritance
Known Direct Subclasses
org.assertj.core.api.InputStreamAssert
Type Parameters
<SELF>
the "self" type of this assertion class. Please read "Emulating 'self types' using Java Generics to simplify fluent API implementation" for more details.
<ACTUAL>
the type of the "actual" value.
Static Imports
java.util.Objects.requireNonNull, org.assertj.core.api.Assertions.assertThat, org.assertj.core.error.ShouldBeEmpty.shouldBeEmpty, .ShouldNotBeEmpty.shouldNotBeEmpty

Base class for all implementations of assertions for InputStreams.
Authors
Matthieu Baechler, Mikhail Mazursky, Stefan Birkner

Field Summary

Modifier and TypeField and Description
pack-priv InputStreams
Inherited from org.assertj.core.api.AbstractAssert:
actualassertionErrorCreatorconditionscustomRepresentationinfomyselfobjectsprintAssertionsDescriptionthrowUnsupportedExceptionOnEquals

Constructor Summary

AccessConstructor and Description
protected
AbstractInputStreamAssert(ACTUAL actual, Class<?> selfType)

Method Summary

Modifier and TypeMethod and Description
private void
private void
public AbstractStringAssert<?>

Returns:

a string assertion object.
asString
(Charset
the Charset to interpret the InputStream's content to a String
charset
)

Converts the content of the actual InputStream to a String by decoding its bytes using the given charset and returns assertions for the computed String allowing String specific assertions from this call.

public SELF

Returns:

this assertion object.
hasBinaryContent
(byte[]
the expected binary content to compare the actual InputStream's content to.
expected
)

Verifies that the binary content of the actual InputStream is exactly equal to the given one.

public SELF

Returns:

this assertion object.
hasContent
(String
the given String to compare the actual InputStream to.
expected
)

Verifies that the content of the actual InputStream is equal to the given String except for newlines wich are ignored.

public SELF

Returns:

this assertion object.
hasContentEqualTo
(InputStream
the given InputStream to compare the actual InputStream to.
expected
)

Deprecated use hasSameContentAs(InputStream) instead
Verifies that the content of the actual InputStream is equal to the content of the given one.
public SELF

Returns:

this assertion object.
hasDigest
(MessageDigest
the MessageDigest used to calculate the digests.
digest
,
byte[]
the expected binary content to compare the actual InputStream's digest to.
expected
)

Verifies that the tested InputStream digest (calculated with the specified MessageDigest) is equal to the given one.

public SELF

Returns:

this assertion object.
hasDigest
(MessageDigest
the MessageDigest used to calculate the digests.
digest
,
String
the expected binary content to compare the actual InputStream's digest to.
expected
)

Verifies that the tested InputStream digest (calculated with the specified MessageDigest) is equal to the given one.

public SELF

Returns:

this assertion object.
hasDigest
(String
the algorithm used to calculate the digests.
algorithm
,
byte[]
the expected binary content to compare the actual InputStream's content to.
expected
)

Verifies that the tested InputStream digest (calculated with the specified algorithm) is equal to the given one.

public SELF

Returns:

this assertion object.
hasDigest
(String
the algorithm used to calculate the digests.
algorithm
,
String
the expected binary content to compare the actual InputStream's content to.
expected
)

Verifies that the tested InputStream digest (calculated with the specified algorithm) is equal to the given one.

public SELF

Returns:

this assertion object.
hasSameContentAs
(InputStream
the given InputStream to compare the actual InputStream to.
expected
)

Verifies that the content of the actual InputStream is equal to the content of the given one.

public SELF

Returns:

this assertion object.
isEmpty
()

Verifies that the content of the actual InputStream is empty.

public SELF

Returns:

this assertion object.
isNotEmpty
()

Verifies that the content of the actual InputStream is not empty.

private String
Inherited from org.assertj.core.api.AbstractAssert:
areEqualasInstanceOfasListassertionErrorasStringdescribedAsdescriptionTextdoesNotHavedoesNotHaveSameClassAsdoesNotHaveSameHashCodeAsdoesNotHaveToStringdoesNotHaveToStringequalsextractingextractingfailurefailureWithActualExpectedfailWithActualExpectedAndMessagefailWithMessagegetWritableAssertionInfohashashCodehasSameClassAshasSameHashCodeAshasToStringhasToStringinBinaryinHexadecimalisisElementOfCustomAssertisEqualToisExactlyInstanceOfisInisInisInstanceOfisInstanceOfAnyisInstanceOfSatisfyingisNotisNotEqualToisNotExactlyInstanceOfisNotInisNotInisNotInstanceOfisNotInstanceOfAnyisNotNullisNotOfAnyClassInisNotSameAsisNullisOfAnyClassInisSameAsmatchesmatchesnewListAssertInstanceoverridingErrorMessageoverridingErrorMessagesatisfiessatisfiessatisfiessatisfiesAnyOfsatisfiesAnyOfsatisfiesAnyOfForProxysatisfiesForProxysetCustomRepresentationsetDescriptionConsumersetPrintAssertionsDescriptionthrowAssertionErrorusingComparatorusingComparatorusingDefaultComparatorusingRecursiveAssertionusingRecursiveAssertionusingRecursiveComparisonusingRecursiveComparisonwithAssertionStatewithFailMessagewithFailMessagewithRepresentationwithThreadDumpOnError

Field Detail

inputStreamsback to summary
pack-priv InputStreams inputStreams
Annotations
@VisibleForTesting

Constructor Detail

AbstractInputStreamAssertback to summary
protected AbstractInputStreamAssert(ACTUAL actual, Class<?> selfType)

Method Detail

assertIsEmptyback to summary
private void assertIsEmpty()
assertIsNotEmptyback to summary
private void assertIsNotEmpty()
asStringback to summary
public AbstractStringAssert<?> asString(Charset charset)

Converts the content of the actual InputStream to a String by decoding its bytes using the given charset and returns assertions for the computed String allowing String specific assertions from this call.

Example :

 InputStream abcInputStream = new ByteArrayInputStream("abc".getBytes());

// assertion succeeds
assertThat(abcInputStream).asString(UTF_8)
                          .startsWith("a");

// assertion fails
assertThat(abcInputStream).asString(UTF_8)
                          .startsWith("e");
Parameters
charset:Charset

the Charset to interpret the InputStream's content to a String

Returns:AbstractStringAssert<?>

a string assertion object.

Annotations
@CheckReturnValue
Exceptions
NullPointerException:
if the given Charset is null.
AssertionError:
if the actual InputStream is null.
InputStreamsException:
if an I/O error occurs.
Since
3.20.0
hasBinaryContentback to summary
public SELF hasBinaryContent(byte[] expected)

Verifies that the binary content of the actual InputStream is exactly equal to the given one.

Example:

 InputStream inputStream = new ByteArrayInputStream(new byte[] {1, 2});

// assertion will pass
assertThat(inputStream).hasContent(new byte[] {1, 2});

// assertions will fail
assertThat(inputStream).hasBinaryContent(new byte[] { });
assertThat(inputStream).hasBinaryContent(new byte[] {0, 0});
Parameters
expected:byte[]

the expected binary content to compare the actual InputStream's content to.

Returns:SELF

this assertion object.

Exceptions
NullPointerException:
if the given content is null.
AssertionError:
  • if the actual InputStream is null.
  • if the content of the actual InputStream is not equal to the given binary content.
InputStreamsException:
if an I/O error occurs.
Since
3.16.0
hasContentback to summary
public SELF hasContent(String expected)

Verifies that the content of the actual InputStream is equal to the given String except for newlines wich are ignored.

This will change in AssertJ 4.0 where newlines will be taken into account, in the meantime, to get this behavior one can use asString(Charset) and then chain with AbstractStringAssert#isEqualTo(String).

Example:

 // assertion will pass
assertThat(new ByteArrayInputStream("a".getBytes())).hasContent("a");

// assertions will fail
assertThat(new ByteArrayInputStream("a".getBytes())).hasContent("");
assertThat(new ByteArrayInputStream("a".getBytes())).hasContent("ab");
Parameters
expected:String

the given String to compare the actual InputStream to.

Returns:SELF

this assertion object.

Exceptions
NullPointerException:
if the given String is null.
AssertionError:
  • if the actual InputStream is null.
  • if the content of the actual InputStream is not equal to the given String.
InputStreamsException:
if an I/O error occurs.
Since
3.11.0
hasContentEqualToback to summary
public SELF hasContentEqualTo(InputStream expected)

Deprecated

use hasSameContentAs(InputStream) instead

Verifies that the content of the actual InputStream is equal to the content of the given one.

Parameters
expected:InputStream

the given InputStream to compare the actual InputStream to.

Returns:SELF

this assertion object.

Annotations
@Deprecated
Exceptions
NullPointerException:
if the given InputStream is null.
AssertionError:
  • if the actual InputStream is null.
  • if the content of the actual InputStream is not equal to the content of the given one.
InputStreamsException:
if an I/O error occurs.
hasDigestback to summary
public SELF hasDigest(MessageDigest digest, byte[] expected)

Verifies that the tested InputStream digest (calculated with the specified MessageDigest) is equal to the given one.

Examples:

 // assume that assertj-core-2.9.0.jar was downloaded from https://repo1.maven.org/maven2/org/assertj/assertj-core/2.9.0/assertj-core-2.9.0.jar
InputStream tested = new FileInputStream(new File("assertj-core-2.9.0.jar"));

// The following assertions succeed:
assertThat(tested).hasDigest(MessageDigest.getInstance("SHA1"), new byte[]{92, 90, -28, 91, 88, -15, 32, 35, -127, 122, -66, 73, 36, 71, -51, -57, -111, 44, 26, 44});
assertThat(tested).hasDigest(MessageDigest.getInstance("MD5"), new byte[]{-36, -77, 1, 92, -46, -124, 71, 100, 76, -127, 10, -13, 82, -125, 44, 25});

// The following assertions fail:
assertThat(tested).hasDigest(MessageDigest.getInstance("SHA1"), "93b9ced2ee5b3f0f4c8e640e77470dab031d4cad".getBytes());
assertThat(tested).hasDigest(MessageDigest.getInstance("MD5"), "3735dff8e1f9df0492a34ef075205b8f".getBytes());
Parameters
digest:MessageDigest

the MessageDigest used to calculate the digests.

expected:byte[]

the expected binary content to compare the actual InputStream's digest to.

Returns:SELF

this assertion object.

Exceptions
NullPointerException:
  • if the given algorithm is null.
  • if the given digest is null.
AssertionError:
  • if the actual InputStream is null.
  • if the actual InputStream is not readable.
  • if the content of the tested InputStream's digest is not equal to the given one.
InputStreamsException:
if an I/O error occurs.
Since
3.11.0
hasDigestback to summary
public SELF hasDigest(MessageDigest digest, String expected)

Verifies that the tested InputStream digest (calculated with the specified MessageDigest) is equal to the given one.

Examples:

 // assume that assertj-core-2.9.0.jar was downloaded from https://repo1.maven.org/maven2/org/assertj/assertj-core/2.9.0/assertj-core-2.9.0.jar
InputStream tested = new FileInputStream(new File("assertj-core-2.9.0.jar"));

// The following assertions succeed:
assertThat(tested).hasDigest(MessageDigest.getInstance("SHA1"), "5c5ae45b58f12023817abe492447cdc7912c1a2c");
assertThat(tested).hasDigest(MessageDigest.getInstance("MD5"), "dcb3015cd28447644c810af352832c19");

// The following assertions fail:
assertThat(tested).hasDigest(MessageDigest.getInstance("SHA1"), "93b9ced2ee5b3f0f4c8e640e77470dab031d4cad");
assertThat(tested).hasDigest(MessageDigest.getInstance("MD5"), "3735dff8e1f9df0492a34ef075205b8f");
Parameters
digest:MessageDigest

the MessageDigest used to calculate the digests.

expected:String

the expected binary content to compare the actual InputStream's digest to.

Returns:SELF

this assertion object.

Exceptions
NullPointerException:
  • if the given algorithm is null.
  • if the given digest is null.
AssertionError:
  • if the actual InputStream is null.
  • if the actual InputStream is not readable.
  • if the content of the tested InputStream's digest is not equal to the given one.
InputStreamsException:
if an I/O error occurs.
Since
3.11.0
hasDigestback to summary
public SELF hasDigest(String algorithm, byte[] expected)

Verifies that the tested InputStream digest (calculated with the specified algorithm) is equal to the given one.

Examples:

 // assume that assertj-core-2.9.0.jar was downloaded from https://repo1.maven.org/maven2/org/assertj/assertj-core/2.9.0/assertj-core-2.9.0.jar
InputStream tested = new FileInputStream(new File("assertj-core-2.9.0.jar"));

// The following assertion succeeds:
assertThat(tested).hasDigest("SHA1", new byte[]{92, 90, -28, 91, 88, -15, 32, 35, -127, 122, -66, 73, 36, 71, -51, -57, -111, 44, 26, 44});
assertThat(tested).hasDigest("MD5", new byte[]{-36, -77, 1, 92, -46, -124, 71, 100, 76, -127, 10, -13, 82, -125, 44, 25});

// The following assertion fails:
assertThat(tested).hasDigest("SHA1", "93b9ced2ee5b3f0f4c8e640e77470dab031d4cad".getBytes());
assertThat(tested).hasDigest("MD5", "3735dff8e1f9df0492a34ef075205b8f".getBytes()); 
Parameters
algorithm:String

the algorithm used to calculate the digests.

expected:byte[]

the expected binary content to compare the actual InputStream's content to.

Returns:SELF

this assertion object.

Exceptions
NullPointerException:
  • if the given algorithm is null.
  • if the given digest is null.
AssertionError:
  • if the actual InputStream is null.
  • if the actual InputStream is not readable.
  • if the content of the tested InputStream's digest is not equal to the given one.
InputStreamsException:
if an I/O error occurs.
Since
3.11.0
hasDigestback to summary
public SELF hasDigest(String algorithm, String expected)

Verifies that the tested InputStream digest (calculated with the specified algorithm) is equal to the given one.

Examples:

 // assume that assertj-core-2.9.0.jar was downloaded from https://repo1.maven.org/maven2/org/assertj/assertj-core/2.9.0/assertj-core-2.9.0.jar
InputStream tested = new FileInputStream(new File("assertj-core-2.9.0.jar"));

// The following assertion succeeds:
assertThat(tested).hasDigest("SHA1", "5c5ae45b58f12023817abe492447cdc7912c1a2c");
assertThat(tested).hasDigest("MD5", "dcb3015cd28447644c810af352832c19");

// The following assertion fails:
assertThat(tested).hasDigest("SHA1", "93b9ced2ee5b3f0f4c8e640e77470dab031d4cad");
assertThat(tested).hasDigest("MD5", "3735dff8e1f9df0492a34ef075205b8f"); 
Parameters
algorithm:String

the algorithm used to calculate the digests.

expected:String

the expected binary content to compare the actual InputStream's content to.

Returns:SELF

this assertion object.

Exceptions
NullPointerException:
  • if the given algorithm is null.
  • if the given digest is null.
AssertionError:
  • if the actual InputStream is null.
  • if the actual InputStream is not readable.
  • if the content of the tested InputStream's digest is not equal to the given one.
InputStreamsException:
if an I/O error occurs.
Since
3.11.0
hasSameContentAsback to summary
public SELF hasSameContentAs(InputStream expected)

Verifies that the content of the actual InputStream is equal to the content of the given one.

Example:

 // assertion will pass
assertThat(new ByteArrayInputStream(new byte[] {0xa})).hasSameContentAs(new ByteArrayInputStream(new byte[] {0xa}));

// assertions will fail
assertThat(new ByteArrayInputStream(new byte[] {0xa})).hasSameContentAs(new ByteArrayInputStream(new byte[] {}));
assertThat(new ByteArrayInputStream(new byte[] {0xa})).hasSameContentAs(new ByteArrayInputStream(new byte[] {0xa, 0xc, 0xd}));
Parameters
expected:InputStream

the given InputStream to compare the actual InputStream to.

Returns:SELF

this assertion object.

Exceptions
NullPointerException:
if the given InputStream is null.
AssertionError:
  • if the actual InputStream is null.
  • if the content of the actual InputStream is not equal to the content of the given one.
InputStreamsException:
if an I/O error occurs.
isEmptyback to summary
public SELF isEmpty()

Verifies that the content of the actual InputStream is empty.

Warning

this will consume the first byte of the InputStream.

Example:

 // assertion will pass
assertThat(new ByteArrayInputStream(new byte[] {})).isEmpty());

// assertions will fail
assertThat(new ByteArrayInputStream(new byte[] {0xa})).isEmpty(); 
Returns:SELF

this assertion object.

Exceptions
NullPointerException:
if the given InputStream is null.
AssertionError:
if the content of the actual InputStream is not empty.
UncheckedIOException:
if an I/O error occurs.
Since
3.17.0
isNotEmptyback to summary
public SELF isNotEmpty()

Verifies that the content of the actual InputStream is not empty.

Warning

this will consume the first byte of the InputStream.

Example:

 // assertion will pass
assertThat(new ByteArrayInputStream(new byte[] {0xa})).isNotEmpty());

// assertions will fail
assertThat(new ByteArrayInputStream(new byte[] {})).isNotEmpty();
Returns:SELF

this assertion object.

Exceptions
NullPointerException:
if the given InputStream is null.
AssertionError:
if the content of the actual InputStream is empty.
UncheckedIOException:
if an I/O error occurs.
Since
3.17.0
readStringback to summary
private String readString(Charset charset)