BAEL-7131 version 2 - code review comments.
This commit is contained in:
parent
b2e89f3574
commit
8aea75fd05
|
@ -13,46 +13,31 @@ 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)){
|
||||
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());
|
||||
byte[] byteArray = { 104, 101, 108, 108, 111 };
|
||||
try (ByteArrayInputStream bais = new ByteArrayInputStream(byteArray)) {
|
||||
String expected = readString(bais);
|
||||
Assert.assertEquals("hello", expected);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static String readString(InputStream inputStream) throws IOException {
|
||||
String strRet = "";
|
||||
int c;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while(!((c = inputStream.read()) != -1)) {
|
||||
while ((c = inputStream.read()) != -1) {
|
||||
sb.append((char) c);
|
||||
}
|
||||
strRet = sb.toString();
|
||||
|
||||
return strRet;
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -13,9 +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 {
|
||||
boolean isMatched = false;
|
||||
|
@ -24,11 +22,10 @@ public class InputStreamReaderTest {
|
|||
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));){
|
||||
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)){
|
||||
while ((ln = br.readLine()) != null) {
|
||||
if (ln.contains(sampleTxt)) {
|
||||
isMatched = true;
|
||||
break;
|
||||
}
|
||||
|
@ -36,5 +33,4 @@ public class InputStreamReaderTest {
|
|||
Assert.assertTrue(isMatched);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue