From 36d843329e78dd33c2efcc6018719dc9f7f2e158 Mon Sep 17 00:00:00 2001 From: eugenp Date: Tue, 7 Jan 2014 12:28:09 +0200 Subject: [PATCH] io testing work --- .../org/baeldung/java/CoreJavaIoUnitTest.java | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/core-java/src/test/java/org/baeldung/java/CoreJavaIoUnitTest.java b/core-java/src/test/java/org/baeldung/java/CoreJavaIoUnitTest.java index 1e2dbc6750..a9eb9d18b1 100644 --- a/core-java/src/test/java/org/baeldung/java/CoreJavaIoUnitTest.java +++ b/core-java/src/test/java/org/baeldung/java/CoreJavaIoUnitTest.java @@ -7,8 +7,8 @@ import static org.junit.Assert.assertThat; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.Reader; -import java.io.StringReader; import java.io.StringWriter; import java.nio.charset.StandardCharsets; import java.util.Scanner; @@ -19,7 +19,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.base.Charsets; -import com.google.common.io.ByteSource; import com.google.common.io.CharStreams; import com.google.common.io.InputSupplier; @@ -43,9 +42,17 @@ public class CoreJavaIoUnitTest { } @Test - public final void givenUsingGuavaNoEncoding_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException { + public final void givenUsingGuava_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException { final String originalString = randomAlphabetic(8); - final InputSupplier readerSupplier = CharStreams.newReaderSupplier(originalString); + final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes()); + + final InputSupplier inputSupplier = new InputSupplier() { + @Override + public final InputStream getInput() throws IOException { + return inputStream; + } + }; + final InputSupplier readerSupplier = CharStreams.newReaderSupplier(inputSupplier, Charsets.UTF_8); // When final String text = CharStreams.toString(readerSupplier); @@ -54,18 +61,21 @@ public class CoreJavaIoUnitTest { } @Test - public final void givenUsingGuavaWithEncoding_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException { + public final void givenUsingGuavaAndJava7_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException { final String originalString = randomAlphabetic(8); - final InputSupplier readerSupplier = ByteSource.wrap(originalString.getBytes()).asCharSource(Charsets.UTF_8); + final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes()); // When - final String text = CharStreams.toString(readerSupplier); + String text = null; + try (final Reader reader = new InputStreamReader(inputStream)) { + text = CharStreams.toString(reader); + } assertThat(text, equalTo(originalString)); } @Test - public final void givenUsingCommonsIoWithEncoding_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException { + public final void givenUsingCommonsIo_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException { final String originalString = randomAlphabetic(8); final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes()); @@ -75,7 +85,7 @@ public class CoreJavaIoUnitTest { } @Test - public final void givenUsingCommonsIoWithEncoding2_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException { + public final void givenUsingCommonsIoWithCopy_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException { final String originalString = randomAlphabetic(8); final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());