Polish FastByteArrayOutputStream[Tests]

See gh-31737
This commit is contained in:
Sam Brannen
2023-12-02 16:19:22 +01:00
parent 7cdacf3083
commit cd62dfe3a9
2 changed files with 50 additions and 47 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,15 +29,13 @@ import static org.assertj.core.api.Assertions.assertThatIOException;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Test suite for {@link FastByteArrayOutputStream}.
* Tests for {@link FastByteArrayOutputStream}.
*
* @author Craig Andrews
*/
class FastByteArrayOutputStreamTests {
private static final int INITIAL_CAPACITY = 256;
private final FastByteArrayOutputStream os = new FastByteArrayOutputStream(INITIAL_CAPACITY);
private final FastByteArrayOutputStream os = new FastByteArrayOutputStream();
private final byte[] helloBytes = "Hello World".getBytes(StandardCharsets.UTF_8);
@@ -63,16 +61,18 @@ class FastByteArrayOutputStreamTests {
assertThat(this.os.toString()).isEqualTo("Hello World");
assertThat(this.os.toString(StandardCharsets.UTF_8)).isEqualTo("Hello World");
@SuppressWarnings("resource")
FastByteArrayOutputStream empty = new FastByteArrayOutputStream();
assertThat(empty.toString()).isEqualTo("");
assertThat(empty.toString(StandardCharsets.US_ASCII)).isEqualTo("");
@SuppressWarnings("resource")
FastByteArrayOutputStream outputStream = new FastByteArrayOutputStream(5);
// Add bytes in multiple writes to ensure we get more than one buffer internally
outputStream.write(this.helloBytes, 0, 5);
outputStream.write(this.helloBytes, 5, 6);
assertThat(outputStream.toString(StandardCharsets.UTF_8)).isEqualTo("Hello World");
assertThat(outputStream.toString()).isEqualTo("Hello World");
assertThat(outputStream.toString(StandardCharsets.UTF_8)).isEqualTo("Hello World");
}
@Test
@@ -102,10 +102,9 @@ class FastByteArrayOutputStreamTests {
}
@Test
void close() throws Exception {
void close() {
this.os.close();
assertThatIOException().isThrownBy(() ->
this.os.write(this.helloBytes));
assertThatIOException().isThrownBy(() -> this.os.write(this.helloBytes));
}
@Test
@@ -128,8 +127,9 @@ class FastByteArrayOutputStreamTests {
@Test
void failResize() throws Exception {
this.os.write(this.helloBytes);
assertThatIllegalArgumentException().isThrownBy(() ->
this.os.resize(5));
assertThatIllegalArgumentException()
.isThrownBy(() -> this.os.resize(5))
.withMessage("New capacity must not be smaller than current size");
}
@Test
@@ -156,7 +156,7 @@ class FastByteArrayOutputStreamTests {
@Test
void getInputStreamReadBytePromotion() throws Exception {
byte[] bytes = new byte[] { -1 };
byte[] bytes = { -1 };
this.os.write(bytes);
InputStream inputStream = this.os.getInputStream();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);