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:
parent
8a0d5939f1
commit
e6d37b867c
@ -81,6 +81,12 @@
|
|||||||
<version>7.1.0</version>
|
<version>7.1.0</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>3.4.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<finalName>core-java-io-apis-2</finalName>
|
<finalName>core-java-io-apis-2</finalName>
|
||||||
@ -94,4 +100,5 @@
|
|||||||
<properties>
|
<properties>
|
||||||
<junit-jupiter-version>5.9.3</junit-jupiter-version>
|
<junit-jupiter-version>5.9.3</junit-jupiter-version>
|
||||||
</properties>
|
</properties>
|
||||||
</project>
|
|
||||||
|
</project>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package com.baeldung.inputstream;
|
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.ByteArrayInputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -20,21 +20,20 @@ import java.util.Map;
|
|||||||
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 InputStreamUnitTest {
|
public class InputStreamUnitTest {
|
||||||
@Test
|
@Test //givenAStringWrittenToAFile_whenReadByX_thenY
|
||||||
public void givenAString_whenWrittenToFileInputStream_thenShouldMatchWhenRead(@TempDir Path tempDir) throws IOException {
|
public void givenAStringWrittenToFile_whenReadWithFileInputStream_thenItShouldExitsInTheInputStream(@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);
|
||||||
File sampleOutFile = sampleOut.toFile();
|
File sampleOutFile = sampleOut.toFile();
|
||||||
try (InputStream inputStream = new FileInputStream(sampleOutFile)) {
|
try (InputStream inputStream = new FileInputStream(sampleOutFile)) {
|
||||||
Assert.assertTrue(readString(inputStream).contains(lines.get(0)));
|
assertThat(readString(inputStream)).contains(lines.get(0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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");
|
Path sampleOut = tempDir.resolve("sample-out.txt");
|
||||||
String expectedText = "Hello. Hi.";
|
String expectedText = "Hello. Hi.";
|
||||||
List<String> lines = Arrays.asList(expectedText);
|
List<String> lines = Arrays.asList(expectedText);
|
||||||
@ -43,23 +42,40 @@ public class InputStreamUnitTest {
|
|||||||
.getAbsolutePath();
|
.getAbsolutePath();
|
||||||
try (FileInputStream fis = new FileInputStream(fileAbsolutePath)) {
|
try (FileInputStream fis = new FileInputStream(fileAbsolutePath)) {
|
||||||
int content;
|
int content;
|
||||||
int availabeBytes = fis.available();
|
int availableBytes = fis.available();
|
||||||
Assert.assertTrue(availabeBytes > 0);
|
Assert.assertTrue(availableBytes > 0);
|
||||||
StringBuilder actualReadText = new StringBuilder();
|
StringBuilder actualReadText = new StringBuilder();
|
||||||
while ((content = fis.read()) != -1) {
|
while ((content = fis.read()) != -1) {
|
||||||
actualReadText.append((char) content);
|
actualReadText.append((char) content);
|
||||||
}
|
}
|
||||||
Assert.assertTrue(actualReadText.toString()
|
assertThat(actualReadText.toString()).contains(expectedText);
|
||||||
.contains(expectedText));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAString_whenWrittenToByteArrayInputStream_thenShouldMatchWhenRead() throws IOException {
|
public void givenAByteArray_whenReadWithByteArrayInputStream_thenTheStringValueOfTheByteArrayShouldMatchTheExpectedString() 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)) {
|
||||||
String expected = readString(bais);
|
assertThat(readString(bais)).isEqualTo("hello");
|
||||||
Assert.assertEquals("hello", expected);
|
}
|
||||||
|
}
|
||||||
|
@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();
|
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"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package com.baeldung.inputstreamreader;
|
package com.baeldung.inputstreamreader;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -10,10 +12,8 @@ import java.nio.file.Path;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
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 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 {
|
||||||
@ -21,9 +21,9 @@ public class InputStreamReaderUnitTest {
|
|||||||
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);
|
||||||
//create and write file
|
|
||||||
Files.write(sampleOut, lines);
|
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;
|
String ln;
|
||||||
while ((ln = br.readLine()) != null) {
|
while ((ln = br.readLine()) != null) {
|
||||||
if (ln.contains(sampleTxt)) {
|
if (ln.contains(sampleTxt)) {
|
||||||
@ -31,7 +31,7 @@ public class InputStreamReaderUnitTest {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Assert.assertTrue(isMatched);
|
assertThat(isMatched).isTrue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user