diff options
author | 2025-03-14 15:56:28 +1100 | |
---|---|---|
committer | 2025-03-19 16:59:36 +1100 | |
commit | 8c5c3c4046b546e7655e5877ee3f574058a2f028 (patch) | |
tree | 9c0c1a3b447ebcc103f34eaf21215f72a56278d9 | |
parent | 98129c980bbd136e8971e268609023e10ac88b2f (diff) |
Modernize ArchiveHandleTest
Use try-with-resources statements where the linter suggests them.
Avoid calling methods having a @NonNull-annotated parameter with a null
argument, because this causes the linter to complain.
Bug: 404658729
Flag: EXEMPT only changed test code
Test: atest DocumentsUIGoogleTests:com.android.documentsui.archives
Change-Id: I8d2d98c295ee2e90765c19a8e6b11645b62382e0
-rw-r--r-- | tests/functional/com/android/documentsui/archives/ArchiveHandleTest.java | 475 |
1 files changed, 194 insertions, 281 deletions
diff --git a/tests/functional/com/android/documentsui/archives/ArchiveHandleTest.java b/tests/functional/com/android/documentsui/archives/ArchiveHandleTest.java index d8a1f4225..46b2698df 100644 --- a/tests/functional/com/android/documentsui/archives/ArchiveHandleTest.java +++ b/tests/functional/com/android/documentsui/archives/ArchiveHandleTest.java @@ -22,6 +22,8 @@ import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static java.util.Objects.requireNonNull; + import android.os.ParcelFileDescriptor; import androidx.annotation.NonNull; @@ -49,27 +51,24 @@ public class ArchiveHandleTest { @Rule public ArchiveFileTestRule mArchiveFileTestRule = new ArchiveFileTestRule(); - - private ArchiveHandle prepareArchiveHandle(String archivePath, String suffix, - String mimeType) throws IOException, CompressorException, ArchiveException { - ParcelFileDescriptor parcelFileDescriptor = mArchiveFileTestRule - .openAssetFile(archivePath, suffix); + private ArchiveHandle prepareArchiveHandle(String archivePath, String suffix, String mimeType) + throws IOException, CompressorException, ArchiveException { + ParcelFileDescriptor parcelFileDescriptor = mArchiveFileTestRule.openAssetFile(archivePath, + suffix); return ArchiveHandle.create(parcelFileDescriptor, mimeType); } - private static ArchiveEntry getFileInArchive(Enumeration<ArchiveEntry> enumeration, - String pathInArchive) { + private static ArchiveEntry getFileInArchive(Enumeration<ArchiveEntry> enumeration) { while (enumeration.hasMoreElements()) { ArchiveEntry entry = enumeration.nextElement(); - if (entry.getName().equals(pathInArchive)) { + if (entry.getName().equals("hello/inside_folder/hello_insside.txt")) { return entry; } } return null; } - private static class ArchiveEntryRecord implements ArchiveEntry { private final String mName; private final long mSize; @@ -91,11 +90,9 @@ public class ArchiveHandleTest { return false; } - if (obj instanceof ArchiveEntryRecord) { - ArchiveEntryRecord recordB = (ArchiveEntryRecord) obj; - return mName.equals(recordB.mName) - && mSize == recordB.mSize - && mIsDirectory == recordB.mIsDirectory; + if (obj instanceof ArchiveEntryRecord record) { + return mName.equals(record.mName) && mSize == record.mSize + && mIsDirectory == record.mIsDirectory; } return false; @@ -124,13 +121,13 @@ public class ArchiveHandleTest { @NonNull @Override public String toString() { - return String.format(Locale.ENGLISH, "name: %s, size: %d, isDirectory: %b", - mName, mSize, mIsDirectory); + return String.format(Locale.ENGLISH, "name: %s, size: %d, isDirectory: %b", mName, + mSize, mIsDirectory); } } private static List<ArchiveEntry> transformToIterable(Enumeration<ArchiveEntry> enumeration) { - List list = new ArrayList<ArchiveEntry>(); + List<ArchiveEntry> list = new ArrayList<>(); while (enumeration.hasMoreElements()) { list.add(new ArchiveEntryRecord(enumeration.nextElement())); } @@ -183,311 +180,259 @@ public class ArchiveHandleTest { } @Test - public void buildArchiveHandle_withoutFileDescriptor_shouldBeIllegal() throws Exception { - try { - ArchiveHandle.create(null, - "application/x-7z-compressed"); - fail("It should not be here!"); - } catch (NullPointerException e) { - /* do nothing */ - } - } - - @Test - public void buildArchiveHandle_withWrongMimeType_shouldBeIllegal() throws Exception { - ParcelFileDescriptor parcelFileDescriptor = mArchiveFileTestRule - .openAssetFile("archives/7z/hello.7z", ".7z"); - - try { - ArchiveHandle.create(parcelFileDescriptor, null); - fail("It should not be here!"); - } catch (IllegalArgumentException e) { - /* do nothing */ - } - } - - @Test public void buildArchiveHandle_sevenZFile_shouldNotNull() throws Exception { - ArchiveHandle archiveHandle = prepareArchiveHandle("archives/7z/hello.7z", - ".7z", "application/x-7z-compressed"); - - assertThat(archiveHandle).isNotNull(); + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/7z/hello.7z", ".7z", + "application/x-7z-compressed")) { + assertThat(archiveHandle).isNotNull(); + } } @Test public void buildArchiveHandle_zipFile_shouldNotNull() throws Exception { - ArchiveHandle archiveHandle = prepareArchiveHandle("archives/zip/hello.zip", - ".zip", "application/zip"); - - assertThat(archiveHandle).isNotNull(); - } - - @Test - public void buildArchiveHandle_zipWithWrongMimeType_shouldBeNull() throws Exception { - try { - prepareArchiveHandle("archives/zip/hello.zip", - ".zip", "application/xxxzip"); - fail("It should not be here!"); - } catch (UnsupportedOperationException e) { - /* do nothing */ + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/zip/hello.zip", ".zip", + "application/zip")) { + assertThat(archiveHandle).isNotNull(); } } @Test public void buildArchiveHandle_tarFile_shouldNotNull() throws Exception { - ArchiveHandle archiveHandle = prepareArchiveHandle("archives/tar/hello.tar", - ".tar", "application/x-gtar"); - - assertThat(archiveHandle).isNotNull(); + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/tar/hello.tar", ".tar", + "application/x-gtar")) { + assertThat(archiveHandle).isNotNull(); + } } @Test public void buildArchiveHandle_tgzFile_shouldNotNull() throws Exception { - ArchiveHandle archiveHandle = prepareArchiveHandle("archives/tar_gz/hello.tgz", - ".tgz", "application/x-compressed-tar"); - - assertThat(archiveHandle).isNotNull(); + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/tar_gz/hello.tgz", ".tgz", + "application/x-compressed-tar")) { + assertThat(archiveHandle).isNotNull(); + } } @Test public void buildArchiveHandle_tarGzFile_shouldNotNull() throws Exception { - ArchiveHandle archiveHandle = - prepareArchiveHandle("archives/tar_gz/hello_tar_gz", ".tar.gz", - "application/x-compressed-tar"); - - assertThat(archiveHandle).isNotNull(); + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/tar_gz/hello_tar_gz", + ".tar.gz", "application/x-compressed-tar")) { + assertThat(archiveHandle).isNotNull(); + } } @Test public void buildArchiveHandle_tarBzipFile_shouldNotNull() throws Exception { - ArchiveHandle archiveHandle = - prepareArchiveHandle("archives/tar_bz2/hello.tar.bz2", - ".tar.bz2", "application/x-bzip-compressed-tar"); - - assertThat(archiveHandle).isNotNull(); + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/tar_bz2/hello.tar.bz2", + ".tar.bz2", "application/x-bzip-compressed-tar")) { + assertThat(archiveHandle).isNotNull(); + } } @Test public void buildArchiveHandle_tarXzFile_shouldNotNull() throws Exception { - ArchiveHandle archiveHandle = - prepareArchiveHandle("archives/xz/hello.tar.xz", ".tar.xz", - "application/x-xz-compressed-tar"); - - assertThat(archiveHandle).isNotNull(); + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/xz/hello.tar.xz", + ".tar.xz", "application/x-xz-compressed-tar")) { + assertThat(archiveHandle).isNotNull(); + } } @Test public void buildArchiveHandle_tarBrFile_shouldNotNull() throws Exception { - ArchiveHandle archiveHandle = - prepareArchiveHandle("archives/brotli/hello.tar.br", ".tar.br", - "application/x-brotli-compressed-tar"); - - assertThat(archiveHandle).isNotNull(); + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/brotli/hello.tar.br", + ".tar.br", "application/x-brotli-compressed-tar")) { + assertThat(archiveHandle).isNotNull(); + } } @Test public void getMimeType_sevenZFile_shouldBeSevenZ() throws CompressorException, ArchiveException, IOException { - ArchiveHandle archiveHandle = prepareArchiveHandle("archives/7z/hello.7z", - ".7z", "application/x-7z-compressed"); - - assertThat(archiveHandle.getMimeType()).isEqualTo("application/x-7z-compressed"); + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/7z/hello.7z", ".7z", + "application/x-7z-compressed")) { + assertThat(archiveHandle.getMimeType()).isEqualTo("application/x-7z-compressed"); + } } @Test public void getMimeType_tarBrotli_shouldBeBrotliCompressedTar() throws CompressorException, ArchiveException, IOException { - ArchiveHandle archiveHandle = - prepareArchiveHandle("archives/brotli/hello.tar.br", ".tar.br", - "application/x-brotli-compressed-tar"); - - assertThat(archiveHandle.getMimeType()) - .isEqualTo("application/x-brotli-compressed-tar"); + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/brotli/hello.tar.br", + ".tar.br", "application/x-brotli-compressed-tar")) { + assertThat(archiveHandle.getMimeType()).isEqualTo( + "application/x-brotli-compressed-tar"); + } } @Test public void getMimeType_tarXz_shouldBeXzCompressedTar() throws CompressorException, ArchiveException, IOException { - ArchiveHandle archiveHandle = - prepareArchiveHandle("archives/xz/hello.tar.xz", ".tar.xz", - "application/x-xz-compressed-tar"); - - assertThat(archiveHandle.getMimeType()) - .isEqualTo("application/x-xz-compressed-tar"); + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/xz/hello.tar.xz", + ".tar.xz", "application/x-xz-compressed-tar")) { + assertThat(archiveHandle.getMimeType()).isEqualTo("application/x-xz-compressed-tar"); + } } @Test public void getMimeType_tarGz_shouldBeCompressedTar() throws CompressorException, ArchiveException, IOException { - ArchiveHandle archiveHandle = - prepareArchiveHandle("archives/tar_gz/hello_tar_gz", ".tar.gz", - "application/x-compressed-tar"); - - assertThat(archiveHandle.getMimeType()) - .isEqualTo("application/x-compressed-tar"); + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/tar_gz/hello_tar_gz", + ".tar.gz", "application/x-compressed-tar")) { + assertThat(archiveHandle.getMimeType()).isEqualTo("application/x-compressed-tar"); + } } @Test public void getCommonArchive_tarBrFile_shouldBeCommonArchiveInputHandle() throws Exception { - ArchiveHandle archiveHandle = - prepareArchiveHandle("archives/brotli/hello.tar.br", ".tar.br", - "application/x-brotli-compressed-tar"); - - assertThat(archiveHandle.toString()).contains("CommonArchiveInputHandle"); + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/brotli/hello.tar.br", + ".tar.br", "application/x-brotli-compressed-tar")) { + assertThat(archiveHandle.toString()).contains("CommonArchiveInputHandle"); + } } @Test public void getCommonArchive_sevenZFile_shouldBeSevenZFileHandle() throws Exception { - ArchiveHandle archiveHandle = prepareArchiveHandle("archives/7z/hello.7z", - ".7z", "application/x-7z-compressed"); - - assertThat(archiveHandle.toString()).contains("SevenZFileHandle"); + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/7z/hello.7z", ".7z", + "application/x-7z-compressed")) { + assertThat(archiveHandle.toString()).contains("SevenZFileHandle"); + } } - @Test public void getCommonArchive_zipFile_shouldBeZipFileHandle() throws Exception { - ArchiveHandle archiveHandle = prepareArchiveHandle("archives/zip/hello.zip", - ".zip", "application/zip"); - - assertThat(archiveHandle.toString()).contains("ZipFileHandle"); + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/zip/hello.zip", ".zip", + "application/zip")) { + assertThat(archiveHandle.toString()).contains("ZipFileHandle"); + } } @Test public void close_zipFile_shouldBeSuccess() throws Exception { - ArchiveHandle archiveHandle = prepareArchiveHandle("archives/zip/hello.zip", - ".zip", "application/zip"); - - archiveHandle.close(); + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/zip/hello.zip", ".zip", + "application/zip")) { + assertThat(archiveHandle).isNotNull(); + } } @Test public void close_sevenZFile_shouldBeSuccess() throws Exception { - ArchiveHandle archiveHandle = prepareArchiveHandle("archives/7z/hello.7z", - ".7z", "application/x-7z-compressed"); - - archiveHandle.close(); + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/7z/hello.7z", ".7z", + "application/x-7z-compressed")) { + assertThat(archiveHandle).isNotNull(); + } } @Test public void closeInputStream_zipFile_shouldBeSuccess() throws Exception { - ArchiveHandle archiveHandle = prepareArchiveHandle("archives/zip/hello.zip", - ".zip", "application/zip"); - - InputStream inputStream = archiveHandle.getInputStream( - getFileInArchive(archiveHandle.getEntries(), - "hello/inside_folder/hello_insside.txt")); - - assertThat(inputStream).isNotNull(); - - inputStream.close(); + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/zip/hello.zip", ".zip", + "application/zip")) { + try (InputStream inputStream = archiveHandle.getInputStream( + requireNonNull(getFileInArchive(archiveHandle.getEntries())))) { + assertThat(inputStream).isNotNull(); + } + } } @Test public void close_zipFile_shouldNotOpen() throws Exception { - ParcelFileDescriptor parcelFileDescriptor = mArchiveFileTestRule - .openAssetFile("archives/zip/hello.zip", ".zip"); + ParcelFileDescriptor parcelFileDescriptor = mArchiveFileTestRule.openAssetFile( + "archives/zip/hello.zip", ".zip"); - ArchiveHandle archiveHandle = ArchiveHandle.create(parcelFileDescriptor, - "application/zip"); + ArchiveHandle archiveHandle = ArchiveHandle.create(parcelFileDescriptor, "application/zip"); archiveHandle.close(); - FileInputStream fileInputStream = - new FileInputStream(parcelFileDescriptor.getFileDescriptor()); + FileInputStream fileInputStream = new FileInputStream( + parcelFileDescriptor.getFileDescriptor()); assertThat(fileInputStream).isNotNull(); } @Test public void getInputStream_zipFile_shouldHaveTheSameContent() throws Exception { - ParcelFileDescriptor parcelFileDescriptor = mArchiveFileTestRule - .openAssetFile("archives/zip/hello.zip", ".zip"); + ParcelFileDescriptor parcelFileDescriptor = mArchiveFileTestRule.openAssetFile( + "archives/zip/hello.zip", ".zip"); String expectedContent = mArchiveFileTestRule.getAssetText( "archives/original/hello/inside_folder/hello_insside.txt"); - ArchiveHandle archiveHandle = ArchiveHandle.create(parcelFileDescriptor, - "application/zip"); + ArchiveHandle archiveHandle = ArchiveHandle.create(parcelFileDescriptor, "application/zip"); InputStream inputStream = archiveHandle.getInputStream( - getFileInArchive(archiveHandle.getEntries(), - "hello/inside_folder/hello_insside.txt")); + requireNonNull(getFileInArchive(archiveHandle.getEntries()))); - assertThat(ArchiveFileTestRule.getStringFromInputStream(inputStream)) - .isEqualTo(expectedContent); + assertThat(ArchiveFileTestRule.getStringFromInputStream(inputStream)).isEqualTo( + expectedContent); } @Test public void getInputStream_zipFileNotExistEntry_shouldFail() throws Exception { - ArchiveHandle archiveHandle = prepareArchiveHandle("archives/zip/hello.zip", - ".zip", "application/zip"); - - ArchiveEntry archiveEntry = mock(ArchiveEntry.class); - when(archiveEntry.getName()).thenReturn("/not_exist_entry"); - - try { - archiveHandle.getInputStream(archiveEntry); - fail("It should not be here."); - } catch (ClassCastException e) { - /* do nothing */ + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/zip/hello.zip", ".zip", + "application/zip")) { + ArchiveEntry archiveEntry = mock(ArchiveEntry.class); + when(archiveEntry.getName()).thenReturn("/not_exist_entry"); + + try { + archiveHandle.getInputStream(archiveEntry); + fail("It should not be here."); + } catch (ClassCastException e) { + /* do nothing */ + } } } @Test public void getInputStream_directoryEntry_shouldFail() throws Exception { - ArchiveHandle archiveHandle = prepareArchiveHandle("archives/zip/hello.zip", - ".zip", "application/zip"); - - ArchiveEntry archiveEntry = mock(ArchiveEntry.class); - when(archiveEntry.isDirectory()).thenReturn(true); - - try { - archiveHandle.getInputStream(archiveEntry); - fail("It should not be here."); - } catch (IllegalArgumentException e) { - /* expected, do nothing */ + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/zip/hello.zip", ".zip", + "application/zip")) { + ArchiveEntry archiveEntry = mock(ArchiveEntry.class); + when(archiveEntry.isDirectory()).thenReturn(true); + + try { + archiveHandle.getInputStream(archiveEntry); + fail("It should not be here."); + } catch (IllegalArgumentException e) { + /* expected, do nothing */ + } } } @Test public void getInputStream_negativeSizeEntry_shouldFail() throws Exception { - ArchiveHandle archiveHandle = prepareArchiveHandle("archives/zip/hello.zip", - ".zip", "application/zip"); - - ArchiveEntry archiveEntry = mock(ArchiveEntry.class); - when(archiveEntry.isDirectory()).thenReturn(false); - when(archiveEntry.getSize()).thenReturn(-1L); - - try { - archiveHandle.getInputStream(archiveEntry); - fail("It should not be here."); - } catch (IllegalArgumentException e) { - /* expected, do nothing */ + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/zip/hello.zip", ".zip", + "application/zip")) { + ArchiveEntry archiveEntry = mock(ArchiveEntry.class); + when(archiveEntry.isDirectory()).thenReturn(false); + when(archiveEntry.getSize()).thenReturn(-1L); + + try { + archiveHandle.getInputStream(archiveEntry); + fail("It should not be here."); + } catch (IllegalArgumentException e) { + /* expected, do nothing */ + } } } @Test public void getInputStream_emptyStringEntry_shouldFail() throws Exception { - ArchiveHandle archiveHandle = prepareArchiveHandle("archives/zip/hello.zip", - ".zip", "application/zip"); - - ArchiveEntry archiveEntry = mock(ArchiveEntry.class); - when(archiveEntry.isDirectory()).thenReturn(false); - when(archiveEntry.getSize()).thenReturn(14L); - when(archiveEntry.getName()).thenReturn(""); - - try { - archiveHandle.getInputStream(archiveEntry); - fail("It should not be here."); - } catch (IllegalArgumentException e) { - /* expected, do nothing */ + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/zip/hello.zip", ".zip", + "application/zip")) { + ArchiveEntry archiveEntry = mock(ArchiveEntry.class); + when(archiveEntry.isDirectory()).thenReturn(false); + when(archiveEntry.getSize()).thenReturn(14L); + when(archiveEntry.getName()).thenReturn(""); + + try { + archiveHandle.getInputStream(archiveEntry); + fail("It should not be here."); + } catch (IllegalArgumentException e) { + /* expected, do nothing */ + } } } @Test public void getInputStream_sevenZFile_shouldHaveTheSameContent() throws Exception { - ParcelFileDescriptor parcelFileDescriptor = mArchiveFileTestRule - .openAssetFile("archives/7z/hello.7z", ".7z"); + ParcelFileDescriptor parcelFileDescriptor = mArchiveFileTestRule.openAssetFile( + "archives/7z/hello.7z", ".7z"); String expectedContent = mArchiveFileTestRule.getAssetText( "archives/original/hello/inside_folder/hello_insside.txt"); @@ -496,17 +441,16 @@ public class ArchiveHandleTest { "application/x-7z-compressed"); InputStream inputStream = archiveHandle.getInputStream( - getFileInArchive(archiveHandle.getEntries(), - "hello/inside_folder/hello_insside.txt")); + requireNonNull(getFileInArchive(archiveHandle.getEntries()))); - assertThat(ArchiveFileTestRule.getStringFromInputStream(inputStream)) - .isEqualTo(expectedContent); + assertThat(ArchiveFileTestRule.getStringFromInputStream(inputStream)).isEqualTo( + expectedContent); } @Test public void getInputStream_tarGzFile_shouldHaveTheSameContent() throws Exception { - ParcelFileDescriptor parcelFileDescriptor = mArchiveFileTestRule - .openAssetFile("archives/tar_gz/hello.tgz", ".tar.gz"); + ParcelFileDescriptor parcelFileDescriptor = mArchiveFileTestRule.openAssetFile( + "archives/tar_gz/hello.tgz", ".tar.gz"); String expectedContent = mArchiveFileTestRule.getAssetText( "archives/original/hello/inside_folder/hello_insside.txt"); @@ -515,40 +459,16 @@ public class ArchiveHandleTest { "application/x-compressed-tar"); InputStream inputStream = archiveHandle.getInputStream( - getFileInArchive(archiveHandle.getEntries(), - "hello/inside_folder/hello_insside.txt")); - - assertThat(ArchiveFileTestRule.getStringFromInputStream(inputStream)) - .isEqualTo(expectedContent); - } - - @Test - public void getInputStream_tarGzFileNullEntry_getNullInputStream() throws Exception { - ParcelFileDescriptor parcelFileDescriptor = mArchiveFileTestRule - .openAssetFile("archives/tar_gz/hello.tgz", ".tar.gz"); - - String expectedContent = mArchiveFileTestRule.getAssetText( - "archives/original/hello/inside_folder/hello_insside.txt"); + requireNonNull(getFileInArchive(archiveHandle.getEntries()))); - ArchiveHandle archiveHandle = ArchiveHandle.create(parcelFileDescriptor, - "application/x-compressed-tar"); - - try { - archiveHandle.getInputStream(null); - fail("It should not here"); - } catch (IllegalArgumentException | ArchiveException | CompressorException e) { - /* expected, do nothing */ - } + assertThat(ArchiveFileTestRule.getStringFromInputStream(inputStream)).isEqualTo( + expectedContent); } - @Test public void getInputStream_tarGzFileInvalidEntry_getNullInputStream() throws Exception { - ParcelFileDescriptor parcelFileDescriptor = mArchiveFileTestRule - .openAssetFile("archives/tar_gz/hello.tgz", ".tar.gz"); - - String expectedContent = mArchiveFileTestRule.getAssetText( - "archives/original/hello/inside_folder/hello_insside.txt"); + ParcelFileDescriptor parcelFileDescriptor = mArchiveFileTestRule.openAssetFile( + "archives/tar_gz/hello.tgz", ".tar.gz"); ArchiveHandle archiveHandle = ArchiveHandle.create(parcelFileDescriptor, "application/x-compressed-tar"); @@ -565,8 +485,8 @@ public class ArchiveHandleTest { @Test public void getInputStream_tarBrotliFile_shouldHaveTheSameContent() throws Exception { - ParcelFileDescriptor parcelFileDescriptor = mArchiveFileTestRule - .openAssetFile("archives/brotli/hello.tar.br", ".tar.br"); + ParcelFileDescriptor parcelFileDescriptor = mArchiveFileTestRule.openAssetFile( + "archives/brotli/hello.tar.br", ".tar.br"); String expectedContent = mArchiveFileTestRule.getAssetText( "archives/original/hello/inside_folder/hello_insside.txt"); @@ -575,70 +495,63 @@ public class ArchiveHandleTest { "application/x-brotli-compressed-tar"); InputStream inputStream = archiveHandle.getInputStream( - getFileInArchive(archiveHandle.getEntries(), - "hello/inside_folder/hello_insside.txt")); + requireNonNull(getFileInArchive(archiveHandle.getEntries()))); - assertThat(ArchiveFileTestRule.getStringFromInputStream(inputStream)) - .isEqualTo(expectedContent); + assertThat(ArchiveFileTestRule.getStringFromInputStream(inputStream)).isEqualTo( + expectedContent); } @Test public void getEntries_zipFile_shouldTheSameWithList() throws Exception { - ArchiveHandle archiveHandle = - prepareArchiveHandle("archives/zip/hello.zip", ".zip", - "application/zip"); - - assertThat(transformToIterable(archiveHandle.getEntries())) - .containsAtLeastElementsIn(sExpectEntries); + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/zip/hello.zip", ".zip", + "application/zip")) { + assertThat(transformToIterable(archiveHandle.getEntries())).containsAtLeastElementsIn( + sExpectEntries); + } } @Test public void getEntries_tarFile_shouldTheSameWithList() throws Exception { - ArchiveHandle archiveHandle = - prepareArchiveHandle("archives/tar/hello.tar", ".tar", - "application/x-gtar"); - - assertThat(transformToIterable(archiveHandle.getEntries())) - .containsAtLeastElementsIn(sExpectEntries); + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/tar/hello.tar", ".tar", + "application/x-gtar")) { + assertThat(transformToIterable(archiveHandle.getEntries())).containsAtLeastElementsIn( + sExpectEntries); + } } @Test public void getEntries_tgzFile_shouldTheSameWithList() throws Exception { - ArchiveHandle archiveHandle = - prepareArchiveHandle("archives/tar_gz/hello.tgz", ".tgz", - "application/x-compressed-tar"); - - assertThat(transformToIterable(archiveHandle.getEntries())) - .containsAtLeastElementsIn(sExpectEntries); + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/tar_gz/hello.tgz", ".tgz", + "application/x-compressed-tar")) { + assertThat(transformToIterable(archiveHandle.getEntries())).containsAtLeastElementsIn( + sExpectEntries); + } } @Test public void getEntries_tarBzFile_shouldTheSameWithList() throws Exception { - ArchiveHandle archiveHandle = - prepareArchiveHandle("archives/tar_bz2/hello.tar.bz2", ".tar.bz2", - "application/x-bzip-compressed-tar"); - - assertThat(transformToIterable(archiveHandle.getEntries())) - .containsAtLeastElementsIn(sExpectEntries); + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/tar_bz2/hello.tar.bz2", + ".tar.bz2", "application/x-bzip-compressed-tar")) { + assertThat(transformToIterable(archiveHandle.getEntries())).containsAtLeastElementsIn( + sExpectEntries); + } } @Test public void getEntries_tarBrotliFile_shouldTheSameWithList() throws Exception { - ArchiveHandle archiveHandle = - prepareArchiveHandle("archives/brotli/hello.tar.br", ".tar.br", - "application/x-brotli-compressed-tar"); - - assertThat(transformToIterable(archiveHandle.getEntries())) - .containsAtLeastElementsIn(sExpectEntries); + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/brotli/hello.tar.br", + ".tar.br", "application/x-brotli-compressed-tar")) { + assertThat(transformToIterable(archiveHandle.getEntries())).containsAtLeastElementsIn( + sExpectEntries); + } } @Test public void getEntries_tarXzFile_shouldTheSameWithList() throws Exception { - ArchiveHandle archiveHandle = - prepareArchiveHandle("archives/xz/hello.tar.xz", ".tar.xz", - "application/x-xz-compressed-tar"); - - assertThat(transformToIterable(archiveHandle.getEntries())) - .containsAtLeastElementsIn(sExpectEntries); + try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/xz/hello.tar.xz", + ".tar.xz", "application/x-xz-compressed-tar")) { + assertThat(transformToIterable(archiveHandle.getEntries())).containsAtLeastElementsIn( + sExpectEntries); + } } } |