Skip to content

Commit

Permalink
Make test more readable and maintainable, and less verbose
Browse files Browse the repository at this point in the history
- Use JUnit 5 APIs
- Be consistent throughout tests
  • Loading branch information
garydgregory committed Oct 1, 2024
1 parent 733fa8f commit 32e83a5
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 59 deletions.
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,6 @@ Brotli, Zstandard and ar, cpio, jar, tar, zip, dump, 7z, arj.
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.luben</groupId>
<artifactId>zstd-jni</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@

package org.apache.commons.compress.archivers.ar;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand Down Expand Up @@ -153,28 +149,23 @@ public void testReadLongNamesGNU() throws Exception {
@Test
public void testSimpleInputStream() throws IOException {
try (InputStream fileInputStream = newInputStream("bla.ar");

// This default implementation of InputStream.available() always returns zero,
// and there are many streams in practice where the total length of the stream is not known.

InputStream simpleInputStream = new InputStream() {
@Override
public int read() throws IOException {
return fileInputStream.read();
}
}) {

try (ArArchiveInputStream archiveInputStream = new ArArchiveInputStream(simpleInputStream)) {
final ArArchiveEntry entry1 = archiveInputStream.getNextEntry();
assertThat(entry1, not(nullValue()));
assertThat(entry1.getName(), equalTo("test1.xml"));
assertThat(entry1.getLength(), equalTo(610L));

assertNotNull(entry1);
assertEquals("test1.xml", entry1.getName());
assertEquals(610L, entry1.getLength());
final ArArchiveEntry entry2 = archiveInputStream.getNextEntry();
assertThat(entry2.getName(), equalTo("test2.xml"));
assertThat(entry2.getLength(), equalTo(82L));

assertThat(archiveInputStream.getNextEntry(), nullValue());
assertEquals("test2.xml", entry2.getName());
assertEquals(82L, entry2.getLength());
assertNull(archiveInputStream.getNextEntry());
}
}
}
Expand All @@ -183,28 +174,23 @@ public int read() throws IOException {
@Test
public void testSimpleInputStreamDeprecated() throws IOException {
try (InputStream fileInputStream = newInputStream("bla.ar");

// This default implementation of InputStream.available() always returns zero,
// and there are many streams in practice where the total length of the stream is not known.

InputStream simpleInputStream = new InputStream() {
@Override
public int read() throws IOException {
return fileInputStream.read();
}
}) {

try (ArArchiveInputStream archiveInputStream = new ArArchiveInputStream(simpleInputStream)) {
final ArArchiveEntry entry1 = archiveInputStream.getNextArEntry();
assertThat(entry1, not(nullValue()));
assertThat(entry1.getName(), equalTo("test1.xml"));
assertThat(entry1.getLength(), equalTo(610L));

assertNotNull(entry1);
assertEquals("test1.xml", entry1.getName());
assertEquals(610L, entry1.getLength());
final ArArchiveEntry entry2 = archiveInputStream.getNextArEntry();
assertThat(entry2.getName(), equalTo("test2.xml"));
assertThat(entry2.getLength(), equalTo(82L));

assertThat(archiveInputStream.getNextArEntry(), nullValue());
assertEquals("test2.xml", entry2.getName());
assertEquals(82L, entry2.getLength());
assertNull(archiveInputStream.getNextArEntry());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
*/
package org.apache.commons.compress.archivers.zip;

import static org.hamcrest.MatcherAssert.assertThat;
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 java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.hamcrest.core.IsInstanceOf;
import org.junit.jupiter.api.Test;

public class ZipClassCoverageTest {
Expand All @@ -38,7 +37,7 @@ public void testConstantConstructor() throws NoSuchMethodException, IllegalAcces
assertFalse(constructor.isAccessible());
constructor.setAccessible(true);
final Object o = constructor.newInstance();
assertThat(o, IsInstanceOf.instanceOf(clazz));
assertInstanceOf(clazz, o);
constructor.setAccessible(false);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

package org.apache.commons.compress.archivers.zip;

import static org.hamcrest.MatcherAssert.assertThat;
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.assertTrue;

Expand All @@ -29,7 +29,6 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.hamcrest.core.IsInstanceOf;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -68,7 +67,7 @@ private void doSimpleEncodingsTest(final int n) throws IOException {
private void doSimpleEncodingTest(final String name, byte[] testBytes) throws IOException {

final ZipEncoding enc = ZipEncodingHelper.getZipEncoding(name);
assertThat(enc, IsInstanceOf.instanceOf(NioZipEncoding.class));
assertInstanceOf(NioZipEncoding.class, enc);
if (testBytes == null) {

testBytes = new byte[256];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
*/
package org.apache.commons.compress.compressors.lz4;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand Down Expand Up @@ -248,7 +246,7 @@ public void testRejectsBlocksWithoutChecksum() {
IOUtils.toByteArray(a);
}
}, "expected exception");
assertThat(ex.getMessage(), containsString("block checksum"));
assertTrue(ex.getMessage().contains("block checksum"));
}

@Test
Expand All @@ -261,7 +259,7 @@ public void testRejectsFileWithBadHeaderChecksum() {
try (InputStream a = new FramedLZ4CompressorInputStream(new ByteArrayInputStream(input))) {
}
}, "expected exception");
assertThat(ex.getMessage(), containsString("header checksum mismatch"));
assertTrue(ex.getMessage().contains("header checksum mismatch"));
}

@Test
Expand All @@ -274,7 +272,7 @@ public void testRejectsFileWithInsufficientContentSize() {
try (InputStream a = new FramedLZ4CompressorInputStream(new ByteArrayInputStream(input))) {
}
}, "expected exception");
assertThat(ex.getMessage(), containsString("content size"));
assertTrue(ex.getMessage().contains("content size"));
}

@Test
Expand All @@ -286,7 +284,7 @@ public void testRejectsFileWithoutBlockSizeByte() {
try (InputStream a = new FramedLZ4CompressorInputStream(new ByteArrayInputStream(input))) {
}
}, "expected exception");
assertThat(ex.getMessage(), containsString("BD byte"));
assertTrue(ex.getMessage().contains("BD byte"));
}

