diff --git a/.gitignore b/.gitignore index 31c18f1..0f1f716 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.iml +/.idea *.ipr *.iws *.log diff --git a/VERSION.txt b/VERSION.txt index f6d007f..c695e4a 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -1,5 +1,7 @@ 1.2.0 (not yet released) +#60: Convert tests to JUnit 5 & refactor tests + (contributed by @Marcono1234) #61: Prevent user code from subclassing `UnsafeChunkEncoder` (contributed by @Marcono1234) - Updated `oss-parent` dep to latest (v72) diff --git a/pom.xml b/pom.xml index 18a5f2e..8ccd3fe 100644 --- a/pom.xml +++ b/pom.xml @@ -1,30 +1,31 @@ - + 4.0.0 - com.fasterxml - oss-parent - 72 + com.fasterxml + oss-parent + 72 com.ning compress-lzf Compress-LZF 1.2.0-SNAPSHOT bundle - -Compression codec for LZF encoding for particularly encoding/decoding, with reasonable compression. -Compressor is basic Lempel-Ziv codec, without Huffman (deflate/gzip) or statistical post-encoding. -See "http://oldhome.schmorp.de/marc/liblzf.html" for more on original LZF package. + + Compression codec for LZF encoding for particularly encoding/decoding, with reasonable compression. + Compressor is basic Lempel-Ziv codec, without Huffman (deflate/gzip) or statistical post-encoding. + See "http://oldhome.schmorp.de/marc/liblzf.html" for more on original LZF package. https://github.com/ning/compress scm:git:git@github.com:ning/compress.git scm:git:git@github.com:ning/compress.git https://github.com/ning/compress - compress-lzf-1.1 + HEAD https://github.com/ning/compress/issues @@ -41,14 +42,14 @@ See "http://oldhome.schmorp.de/marc/liblzf.html" for more on original LZF packag - - Jon Hartlaub - jhartlaub@gmail.com - - - Cédrik Lime - 2013@cedrik.fr - + + Jon Hartlaub + jhartlaub@gmail.com + + + Cédrik Lime + 2013@cedrik.fr + @@ -57,23 +58,34 @@ See "http://oldhome.schmorp.de/marc/liblzf.html" for more on original LZF packag repo + + + + + org.junit + junit-bom + ${version.junit5} + pom + import + + + - org.testng - testng - 7.5.1 - jar + org.junit.jupiter + junit-jupiter test + install - - org.sonatype.central - central-publishing-maven-plugin - - + + org.sonatype.central + central-publishing-maven-plugin + + org.apache.maven.plugins maven-compiler-plugin @@ -89,7 +101,6 @@ See "http://oldhome.schmorp.de/marc/liblzf.html" for more on original LZF packag maven-javadoc-plugin 1.8 - 1.8 UTF-8 https://docs.oracle.com/javase/8/docs/api/ @@ -109,62 +120,62 @@ See "http://oldhome.schmorp.de/marc/liblzf.html" for more on original LZF packag org.apache.maven.plugins maven-release-plugin - forked-path + forked-path - org.apache.felix - maven-bundle-plugin - true - - - http://ning.com - - -sun.misc;resolution:=optional, * - - -com.ning.compress.lzf.impl - - - - - com.ning.compress.lzf.LZF - - - + org.apache.felix + maven-bundle-plugin + true + + + http://ning.com + + + sun.misc;resolution:=optional, * + + + com.ning.compress.lzf.impl + + + + + com.ning.compress.lzf.LZF + + + - - - org.moditect - moditect-maven-plugin - - - add-module-infos - package - - add-module-info - - - true - - src/moditect/module-info.java - - - - - + + + org.moditect + moditect-maven-plugin + + + add-module-infos + package + + add-module-info + + + true + + src/moditect/module-info.java + + + + + - + + + release-sign-artifacts diff --git a/src/test/java/com/ning/compress/gzip/TestGzipStreams.java b/src/test/java/com/ning/compress/gzip/TestGzipStreams.java index a6e6a6f..d75c4b8 100644 --- a/src/test/java/com/ning/compress/gzip/TestGzipStreams.java +++ b/src/test/java/com/ning/compress/gzip/TestGzipStreams.java @@ -1,12 +1,13 @@ package com.ning.compress.gzip; import java.io.*; +import java.nio.charset.StandardCharsets; import java.util.zip.*; -import org.testng.Assert; -import org.testng.annotations.Test; - import com.ning.compress.BaseForTests; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; public class TestGzipStreams extends BaseForTests { @@ -14,7 +15,7 @@ public class TestGzipStreams extends BaseForTests private final static byte[] INPUT_BYTES; static { try { - INPUT_BYTES = INPUT_STR.getBytes("UTF-8"); + INPUT_BYTES = INPUT_STR.getBytes(StandardCharsets.UTF_8); } catch (Exception e) { throw new RuntimeException(e); } @@ -33,7 +34,7 @@ public void testReusableInputStreams() throws IOException byte[] raw = bytes.toByteArray(); OptimizedGZIPInputStream re = new OptimizedGZIPInputStream(new ByteArrayInputStream(raw)); byte[] b = _readAll(re); - Assert.assertEquals(INPUT_BYTES, b); + assertArrayEquals(INPUT_BYTES, b); re.close(); } @@ -48,7 +49,7 @@ public void testReusableOutputStreams() throws IOException byte[] raw = bytes.toByteArray(); byte[] b = _readAll(new GZIPInputStream(new ByteArrayInputStream(raw))); - Assert.assertEquals(INPUT_BYTES, b); + assertArrayEquals(INPUT_BYTES, b); } private byte[] _readAll(InputStream in) throws IOException diff --git a/src/test/java/com/ning/compress/gzip/TestGzipUncompressor.java b/src/test/java/com/ning/compress/gzip/TestGzipUncompressor.java index c235dc0..c4c4f68 100644 --- a/src/test/java/com/ning/compress/gzip/TestGzipUncompressor.java +++ b/src/test/java/com/ning/compress/gzip/TestGzipUncompressor.java @@ -3,16 +3,16 @@ import java.io.*; import java.util.Random; -import org.testng.Assert; -import org.testng.annotations.Test; - import com.ning.compress.BaseForTests; import com.ning.compress.DataHandler; import com.ning.compress.UncompressorOutputStream; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; public class TestGzipUncompressor extends BaseForTests { - @Test + @Test public void testSimpleSmall1by1() throws IOException { byte[] fluff = constructFluff(4000); @@ -25,8 +25,8 @@ public void testSimpleSmall1by1() throws IOException } uncomp.complete(); byte[] result = co.getBytes(); - - Assert.assertEquals(fluff, result); + + assertArrayEquals(fluff, result); } @Test @@ -41,7 +41,7 @@ public void testSimpleSmallAsChunk() throws IOException uncomp.feedCompressedData(comp, 0, comp.length); uncomp.complete(); byte[] result = co.getBytes(); - Assert.assertEquals(fluff, result); + assertArrayEquals(fluff, result); } @Test @@ -61,8 +61,8 @@ public void testSimpleBiggerVarLength() throws IOException } uncomp.complete(); byte[] result = co.getBytes(); - - Assert.assertEquals(fluff, result); + + assertArrayEquals(fluff, result); } @Test @@ -77,8 +77,8 @@ public void testSimpleBiggerOneChunk() throws IOException uncomp.feedCompressedData(comp, 0, comp.length); uncomp.complete(); byte[] result = co.getBytes(); - - Assert.assertEquals(fluff, result); + + assertArrayEquals(fluff, result); } @Test @@ -91,8 +91,8 @@ public void testSimpleBiggerAsStream() throws IOException out.write(comp, 0, comp.length); out.close(); byte[] result = co.getBytes(); - - Assert.assertEquals(fluff, result); + + assertArrayEquals(fluff, result); } /* diff --git a/src/test/java/com/ning/compress/lzf/LZFEncoderTest.java b/src/test/java/com/ning/compress/lzf/LZFEncoderTest.java index 1a5cef2..799f7e1 100644 --- a/src/test/java/com/ning/compress/lzf/LZFEncoderTest.java +++ b/src/test/java/com/ning/compress/lzf/LZFEncoderTest.java @@ -3,11 +3,11 @@ import java.io.*; import java.util.Arrays; -import org.testng.Assert; -import org.testng.annotations.Test; - import com.ning.compress.BaseForTests; import com.ning.compress.lzf.util.ChunkEncoderFactory; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; public class LZFEncoderTest extends BaseForTests { @@ -26,7 +26,7 @@ public void testBigSizeEstimate() int expMin = 2 + amt + (chunks * 5); // 5-byte header for uncompressed; however, not enough workspace int expMax = ((int) (0.05 * 0xFFFF)) + amt + (chunks * 7); if (estimate < expMin || estimate > expMax) { - Assert.fail("Expected ratio for "+amt+" to be "+expMin+" <= x <= "+expMax+", was: "+estimate); + fail("Expected ratio for "+amt+" to be "+expMin+" <= x <= "+expMax+", was: "+estimate); } //System.err.printf("%d < %d < %d\n", expMin, estimate, expMax); } @@ -58,14 +58,12 @@ private void _testCompressableChunksSingle(byte[] source, ChunkEncoder encoder) // and make sure we get identical compression byte[] bufferAsBlock = Arrays.copyOf(buffer, compLen); byte[] asBlockStd = LZFEncoder.encode(source); - Assert.assertEquals(compLen, asBlockStd.length); - Assert.assertEquals(bufferAsBlock, asBlockStd); + assertArrayEquals(bufferAsBlock, asBlockStd); // then uncompress, verify byte[] uncomp = uncompress(buffer, 0, compLen); - Assert.assertEquals(uncomp.length, source.length); - Assert.assertEquals(uncomp, source); + assertArrayEquals(source, uncomp); } @Test @@ -85,14 +83,12 @@ private void _testCompressableChunksMulti(byte[] source, ChunkEncoder encoder) t // and make sure we get identical compression byte[] bufferAsBlock = Arrays.copyOf(buffer, compLen); byte[] asBlockStd = LZFEncoder.encode(encoder, source, 0, source.length); - Assert.assertEquals(compLen, asBlockStd.length); - Assert.assertEquals(bufferAsBlock, asBlockStd); + assertArrayEquals(bufferAsBlock, asBlockStd); // then uncompress, verify byte[] uncomp = uncompress(buffer, 0, compLen); - Assert.assertEquals(uncomp.length, source.length); - Assert.assertEquals(uncomp, source); + assertArrayEquals(source, uncomp); } @Test @@ -111,14 +107,12 @@ private void _testNonCompressableChunksSingle(byte[] source, ChunkEncoder encode // and make sure we get identical compression byte[] bufferAsBlock = Arrays.copyOf(buffer, compLen); byte[] asBlockStd = LZFEncoder.encode(encoder, source, 0, source.length); - Assert.assertEquals(compLen, asBlockStd.length); - Assert.assertEquals(bufferAsBlock, asBlockStd); + assertArrayEquals(bufferAsBlock, asBlockStd); // then uncompress, verify byte[] uncomp = uncompress(buffer, 0, compLen); - Assert.assertEquals(uncomp.length, source.length); - Assert.assertEquals(uncomp, source); + assertArrayEquals(source, uncomp); } @Test @@ -136,37 +130,37 @@ private void _testConditionalCompression(ChunkEncoder enc, final byte[] input) t byte[] comp = enc.encodeChunk(input, 0, input.length).getData(); int pct = (int) (100.0 * comp.length / input.length); // happens to compress to about 61%, good - Assert.assertEquals(pct, 61); + assertEquals(61, pct); // should be ok if we only require down to 70% compression byte[] buf = new byte[60000]; int offset = enc.appendEncodedIfCompresses(input, 0.70, 0, input.length, buf, 0); - Assert.assertEquals(offset, comp.length); + assertEquals(comp.length, offset); // but not to 60% offset = enc.appendEncodedIfCompresses(input, 0.60, 0, input.length, buf, 0); - Assert.assertEquals(offset, -1); + assertEquals(-1, offset); // // // Second part: OutputStream alternatives ByteArrayOutputStream bytes = new ByteArrayOutputStream(60000); - Assert.assertTrue(enc.encodeAndWriteChunkIfCompresses(input, 0, input.length, bytes, 0.70)); - Assert.assertEquals(comp.length, bytes.size()); + assertTrue(enc.encodeAndWriteChunkIfCompresses(input, 0, input.length, bytes, 0.70)); + assertEquals(comp.length, bytes.size()); byte[] output = bytes.toByteArray(); - Assert.assertEquals(output, comp); + assertArrayEquals(comp, output); bytes = new ByteArrayOutputStream(60000); - Assert.assertFalse(enc.encodeAndWriteChunkIfCompresses(input, 0, input.length, bytes, 0.60)); - Assert.assertEquals(0, bytes.size()); + assertFalse(enc.encodeAndWriteChunkIfCompresses(input, 0, input.length, bytes, 0.60)); + assertEquals(0, bytes.size()); // // // Third part: chunk creation LZFChunk chunk = enc.encodeChunkIfCompresses(input, 0, input.length, 0.70); - Assert.assertNotNull(chunk); - Assert.assertEquals(chunk.length(), comp.length); - Assert.assertEquals(chunk.getData(), comp); + assertNotNull(chunk); + assertEquals(comp.length, chunk.length()); + assertArrayEquals(comp, chunk.getData()); chunk = enc.encodeChunkIfCompresses(input, 0, input.length, 0.60); - Assert.assertNull(chunk); + assertNull(chunk); } } diff --git a/src/test/lzf/TestLZF.java b/src/test/java/com/ning/compress/lzf/ManualTestLZF.java similarity index 85% rename from src/test/lzf/TestLZF.java rename to src/test/java/com/ning/compress/lzf/ManualTestLZF.java index 38ced2e..4fb5d69 100644 --- a/src/test/lzf/TestLZF.java +++ b/src/test/java/com/ning/compress/lzf/ManualTestLZF.java @@ -9,27 +9,27 @@ * governing permissions and limitations under the License. */ -package lzf; +package com.ning.compress.lzf; import java.io.*; -import junit.framework.TestCase; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; /** * Semi-automatic non-unit test: will use all files on current working * directory (and its subdirs) for testing that LZF encode+decode * will correctly round-trip content. */ -public class TestLZF +public class ManualTestLZF { - public void testWithFiles() throws Exception + public static void main(String... args) throws Exception { File currDir = new File("").getAbsoluteFile(); int count = _handleFiles(currDir); System.out.println("OK: tested with "+count+" files."); } - private int _handleFiles(File dir) throws IOException + private static int _handleFiles(File dir) throws IOException { System.out.println("Testing files from dir '"+dir.getAbsolutePath()+"'..."); int count = 0; @@ -40,7 +40,7 @@ private int _handleFiles(File dir) throws IOException byte[] data = _readData(f); byte[] enc = LZFEncoder.encode(data); byte[] dec = LZFDecoder.decode(enc); - assertArrayEquals("File '"+f.getAbsolutePath()+"'", data, dec); + assertArrayEquals(data, dec, "File '"+f.getAbsolutePath()+"'"); ++count; } } diff --git a/src/test/java/com/ning/compress/lzf/TestLZFCompressingInputStream.java b/src/test/java/com/ning/compress/lzf/TestLZFCompressingInputStream.java index 2acb65a..3c458b4 100644 --- a/src/test/java/com/ning/compress/lzf/TestLZFCompressingInputStream.java +++ b/src/test/java/com/ning/compress/lzf/TestLZFCompressingInputStream.java @@ -2,10 +2,11 @@ import java.io.*; -import org.testng.Assert; -import org.testng.annotations.Test; - import com.ning.compress.BaseForTests; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; public class TestLZFCompressingInputStream extends BaseForTests { @@ -17,11 +18,11 @@ public void testSimpleCompression() throws IOException LZFCompressingInputStream compIn = new LZFCompressingInputStream(new ByteArrayInputStream(source)); byte[] comp = readAll(compIn); byte[] uncomp = uncompress(comp); - Assert.assertEquals(uncomp, source); + assertArrayEquals(source, uncomp); // and then check that size is about same as with static methods byte[] comp2 = compress(source); - Assert.assertEquals(comp2.length, comp.length); + assertEquals(comp.length, comp2.length); } @Test @@ -32,8 +33,8 @@ public void testSimpleNonCompressed() throws IOException LZFCompressingInputStream compIn = new LZFCompressingInputStream(new ByteArrayInputStream(source)); byte[] comp = readAll(compIn); // 2 non-compressed chunks with headers: - Assert.assertEquals(comp.length, 89000 + 5 + 5); + assertEquals(89000 + 5 + 5, comp.length); byte[] uncomp = uncompress(comp); - Assert.assertEquals(uncomp, source); + assertArrayEquals(source, uncomp); } } diff --git a/src/test/java/com/ning/compress/lzf/TestLZFDecoder.java b/src/test/java/com/ning/compress/lzf/TestLZFDecoder.java index b2718c4..2114a35 100644 --- a/src/test/java/com/ning/compress/lzf/TestLZFDecoder.java +++ b/src/test/java/com/ning/compress/lzf/TestLZFDecoder.java @@ -1,12 +1,13 @@ package com.ning.compress.lzf; import java.io.*; - -import org.testng.Assert; -import org.testng.annotations.Test; +import java.nio.charset.StandardCharsets; import com.ning.compress.BaseForTests; import com.ning.compress.lzf.util.ChunkDecoderFactory; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; public class TestLZFDecoder extends BaseForTests { @@ -36,21 +37,21 @@ public void testChunks() throws IOException { private void _testSimple(ChunkDecoder decoder) throws IOException { - byte[] orig = "Another trivial test".getBytes("UTF-8"); + byte[] orig = "Another trivial test".getBytes(StandardCharsets.UTF_8); byte[] compressed = compress(orig); byte[] result = decoder.decode(compressed); - Assert.assertEquals(result, orig); + assertArrayEquals(orig, result); // also, ensure that offset, length are passed byte[] compressed2 = new byte[compressed.length + 4]; System.arraycopy(compressed, 0, compressed2, 2, compressed.length); result = decoder.decode(compressed2, 2, compressed.length); - Assert.assertEquals(result, orig); + assertArrayEquals(orig, result); // two ways to do that as well: result = LZFDecoder.decode(compressed2, 2, compressed.length); - Assert.assertEquals(result, orig); + assertArrayEquals(orig, result); } private void _testLonger(ChunkDecoder decoder) throws IOException @@ -58,24 +59,24 @@ private void _testLonger(ChunkDecoder decoder) throws IOException byte[] orig = this.constructFluff(250000); // 250k byte[] compressed = compress(orig); byte[] result = decoder.decode(compressed); - Assert.assertEquals(result, orig); + assertArrayEquals(orig, result); // also, ensure that offset, length are passed byte[] compressed2 = new byte[compressed.length + 4]; System.arraycopy(compressed, 0, compressed2, 2, compressed.length); result = decoder.decode(compressed2, 2, compressed.length); - Assert.assertEquals(result, orig); + assertArrayEquals(orig, result); // two ways to do that as well: result = LZFDecoder.decode(compressed2, 2, compressed.length); - Assert.assertEquals(result, orig); + assertArrayEquals(orig, result); } private void _testChunks(ChunkDecoder decoder) throws IOException { - byte[] orig1 = "Another trivial test".getBytes("UTF-8"); - byte[] orig2 = " with some of repepepepepetitition too!".getBytes("UTF-8"); + byte[] orig1 = "Another trivial test".getBytes(StandardCharsets.UTF_8); + byte[] orig2 = " with some of repepepepepetitition too!".getBytes(StandardCharsets.UTF_8); ByteArrayOutputStream out = new ByteArrayOutputStream(); out.write(orig1); out.write(orig2); @@ -89,6 +90,6 @@ private void _testChunks(ChunkDecoder decoder) throws IOException byte[] compressed = out.toByteArray(); byte[] result = decoder.decode(compressed); - Assert.assertEquals(result, orig); + assertArrayEquals(orig, result); } } diff --git a/src/test/java/com/ning/compress/lzf/TestLZFInputStream.java b/src/test/java/com/ning/compress/lzf/TestLZFInputStream.java index df22a78..d9e7c2c 100644 --- a/src/test/java/com/ning/compress/lzf/TestLZFInputStream.java +++ b/src/test/java/com/ning/compress/lzf/TestLZFInputStream.java @@ -1,30 +1,31 @@ package com.ning.compress.lzf; import java.io.*; +import java.nio.charset.StandardCharsets; import java.util.Random; import java.security.SecureRandom; -import org.testng.Assert; -import org.testng.annotations.BeforeTest; -import org.testng.annotations.Test; - import com.ning.compress.BaseForTests; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; public class TestLZFInputStream extends BaseForTests { - private static int BUFFER_SIZE = LZFChunk.MAX_CHUNK_LEN * 64; - private byte[] nonEncodableBytesToWrite = new byte[BUFFER_SIZE]; - private byte[] bytesToWrite = new byte[BUFFER_SIZE]; + private static final int BUFFER_SIZE = LZFChunk.MAX_CHUNK_LEN * 64; + private final byte[] nonEncodableBytesToWrite = new byte[BUFFER_SIZE]; + private final byte[] bytesToWrite = new byte[BUFFER_SIZE]; private byte[] nonCompressableBytes; - private int compressableInputLength = BUFFER_SIZE; + private final int compressableInputLength = BUFFER_SIZE; private byte[] compressedBytes; - @BeforeTest(alwaysRun = true) + @BeforeEach public void setUp() throws Exception { SecureRandom.getInstance("SHA1PRNG").nextBytes(nonEncodableBytesToWrite); String phrase = "all work and no play make Jack a dull boy"; - byte[] bytes = phrase.getBytes(); + byte[] bytes = phrase.getBytes(StandardCharsets.UTF_8); int cursor = 0; while(cursor <= bytesToWrite.length) { System.arraycopy(bytes, 0, bytesToWrite, cursor, (bytes.length+cursor < bytesToWrite.length)?bytes.length:bytesToWrite.length-cursor); @@ -68,13 +69,13 @@ public void testRead0() throws IOException { ByteArrayInputStream bis = new ByteArrayInputStream(compressedBytes); InputStream is = new LZFInputStream(bis); - Assert.assertEquals(0, is.available()); + assertEquals(0, is.available()); byte[] buffer = new byte[65536+23]; int val = is.read(buffer, 0, 0); // read of 0 or less should return a 0-byte read. - Assert.assertEquals(0, val); + assertEquals(0, val); val = is.read(buffer, 0, -1); - Assert.assertEquals(0, val); + assertEquals(0, val); // close should work. is.close(); } @@ -84,25 +85,25 @@ public void testAvailable() throws IOException { ByteArrayInputStream bis = new ByteArrayInputStream(compressedBytes); LZFInputStream is = new LZFInputStream(bis); - Assert.assertSame(is.getUnderlyingInputStream(), bis); - Assert.assertEquals(0, is.available()); + assertSame(bis, is.getUnderlyingInputStream()); + assertEquals(0, is.available()); // read one byte; should decode bunch more, make available is.read(); int total = 1; // since we read one byte already - Assert.assertEquals(is.available(), 65534); + assertEquals(65534, is.available()); // and after we skip through all of it, end with -1 for EOF long count; while ((count = is.skip(16384L)) > 0L) { total += (int) count; } // nothing more available; but we haven't yet closed so: - Assert.assertEquals(is.available(), 0); + assertEquals(0, is.available()); // and then we close it: is.close(); - Assert.assertEquals(is.available(), 0); - Assert.assertEquals(total, compressableInputLength); + assertEquals(0, is.available()); + assertEquals(compressableInputLength, total); } - + @Test void testIncrementalWithFullReads() throws IOException { doTestIncremental(true); } @@ -121,8 +122,7 @@ public void testReadAndWrite() throws Exception in.readAndWrite(bytes); in.close(); byte[] actual = bytes.toByteArray(); - Assert.assertEquals(actual.length, fluff.length); - Assert.assertEquals(actual, fluff); + assertArrayEquals(fluff, actual); } // Mostly for [Issue#19] @@ -138,13 +138,13 @@ public void testLongSkips() throws Exception LZFInputStream in = new LZFInputStream(new ByteArrayInputStream(comp)); // read one byte for fun - Assert.assertEquals(in.read(), fluff[0] & 0xFF); + assertEquals(fluff[0] & 0xFF, in.read()); // then skip all but one long amt = in.skip(LENGTH-2); - Assert.assertEquals(amt, (long) (LENGTH-2)); - Assert.assertEquals(in.read(), fluff[LENGTH-1] & 0xFF); + assertEquals(LENGTH-2, amt); + assertEquals(fluff[LENGTH-1] & 0xFF, in.read()); - Assert.assertEquals(in.read(), -1); + assertEquals(-1, in.read()); in.close(); } @@ -172,7 +172,7 @@ private void doTestIncremental(boolean fullReads) throws IOException sb.append(i); } } - byte[] uncomp = sb.toString().getBytes("UTF-8"); + byte[] uncomp = sb.toString().getBytes(StandardCharsets.UTF_8); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); LZFOutputStream lzOut = new LZFOutputStream(bytes); lzOut.write(uncomp); @@ -194,7 +194,7 @@ private void doTestIncremental(boolean fullReads) throws IOException break; } if (count > len) { - Assert.fail("Requested "+len+" bytes (offset "+offset+", array length "+buffer.length+"), got "+count); + fail("Requested "+len+" bytes (offset "+offset+", array length "+buffer.length+"), got "+count); } pos += count; // with full reads, ought to get full results @@ -202,15 +202,14 @@ private void doTestIncremental(boolean fullReads) throws IOException if (fullReads) { // Except at the end, with last incomplete chunk if (pos != uncomp.length) { - Assert.fail("Got partial read (when requested full read!), position "+pos+" (of full "+uncomp.length+")"); + fail("Got partial read (when requested full read!), position "+pos+" (of full "+uncomp.length+")"); } } } bytes.write(buffer, offset, count); } byte[] result = bytes.toByteArray(); - Assert.assertEquals(result.length, uncomp.length); - Assert.assertEquals(result, uncomp); + assertArrayEquals(uncomp, result); lzIn.close(); } @@ -219,10 +218,10 @@ private void doDecompressReadByte(byte[] bytes, byte[] reference) throws IOExcep ByteArrayInputStream bis = new ByteArrayInputStream(bytes); InputStream is = new LZFInputStream(bis); int i = 0; - int testVal = 0; + int testVal; while((testVal=is.read()) != -1) { int rVal = ((int)reference[i]) & 255; - Assert.assertEquals(rVal, testVal); + assertEquals(rVal, testVal); ++i; } is.close(); @@ -238,11 +237,11 @@ private void doDecompressReadBlock(byte[] bytes, byte[] reference) throws IOExce while((val=is.read(buffer)) != -1) { for(int i = 0; i < val; i++) { byte testVal = buffer[i]; - Assert.assertTrue(testVal == reference[outputBytes]); + assertEquals(reference[outputBytes], testVal); ++outputBytes; } } - Assert.assertTrue(outputBytes == reference.length); + assertEquals(reference.length, outputBytes); is.close(); } } diff --git a/src/test/java/com/ning/compress/lzf/TestLZFOutputStream.java b/src/test/java/com/ning/compress/lzf/TestLZFOutputStream.java index bf82539..d923ce3 100644 --- a/src/test/java/com/ning/compress/lzf/TestLZFOutputStream.java +++ b/src/test/java/com/ning/compress/lzf/TestLZFOutputStream.java @@ -3,26 +3,27 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; -import org.testng.Assert; -import org.testng.annotations.BeforeTest; -import org.testng.annotations.Test; +import org.junit.jupiter.api.BeforeEach; import com.ning.compress.BaseForTests; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; public class TestLZFOutputStream extends BaseForTests { - private static int BUFFER_SIZE = LZFChunk.MAX_CHUNK_LEN * 64; + private static final int BUFFER_SIZE = LZFChunk.MAX_CHUNK_LEN * 64; private byte[] nonEncodableBytesToWrite; private byte[] bytesToWrite; - @BeforeTest(alwaysRun = true) - public void setUp() throws Exception - { + @BeforeEach + public void setUp() { nonEncodableBytesToWrite = constructUncompressable(BUFFER_SIZE); String phrase = "all work and no play make Jack a dull boy"; bytesToWrite = new byte[BUFFER_SIZE]; - byte[] bytes = phrase.getBytes(); + byte[] bytes = phrase.getBytes(StandardCharsets.UTF_8); int cursor = 0; while(cursor <= bytesToWrite.length) { System.arraycopy(bytes, 0, bytesToWrite, cursor, (bytes.length+cursor < bytesToWrite.length)?bytes.length:bytesToWrite.length-cursor); @@ -37,7 +38,7 @@ public void testUnencodable() throws Exception OutputStream os = new LZFOutputStream(bos); os.write(nonEncodableBytesToWrite); os.close(); - Assert.assertTrue(bos.toByteArray().length > nonEncodableBytesToWrite.length); + assertTrue(bos.toByteArray().length > nonEncodableBytesToWrite.length); verifyOutputStream(bos, nonEncodableBytesToWrite); } @@ -51,7 +52,7 @@ public void testStreaming() throws Exception int len = bos.toByteArray().length; int max = bytesToWrite.length/2; if (len <= 10 || len >= max) { - Assert.fail("Sanity check: should have 10 < len < "+max+"; len = "+len); + fail("Sanity check: should have 10 < len < "+max+"; len = "+len); } verifyOutputStream(bos, bytesToWrite); } @@ -72,7 +73,7 @@ public void testSingleByte() throws Exception int len = bos.toByteArray().length; int max = bytesToWrite.length/2; if (len <= 10 || len >= max) { - Assert.fail("Sanity check: should have 10 < len < "+max+"; len = "+len); + fail("Sanity check: should have 10 < len < "+max+"; len = "+len); } verifyOutputStream(bos, bytesToWrite); } @@ -86,8 +87,8 @@ public void testPartialBuffer() throws Exception OutputStream os = new LZFOutputStream(bos); os.write(bytesToWrite, offset, len); os.close(); - Assert.assertTrue(bos.toByteArray().length > 10); - Assert.assertTrue(bos.toByteArray().length < bytesToWrite.length*.5); + assertTrue(bos.toByteArray().length > 10); + assertTrue(bos.toByteArray().length < bytesToWrite.length*.5); int bytesToCopy = Math.min(len, bytesToWrite.length); byte[] compareBytes = new byte[bytesToCopy]; System.arraycopy(bytesToWrite, offset, compareBytes, 0, bytesToCopy); @@ -104,7 +105,7 @@ public void testEmptyBuffer() throws Exception os.close(); int len = bos.toByteArray().length; if (len != 0) { - Assert.fail("Sanity check: should have len == 0; len = "+len); + fail("Sanity check: should have len == 0; len = "+len); } verifyOutputStream(bos, input); } @@ -113,11 +114,11 @@ private void verifyOutputStream(ByteArrayOutputStream bos, byte[] reference) thr { ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); LZFInputStream lzfi = new LZFInputStream(bis); - int val =0; + int val; int idx = 0; while((val = lzfi.read()) != -1) { int refVal = ((int)reference[idx++]) & 255; - Assert.assertEquals(refVal, val); + assertEquals(refVal, val); } lzfi.close(); } diff --git a/src/test/java/com/ning/compress/lzf/TestLZFRoundTrip.java b/src/test/java/com/ning/compress/lzf/TestLZFRoundTrip.java index 9ab4e51..bd926f2 100644 --- a/src/test/java/com/ning/compress/lzf/TestLZFRoundTrip.java +++ b/src/test/java/com/ning/compress/lzf/TestLZFRoundTrip.java @@ -1,16 +1,16 @@ package com.ning.compress.lzf; import java.io.*; -import java.util.Arrays; +import java.nio.file.Files; +import java.nio.file.Path; -import org.testng.Assert; -import org.testng.annotations.Test; - -import com.ning.compress.BaseForTests; -import com.ning.compress.lzf.LZFChunk; import com.ning.compress.lzf.impl.UnsafeChunkDecoder; import com.ning.compress.lzf.impl.VanillaChunkDecoder; import com.ning.compress.lzf.util.ChunkEncoderFactory; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import static org.junit.jupiter.api.Assertions.*; public class TestLZFRoundTrip { @@ -23,22 +23,25 @@ public class TestLZFRoundTrip ,"/binary/help.bin" ,"/binary/word.doc" }; - - @Test + + @TempDir + Path tempDir; + + @Test public void testVanillaCodec() throws Exception { _testUsingBlock(new VanillaChunkDecoder()); _testUsingReader(new VanillaChunkDecoder()); } - @Test + @Test public void testUnsafeCodec() throws IOException { _testUsingBlock(new UnsafeChunkDecoder()); _testUsingReader(new UnsafeChunkDecoder()); } - @Test + @Test public void testLZFCompressionOnTestFiles() throws IOException { for (int i = 0; i < 100; i++) { testLZFCompressionOnDir(new File("src/test/resources/shakespeare")); @@ -48,47 +51,44 @@ public void testLZFCompressionOnTestFiles() throws IOException { private void testLZFCompressionOnDir(File dir) throws IOException { File[] files = dir.listFiles(); - for (int i = 0; i < files.length; i++) { - File file = files[i]; + for (File file : files) { if (!file.isDirectory()) { - testLZFCompressionOnFile(file); + testLZFCompressionOnFile(file.toPath()); } else { testLZFCompressionOnDir(file); } } } - private void testLZFCompressionOnFile(File file) throws IOException + private void testLZFCompressionOnFile(Path file) throws IOException { final ChunkDecoder decoder = new UnsafeChunkDecoder(); - - // File compressedFile = createEmptyFile("test.lzf"); - File compressedFile = new File("/tmp/test.lzf"); - InputStream in = new BufferedInputStream(new FileInputStream(file)); - OutputStream out = new LZFOutputStream(new BufferedOutputStream( - new FileOutputStream(compressedFile))); byte[] buf = new byte[64 * 1024]; - int len; - while ((len = in.read(buf, 0, buf.length)) >= 0) { - out.write(buf, 0, len); + + Path compressedFile = Files.createTempFile(tempDir, "test", ".lzf"); + try (InputStream in = new BufferedInputStream(Files.newInputStream(file)); + OutputStream out = new LZFOutputStream(new BufferedOutputStream( + Files.newOutputStream(compressedFile)))) { + int len; + while ((len = in.read(buf, 0, buf.length)) >= 0) { + out.write(buf, 0, len); + } } - in.close(); - out.close(); // decompress and verify bytes haven't changed - in = new BufferedInputStream(new FileInputStream(file)); - DataInputStream compressedIn = new DataInputStream(new LZFInputStream(decoder, - new FileInputStream(compressedFile), false)); - while ((len = in.read(buf, 0, buf.length)) >= 0) { - byte[] buf2 = new byte[len]; - compressedIn.readFully(buf2, 0, len); - byte[] trimmedBuf = new byte[len]; - System.arraycopy(buf, 0, trimmedBuf, 0, len); - Assert.assertEquals(trimmedBuf, buf2); + try (InputStream in = new BufferedInputStream(Files.newInputStream(file)); + DataInputStream compressedIn = new DataInputStream(new LZFInputStream(decoder, + Files.newInputStream(compressedFile), false))) { + int len; + while ((len = in.read(buf, 0, buf.length)) >= 0) { + byte[] buf2 = new byte[len]; + compressedIn.readFully(buf2, 0, len); + byte[] trimmedBuf = new byte[len]; + System.arraycopy(buf, 0, trimmedBuf, 0, len); + assertArrayEquals(trimmedBuf, buf2); + } + assertEquals(-1, compressedIn.read()); } - Assert.assertEquals(-1, compressedIn.read()); - in.close(); - compressedIn.close(); } @Test @@ -107,12 +107,12 @@ public void testHashCollision() throws IOException ChunkEncoder encoder = ChunkEncoderFactory.safeInstance(); ChunkDecoder decoder = new VanillaChunkDecoder(); _testCollision(encoder, decoder, b1, 0, b1.length); - _testCollision(encoder, decoder, b2, off, b2.length - off); + _testCollision(encoder, decoder, b2, off, b2.length - off); encoder = ChunkEncoderFactory.optimalInstance(); decoder = new UnsafeChunkDecoder(); _testCollision(encoder, decoder, b1, 0, b1.length); - _testCollision(encoder, decoder, b2, off, b2.length - off); + _testCollision(encoder, decoder, b2, off, b2.length - off); } private void _testCollision(ChunkEncoder encoder, ChunkDecoder decoder, byte[] bytes, int offset, int length) throws IOException @@ -124,8 +124,8 @@ private void _testCollision(ChunkEncoder encoder, ChunkDecoder decoder, byte[] b System.arraycopy(bytes, offset, expected, 0, length); encoder.encodeAndWriteChunk(bytes, offset, length, outputStream); InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); - Assert.assertEquals(decoder.decodeChunk(inputStream, buffer, output), length); - Assert.assertEquals(expected, output); + assertEquals(length, decoder.decodeChunk(inputStream, buffer, output)); + assertArrayEquals(expected, output); } /* @@ -142,8 +142,7 @@ protected void _testUsingBlock(ChunkDecoder decoder) throws IOException byte[] lzf = LZFEncoder.encode(data); byte[] decoded = decoder.decode(lzf); - Assert.assertEquals(decoded.length, data.length); - Assert.assertEquals(decoded, data, + assertArrayEquals(data, decoded, String.format("File '%s', %d->%d bytes", name, data.length, lzf.length)); } } @@ -155,12 +154,11 @@ protected void _testUsingReader(ChunkDecoder decoder) throws IOException byte[] lzf = LZFEncoder.encode(data); LZFInputStream comp = new LZFInputStream(decoder, new ByteArrayInputStream(lzf), false); byte[] decoded = readAll(comp); - - Assert.assertEquals(decoded.length, data.length); - Assert.assertEquals(decoded, data); + + assertArrayEquals(data, decoded); } } - + protected byte[] readResource(String name) throws IOException { return readAll(getClass().getResourceAsStream(name)); @@ -168,7 +166,7 @@ protected byte[] readResource(String name) throws IOException protected byte[] readAll(InputStream in) throws IOException { - Assert.assertNotNull(in); + assertNotNull(in); byte[] buffer = new byte[4000]; int count; ByteArrayOutputStream bytes = new ByteArrayOutputStream(4000); diff --git a/src/test/java/com/ning/compress/lzf/TestLZFUncompressor.java b/src/test/java/com/ning/compress/lzf/TestLZFUncompressor.java index 97c2e62..4f7ed8c 100644 --- a/src/test/java/com/ning/compress/lzf/TestLZFUncompressor.java +++ b/src/test/java/com/ning/compress/lzf/TestLZFUncompressor.java @@ -3,16 +3,16 @@ import java.io.*; import java.util.Random; -import org.testng.Assert; -import org.testng.annotations.Test; - import com.ning.compress.BaseForTests; import com.ning.compress.DataHandler; import com.ning.compress.UncompressorOutputStream; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; public class TestLZFUncompressor extends BaseForTests { - @Test + @Test public void testSimpleSmall1by1() throws IOException { byte[] fluff = constructFluff(4000); @@ -26,7 +26,7 @@ public void testSimpleSmall1by1() throws IOException uncomp.complete(); byte[] result = co.getBytes(); - Assert.assertEquals(fluff, result); + assertArrayEquals(fluff, result); } @Test @@ -41,7 +41,7 @@ public void testSimpleSmallAsChunk() throws IOException uncomp.feedCompressedData(comp, 0, comp.length); uncomp.complete(); byte[] result = co.getBytes(); - Assert.assertEquals(fluff, result); + assertArrayEquals(fluff, result); } @Test @@ -62,7 +62,7 @@ public void testSimpleBiggerVarLength() throws IOException uncomp.complete(); byte[] result = co.getBytes(); - Assert.assertEquals(fluff, result); + assertArrayEquals(fluff, result); } @Test @@ -78,7 +78,7 @@ public void testSimpleBiggerOneChunk() throws IOException uncomp.complete(); byte[] result = co.getBytes(); - Assert.assertEquals(fluff, result); + assertArrayEquals(fluff, result); } @@ -93,7 +93,7 @@ public void testSimpleBiggerAsStream() throws IOException out.close(); byte[] result = co.getBytes(); - Assert.assertEquals(fluff, result); + assertArrayEquals(fluff, result); } private final static class Collector implements DataHandler diff --git a/src/test/java/com/ning/compress/lzf/util/TestFileStreams.java b/src/test/java/com/ning/compress/lzf/util/TestFileStreams.java index da48be6..0cbe57c 100644 --- a/src/test/java/com/ning/compress/lzf/util/TestFileStreams.java +++ b/src/test/java/com/ning/compress/lzf/util/TestFileStreams.java @@ -1,22 +1,28 @@ package com.ning.compress.lzf.util; import java.io.*; - -import org.testng.annotations.Test; -import org.testng.Assert; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; import com.ning.compress.BaseForTests; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; public class TestFileStreams extends BaseForTests { - @Test + @TempDir + Path tempDir; + + @Test public void testStreams() throws Exception { - File f = File.createTempFile("lzf-test", ".lzf"); - f.deleteOnExit(); + File f = tempDir.resolve("lzf-test.lzf").toFile(); // First, write encoded stuff (won't compress, but produces something) - byte[] input = "Whatever stuff...".getBytes("UTF-8"); + byte[] input = "Whatever stuff...".getBytes(StandardCharsets.UTF_8); LZFFileOutputStream out = new LZFFileOutputStream(f); out.write(input); @@ -24,21 +30,20 @@ public void testStreams() throws Exception long len = f.length(); // happens to be 22; 17 bytes uncompressed, with 5 byte header - Assert.assertEquals(len, 22L); + assertEquals(22L, len); LZFFileInputStream in = new LZFFileInputStream(f); - for (int i = 0; i < input.length; ++i) { - Assert.assertEquals(in.read(), input[i] & 0xFF); + for (byte b : input) { + assertEquals(b & 0xFF, in.read()); } - Assert.assertEquals(in.read(), -1); + assertEquals(-1, in.read()); in.close(); } @Test public void testReadAndWrite() throws Exception { - File f = File.createTempFile("lzf-test", ".lzf"); - f.deleteOnExit(); + File f = tempDir.resolve("lzf-test.lzf").toFile(); byte[] fluff = constructFluff(132000); LZFFileOutputStream fout = new LZFFileOutputStream(f); @@ -50,7 +55,6 @@ public void testReadAndWrite() throws Exception in.readAndWrite(bytes); in.close(); byte[] actual = bytes.toByteArray(); - Assert.assertEquals(actual.length, fluff.length); - Assert.assertEquals(actual, fluff); + assertArrayEquals(fluff, actual); } }