2 new simple conversion tests

This commit is contained in:
eugenp 2016-08-08 22:58:26 +03:00
parent d273566d4a
commit ce0fb89076
1 changed files with 31 additions and 1 deletions

View File

@ -1,5 +1,8 @@
package org.baeldung.java.io; package org.baeldung.java.io;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
@ -187,10 +190,24 @@ public class JavaReaderToXUnitTest {
targetStream.close(); targetStream.close();
} }
@Test
public void givenUsingCommonsIO_whenConvertingReaderIntoInputStream_thenCorrect() throws IOException {
String initialString = "With Commons IO";
final Reader initialReader = new StringReader(initialString);
final InputStream targetStream = IOUtils.toInputStream(IOUtils.toString(initialReader));
final String finalString = IOUtils.toString(targetStream);
assertThat(finalString, equalTo(initialString));
initialReader.close();
targetStream.close();
}
// tests - Reader to InputStream with encoding // tests - Reader to InputStream with encoding
@Test @Test
public void givenUsingPlainJava_whenConvertingReaderIntoInputStreamWithCharset_thenCorrect() throws IOException { public void givenUsingPlainJava_whenConvertingReaderIntoInputStreamWithCharset() throws IOException {
final Reader initialReader = new StringReader("With Java"); final Reader initialReader = new StringReader("With Java");
final char[] charBuffer = new char[8 * 1024]; final char[] charBuffer = new char[8 * 1024];
@ -225,4 +242,17 @@ public class JavaReaderToXUnitTest {
targetStream.close(); targetStream.close();
} }
@Test
public void givenUsingCommonsIO_whenConvertingReaderIntoInputStreamWithEncoding_thenCorrect() throws IOException {
String initialString = "With Commons IO";
final Reader initialReader = new StringReader(initialString);
final InputStream targetStream = IOUtils.toInputStream(IOUtils.toString(initialReader), Charsets.UTF_8);
String finalString = IOUtils.toString(targetStream, Charsets.UTF_8);
assertThat(finalString, equalTo(initialString));
initialReader.close();
targetStream.close();
}
} }