Skip to content

Commit

Permalink
Use Assertions.assertInstanceOf()
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Sep 1, 2024
1 parent 2ed8616 commit f11fe5a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 41 deletions.
22 changes: 11 additions & 11 deletions src/test/java/org/apache/commons/compress/DetectArchiverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.BufferedInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -79,15 +79,15 @@ private BufferedInputStream createBufferedInputStream(final String resource) thr
public void testCOMPRESS_117() throws Exception {
try (ArchiveInputStream<?> tar = createArchiveInputStream("COMPRESS-117.tar")) {
assertNotNull(tar);
assertTrue(tar instanceof TarArchiveInputStream);
assertInstanceOf(TarArchiveInputStream.class, tar);
}
}

@Test
public void testCOMPRESS_335() throws Exception {
try (ArchiveInputStream<?> tar = createArchiveInputStream("COMPRESS-335.tar")) {
assertNotNull(tar);
assertTrue(tar instanceof TarArchiveInputStream);
assertInstanceOf(TarArchiveInputStream.class, tar);
}
}

Expand All @@ -96,38 +96,38 @@ public void testDetection() throws Exception {

try (ArchiveInputStream<?> ar = createArchiveInputStream("bla.ar")) {
assertNotNull(ar);
assertTrue(ar instanceof ArArchiveInputStream);
assertInstanceOf(ArArchiveInputStream.class, ar);
}

try (ArchiveInputStream<?> tar = createArchiveInputStream("bla.tar")) {
assertNotNull(tar);
assertTrue(tar instanceof TarArchiveInputStream);
assertInstanceOf(TarArchiveInputStream.class, tar);
}

try (ArchiveInputStream<?> zip = createArchiveInputStream("bla.zip")) {
assertNotNull(zip);
assertTrue(zip instanceof ZipArchiveInputStream);
assertInstanceOf(ZipArchiveInputStream.class, zip);
}

try (ArchiveInputStream<?> jar = createArchiveInputStream("bla.jar")) {
assertNotNull(jar);
assertTrue(jar instanceof ZipArchiveInputStream);
assertInstanceOf(ZipArchiveInputStream.class, jar);
}

try (ArchiveInputStream<?> cpio = createArchiveInputStream("bla.cpio")) {
assertNotNull(cpio);
assertTrue(cpio instanceof CpioArchiveInputStream);
assertInstanceOf(CpioArchiveInputStream.class, cpio);
}

try (ArchiveInputStream<?> arj = createArchiveInputStream("bla.arj")) {
assertNotNull(arj);
assertTrue(arj instanceof ArjArchiveInputStream);
assertInstanceOf(ArjArchiveInputStream.class, arj);
}

// Not yet implemented
// final ArchiveInputStream<?> tgz = getStreamFor("bla.tgz");
// assertNotNull(tgz);
// assertTrue(tgz instanceof TarArchiveInputStream);
// assertInstanceOf(TarArchiveInputStream.class, tgz);

}

