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 d058fb2 Use assertThrows(). d058fb2 is described below commit d058fb23e16616fbc8283ee8145ece2898c7499f Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sun Dec 26 20:11:34 2021 -0500 Use assertThrows(). --- .../commons/compress/DetectArchiverTestCase.java | 8 ++--- .../compress/archivers/jar/JarMarkerTest.java | 11 +++---- .../compress/archivers/sevenz/CoverageTest.java | 13 +++----- .../archivers/tar/TarArchiveEntryTest.java | 11 +++---- .../archivers/tar/TarArchiveOutputStreamTest.java | 30 ++++++++---------- .../compress/archivers/zip/BinaryTreeTest.java | 37 +++++++--------------- 6 files changed, 40 insertions(+), 70 deletions(-) diff --git a/src/test/java/org/apache/commons/compress/DetectArchiverTestCase.java b/src/test/java/org/apache/commons/compress/DetectArchiverTestCase.java index 2e32342..6d616ff 100644 --- a/src/test/java/org/apache/commons/compress/DetectArchiverTestCase.java +++ b/src/test/java/org/apache/commons/compress/DetectArchiverTestCase.java @@ -19,6 +19,7 @@ package org.apache.commons.compress; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -42,12 +43,7 @@ public final class DetectArchiverTestCase extends AbstractTestCase { @Test public void testDetectionNotArchive() throws IOException { - try { - getStreamFor("test.txt"); - fail("Expected ArchiveException"); - } catch (final ArchiveException e) { - // expected - } + assertThrows(ArchiveException.class, () -> getStreamFor("test.txt")); } @Test diff --git a/src/test/java/org/apache/commons/compress/archivers/jar/JarMarkerTest.java b/src/test/java/org/apache/commons/compress/archivers/jar/JarMarkerTest.java index 7dce86b..840ff50 100644 --- a/src/test/java/org/apache/commons/compress/archivers/jar/JarMarkerTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/jar/JarMarkerTest.java @@ -18,9 +18,12 @@ */ package org.apache.commons.compress.archivers.jar; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.fail; import java.util.zip.ZipException; + +import org.apache.commons.compress.archivers.ArchiveException; import org.apache.commons.compress.archivers.zip.JarMarker; import org.junit.jupiter.api.Test; @@ -28,13 +31,7 @@ public class JarMarkerTest { @Test public void testJarMarkerLengthCheck() { - final JarMarker jarMarker = JarMarker.getInstance(); - try { - jarMarker.parseFromLocalFileData(null,0,1); - fail("should have thrown exception due to length of 1"); - } catch (final ZipException e) { - - } + assertThrows(ZipException.class, () -> JarMarker.getInstance().parseFromLocalFileData(null, 0, 1)); } } diff --git a/src/test/java/org/apache/commons/compress/archivers/sevenz/CoverageTest.java b/src/test/java/org/apache/commons/compress/archivers/sevenz/CoverageTest.java index 127968f..760917e 100644 --- a/src/test/java/org/apache/commons/compress/archivers/sevenz/CoverageTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/sevenz/CoverageTest.java @@ -18,10 +18,10 @@ */ package org.apache.commons.compress.archivers.sevenz; -import org.junit.jupiter.api.Test; - import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; public class CoverageTest { @@ -34,11 +34,6 @@ public class CoverageTest { public void testCLIInstance() { final CLI foo = new CLI(); assertNotNull(foo); - try { - CLI.main(new String[]{"/dev/null/not-there"}); - fail("shouldn't be able to list contents of a file that isn't there"); - } catch (final Exception ignored) { - - } + assertThrows(Exception.class, () -> CLI.main(new String[] { "/dev/null/not-there" })); } } diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java index 4f3dc2a..89c2b0f 100644 --- a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java @@ -18,7 +18,7 @@ package org.apache.commons.compress.archivers.tar; -import static java.nio.charset.StandardCharsets.*; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.apache.commons.compress.AbstractTestCase.getFile; import static org.apache.commons.compress.AbstractTestCase.getPath; import static org.junit.Assert.assertEquals; @@ -26,8 +26,8 @@ import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import static org.junit.Assume.assumeTrue; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -38,6 +38,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Locale; + import org.apache.commons.compress.AbstractTestCase; import org.apache.commons.compress.archivers.zip.ZipEncodingHelper; import org.apache.commons.compress.utils.CharsetNames; @@ -126,11 +127,7 @@ public class TarArchiveEntryTest implements TarConstants { final TarArchiveEntry t = new TarArchiveEntry(""); t.setSize(0); t.setSize(1); - try { - t.setSize(-1); - fail("Should have generated IllegalArgumentException"); - } catch (final IllegalArgumentException expected) { - } + assertThrows(IllegalArgumentException.class, () -> t.setSize(-1)); t.setSize(077777777777L); t.setSize(0100000000000L); } diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java index 659d84e..5ffb286 100644 --- a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java @@ -18,13 +18,14 @@ package org.apache.commons.compress.archivers.tar; -import static java.nio.charset.StandardCharsets.*; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -41,6 +42,7 @@ import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.TimeZone; + import org.apache.commons.compress.AbstractTestCase; import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.ArchiveOutputStream; @@ -648,6 +650,7 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase { assertEquals("recordSize",512,tos.getRecordSize()); } } + @Test public void testBlockSizes() throws Exception { final String fileName = "/test1.xml"; @@ -656,22 +659,17 @@ public class TarArchiveOutputStreamTest extends AbstractTestCase { testPadding(5120, fileName, contents); // PAX default testPadding(1<<15, fileName, contents); //PAX max testPadding(-2, fileName, contents); // don't specify a block size -> use minimum length - try { - testPadding(511, fileName, contents); // don't specify a block size -> use minimum length - fail("should have thrown an illegal argument exception"); - } catch (final IllegalArgumentException e) { - //expected - } - try { - testPadding(0, fileName, contents); // don't specify a block size -> use minimum length - fail("should have thrown an illegal argument exception"); - } catch (final IllegalArgumentException e) { - //expected - } + + // don't specify a block size -> use minimum length + assertThrows(IllegalArgumentException.class, () -> testPadding(511, fileName, contents)); + + // don't specify a block size -> use minimum length + assertThrows(IllegalArgumentException.class, () -> testPadding(0, fileName, contents)); + // test with "content" that is an exact multiple of record length - contents = new byte[2048]; - java.util.Arrays.fill(contents, (byte) 42); - testPadding(TarConstants.DEFAULT_BLKSIZE, fileName, contents); + byte[] contents2 = new byte[2048]; + java.util.Arrays.fill(contents2, (byte) 42); + testPadding(TarConstants.DEFAULT_BLKSIZE, fileName, contents2); } private void testPadding(int blockSize, final String fileName, final byte[] contents) throws IOException { diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/BinaryTreeTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/BinaryTreeTest.java index 1d71fcb..263c3ef 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/BinaryTreeTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/BinaryTreeTest.java @@ -19,16 +19,16 @@ package org.apache.commons.compress.archivers.zip; -import org.junit.jupiter.api.Test; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import org.junit.jupiter.api.Test; + public class BinaryTreeTest { @Test @@ -49,28 +49,15 @@ public class BinaryTreeTest { assertEquals(6, tree.read(stream)); assertEquals(7, tree.read(stream)); } - @Test - public void testExceptions() { - BinaryTree binaryFinary = new BinaryTree(4); - binaryFinary.addLeaf(0,0,0,1); - try { - binaryFinary.addLeaf(0,0,0,1); - fail("should have thrown illegalArgumentException"); - } catch (final IllegalArgumentException e) { - } - - final InputStream is = new ByteArrayInputStream(new byte[]{}); - try { - BinaryTree.decode(is,0); - fail("should have thrown IOException"); - } catch (final IOException e) { - } - binaryFinary = new BinaryTree(4); - try { - binaryFinary.read(new BitStream(new ByteArrayInputStream(new byte[] {0}))); - fail("expected read fail"); - } catch (final IOException e) { - } + @Test + public void testExceptions() { + final BinaryTree binaryFinary = new BinaryTree(4); + binaryFinary.addLeaf(0, 0, 0, 1); + assertThrows(IllegalArgumentException.class, () -> binaryFinary.addLeaf(0, 0, 0, 1)); + + final InputStream is = new ByteArrayInputStream(new byte[] {}); + assertThrows(IOException.class, () -> BinaryTree.decode(is, 0)); + assertThrows(IOException.class, () -> new BinaryTree(4).read(new BitStream(new ByteArrayInputStream(new byte[] { 0 })))); } }