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 52ba5cfd7 Use try-with-resources 52ba5cfd7 is described below commit 52ba5cfd759dbf99a3e85139c9a04c32cb83effe Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Fri May 17 13:57:06 2024 -0400 Use try-with-resources Internal refactoring --- .../compress/archivers/zip/ZipIoUtilTest.java | 177 ++++++++++----------- 1 file changed, 87 insertions(+), 90 deletions(-) diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipIoUtilTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipIoUtilTest.java index e71f2cd07..a963b5bf8 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipIoUtilTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipIoUtilTest.java @@ -38,117 +38,114 @@ import org.junit.jupiter.api.Test; */ public class ZipIoUtilTest extends AbstractTempDirTest { - @Test - public void testWriteFully_whenFullAtOnce_thenSucceed() throws IOException { - final SeekableByteChannel channel = mock(SeekableByteChannel.class); - - when(channel.write((ByteBuffer) any())).thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(5); - return 5; - }).thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(6); - return 6; - }); + private FileChannel mockFileChannel() { + return mock(FileChannel.class); + } - ZipIoUtil.writeFully(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8))); - ZipIoUtil.writeFully(channel, ByteBuffer.wrap("world\n".getBytes(StandardCharsets.UTF_8))); + private SeekableByteChannel mockSeekableByteChannel() { + return mock(SeekableByteChannel.class); + } - verify(channel, times(2)).write((ByteBuffer) any()); + @Test + public void testWriteFully_whenFullAtOnce_thenSucceed() throws IOException { + try (SeekableByteChannel channel = mockSeekableByteChannel()) { + when(channel.write((ByteBuffer) any())).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(5); + return 5; + }).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(6); + return 6; + }); + ZipIoUtil.writeFully(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8))); + ZipIoUtil.writeFully(channel, ByteBuffer.wrap("world\n".getBytes(StandardCharsets.UTF_8))); + verify(channel, times(2)).write((ByteBuffer) any()); + } } @Test public void testWriteFully_whenFullButPartial_thenSucceed() throws IOException { - final SeekableByteChannel channel = mock(SeekableByteChannel.class); - - when(channel.write((ByteBuffer) any())).thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(3); - return 3; - }).thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(5); - return 2; - }).thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(6); - return 6; - }); - - ZipIoUtil.writeFully(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8))); - ZipIoUtil.writeFully(channel, ByteBuffer.wrap("world\n".getBytes(StandardCharsets.UTF_8))); - - verify(channel, times(3)).write((ByteBuffer) any()); + try (SeekableByteChannel channel = mockSeekableByteChannel()) { + when(channel.write((ByteBuffer) any())).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(3); + return 3; + }).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(5); + return 2; + }).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(6); + return 6; + }); + ZipIoUtil.writeFully(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8))); + ZipIoUtil.writeFully(channel, ByteBuffer.wrap("world\n".getBytes(StandardCharsets.UTF_8))); + verify(channel, times(3)).write((ByteBuffer) any()); + } } @Test public void testWriteFully_whenPartial_thenFail() throws IOException { - final SeekableByteChannel channel = mock(SeekableByteChannel.class); - - when(channel.write((ByteBuffer) any())).thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(3); - return 3; - }).thenAnswer(answer -> 0); - - assertThrows(IOException.class, () -> ZipIoUtil.writeFully(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)))); - - verify(channel, times(2)).write((ByteBuffer) any()); + try (SeekableByteChannel channel = mockSeekableByteChannel()) { + when(channel.write((ByteBuffer) any())).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(3); + return 3; + }).thenAnswer(answer -> 0); + assertThrows(IOException.class, () -> ZipIoUtil.writeFully(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)))); + verify(channel, times(2)).write((ByteBuffer) any()); + } } @Test public void testWriteFullyAt_whenFullAtOnce_thenSucceed() throws IOException { - final FileChannel channel = mock(FileChannel.class); - - when(channel.write((ByteBuffer) any(), eq(20L))).thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(5); - return 5; - }); - when(channel.write((ByteBuffer) any(), eq(30L))).thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(6); - return 6; - }); - - ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)), 20); - ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("world\n".getBytes(StandardCharsets.UTF_8)), 30); - - verify(channel, times(1)).write((ByteBuffer) any(), eq(20L)); - verify(channel, times(1)).write((ByteBuffer) any(), eq(30L)); + try (FileChannel channel = mockFileChannel()) { + when(channel.write((ByteBuffer) any(), eq(20L))).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(5); + return 5; + }); + when(channel.write((ByteBuffer) any(), eq(30L))).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(6); + return 6; + }); + ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)), 20); + ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("world\n".getBytes(StandardCharsets.UTF_8)), 30); + verify(channel, times(1)).write((ByteBuffer) any(), eq(20L)); + verify(channel, times(1)).write((ByteBuffer) any(), eq(30L)); + } } @Test public void testWriteFullyAt_whenFullButPartial_thenSucceed() throws IOException { - final FileChannel channel = mock(FileChannel.class); - - when(channel.write((ByteBuffer) any(), eq(20L))).thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(3); - return 3; - }); - when(channel.write((ByteBuffer) any(), eq(23L))).thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(5); - return 2; - }); - when(channel.write((ByteBuffer) any(), eq(30L))).thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(6); - return 6; - }); - - ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)), 20); - ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("world\n".getBytes(StandardCharsets.UTF_8)), 30); - - verify(channel, times(1)).write((ByteBuffer) any(), eq(20L)); - verify(channel, times(1)).write((ByteBuffer) any(), eq(23L)); - verify(channel, times(1)).write((ByteBuffer) any(), eq(30L)); + try (FileChannel channel = mockFileChannel()) { + when(channel.write((ByteBuffer) any(), eq(20L))).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(3); + return 3; + }); + when(channel.write((ByteBuffer) any(), eq(23L))).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(5); + return 2; + }); + when(channel.write((ByteBuffer) any(), eq(30L))).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(6); + return 6; + }); + ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)), 20); + ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("world\n".getBytes(StandardCharsets.UTF_8)), 30); + verify(channel, times(1)).write((ByteBuffer) any(), eq(20L)); + verify(channel, times(1)).write((ByteBuffer) any(), eq(23L)); + verify(channel, times(1)).write((ByteBuffer) any(), eq(30L)); + } } @Test public void testWriteFullyAt_whenPartial_thenFail() throws IOException { - final FileChannel channel = mock(FileChannel.class); - - when(channel.write((ByteBuffer) any(), eq(20L))).thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(3); - return 3; - }); - when(channel.write((ByteBuffer) any(), eq(23L))).thenAnswer(answer -> 0); - assertThrows(IOException.class, () -> ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)), 20)); - - verify(channel, times(1)).write((ByteBuffer) any(), eq(20L)); - verify(channel, times(1)).write((ByteBuffer) any(), eq(23L)); - verify(channel, times(0)).write((ByteBuffer) any(), eq(25L)); + try (FileChannel channel = mockFileChannel()) { + when(channel.write((ByteBuffer) any(), eq(20L))).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(3); + return 3; + }); + when(channel.write((ByteBuffer) any(), eq(23L))).thenAnswer(answer -> 0); + assertThrows(IOException.class, () -> ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)), 20)); + verify(channel, times(1)).write((ByteBuffer) any(), eq(20L)); + verify(channel, times(1)).write((ByteBuffer) any(), eq(23L)); + verify(channel, times(0)).write((ByteBuffer) any(), eq(25L)); + } } }