BAEL-7131 version 2 - code review comments.

This commit is contained in:
adalagandev 2024-02-10 23:44:42 +01:00
parent b2e89f3574
commit 8aea75fd05
2 changed files with 10 additions and 29 deletions

View File

@ -13,12 +13,9 @@ import java.util.List;
import org.junit.Assert; import org.junit.Assert;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.api.io.TempDir;
public class InputStreamTest { public class InputStreamTest {
@Test @Test
public void givenAString_whenWrittenToFileInputStream_thenShouldMatchWhenRead(@TempDir Path tempDir) throws IOException { public void givenAString_whenWrittenToFileInputStream_thenShouldMatchWhenRead(@TempDir Path tempDir) throws IOException {
Path sampleOut = tempDir.resolve("sample-out.txt"); Path sampleOut = tempDir.resolve("sample-out.txt");
List<String> lines = Arrays.asList("Hello. This is just a test. Good bye."); List<String> lines = Arrays.asList("Hello. This is just a test. Good bye.");
Files.write(sampleOut, lines); Files.write(sampleOut, lines);
@ -27,32 +24,20 @@ public class InputStreamTest {
Assert.assertTrue(readString(inputStream).contains(lines.get(0))); Assert.assertTrue(readString(inputStream).contains(lines.get(0)));
} }
} }
@Test @Test
public void givenAString_whenWrittenToByteArrayInputStream_thenShouldMatchWhenRead() throws IOException { public void givenAString_whenWrittenToByteArrayInputStream_thenShouldMatchWhenRead() throws IOException {
byte[] byteArray = { 104, 101, 108, 108, 111 }; byte[] byteArray = { 104, 101, 108, 108, 111 };
try (ByteArrayInputStream bais = new ByteArrayInputStream(byteArray)) { try (ByteArrayInputStream bais = new ByteArrayInputStream(byteArray)) {
int content; String expected = readString(bais);
StringBuilder actualReadText = new StringBuilder(); Assert.assertEquals("hello", expected);
while ((content = bais.read()) != -1) {
actualReadText.append((char) content);
} }
Assert.assertEquals( "hello",actualReadText.toString());
} }
}
private static String readString(InputStream inputStream) throws IOException { private static String readString(InputStream inputStream) throws IOException {
String strRet = "";
int c; int c;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
while(!((c = inputStream.read()) != -1)) { while ((c = inputStream.read()) != -1) {
sb.append((char) c); sb.append((char) c);
} }
strRet = sb.toString(); return sb.toString();
return strRet;
} }
} }

View File

@ -13,9 +13,7 @@ import java.util.List;
import org.junit.Assert; import org.junit.Assert;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.api.io.TempDir;
public class InputStreamReaderTest { public class InputStreamReaderTest {
@Test @Test
public void givenAStringWrittenToAFile_whenReadByInputStreamReader_thenShouldMatchWhenRead(@TempDir Path tempDir) throws IOException { public void givenAStringWrittenToAFile_whenReadByInputStreamReader_thenShouldMatchWhenRead(@TempDir Path tempDir) throws IOException {
boolean isMatched = false; boolean isMatched = false;
@ -24,8 +22,7 @@ public class InputStreamReaderTest {
List<String> lines = Arrays.asList(sampleTxt); List<String> lines = Arrays.asList(sampleTxt);
//create and write file //create and write file
Files.write(sampleOut, lines); Files.write(sampleOut, lines);
try(FileInputStream fis = new FileInputStream(String.valueOf(sampleOut.toAbsolutePath())); try (FileInputStream fis = new FileInputStream(String.valueOf(sampleOut.toAbsolutePath())); BufferedReader br = new BufferedReader(new InputStreamReader(fis, StandardCharsets.UTF_8))) {
BufferedReader br = new BufferedReader(new InputStreamReader(fis, StandardCharsets.UTF_8));){
String ln; String ln;
while ((ln = br.readLine()) != null) { while ((ln = br.readLine()) != null) {
if (ln.contains(sampleTxt)) { if (ln.contains(sampleTxt)) {
@ -36,5 +33,4 @@ public class InputStreamReaderTest {
Assert.assertTrue(isMatched); Assert.assertTrue(isMatched);
} }
} }
} }