Update OutputStreamtoByteArrayUnitTest.java

This commit is contained in:
DiegoMarti2 2023-12-11 17:25:25 +02:00 committed by GitHub
parent b56eaf9b1b
commit 4e0e51139b
1 changed files with 16 additions and 22 deletions

View File

@ -1,16 +1,16 @@
package com.baeldung.outputstreamtobytearray; package com.baeldung.outputstreamtobytearray;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.api.io.TempDir;
import java.io.*; import java.io.*;
import java.nio.file.Files; import java.nio.charset.StandardCharsets;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class OutputStreamtoByteArrayUnitTest { public class OutputStreamToByteArrayUnitTest {
@Test @Test
public void givenFileOutputStream_whenUsingFileUtilsToReadTheFile_thenReturnByteArray(@TempDir Path tempDir) throws IOException { public void givenFileOutputStream_whenUsingFileUtilsToReadTheFile_thenReturnByteArray(@TempDir Path tempDir) throws IOException {
@ -18,37 +18,31 @@ public class OutputStreamtoByteArrayUnitTest {
String fileName = "file.txt"; String fileName = "file.txt";
Path filePath = tempDir.resolve(fileName); Path filePath = tempDir.resolve(fileName);
try (DrainableOutputStream drainableOutputStream = new DrainableOutputStream(new FileOutputStream(filePath.toFile()))) { try (FileOutputStream outputStream = new FileOutputStream(filePath.toFile())) {
drainableOutputStream.write(data.getBytes()); outputStream.write(data.getBytes(StandardCharsets.UTF_8));
} }
byte[] intercepted = Files.readAllBytes(filePath); byte[] writtenData = FileUtils.readFileToByteArray(new File(filePath.toUri()));
String result = new String(intercepted); String result = new String(writtenData, StandardCharsets.UTF_8);
assertEquals(data, result); assertEquals(data, result);
} }
@Test @Test
public void givenFileOutputStream_whenUsingDrainableOutputStream_thenReturnByteArray() throws IOException { public void givenFileOutputStream_whenUsingDrainableOutputStream_thenReturnByteArray(@TempDir Path tempDir) throws IOException {
String data = "Welcome to Baeldung!"; String data = "Welcome to Baeldung!";
String filePath = "file.txt"; String fileName = "file.txt";
Path filePath = tempDir.resolve(fileName);
DrainableOutputStream drainableOutputStream = null; try (DrainableOutputStream drainableOutputStream = new DrainableOutputStream(new FileOutputStream(filePath.toFile()))) {
drainableOutputStream.write(data.getBytes(StandardCharsets.UTF_8));
try { byte[] writtenData = drainableOutputStream.toByteArray();
drainableOutputStream = new DrainableOutputStream(new FileOutputStream(filePath)); assertEquals(data, new String(writtenData, StandardCharsets.UTF_8));
drainableOutputStream.write(data.getBytes()); } catch (IOException e) {
} finally { e.printStackTrace();
if (drainableOutputStream != null) {
drainableOutputStream.close();
} }
} }
byte[] intercepted = Files.readAllBytes(Paths.get(filePath));
String result = new String(intercepted);
assertEquals(data, result);
}
public class DrainableOutputStream extends FilterOutputStream { public class DrainableOutputStream extends FilterOutputStream {
private final ByteArrayOutputStream buffer; private final ByteArrayOutputStream buffer;