From fd9a0406b69252c23a3c97970c7ec528aad147f9 Mon Sep 17 00:00:00 2001 From: shuhai65 Date: Sat, 27 May 2023 16:14:15 +0800 Subject: [PATCH] 1.change testTryDecompress to testTryDecompressInputStream and testTryDecompressByteArray 2.remove test resource file --- .../cn/hippo4j/common/toolkit/IoUtilTest.java | 50 ++++++++++-------- .../test/resource/IoUnitTest/testIoUtil.txt | 3 -- .../resource/IoUnitTest/testIoUtil.txt.gz | Bin 57 -> 0 bytes 3 files changed, 29 insertions(+), 24 deletions(-) delete mode 100644 infra/common/src/test/resource/IoUnitTest/testIoUtil.txt delete mode 100644 infra/common/src/test/resource/IoUnitTest/testIoUtil.txt.gz diff --git a/infra/common/src/test/java/cn/hippo4j/common/toolkit/IoUtilTest.java b/infra/common/src/test/java/cn/hippo4j/common/toolkit/IoUtilTest.java index 7daf0d47..0f48c046 100644 --- a/infra/common/src/test/java/cn/hippo4j/common/toolkit/IoUtilTest.java +++ b/infra/common/src/test/java/cn/hippo4j/common/toolkit/IoUtilTest.java @@ -23,7 +23,6 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.jupiter.api.Assertions; -import org.springframework.core.io.ClassPathResource; import java.io.BufferedReader; import java.io.PrintWriter; import java.io.Reader; @@ -44,6 +43,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.List; import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; /** * test for {@link IoUtil} @@ -75,22 +75,32 @@ public class IoUtilTest { } @Test - public void testTryDecompress() throws Exception { - String gzipPath = "IoUnitTest/testIoUtil.txt.gz"; - String rawPath = "IoUnitTest/testIoUtil.txt"; - ClassPathResource rawResource = new ClassPathResource(rawPath); - InputStream rawInputStream = rawResource.getInputStream(); - byte[] rawBytes = new byte[rawInputStream.available()]; - rawInputStream.read(rawBytes); - rawInputStream.close(); - rawResource.getInputStream().close(); - ClassPathResource gzipResource = new ClassPathResource(gzipPath); - InputStream gzipInputStream = gzipResource.getInputStream(); - Assert.assertArrayEquals(rawBytes, IoUtil.tryDecompress(gzipInputStream)); - InputStream readGzipInputStream = gzipResource.getInputStream(); - byte[] gzipBytes = new byte[readGzipInputStream.available()]; - readGzipInputStream.read(gzipBytes); - Assert.assertArrayEquals(rawBytes, IoUtil.tryDecompress(gzipBytes)); + public void testTryDecompressInputStream() throws IOException { + byte[] inputBytes = "This is a test string.".getBytes("UTF-8"); + ByteArrayOutputStream compressedOutput = new ByteArrayOutputStream(); + try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(compressedOutput)) { + gzipOutputStream.write(inputBytes); + } + byte[] compressedBytes = compressedOutput.toByteArray(); + ByteArrayInputStream compressedInput = new ByteArrayInputStream(compressedBytes); + byte[] decompressedBytes = IoUtil.tryDecompress(compressedInput); + Assert.assertNotNull(decompressedBytes); + Assert.assertTrue(decompressedBytes.length > 0); + Assert.assertArrayEquals(inputBytes, decompressedBytes); + } + + @Test + public void testTryDecompressByteArray() throws Exception { + byte[] inputBytes = "This is a test string.".getBytes("UTF-8"); + ByteArrayOutputStream compressedOutput = new ByteArrayOutputStream(); + try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(compressedOutput)) { + gzipOutputStream.write(inputBytes); + } + byte[] compressedBytes = compressedOutput.toByteArray(); + byte[] decompressedBytes = IoUtil.tryDecompress(compressedBytes); + Assert.assertNotNull(decompressedBytes); + Assert.assertTrue(decompressedBytes.length > 0); + Assert.assertArrayEquals(inputBytes, decompressedBytes); } @Test @@ -101,8 +111,7 @@ public class IoUtilTest { Assert.assertNotNull(compressedBytes); Assert.assertTrue(compressedBytes.length > 0); try ( - GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(compressedBytes)) - ) { + GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(compressedBytes))) { byte[] decompressedBytes = new byte[1024]; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int readBytes; @@ -129,8 +138,7 @@ public class IoUtilTest { public void testReadLines() throws IOException { File tempFile = new File(tempDir.toFile(), "testReadLines.txt"); try ( - PrintWriter writer = new PrintWriter(tempFile) - ) { + PrintWriter writer = new PrintWriter(tempFile)) { writer.println("test string 1"); writer.println("test string 2"); writer.println("test string 3"); diff --git a/infra/common/src/test/resource/IoUnitTest/testIoUtil.txt b/infra/common/src/test/resource/IoUnitTest/testIoUtil.txt deleted file mode 100644 index c0e662a9..00000000 --- a/infra/common/src/test/resource/IoUnitTest/testIoUtil.txt +++ /dev/null @@ -1,3 +0,0 @@ -IoUtilTest -second line -empty line next diff --git a/infra/common/src/test/resource/IoUnitTest/testIoUtil.txt.gz b/infra/common/src/test/resource/IoUnitTest/testIoUtil.txt.gz deleted file mode 100644 index ae01ebea3a7f43b4dacd8c245375ae4674b1030a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 57 zcmV-90LK3xiwFQZWpHEw0Q1ZbEy>IYNi8nngAvEhwo3vla4E PD}b^9=nEn4DgXcgpN$ts