java io testing work
This commit is contained in:
parent
cbd92a85b6
commit
add603644a
|
@ -1,2 +0,0 @@
|
||||||
line 1
|
|
||||||
a second line
|
|
|
@ -2,7 +2,6 @@ package org.baeldung.java.io;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileReader;
|
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
@ -74,24 +73,26 @@ public class JavaReaderToXUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUsingPlainJava_whenWritingReaderContentsToFile_thenCorrect() throws IOException {
|
public void givenUsingPlainJava_whenWritingReaderContentsToFile_thenCorrect() throws IOException {
|
||||||
final File sourceFile = new File("src/test/resources/sourceFile.txt");
|
final Reader initialReader = new StringReader("Some text");
|
||||||
sourceFile.createNewFile();
|
|
||||||
final Reader initialReader = new FileReader(sourceFile);
|
int intValueOfChar;
|
||||||
final char[] buffer = new char[(int) sourceFile.length()];
|
final StringBuilder buffer = new StringBuilder();
|
||||||
initialReader.read(buffer);
|
while ((intValueOfChar = initialReader.read()) != -1) {
|
||||||
|
buffer.append((char) intValueOfChar);
|
||||||
|
}
|
||||||
initialReader.close();
|
initialReader.close();
|
||||||
|
|
||||||
final File targetFile = new File("src/test/resources/targetFile.txt");
|
final File targetFile = new File("src/test/resources/targetFile.txt");
|
||||||
targetFile.createNewFile();
|
targetFile.createNewFile();
|
||||||
|
|
||||||
final Writer targetFileWriter = new FileWriter(targetFile);
|
final Writer targetFileWriter = new FileWriter(targetFile);
|
||||||
targetFileWriter.write(buffer);
|
targetFileWriter.write(buffer.toString());
|
||||||
targetFileWriter.close();
|
targetFileWriter.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUsingGuava_whenWritingReaderContentsToFile_thenCorrect() throws IOException {
|
public void givenUsingGuava_whenWritingReaderContentsToFile_thenCorrect() throws IOException {
|
||||||
final Reader initialReader = CharSource.wrap("IDDQD").openStream();
|
final Reader initialReader = new StringReader("Some text");
|
||||||
|
|
||||||
final File targetFile = new File("src/test/resources/targetFile.txt");
|
final File targetFile = new File("src/test/resources/targetFile.txt");
|
||||||
com.google.common.io.Files.touch(targetFile);
|
com.google.common.io.Files.touch(targetFile);
|
||||||
|
@ -103,6 +104,7 @@ public class JavaReaderToXUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenUsingCommonsIO_whenWritingReaderContentsToFile_thenCorrect() throws IOException {
|
public void givenUsingCommonsIO_whenWritingReaderContentsToFile_thenCorrect() throws IOException {
|
||||||
final Reader initialReader = new CharSequenceReader("CharSequenceReader extends Reader");
|
final Reader initialReader = new CharSequenceReader("CharSequenceReader extends Reader");
|
||||||
|
|
||||||
final File targetFile = new File("src/test/resources/targetFile.txt");
|
final File targetFile = new File("src/test/resources/targetFile.txt");
|
||||||
FileUtils.touch(targetFile);
|
FileUtils.touch(targetFile);
|
||||||
final byte[] buffer = IOUtils.toByteArray(initialReader);
|
final byte[] buffer = IOUtils.toByteArray(initialReader);
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
CharSequenceReader extends Reader
|
|
Loading…
Reference in New Issue