BAEL-7131 version 1 - POST REPO re-writing the commit history issue.
This commit is contained in:
parent
d458fe57e9
commit
b2e89f3574
@ -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<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)){
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -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<String> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user