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 new file mode 100644 index 0000000000..7aca698446 --- /dev/null +++ b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/inputstream/InputStreamTest.java @@ -0,0 +1,58 @@ +package com.baeldung.inputstream; + +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.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 { + + Path sampleOut = tempDir.resolve("sample-out.txt"); + List 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)){ + Assert.assertTrue(readString(inputStream).contains(lines.get(0))); + } + } + + @Test + public void givenAString_whenWrittenToByteArrayInputStream_thenShouldMatchWhenRead() throws IOException { + byte[] byteArray = {104, 101, 108, 108,111}; + try(ByteArrayInputStream bais = new ByteArrayInputStream(byteArray)){ + int content; + StringBuilder actualReadText = new StringBuilder(); + while ((content = bais.read()) != -1) { + actualReadText.append((char) content); + } + Assert.assertEquals( "hello",actualReadText.toString()); + } + + } + + private static String readString(InputStream inputStream) throws IOException { + String strRet = ""; + int c; + StringBuilder sb = new StringBuilder(); + while(!((c = inputStream.read()) != -1)) { + sb.append((char) c); + } + strRet = sb.toString(); + + return strRet; + } + + +} 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 new file mode 100644 index 0000000000..7009297c86 --- /dev/null +++ b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/inputstreamreader/InputStreamReaderTest.java @@ -0,0 +1,40 @@ +package com.baeldung.inputstreamreader; + +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +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 { + boolean isMatched = false; + String sampleTxt = "Good day. This is just a test. Good bye."; + Path sampleOut = tempDir.resolve("sample-out.txt"); + List lines = Arrays.asList(sampleTxt); + //create and write file + Files.write(sampleOut, lines); + try(FileInputStream fis = new FileInputStream(String.valueOf(sampleOut.toAbsolutePath())); + BufferedReader br = new BufferedReader(new InputStreamReader(fis, StandardCharsets.UTF_8));){ + String ln; + while((ln = br.readLine())!= null){ + if(ln.contains(sampleTxt)){ + isMatched = true; + break; + } + } + Assert.assertTrue(isMatched); + } + } + +}