diff --git a/core-java/pom.xml b/core-java/pom.xml index ed3cb6ce435c..cc745fb96f6d 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -28,6 +28,12 @@ 2.4 + + org.apache.commons + commons-lang3 + 3.2 + + 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 424621bc5953..8d01ebefd82c 100644 --- a/core-java/src/test/java/org/baeldung/java/CoreJavaIoUnitTest.java +++ b/core-java/src/test/java/org/baeldung/java/CoreJavaIoUnitTest.java @@ -1,8 +1,17 @@ package org.baeldung.java; +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.io.StringReader; +import java.nio.charset.StandardCharsets; import java.util.Scanner; import org.apache.commons.io.FileUtils; @@ -11,12 +20,15 @@ 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.Files; +import com.google.common.io.InputSupplier; public class CoreJavaIoUnitTest { protected final Logger logger = LoggerFactory.getLogger(getClass()); - // tests - + // tests - iterate lines in a file @Test public final void givenUsingGuava_whenIteratingAFile_thenCorrect() throws IOException { @@ -70,7 +82,45 @@ public final void whenStreamingThroughAFile_thenCorrect() throws IOException { logMemory(); } - // + // tests - InputStream to String + + @Test + public final void givenUsingJava7_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException { + final String originalString = randomAlphabetic(8); + final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes()); // exampleString.getBytes(StandardCharsets.UTF_8); + + // When + String text = null; + try (Scanner scanner = new Scanner(inputStream, StandardCharsets.UTF_8.name())) { + text = scanner.useDelimiter("\\A").next(); + } + + assertThat(text, equalTo(originalString)); + } + + @Test + public final void givenUsingGuavaNoEncoding_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException { + final String originalString = randomAlphabetic(8); + final InputSupplier readerSupplier = CharStreams.newReaderSupplier(originalString); + + // When + final String text = CharStreams.toString(readerSupplier); + + assertThat(text, equalTo(originalString)); + } + + @Test + public final void givenUsingGuavaWithEncoding_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException { + final String originalString = randomAlphabetic(8); + final InputSupplier readerSupplier = ByteSource.wrap(originalString.getBytes()).asCharSource(Charsets.UTF_8); + + // When + final String text = CharStreams.toString(readerSupplier); + + assertThat(text, equalTo(originalString)); + } + + // utils private final void logMemory() { logger.info("Max Memory: {} Mb", Runtime.getRuntime().maxMemory() / 1048576); @@ -79,4 +129,3 @@ private final void logMemory() { } } -// \ No newline at end of file diff --git a/guava/pom.xml b/guava/pom.xml index f9994defec69..2b078d979146 100644 --- a/guava/pom.xml +++ b/guava/pom.xml @@ -1,129 +1,134 @@ - - 4.0.0 - org.baeldung - spring-rest - 0.1-SNAPSHOT - - spring-rest - - - - - - - com.google.guava - guava - 15.0 - - - - org.apache.commons - commons-collections4 - 4.0 - - - - - - - - junit - junit-dep - ${junit.version} - test - - - - org.hamcrest - hamcrest-core - ${org.hamcrest.version} - test - - - org.hamcrest - hamcrest-library - ${org.hamcrest.version} - test - - - - org.mockito - mockito-core - ${mockito.version} - test - - - - - - spring-rest - - - src/main/resources - true - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.7 - 1.7 - - - - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - - - - - - - - - 4.0.0.RELEASE - 3.2.0.RELEASE - - - 4.3.0.Final - 5.1.27 - - - 1.7.5 - 1.0.11 - - - 5.0.1.Final - - - 15.0 - 3.1 - - - 1.3 - 4.11 - 1.9.5 - - 4.3 - 4.3.1 - - 2.1.0 - - - 3.1 - 2.4 - 2.16 - 2.6 - 1.4.5 - - + + 4.0.0 + org.baeldung + spring-rest + 0.1-SNAPSHOT + + spring-rest + + + + + + + com.google.guava + guava + 15.0 + + + + org.apache.commons + commons-collections4 + 4.0 + + + + org.apache.commons + commons-lang3 + 3.2 + + + + + + + + junit + junit-dep + ${junit.version} + test + + + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + + + + org.mockito + mockito-core + ${mockito.version} + test + + + + + + spring-rest + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.7 + 1.7 + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + + + + + + + 4.0.0.RELEASE + 3.2.0.RELEASE + + + 4.3.0.Final + 5.1.27 + + + 1.7.5 + 1.0.11 + + + 5.0.1.Final + + + 15.0 + 3.1 + + + 1.3 + 4.11 + 1.9.5 + + 4.3 + 4.3.1 + + 2.1.0 + + + 3.1 + 2.4 + 2.16 + 2.6 + 1.4.5 + + \ No newline at end of file