BAEL-7131 version 6 - updated for PR comments, renamed unit test method, added assertJ dependency and used that in unit tests instead.

This commit is contained in:
adalagandev 2024-02-20 00:11:06 +01:00
parent 8a0d5939f1
commit e6d37b867c
3 changed files with 42 additions and 42 deletions

View File

@ -81,6 +81,12 @@
<version>7.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.4.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-io-apis-2</finalName>
@ -94,4 +100,5 @@
<properties>
<junit-jupiter-version>5.9.3</junit-jupiter-version>
</properties>
</project>

View File

@ -1,6 +1,6 @@
package com.baeldung.inputstream;
import static org.junit.jupiter.api.io.CleanupMode.ALWAYS;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.ByteArrayInputStream;
import java.io.File;
@ -20,21 +20,20 @@ import java.util.Map;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
public class InputStreamUnitTest {
@Test
public void givenAString_whenWrittenToFileInputStream_thenShouldMatchWhenRead(@TempDir Path tempDir) throws IOException {
@Test //givenAStringWrittenToAFile_whenReadByX_thenY
public void givenAStringWrittenToFile_whenReadWithFileInputStream_thenItShouldExitsInTheInputStream(@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)));
assertThat(readString(inputStream)).contains(lines.get(0));
}
}
@Test
public void givenAString_whenWrittenToFileInputStreamWithStringConstructor_thenShouldMatchWhenRead(@TempDir Path tempDir) throws IOException {
public void givenAStringWrittenToFile_whenReadWithFileInputStreamWithStringConstructor_thenItShouldExitsInTheInputStream(@TempDir Path tempDir) throws IOException {
Path sampleOut = tempDir.resolve("sample-out.txt");
String expectedText = "Hello. Hi.";
List<String> lines = Arrays.asList(expectedText);
@ -43,23 +42,40 @@ public class InputStreamUnitTest {
.getAbsolutePath();
try (FileInputStream fis = new FileInputStream(fileAbsolutePath)) {
int content;
int availabeBytes = fis.available();
Assert.assertTrue(availabeBytes > 0);
int availableBytes = fis.available();
Assert.assertTrue(availableBytes > 0);
StringBuilder actualReadText = new StringBuilder();
while ((content = fis.read()) != -1) {
actualReadText.append((char) content);
}
Assert.assertTrue(actualReadText.toString()
.contains(expectedText));
assertThat(actualReadText.toString()).contains(expectedText);
}
}
@Test
public void givenAString_whenWrittenToByteArrayInputStream_thenShouldMatchWhenRead() throws IOException {
public void givenAByteArray_whenReadWithByteArrayInputStream_thenTheStringValueOfTheByteArrayShouldMatchTheExpectedString() throws IOException {
byte[] byteArray = { 104, 101, 108, 108, 111 };
try (ByteArrayInputStream bais = new ByteArrayInputStream(byteArray)) {
String expected = readString(bais);
Assert.assertEquals("hello", expected);
assertThat(readString(bais)).isEqualTo("hello");
}
}
@Test
public void givenKeyValuePairsWrittenInAFile_whenPassedInOutputStreams_thenThePairsShouldExistsInObjectInputStreams(
@TempDir Path tempDir) throws IOException, ClassNotFoundException {
Path sampleOut = tempDir.resolve("sample-out.txt");
File fileText = sampleOut.toFile();
//Serialize a Hashmap then write it into a file.
Map<String, String> kv = new HashMap<>();
try (ObjectOutputStream objectOutStream = new ObjectOutputStream(new FileOutputStream(fileText))) {
kv.put("baseURL", "baeldung.com");
kv.put("apiKey", "this_is_a_test_key");
objectOutStream.writeObject(kv);
}
//Deserialize the contents of a file then transform it back into a Hashmap
try (ObjectInputStream input = new ObjectInputStream(new FileInputStream(fileText))) {
HashMap<String, String> inputKv = (HashMap<String, String>) input.readObject();
assertThat(kv.get("baseURL")).isEqualTo( inputKv.get("baseURL"));
assertThat(kv.get("apiKey")).isEqualTo( inputKv.get("apiKey"));
}
}
@ -72,27 +88,4 @@ public class InputStreamUnitTest {
return sb.toString();
}
@Test
public void givenKeyValuePairs_whenWrittenInATextFile_thenSuccessWhenParsedWithObjectInputStream(
@TempDir(cleanup = ALWAYS) Path tempDir) throws IOException, ClassNotFoundException {
Path sampleOut = tempDir.resolve("sample-out.txt");
File fileText = sampleOut.toFile();
//Serialize a Hashmap then write it into a file.
Map<String, String> kv = new HashMap<>();
try (FileOutputStream fileOutStream = new FileOutputStream(fileText)) {
try (ObjectOutputStream objectOutStream = new ObjectOutputStream(fileOutStream)) {
kv.put("baseURL", "baeldung.com");
kv.put("apiKey", "this_is_a_test_key");
objectOutStream.writeObject(kv);
}
}
//Deserialize the contents of a file then transform it back into a Hashmap
try (FileInputStream fileStream = new FileInputStream(fileText)) {
try (ObjectInputStream input = new ObjectInputStream(fileStream)) {
HashMap<String, String> inputKv = (HashMap<String, String>) input.readObject();
Assert.assertEquals(kv.get("baseURL"), inputKv.get("baseURL"));
Assert.assertEquals(kv.get("apiKey"), inputKv.get("apiKey"));
}
}
}
}

View File

@ -1,5 +1,7 @@
package com.baeldung.inputstreamreader;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
@ -10,10 +12,8 @@ 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 InputStreamReaderUnitTest {
@Test
public void givenAStringWrittenToAFile_whenReadByInputStreamReader_thenShouldMatchWhenRead(@TempDir Path tempDir) throws IOException {
@ -21,9 +21,9 @@ public class InputStreamReaderUnitTest {
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 absolutePath = String.valueOf(sampleOut.toAbsolutePath());
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(absolutePath), StandardCharsets.UTF_8))) {
String ln;
while ((ln = br.readLine()) != null) {
if (ln.contains(sampleTxt)) {
@ -31,7 +31,7 @@ public class InputStreamReaderUnitTest {
break;
}
}
Assert.assertTrue(isMatched);
assertThat(isMatched).isTrue();
}
}
}