1.change testTryDecompress to testTryDecompressInputStream and testTryDecompressByteArray

2.remove test resource file
pull/1352/head
shuhai65 2 years ago
parent 15f4558d21
commit fd9a0406b6

@ -23,7 +23,6 @@ import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.springframework.core.io.ClassPathResource;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.Reader; import java.io.Reader;
@ -44,6 +43,7 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.List; import java.util.List;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
/** /**
* test for {@link IoUtil} * test for {@link IoUtil}
@ -75,22 +75,32 @@ public class IoUtilTest {
} }
@Test @Test
public void testTryDecompress() throws Exception { public void testTryDecompressInputStream() throws IOException {
String gzipPath = "IoUnitTest/testIoUtil.txt.gz"; byte[] inputBytes = "This is a test string.".getBytes("UTF-8");
String rawPath = "IoUnitTest/testIoUtil.txt"; ByteArrayOutputStream compressedOutput = new ByteArrayOutputStream();
ClassPathResource rawResource = new ClassPathResource(rawPath); try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(compressedOutput)) {
InputStream rawInputStream = rawResource.getInputStream(); gzipOutputStream.write(inputBytes);
byte[] rawBytes = new byte[rawInputStream.available()]; }
rawInputStream.read(rawBytes); byte[] compressedBytes = compressedOutput.toByteArray();
rawInputStream.close(); ByteArrayInputStream compressedInput = new ByteArrayInputStream(compressedBytes);
rawResource.getInputStream().close(); byte[] decompressedBytes = IoUtil.tryDecompress(compressedInput);
ClassPathResource gzipResource = new ClassPathResource(gzipPath); Assert.assertNotNull(decompressedBytes);
InputStream gzipInputStream = gzipResource.getInputStream(); Assert.assertTrue(decompressedBytes.length > 0);
Assert.assertArrayEquals(rawBytes, IoUtil.tryDecompress(gzipInputStream)); Assert.assertArrayEquals(inputBytes, decompressedBytes);
InputStream readGzipInputStream = gzipResource.getInputStream(); }
byte[] gzipBytes = new byte[readGzipInputStream.available()];
readGzipInputStream.read(gzipBytes); @Test
Assert.assertArrayEquals(rawBytes, IoUtil.tryDecompress(gzipBytes)); 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 @Test
@ -101,8 +111,7 @@ public class IoUtilTest {
Assert.assertNotNull(compressedBytes); Assert.assertNotNull(compressedBytes);
Assert.assertTrue(compressedBytes.length > 0); Assert.assertTrue(compressedBytes.length > 0);
try ( try (
GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(compressedBytes)) GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(compressedBytes))) {
) {
byte[] decompressedBytes = new byte[1024]; byte[] decompressedBytes = new byte[1024];
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int readBytes; int readBytes;
@ -129,8 +138,7 @@ public class IoUtilTest {
public void testReadLines() throws IOException { public void testReadLines() throws IOException {
File tempFile = new File(tempDir.toFile(), "testReadLines.txt"); File tempFile = new File(tempDir.toFile(), "testReadLines.txt");
try ( try (
PrintWriter writer = new PrintWriter(tempFile) PrintWriter writer = new PrintWriter(tempFile)) {
) {
writer.println("test string 1"); writer.println("test string 1");
writer.println("test string 2"); writer.println("test string 2");
writer.println("test string 3"); writer.println("test string 3");

@ -1,3 +0,0 @@
IoUtilTest
second line
empty line next
Loading…
Cancel
Save