From 94fceedf7c2de5963add1f23f6ed6bcc38baf418 Mon Sep 17 00:00:00 2001 From: adalagandev Date: Sun, 11 Feb 2024 19:11:39 +0100 Subject: [PATCH] BAEL-7131 version 3 - code review comments. --- .../baeldung/inputstream/InputStreamTest.java | 24 +++++++++++++++++++ .../InputStreamReaderTest.java | 1 + 2 files changed, 25 insertions(+) diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/inputstream/InputStreamTest.java b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/inputstream/InputStreamTest.java index b238067654..553085d2fe 100644 --- a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/inputstream/InputStreamTest.java +++ b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/inputstream/InputStreamTest.java @@ -13,6 +13,7 @@ import java.util.List; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; + public class InputStreamTest { @Test public void givenAString_whenWrittenToFileInputStream_thenShouldMatchWhenRead(@TempDir Path tempDir) throws IOException { @@ -24,6 +25,28 @@ public class InputStreamTest { Assert.assertTrue(readString(inputStream).contains(lines.get(0))); } } + + @Test + public void givenAString_whenWrittenToFileInputStreamWithStringConstructor_thenShouldMatchWhenRead(@TempDir Path tempDir) throws IOException { + Path sampleOut = tempDir.resolve("sample-out.txt"); + String expectedText = "Hello. Hi."; + List lines = Arrays.asList(expectedText); + Files.write(sampleOut, lines); + String fileAbsolutePath = sampleOut.toFile() + .getAbsolutePath(); + try (FileInputStream fis = new FileInputStream(fileAbsolutePath)) { + int content; + int availabeBytes = fis.available(); + Assert.assertTrue(availabeBytes > 0); + StringBuilder actualReadText = new StringBuilder(); + while ((content = fis.read()) != -1) { + actualReadText.append((char) content); + } + Assert.assertTrue(actualReadText.toString() + .contains(expectedText)); + } + } + @Test public void givenAString_whenWrittenToByteArrayInputStream_thenShouldMatchWhenRead() throws IOException { byte[] byteArray = { 104, 101, 108, 108, 111 }; @@ -32,6 +55,7 @@ public class InputStreamTest { Assert.assertEquals("hello", expected); } } + private static String readString(InputStream inputStream) throws IOException { int c; StringBuilder sb = new StringBuilder(); diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/inputstreamreader/InputStreamReaderTest.java b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/inputstreamreader/InputStreamReaderTest.java index d14a96c9a0..e52bb99363 100644 --- a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/inputstreamreader/InputStreamReaderTest.java +++ b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/inputstreamreader/InputStreamReaderTest.java @@ -13,6 +13,7 @@ import java.util.List; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; + public class InputStreamReaderTest { @Test public void givenAStringWrittenToAFile_whenReadByInputStreamReader_thenShouldMatchWhenRead(@TempDir Path tempDir) throws IOException {