BAEL-7131 - reapplied Baeldung Formatter, code changes from PR.

This commit is contained in:
adalagandev 2024-02-21 11:10:39 +01:00
parent ed1e58c45e
commit 9178b53dc9
2 changed files with 12 additions and 15 deletions

View File

@ -17,9 +17,9 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
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 InputStreamUnitTest { public class InputStreamUnitTest {
@Test @Test
public void givenAStringWrittenToFile_whenReadWithFileInputStream_thenItShouldExitsInTheInputStream(@TempDir Path tempDir) throws IOException { public void givenAStringWrittenToFile_whenReadWithFileInputStream_thenItShouldExitsInTheInputStream(@TempDir Path tempDir) throws IOException {
@ -41,14 +41,9 @@ public class InputStreamUnitTest {
String fileAbsolutePath = sampleOut.toFile() String fileAbsolutePath = sampleOut.toFile()
.getAbsolutePath(); .getAbsolutePath();
try (FileInputStream fis = new FileInputStream(fileAbsolutePath)) { try (FileInputStream fis = new FileInputStream(fileAbsolutePath)) {
int content;
int availableBytes = fis.available(); int availableBytes = fis.available();
Assert.assertTrue(availableBytes > 0); assertThat(availableBytes > 0);
StringBuilder actualReadText = new StringBuilder(); assertThat(readString(fis)).contains(expectedText);
while ((content = fis.read()) != -1) {
actualReadText.append((char) content);
}
assertThat(actualReadText.toString()).contains(expectedText);
} }
} }
@ -59,6 +54,7 @@ public class InputStreamUnitTest {
assertThat(readString(bais)).isEqualTo("hello"); assertThat(readString(bais)).isEqualTo("hello");
} }
} }
@Test @Test
public void givenKeyValuePairsWrittenInAFile_whenPassedInOutputStreams_thenThePairsShouldExistsInObjectInputStreams(@TempDir Path tempDir) throws IOException, ClassNotFoundException { public void givenKeyValuePairsWrittenInAFile_whenPassedInOutputStreams_thenThePairsShouldExistsInObjectInputStreams(@TempDir Path tempDir) throws IOException, ClassNotFoundException {
Path sampleOut = tempDir.resolve("sample-out.txt"); Path sampleOut = tempDir.resolve("sample-out.txt");
@ -66,15 +62,15 @@ public class InputStreamUnitTest {
//Serialize a Hashmap then write it into a file. //Serialize a Hashmap then write it into a file.
Map<String, String> kv = new HashMap<>(); Map<String, String> kv = new HashMap<>();
try (ObjectOutputStream objectOutStream = new ObjectOutputStream(new FileOutputStream(textFile))) { try (ObjectOutputStream objectOutStream = new ObjectOutputStream(new FileOutputStream(textFile))) {
kv.put("baseURL", "baeldung.com"); kv.put("baseURL", "baeldung.com");
kv.put("apiKey", "this_is_a_test_key"); kv.put("apiKey", "this_is_a_test_key");
objectOutStream.writeObject(kv); objectOutStream.writeObject(kv);
} }
//Deserialize the contents of a file then transform it back into a Hashmap //Deserialize the contents of a file then transform it back into a Hashmap
try (ObjectInputStream input = new ObjectInputStream(new FileInputStream(textFile))) { try (ObjectInputStream input = new ObjectInputStream(new FileInputStream(textFile))) {
HashMap<String, String> inputKv = (HashMap<String, String>) input.readObject(); HashMap<String, String> inputKv = (HashMap<String, String>) input.readObject();
assertThat(kv.get("baseURL")).isEqualTo( inputKv.get("baseURL")); assertThat(kv.get("baseURL")).isEqualTo(inputKv.get("baseURL"));
assertThat(kv.get("apiKey")).isEqualTo( inputKv.get("apiKey")); assertThat(kv.get("apiKey")).isEqualTo(inputKv.get("apiKey"));
} }
} }

View File

@ -14,10 +14,10 @@ import java.util.List;
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 InputStreamReaderUnitTest { public class InputStreamReaderUnitTest {
@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;
String sampleTxt = "Good day. This is just a test. Good bye."; String sampleTxt = "Good day. This is just a test. Good bye.";
Path sampleOut = tempDir.resolve("sample-out.txt"); Path sampleOut = tempDir.resolve("sample-out.txt");
List<String> lines = Arrays.asList(sampleTxt); List<String> lines = Arrays.asList(sampleTxt);
@ -25,6 +25,7 @@ public class InputStreamReaderUnitTest {
String absolutePath = String.valueOf(sampleOut.toAbsolutePath()); String absolutePath = String.valueOf(sampleOut.toAbsolutePath());
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(absolutePath), StandardCharsets.UTF_8))) { try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(absolutePath), StandardCharsets.UTF_8))) {
String ln; String ln;
boolean isMatched = false;
while ((ln = br.readLine()) != null) { while ((ln = br.readLine()) != null) {
if (ln.contains(sampleTxt)) { if (ln.contains(sampleTxt)) {
isMatched = true; isMatched = true;