BAEL-7131 - Removed InputStreamUnitTest as per Final review sugestions.

This commit is contained in:
adalagandev 2024-03-15 18:08:35 +01:00
parent 63741db3d9
commit 83cc018515

View File

@ -1,47 +0,0 @@
package com.baeldung.inputstream;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
public class InputStreamUnitTest {
@Test
public void givenAStringWrittenToFile_whenReadWithFileInputStream_thenItShouldExitsInTheInputStream(@TempDir Path tempDir) throws IOException {
Path sampleOut = tempDir.resolve("sample-out.txt");
List<String> lines = Arrays.asList("Hello. This is just a test. Good bye.");
Files.write(sampleOut, lines);
File sampleOutFile = sampleOut.toFile();
try (InputStream inputStream = new FileInputStream(sampleOutFile)) {
assertThat(readString(inputStream)).contains(lines.get(0));
}
}
@Test
public void givenAByteArray_whenReadWithByteArrayInputStream_thenTheStringValueOfTheByteArrayShouldMatchTheExpectedString() throws IOException {
byte[] byteArray = { 104, 101, 108, 108, 111 };
try (ByteArrayInputStream bais = new ByteArrayInputStream(byteArray)) {
assertThat(readString(bais)).isEqualTo("hello");
}
}
private static String readString(InputStream inputStream) throws IOException {
int c;
StringBuilder sb = new StringBuilder();
while ((c = inputStream.read()) != -1) {
sb.append((char) c);
}
return sb.toString();
}
}