summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Ben Reich <benreich@google.com> 2025-03-20 18:57:56 -0700
committer Android (Google) Code Review <android-gerrit@google.com> 2025-03-20 18:57:56 -0700
commit90df5c40efe25b96d19abae58fc2c6eb55d0dc17 (patch)
treed0b98fbb7917546e91bb28f1054b403e8cea7be2
parent8c5c3c4046b546e7655e5877ee3f574058a2f028 (diff)
Revert "Modernize ArchiveHandleTest"
This reverts commit 8c5c3c4046b546e7655e5877ee3f574058a2f028. Reason for revert: As mentioned we don't know if this works on Android 11-13 which DocumentsUI requires to support. Change-Id: If85391a87532b782d77435034eb7ea5d506fc77e
-rw-r--r--tests/functional/com/android/documentsui/archives/ArchiveHandleTest.java475
1 files changed, 281 insertions, 194 deletions
diff --git a/tests/functional/com/android/documentsui/archives/ArchiveHandleTest.java b/tests/functional/com/android/documentsui/archives/ArchiveHandleTest.java
index 46b2698df..d8a1f4225 100644
--- a/tests/functional/com/android/documentsui/archives/ArchiveHandleTest.java
+++ b/tests/functional/com/android/documentsui/archives/ArchiveHandleTest.java
@@ -22,8 +22,6 @@ 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;
@@ -51,24 +49,27 @@ 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) {
+ private static ArchiveEntry getFileInArchive(Enumeration<ArchiveEntry> enumeration,
+ String pathInArchive) {
while (enumeration.hasMoreElements()) {
ArchiveEntry entry = enumeration.nextElement();
- if (entry.getName().equals("hello/inside_folder/hello_insside.txt")) {
+ if (entry.getName().equals(pathInArchive)) {
return entry;
}
}
return null;
}
+
private static class ArchiveEntryRecord implements ArchiveEntry {
private final String mName;
private final long mSize;
@@ -90,9 +91,11 @@ public class ArchiveHandleTest {
return false;
}
- if (obj instanceof ArchiveEntryRecord record) {
- return mName.equals(record.mName) && mSize == record.mSize
- && mIsDirectory == record.mIsDirectory;
+ if (obj instanceof ArchiveEntryRecord) {
+ ArchiveEntryRecord recordB = (ArchiveEntryRecord) obj;
+ return mName.equals(recordB.mName)
+ && mSize == recordB.mSize
+ && mIsDirectory == recordB.mIsDirectory;
}
return false;
@@ -121,13 +124,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<ArchiveEntry> list = new ArrayList<>();
+ List list = new ArrayList<ArchiveEntry>();
while (enumeration.hasMoreElements()) {
list.add(new ArchiveEntryRecord(enumeration.nextElement()));
}
@@ -180,259 +183,311 @@ public class ArchiveHandleTest {
}
@Test
- public void buildArchiveHandle_sevenZFile_shouldNotNull() throws Exception {
- try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/7z/hello.7z", ".7z",
- "application/x-7z-compressed")) {
- assertThat(archiveHandle).isNotNull();
+ 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();
+ }
+
+ @Test
public void buildArchiveHandle_zipFile_shouldNotNull() throws Exception {
- try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/zip/hello.zip", ".zip",
- "application/zip")) {
- assertThat(archiveHandle).isNotNull();
+ 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 */
}
}
@Test
public void buildArchiveHandle_tarFile_shouldNotNull() throws Exception {
- try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/tar/hello.tar", ".tar",
- "application/x-gtar")) {
- assertThat(archiveHandle).isNotNull();
- }
+ ArchiveHandle archiveHandle = prepareArchiveHandle("archives/tar/hello.tar",
+ ".tar", "application/x-gtar");
+
+ assertThat(archiveHandle).isNotNull();
}
@Test
public void buildArchiveHandle_tgzFile_shouldNotNull() throws Exception {
- try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/tar_gz/hello.tgz", ".tgz",
- "application/x-compressed-tar")) {
- assertThat(archiveHandle).isNotNull();
- }
+ ArchiveHandle archiveHandle = prepareArchiveHandle("archives/tar_gz/hello.tgz",
+ ".tgz", "application/x-compressed-tar");
+
+ assertThat(archiveHandle).isNotNull();
}
@Test
public void buildArchiveHandle_tarGzFile_shouldNotNull() throws Exception {
- try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/tar_gz/hello_tar_gz",
- ".tar.gz", "application/x-compressed-tar")) {
- assertThat(archiveHandle).isNotNull();
- }
+ 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 {
- try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/tar_bz2/hello.tar.bz2",
- ".tar.bz2", "application/x-bzip-compressed-tar")) {
- assertThat(archiveHandle).isNotNull();
- }
+ 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 {
- try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/xz/hello.tar.xz",
- ".tar.xz", "application/x-xz-compressed-tar")) {
- assertThat(archiveHandle).isNotNull();
- }
+ 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 {
- try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/brotli/hello.tar.br",
- ".tar.br", "application/x-brotli-compressed-tar")) {
- assertThat(archiveHandle).isNotNull();
- }
+ 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 {
- try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/7z/hello.7z", ".7z",
- "application/x-7z-compressed")) {
- assertThat(archiveHandle.getMimeType()).isEqualTo("application/x-7z-compressed");
- }
+ 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 {
- 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");
- }
+ 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 {
- 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");
- }
+ 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 {
- try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/tar_gz/hello_tar_gz",
- ".tar.gz", "application/x-compressed-tar")) {
- assertThat(archiveHandle.getMimeType()).isEqualTo("application/x-compressed-tar");
- }
+ 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 {
- try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/brotli/hello.tar.br",
- ".tar.br", "application/x-brotli-compressed-tar")) {
- assertThat(archiveHandle.toString()).contains("CommonArchiveInputHandle");
- }
+ 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 {
- try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/7z/hello.7z", ".7z",
- "application/x-7z-compressed")) {
- assertThat(archiveHandle.toString()).contains("SevenZFileHandle");
- }
+ 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 {
- try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/zip/hello.zip", ".zip",
- "application/zip")) {
- assertThat(archiveHandle.toString()).contains("ZipFileHandle");
- }
+ ArchiveHandle archiveHandle = prepareArchiveHandle("archives/zip/hello.zip",
+ ".zip", "application/zip");
+
+ assertThat(archiveHandle.toString()).contains("ZipFileHandle");
}
@Test
public void close_zipFile_shouldBeSuccess() throws Exception {
- try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/zip/hello.zip", ".zip",
- "application/zip")) {
- assertThat(archiveHandle).isNotNull();
- }
+ ArchiveHandle archiveHandle = prepareArchiveHandle("archives/zip/hello.zip",
+ ".zip", "application/zip");
+
+ archiveHandle.close();
}
@Test
public void close_sevenZFile_shouldBeSuccess() throws Exception {
- try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/7z/hello.7z", ".7z",
- "application/x-7z-compressed")) {
- assertThat(archiveHandle).isNotNull();
- }
+ ArchiveHandle archiveHandle = prepareArchiveHandle("archives/7z/hello.7z",
+ ".7z", "application/x-7z-compressed");
+
+ archiveHandle.close();
}
@Test
public void closeInputStream_zipFile_shouldBeSuccess() throws Exception {
- try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/zip/hello.zip", ".zip",
- "application/zip")) {
- try (InputStream inputStream = archiveHandle.getInputStream(
- requireNonNull(getFileInArchive(archiveHandle.getEntries())))) {
- assertThat(inputStream).isNotNull();
- }
- }
+ 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();
}
@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(
- requireNonNull(getFileInArchive(archiveHandle.getEntries())));
+ getFileInArchive(archiveHandle.getEntries(),
+ "hello/inside_folder/hello_insside.txt"));
- assertThat(ArchiveFileTestRule.getStringFromInputStream(inputStream)).isEqualTo(
- expectedContent);
+ assertThat(ArchiveFileTestRule.getStringFromInputStream(inputStream))
+ .isEqualTo(expectedContent);
}
@Test
public void getInputStream_zipFileNotExistEntry_shouldFail() throws Exception {
- 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 */
- }
+ 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 {
- 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 */
- }
+ 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 {
- 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 */
- }
+ 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 {
- 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 */
- }
+ 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");
@@ -441,16 +496,17 @@ public class ArchiveHandleTest {
"application/x-7z-compressed");
InputStream inputStream = archiveHandle.getInputStream(
- requireNonNull(getFileInArchive(archiveHandle.getEntries())));
+ getFileInArchive(archiveHandle.getEntries(),
+ "hello/inside_folder/hello_insside.txt"));
- 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");
@@ -459,16 +515,40 @@ public class ArchiveHandleTest {
"application/x-compressed-tar");
InputStream inputStream = archiveHandle.getInputStream(
- requireNonNull(getFileInArchive(archiveHandle.getEntries())));
+ 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");
- assertThat(ArchiveFileTestRule.getStringFromInputStream(inputStream)).isEqualTo(
- expectedContent);
+ 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 */
+ }
}
+
@Test
public void getInputStream_tarGzFileInvalidEntry_getNullInputStream() 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");
ArchiveHandle archiveHandle = ArchiveHandle.create(parcelFileDescriptor,
"application/x-compressed-tar");
@@ -485,8 +565,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");
@@ -495,63 +575,70 @@ public class ArchiveHandleTest {
"application/x-brotli-compressed-tar");
InputStream inputStream = archiveHandle.getInputStream(
- requireNonNull(getFileInArchive(archiveHandle.getEntries())));
+ getFileInArchive(archiveHandle.getEntries(),
+ "hello/inside_folder/hello_insside.txt"));
- assertThat(ArchiveFileTestRule.getStringFromInputStream(inputStream)).isEqualTo(
- expectedContent);
+ assertThat(ArchiveFileTestRule.getStringFromInputStream(inputStream))
+ .isEqualTo(expectedContent);
}
@Test
public void getEntries_zipFile_shouldTheSameWithList() throws Exception {
- try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/zip/hello.zip", ".zip",
- "application/zip")) {
- assertThat(transformToIterable(archiveHandle.getEntries())).containsAtLeastElementsIn(
- sExpectEntries);
- }
+ ArchiveHandle archiveHandle =
+ prepareArchiveHandle("archives/zip/hello.zip", ".zip",
+ "application/zip");
+
+ assertThat(transformToIterable(archiveHandle.getEntries()))
+ .containsAtLeastElementsIn(sExpectEntries);
}
@Test
public void getEntries_tarFile_shouldTheSameWithList() throws Exception {
- try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/tar/hello.tar", ".tar",
- "application/x-gtar")) {
- assertThat(transformToIterable(archiveHandle.getEntries())).containsAtLeastElementsIn(
- sExpectEntries);
- }
+ 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 {
- try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/tar_gz/hello.tgz", ".tgz",
- "application/x-compressed-tar")) {
- assertThat(transformToIterable(archiveHandle.getEntries())).containsAtLeastElementsIn(
- sExpectEntries);
- }
+ 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 {
- try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/tar_bz2/hello.tar.bz2",
- ".tar.bz2", "application/x-bzip-compressed-tar")) {
- assertThat(transformToIterable(archiveHandle.getEntries())).containsAtLeastElementsIn(
- sExpectEntries);
- }
+ 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 {
- try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/brotli/hello.tar.br",
- ".tar.br", "application/x-brotli-compressed-tar")) {
- assertThat(transformToIterable(archiveHandle.getEntries())).containsAtLeastElementsIn(
- sExpectEntries);
- }
+ 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 {
- try (ArchiveHandle archiveHandle = prepareArchiveHandle("archives/xz/hello.tar.xz",
- ".tar.xz", "application/x-xz-compressed-tar")) {
- assertThat(transformToIterable(archiveHandle.getEntries())).containsAtLeastElementsIn(
- sExpectEntries);
- }
+ ArchiveHandle archiveHandle =
+ prepareArchiveHandle("archives/xz/hello.tar.xz", ".tar.xz",
+ "application/x-xz-compressed-tar");
+
+ assertThat(transformToIterable(archiveHandle.getEntries()))
+ .containsAtLeastElementsIn(sExpectEntries);
}
}