@Test
Expand All @@ -297,7 +295,7 @@ public void testRejectsFileWithoutFrameDescriptor() {
try (InputStream a = new FramedLZ4CompressorInputStream(new ByteArrayInputStream(input))) {
}
}, "expected exception");
assertThat(ex.getMessage(), containsString("frame flags"));
assertTrue(ex.getMessage().contains("frame flags"));
}

@Test
Expand All @@ -310,7 +308,7 @@ public void testRejectsFileWithoutHeaderChecksum() {
try (InputStream a = new FramedLZ4CompressorInputStream(new ByteArrayInputStream(input))) {
}
}, "expected exception");
assertThat(ex.getMessage(), containsString("header checksum"));
assertTrue(ex.getMessage().contains("header checksum"));
}

@Test
Expand All @@ -322,7 +320,7 @@ public void testRejectsFileWithWrongVersion() {
try (InputStream a = new FramedLZ4CompressorInputStream(new ByteArrayInputStream(input))) {
}
}, "expected exception");
assertThat(ex.getMessage(), containsString("version"));
assertTrue(ex.getMessage().contains("version"));
}

@Test
Expand All @@ -349,7 +347,7 @@ public void testRejectsSkippableFrameFollowedByJunk() {
IOUtils.toByteArray(a);
}
}, "expected exception");
assertThat(ex.getMessage(), containsString("garbage"));
assertTrue(ex.getMessage().contains("garbage"));
}

@Test
Expand All @@ -371,7 +369,7 @@ public void testRejectsSkippableFrameFollowedByTooFewBytes() {
IOUtils.toByteArray(a);
}
}, "expected exception");
assertThat(ex.getMessage(), containsString("garbage"));
assertTrue(ex.getMessage().contains("garbage"));
}

@Test
Expand All @@ -391,7 +389,7 @@ public void testRejectsSkippableFrameWithBadSignaturePrefix() {
fail();
}
}, "expected exception");
assertThat(ex.getMessage(), containsString("garbage"));
assertTrue(ex.getMessage().contains("garbage"));
}

@Test
Expand All @@ -410,7 +408,7 @@ public void testRejectsSkippableFrameWithBadSignatureTrailer() {
IOUtils.toByteArray(a);
}
}, "expected exception");
assertThat(ex.getMessage(), containsString("garbage"));
assertTrue(ex.getMessage().contains("garbage"));
}

@Test
Expand All @@ -431,7 +429,7 @@ public void testRejectsSkippableFrameWithPrematureEnd() {
IOUtils.toByteArray(a);
}
}, "expected exception");
assertThat(ex.getMessage(), containsString("Premature end of stream while skipping frame"));
assertTrue(ex.getMessage().contains("Premature end of stream while skipping frame"));
}

@Test
Expand All @@ -451,7 +449,7 @@ public void testRejectsSkippableFrameWithPrematureEndInLengthBytes() {
IOUtils.toByteArray(a);
}
}, "expected exception");
assertThat(ex.getMessage(), containsString("Premature end of data"));
assertTrue(ex.getMessage().contains("Premature end of data"));
}

@Test
Expand All @@ -469,7 +467,7 @@ public void testRejectsStreamsWithBadContentChecksum() {
IOUtils.toByteArray(a);
}
}, "expected exception");
assertThat(ex.getMessage(), containsString("content checksum mismatch"));
assertTrue(ex.getMessage().contains("content checksum mismatch"));
}

@Test
Expand All @@ -487,7 +485,7 @@ public void testRejectsStreamsWithoutContentChecksum() {
IOUtils.toByteArray(a);
}
}, "expected exception");
assertThat(ex.getMessage(), containsString("content checksum"));
assertTrue(ex.getMessage().contains("content checksum"));
}

@Test
Expand All @@ -506,7 +504,7 @@ public void testRejectsTrailingBytesAfterValidFrame() {
IOUtils.toByteArray(a);
}
}, "expected exception");
assertThat(ex.getMessage(), containsString("garbage"));
assertTrue(ex.getMessage().contains("garbage"));
}

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

import static java.nio.charset.StandardCharsets.US_ASCII;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -124,7 +122,7 @@ public int write(final ByteBuffer src) throws IOException {
}

private static void assertContainsAtOffset(final String msg, final byte[] expected, final int offset, final byte[] actual) {
assertThat(actual.length, greaterThanOrEqualTo(offset + expected.length));
assertTrue(actual.length >= offset + expected.length);
for (int i = 0; i < expected.length; i++) {
assertEquals(expected[i], actual[i + offset], String.format("%s ([%d])", msg, i));
}
Expand Down

0 comments on commit 32e83a5

Please sign in to comment.