This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
The following commit(s) were added to refs/heads/master by this push:
new bb6363fd Use try-with-resources
bb6363fd is described below
commit bb6363fd2dfdef1e50f892b8f63ef040a4556580
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Nov 4 07:12:48 2023 -0400
Use try-with-resources
More assertions
---
.../commons/compress/utils/ByteUtilsTest.java | 81 ++++++++++++++--------
1 file changed, 51 insertions(+), 30 deletions(-)
diff --git a/src/test/java/org/apache/commons/compress/utils/ByteUtilsTest.java
b/src/test/java/org/apache/commons/compress/utils/ByteUtilsTest.java
index d652eabf..6f7fa8d8 100644
--- a/src/test/java/org/apache/commons/compress/utils/ByteUtilsTest.java
+++ b/src/test/java/org/apache/commons/compress/utils/ByteUtilsTest.java
@@ -128,7 +128,8 @@ public class ByteUtilsTest {
@Test
public void testFromLittleEndianFromSupplierThrowsForLengthTooBig() {
- assertThrows(IllegalArgumentException.class, () ->
fromLittleEndian(new InputStreamByteSupplier(new
ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY)), 9));
+ assertThrows(IllegalArgumentException.class,
+ () -> fromLittleEndian(new InputStreamByteSupplier(new
ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY)), 9));
}
@Test
@@ -140,8 +141,7 @@ public class ByteUtilsTest {
@Test
public void testFromLittleEndianFromSupplierUnsignedInt32() throws
IOException {
final ByteArrayInputStream bin = new ByteArrayInputStream(new byte[] {
2, 3, 4, (byte) 128 });
- assertEquals(2 + 3 * 256 + 4 * 256 * 256 + 128L * 256 * 256 * 256,
- fromLittleEndian(new InputStreamByteSupplier(bin), 4));
+ assertEquals(2 + 3 * 256 + 4 * 256 * 256 + 128L * 256 * 256 * 256,
fromLittleEndian(new InputStreamByteSupplier(bin), 4));
}
@Test
@@ -160,52 +160,73 @@ public class ByteUtilsTest {
@Test
public void toLittleEndianToConsumer() throws IOException {
- final ByteArrayOutputStream bos = new ByteArrayOutputStream();
- toLittleEndian(new OutputStreamByteConsumer(bos), 2 + 3 * 256 + 4 *
256 * 256, 3);
- bos.close();
- assertArrayEquals(new byte[] { 2, 3, 4 }, bos.toByteArray());
+ final byte[] byteArray;
+ final byte[] expected = { 2, 3, 4 };
+ try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
+ toLittleEndian(new OutputStreamByteConsumer(bos), 2 + 3 * 256 + 4
* 256 * 256, 3);
+ byteArray = bos.toByteArray();
+ assertArrayEquals(expected, byteArray);
+ }
+ assertArrayEquals(expected, byteArray);
}
@Test
public void toLittleEndianToConsumerUnsignedInt32() throws IOException {
- final ByteArrayOutputStream bos = new ByteArrayOutputStream();
- toLittleEndian(new OutputStreamByteConsumer(bos), 2 + 3 * 256 + 4 *
256 * 256 + 128L * 256 * 256 * 256, 4);
- bos.close();
- assertArrayEquals(new byte[] { 2, 3, 4, (byte) 128 },
bos.toByteArray());
+ final byte[] byteArray;
+ final byte[] expected = { 2, 3, 4, (byte) 128 };
+ try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
+ toLittleEndian(new OutputStreamByteConsumer(bos), 2 + 3 * 256 + 4
* 256 * 256 + 128L * 256 * 256 * 256, 4);
+ byteArray = bos.toByteArray();
+ assertArrayEquals(expected, byteArray);
+ }
+ assertArrayEquals(expected, byteArray);
}
@Test
public void toLittleEndianToDataOutput() throws IOException {
- final ByteArrayOutputStream bos = new ByteArrayOutputStream();
- final DataOutput dos = new DataOutputStream(bos);
- toLittleEndian(dos, 2 + 3 * 256 + 4 * 256 * 256, 3);
- bos.close();
- assertArrayEquals(new byte[] { 2, 3, 4 }, bos.toByteArray());
+ final byte[] byteArray;
+ final byte[] expected = { 2, 3, 4 };
+ try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
+ final DataOutput dos = new DataOutputStream(bos);
+ toLittleEndian(dos, 2 + 3 * 256 + 4 * 256 * 256, 3);
+ byteArray = bos.toByteArray();
+ assertArrayEquals(expected, byteArray);
+ }
+ assertArrayEquals(expected, byteArray);
}
@Test
public void toLittleEndianToDataOutputUnsignedInt32() throws IOException {
- final ByteArrayOutputStream bos = new ByteArrayOutputStream();
- final DataOutput dos = new DataOutputStream(bos);
- toLittleEndian(dos, 2 + 3 * 256 + 4 * 256 * 256 + 128L * 256 * 256 *
256, 4);
- bos.close();
- assertArrayEquals(new byte[] { 2, 3, 4, (byte) 128 },
bos.toByteArray());
+ final byte[] byteArray;
+ final byte[] expected = { 2, 3, 4, (byte) 128 };
+ try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
+ final DataOutput dos = new DataOutputStream(bos);
+ toLittleEndian(dos, 2 + 3 * 256 + 4 * 256 * 256 + 128L * 256 * 256
* 256, 4);
+ assertArrayEquals(new byte[] { 2, 3, 4, (byte) 128 },
bos.toByteArray());
+ }
}
-
@Test
public void toLittleEndianToStream() throws IOException {
- final ByteArrayOutputStream bos = new ByteArrayOutputStream();
- toLittleEndian(bos, 2 + 3 * 256 + 4 * 256 * 256, 3);
- bos.close();
- assertArrayEquals(new byte[] { 2, 3, 4 }, bos.toByteArray());
+ final byte[] byteArray;
+ final byte[] expected = { 2, 3, 4 };
+ try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
+ toLittleEndian(bos, 2 + 3 * 256 + 4 * 256 * 256, 3);
+ byteArray = bos.toByteArray();
+ assertArrayEquals(expected, byteArray);
+ }
+ assertArrayEquals(expected, byteArray);
}
@Test
public void toLittleEndianToStreamUnsignedInt32() throws IOException {
- final ByteArrayOutputStream bos = new ByteArrayOutputStream();
- toLittleEndian(bos, 2 + 3 * 256 + 4 * 256 * 256 + 128L * 256 * 256 *
256, 4);
- bos.close();
- assertArrayEquals(new byte[] { 2, 3, 4, (byte) 128 },
bos.toByteArray());
+ final byte[] byteArray;
+ final byte[] expected = { 2, 3, 4, (byte) 128 };
+ try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
+ toLittleEndian(bos, 2 + 3 * 256 + 4 * 256 * 256 + 128L * 256 * 256
* 256, 4);
+ byteArray = bos.toByteArray();
+ assertArrayEquals(expected, byteArray);
+ }
+ assertArrayEquals(expected, byteArray);
}
}