Expand All @@ -147,7 +147,7 @@ public void testDetectionNotArchive() {
public void testDetectOldTarFormatArchive() throws Exception {
try (ArchiveInputStream<?> tar = createArchiveInputStream("COMPRESS-612/test-times-star-folder.tar")) {
assertNotNull(tar);
assertTrue(tar instanceof TarArchiveInputStream);
assertInstanceOf(TarArchiveInputStream.class, tar);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.commons.compress.archivers;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -414,7 +415,7 @@ public void testSkipsPK00Prefix() throws Exception {
try (InputStream fis = newInputStream("COMPRESS-208.zip")) {
try (InputStream bis = new BufferedInputStream(fis)) {
try (ArchiveInputStream<?> ais = ArchiveStreamFactory.DEFAULT.createArchiveInputStream(bis)) {
assertTrue(ais instanceof ZipArchiveInputStream);
assertInstanceOf(ZipArchiveInputStream.class, ais);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.commons.compress.archivers;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

Expand Down Expand Up @@ -107,11 +108,11 @@ public void testArchive(final File file) throws Exception {
try (ArchiveInputStream<?> ais = factory.createArchiveInputStream(new BufferedInputStream(Files.newInputStream(file.toPath())))) {
// check if expected type recognized
if (name.endsWith(".tar")) {
assertTrue(ais instanceof TarArchiveInputStream);
assertInstanceOf(TarArchiveInputStream.class, ais);
} else if (name.endsWith(".jar") || name.endsWith(".zip")) {
assertTrue(ais instanceof ZipArchiveInputStream);
assertInstanceOf(ZipArchiveInputStream.class, ais);
} else if (name.endsWith(".cpio")) {
assertTrue(ais instanceof CpioArchiveInputStream);
assertInstanceOf(CpioArchiveInputStream.class, ais);
// Hack: cpio does not add trailing "/" to directory names
for (int i = 0; i < expected.size(); i++) {
final String ent = expected.get(i);
Expand All @@ -120,7 +121,7 @@ public void testArchive(final File file) throws Exception {
}
}
} else if (name.endsWith(".ar")) {
assertTrue(ais instanceof ArArchiveInputStream);
assertInstanceOf(ArArchiveInputStream.class, ais);
// CPIO does not store directories or directory names
expected.clear();
for (final String ent : FILELIST) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.commons.compress.archivers;

import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

Expand Down Expand Up @@ -106,11 +107,11 @@ public void testArchive(final File file) throws Exception {
try (ArchiveInputStream<?> ais = factory.createArchiveInputStream(new BufferedInputStream(Files.newInputStream(file.toPath())))) {
// check if expected type recognized
if (name.endsWith(".tar")) {
assertTrue(ais instanceof TarArchiveInputStream);
assertInstanceOf(TarArchiveInputStream.class, ais);
} else if (name.endsWith(".jar") || name.endsWith(".zip")) {
assertTrue(ais instanceof ZipArchiveInputStream);
assertInstanceOf(ZipArchiveInputStream.class, ais);
} else if (name.endsWith(".cpio")) {
assertTrue(ais instanceof CpioArchiveInputStream);
assertInstanceOf(CpioArchiveInputStream.class, ais);
// Hack: cpio does not add trailing "/" to directory names
for (int i = 0; i < expected.size(); i++) {
final String ent = expected.get(i);
Expand All @@ -119,7 +120,7 @@ public void testArchive(final File file) throws Exception {
}
}
} else if (name.endsWith(".ar")) {
assertTrue(ais instanceof ArArchiveInputStream);
assertInstanceOf(ArArchiveInputStream.class, ais);
// CPIO does not store directories or directory names
expected.clear();
for (final String ent : FILELIST) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.apache.commons.compress.archivers.zip.ZipArchiveEntryRequest.createZipArchiveEntryRequest;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -107,11 +108,11 @@ public void testForPathsReturnCorrectClassInMemory() throws IOException {
list.add(secondFile);

try (SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.forPaths(lastFile, list)) {
assertTrue(channel instanceof ZipSplitReadOnlySeekableByteChannel);
assertInstanceOf(ZipSplitReadOnlySeekableByteChannel.class, channel);
}

try (SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.forPaths(firstFile, secondFile, lastFile)) {
assertTrue(channel instanceof ZipSplitReadOnlySeekableByteChannel);
assertInstanceOf(ZipSplitReadOnlySeekableByteChannel.class, channel);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.apache.commons.compress.AbstractTest.getFile;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -168,32 +169,32 @@ private String detect(final String testFileName, final Set<String> compressorNam
public void testCreateLimitedByName() throws Exception {
try (CompressorInputStream bzip2 = createCompressorInputStream("bla.txt.bz2", Collections.singleton(CompressorStreamFactory.BZIP2))) {
assertNotNull(bzip2);
assertTrue(bzip2 instanceof BZip2CompressorInputStream);
assertInstanceOf(BZip2CompressorInputStream.class, bzip2);
}

try (CompressorInputStream gzip = createCompressorInputStream("bla.tgz", Collections.singleton(CompressorStreamFactory.GZIP))) {
assertNotNull(gzip);
assertTrue(gzip instanceof GzipCompressorInputStream);
assertInstanceOf(GzipCompressorInputStream.class, gzip);
}

try (CompressorInputStream pack200 = createCompressorInputStream("bla.pack", Collections.singleton(CompressorStreamFactory.PACK200))) {
assertNotNull(pack200);
assertTrue(pack200 instanceof Pack200CompressorInputStream);
assertInstanceOf(Pack200CompressorInputStream.class, pack200);
}

try (CompressorInputStream xz = createCompressorInputStream("bla.tar.xz", Collections.singleton(CompressorStreamFactory.XZ))) {
assertNotNull(xz);
assertTrue(xz instanceof XZCompressorInputStream);
assertInstanceOf(XZCompressorInputStream.class, xz);
}

try (CompressorInputStream zlib = createCompressorInputStream("bla.tar.deflatez", Collections.singleton(CompressorStreamFactory.DEFLATE))) {
assertNotNull(zlib);
assertTrue(zlib instanceof DeflateCompressorInputStream);
assertInstanceOf(DeflateCompressorInputStream.class, zlib);
}

try (CompressorInputStream zstd = createCompressorInputStream("bla.tar.zst", Collections.singleton(CompressorStreamFactory.ZSTANDARD))) {
assertNotNull(zstd);
assertTrue(zstd instanceof ZstdCompressorInputStream);
assertInstanceOf(ZstdCompressorInputStream.class, zstd);
}
}

Expand All @@ -211,32 +212,32 @@ public void testCreateLimitedByNameNotFound() throws Exception {
public void testCreateWithAutoDetection() throws Exception {
try (CompressorInputStream bzip2 = createCompressorInputStream("bla.txt.bz2")) {
assertNotNull(bzip2);
assertTrue(bzip2 instanceof BZip2CompressorInputStream);
assertInstanceOf(BZip2CompressorInputStream.class, bzip2);
}

try (CompressorInputStream gzip = createCompressorInputStream("bla.tgz")) {
assertNotNull(gzip);
assertTrue(gzip instanceof GzipCompressorInputStream);
assertInstanceOf(GzipCompressorInputStream.class, gzip);
}

try (CompressorInputStream pack200 = createCompressorInputStream("bla.pack")) {
assertNotNull(pack200);
assertTrue(pack200 instanceof Pack200CompressorInputStream);
assertInstanceOf(Pack200CompressorInputStream.class, pack200);
}

try (CompressorInputStream xz = createCompressorInputStream("bla.tar.xz")) {
assertNotNull(xz);
assertTrue(xz instanceof XZCompressorInputStream);
assertInstanceOf(XZCompressorInputStream.class, xz);
}

try (CompressorInputStream zlib = createCompressorInputStream("bla.tar.deflatez")) {
assertNotNull(zlib);
assertTrue(zlib instanceof DeflateCompressorInputStream);
assertInstanceOf(DeflateCompressorInputStream.class, zlib);
}

try (CompressorInputStream zstd = createCompressorInputStream("bla.tar.zst")) {
assertNotNull(zstd);
assertTrue(zstd instanceof ZstdCompressorInputStream);
assertInstanceOf(ZstdCompressorInputStream.class, zstd);
}

assertThrows(CompressorException.class, () -> factory.createCompressorInputStream(new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import static org.apache.commons.compress.AbstractTest.getFile;
import static org.apache.commons.compress.AbstractTest.getPath;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -100,11 +100,11 @@ public void testForFilesReturnCorrectClass() throws IOException {
list.add(secondFile);

try (SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.forFiles(lastFile, list)) {
assertTrue(channel instanceof ZipSplitReadOnlySeekableByteChannel);
assertInstanceOf(ZipSplitReadOnlySeekableByteChannel.class, channel);
}

try (SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.forFiles(firstFile, secondFile, lastFile)) {
assertTrue(channel instanceof ZipSplitReadOnlySeekableByteChannel);
assertInstanceOf(ZipSplitReadOnlySeekableByteChannel.class, channel);
}
}

Expand Down Expand Up @@ -134,11 +134,11 @@ public void testForOrderedSeekableByteChannelsReturnCorrectClass() throws IOExce

@SuppressWarnings("resource") // try-with-resources closes
final SeekableByteChannel channel1 = ZipSplitReadOnlySeekableByteChannel.forOrderedSeekableByteChannels(lastChannel, channels);
assertTrue(channel1 instanceof ZipSplitReadOnlySeekableByteChannel);
assertInstanceOf(ZipSplitReadOnlySeekableByteChannel.class, channel1);

@SuppressWarnings("resource") // try-with-resources closes
final SeekableByteChannel channel2 = ZipSplitReadOnlySeekableByteChannel.forOrderedSeekableByteChannels(firstChannel, secondChannel, lastChannel);
assertTrue(channel2 instanceof ZipSplitReadOnlySeekableByteChannel);
assertInstanceOf(ZipSplitReadOnlySeekableByteChannel.class, channel2);
}
}

Expand Down Expand Up @@ -171,11 +171,11 @@ public void testForPathsReturnCorrectClass() throws IOException {
list.add(secondFile);

try (SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.forPaths(lastFile, list)) {
assertTrue(channel instanceof ZipSplitReadOnlySeekableByteChannel);
assertInstanceOf(ZipSplitReadOnlySeekableByteChannel.class, channel);
}

try (SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.forPaths(firstFile, secondFile, lastFile)) {
assertTrue(channel instanceof ZipSplitReadOnlySeekableByteChannel);
assertInstanceOf(ZipSplitReadOnlySeekableByteChannel.class, channel);
}
}

Expand Down

0 comments on commit f11fe5a

Please sign in to comment.