You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/11/04 11:21:42 UTC
(commons-compress) 02/02: Use try-with-resources
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
commit 5149a80c9de47f1fee43ea9dfaa9d061d4c53730
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Nov 4 07:21:35 2023 -0400
Use try-with-resources
---
.../ZipSplitReadOnlySeekableByteChannelTest.java | 32 ++++++++++++----------
1 file changed, 18 insertions(+), 14 deletions(-)
diff --git a/src/test/java/org/apache/commons/compress/utils/ZipSplitReadOnlySeekableByteChannelTest.java b/src/test/java/org/apache/commons/compress/utils/ZipSplitReadOnlySeekableByteChannelTest.java
index 6e1260e8..fceb1647 100644
--- a/src/test/java/org/apache/commons/compress/utils/ZipSplitReadOnlySeekableByteChannelTest.java
+++ b/src/test/java/org/apache/commons/compress/utils/ZipSplitReadOnlySeekableByteChannelTest.java
@@ -72,8 +72,10 @@ public class ZipSplitReadOnlySeekableByteChannelTest {
public void testConstructorThrowsOnNonSplitZipFiles() throws IOException {
final List<SeekableByteChannel> channels = new ArrayList<>();
final File file = getFile("COMPRESS-189.zip");
- channels.add(Files.newByteChannel(file.toPath(), StandardOpenOption.READ));
- assertThrows(IOException.class, () -> new ZipSplitReadOnlySeekableByteChannel(channels));
+ try (final SeekableByteChannel byteChannel = Files.newByteChannel(file.toPath(), StandardOpenOption.READ)) {
+ channels.add(byteChannel);
+ assertThrows(IOException.class, () -> new ZipSplitReadOnlySeekableByteChannel(channels));
+ }
}
@Test
@@ -118,23 +120,25 @@ public class ZipSplitReadOnlySeekableByteChannelTest {
@Test
public void testForOrderedSeekableByteChannelsReturnCorrectClass() throws IOException {
final File file1 = getFile("COMPRESS-477/split_zip_created_by_zip/split_zip_created_by_zip.z01");
- final SeekableByteChannel firstChannel = Files.newByteChannel(file1.toPath(), StandardOpenOption.READ);
-
final File file2 = getFile("COMPRESS-477/split_zip_created_by_zip/split_zip_created_by_zip.z02");
- final SeekableByteChannel secondChannel = Files.newByteChannel(file2.toPath(), StandardOpenOption.READ);
-
final File lastFile = getFile("COMPRESS-477/split_zip_created_by_zip/split_zip_created_by_zip.zip");
- final SeekableByteChannel lastChannel = Files.newByteChannel(lastFile.toPath(), StandardOpenOption.READ);
- final List<SeekableByteChannel> channels = new ArrayList<>();
- channels.add(firstChannel);
- channels.add(secondChannel);
+ try (SeekableByteChannel firstChannel = Files.newByteChannel(file1.toPath(), StandardOpenOption.READ);
+ final SeekableByteChannel secondChannel = Files.newByteChannel(file2.toPath(), StandardOpenOption.READ);
+ final SeekableByteChannel lastChannel = Files.newByteChannel(lastFile.toPath(), StandardOpenOption.READ)) {
+
+ final List<SeekableByteChannel> channels = new ArrayList<>();
+ channels.add(firstChannel);
+ channels.add(secondChannel);
- SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.forOrderedSeekableByteChannels(lastChannel, channels);
- assertTrue(channel instanceof ZipSplitReadOnlySeekableByteChannel);
+ @SuppressWarnings("resource") // try-with-resources closes
+ SeekableByteChannel channel1 = ZipSplitReadOnlySeekableByteChannel.forOrderedSeekableByteChannels(lastChannel, channels);
+ assertTrue(channel1 instanceof ZipSplitReadOnlySeekableByteChannel);
- channel = ZipSplitReadOnlySeekableByteChannel.forOrderedSeekableByteChannels(firstChannel, secondChannel, lastChannel);
- assertTrue(channel instanceof ZipSplitReadOnlySeekableByteChannel);
+ @SuppressWarnings("resource") // try-with-resources closes
+ SeekableByteChannel channel2 = ZipSplitReadOnlySeekableByteChannel.forOrderedSeekableByteChannels(firstChannel, secondChannel, lastChannel);
+ assertTrue(channel2 instanceof ZipSplitReadOnlySeekableByteChannel);
+ }
}
@Test