IO testing work
This commit is contained in:
parent
6c52dfacbd
commit
979cd6e3a4
|
@ -1,9 +1,11 @@
|
|||
package org.baeldung.java.io;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.io.Writer;
|
||||
|
@ -143,4 +145,42 @@ public class JavaReaderToXUnitTest {
|
|||
initialReader.close();
|
||||
}
|
||||
|
||||
// tests - Reader to InputStream
|
||||
|
||||
@Test
|
||||
public void givenUsingPlainJava_whenConvertingReaderIntoInputStream_thenCorrect() throws IOException {
|
||||
final Reader initialReader = new StringReader("With Java");
|
||||
|
||||
final char[] charBuffer = new char[8 * 1024];
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
int numCharsRead;
|
||||
while ((numCharsRead = initialReader.read(charBuffer, 0, charBuffer.length)) != -1) {
|
||||
builder.append(charBuffer, 0, numCharsRead);
|
||||
}
|
||||
final InputStream targetStream = new ByteArrayInputStream(builder.toString().getBytes());
|
||||
|
||||
initialReader.close();
|
||||
targetStream.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingGuava_whenConvertingReaderIntoInputStream_thenCorrect() throws IOException {
|
||||
final Reader initialReader = new StringReader("With Guava");
|
||||
|
||||
final InputStream targetStream = new ByteArrayInputStream(CharStreams.toString(initialReader).getBytes());
|
||||
|
||||
initialReader.close();
|
||||
targetStream.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingCommonsIO_whenConvertingReaderIntoInputStream() throws IOException {
|
||||
final Reader initialReader = new StringReader("With Commons IO");
|
||||
|
||||
final InputStream targetStream = IOUtils.toInputStream(initialReader.toString());
|
||||
|
||||
initialReader.close();
|
||||
targetStream.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,18 +1,24 @@
|
|||
package org.baeldung.java.io;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.io.input.CharSequenceReader;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.io.ByteSource;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.google.common.io.CharSource;
|
||||
|
||||
public class JavaXToReaderUnitTest {
|
||||
|
@ -94,4 +100,35 @@ public class JavaXToReaderUnitTest {
|
|||
targetReader.close();
|
||||
}
|
||||
|
||||
// tests - InputStream to Reader
|
||||
|
||||
@Test
|
||||
public void givenUsingPlainJava_whenConvertingInputStreamIntoReader_thenCorrect() throws IOException {
|
||||
final InputStream initialStream = new ByteArrayInputStream("With Java".getBytes());
|
||||
final Reader targetReader = new InputStreamReader(initialStream);
|
||||
|
||||
initialStream.close();
|
||||
targetReader.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingGuava_whenConvertingInputStreamIntoReader_thenCorrect() throws IOException {
|
||||
final InputStream initialStream = ByteSource.wrap("With Guava".getBytes()).openStream();
|
||||
final byte[] buffer = ByteStreams.toByteArray(initialStream);
|
||||
final Reader targetReader = CharSource.wrap(new String(buffer)).openStream();
|
||||
|
||||
initialStream.close();
|
||||
targetReader.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingCommonsIO_whenConvertingInputStreamIntoReader_thenCorrect() throws IOException {
|
||||
final InputStream initialStream = IOUtils.toInputStream("With Commons IO");
|
||||
final byte[] buffer = IOUtils.toByteArray(initialStream);
|
||||
final Reader targetReader = new CharSequenceReader(new String(buffer));
|
||||
|
||||
initialStream.close();
|
||||
targetReader.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue