Merge pull request #49 from Doha2012/master
Modify JavaReadFromFileTest
This commit is contained in:
commit
38e8bfd59a
|
@ -3,16 +3,11 @@ package org.baeldung.java.io;
|
|||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.RandomAccessFile;
|
||||
|
@ -23,9 +18,7 @@ import java.nio.channels.FileChannel;
|
|||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Scanner;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -34,31 +27,22 @@ import org.junit.Test;
|
|||
MappedByteBuffer
|
||||
|
||||
*/
|
||||
public class TestReader {
|
||||
public class JavaReadFromFileTest {
|
||||
|
||||
@Test
|
||||
public void whenReadWithBufferedReader_thenCorrect() throws IOException {
|
||||
final String str = "Hello world";
|
||||
|
||||
final BufferedWriter writer = new BufferedWriter(new FileWriter("src/test/resources/test_read.txt"));
|
||||
writer.write(str);
|
||||
writer.close();
|
||||
final String expected_value = "Hello world";
|
||||
|
||||
final BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/test_read.txt"));
|
||||
final String currentLine = reader.readLine();
|
||||
reader.close();
|
||||
|
||||
assertEquals(str, currentLine);
|
||||
assertEquals(expected_value, currentLine);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadWithScanner_thenCorrect() throws IOException {
|
||||
final String str = "Hello world 1 2,3,4";
|
||||
final BufferedWriter writer = new BufferedWriter(new FileWriter("src/test/resources/test_read.txt"));
|
||||
writer.write(str);
|
||||
writer.close();
|
||||
|
||||
final Scanner scanner = new Scanner(new File("src/test/resources/test_read.txt"));
|
||||
final Scanner scanner = new Scanner(new File("src/test/resources/test_read1.txt"));
|
||||
scanner.useDelimiter(" ");
|
||||
|
||||
assertTrue(scanner.hasNext());
|
||||
|
@ -66,6 +50,13 @@ public class TestReader {
|
|||
assertEquals("world", scanner.next());
|
||||
assertEquals(1, scanner.nextInt());
|
||||
|
||||
scanner.close();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadWithScannerTwoDelimiters_thenCorrect() throws IOException {
|
||||
final Scanner scanner = new Scanner(new File("src/test/resources/test_read2.txt"));
|
||||
scanner.useDelimiter(",| ");
|
||||
|
||||
assertTrue(scanner.hasNextInt());
|
||||
|
@ -74,17 +65,11 @@ public class TestReader {
|
|||
assertEquals(4, scanner.nextInt());
|
||||
|
||||
scanner.close();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadWithStreamTokenizer_thenCorrectTokens() throws IOException {
|
||||
final String str = "Hello 1";
|
||||
final BufferedWriter writer = new BufferedWriter(new FileWriter("src/test/resources/test_read.txt"));
|
||||
writer.write(str);
|
||||
writer.close();
|
||||
|
||||
final FileReader reader = new FileReader("src/test/resources/test_read.txt");
|
||||
final FileReader reader = new FileReader("src/test/resources/test_read3.txt");
|
||||
final StreamTokenizer tokenizer = new StreamTokenizer(reader);
|
||||
|
||||
tokenizer.nextToken();
|
||||
|
@ -101,90 +86,48 @@ public class TestReader {
|
|||
|
||||
@Test
|
||||
public void whenReadWithDataInputStream_thenCorrect() throws IOException {
|
||||
final String value = "Hello";
|
||||
final DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("src/test/resources/test_read.txt")));
|
||||
outStream.writeUTF(value);
|
||||
outStream.close();
|
||||
final String expected_value = "Hello";
|
||||
|
||||
String result;
|
||||
final DataInputStream reader = new DataInputStream(new FileInputStream("src/test/resources/test_read.txt"));
|
||||
final DataInputStream reader = new DataInputStream(new FileInputStream("src/test/resources/test_read4.txt"));
|
||||
result = reader.readUTF();
|
||||
reader.close();
|
||||
|
||||
assertEquals(value, result);
|
||||
assertEquals(expected_value, result);
|
||||
}
|
||||
|
||||
public void whenReadTwoFilesWithSequenceInputStream_thenCorrect() throws IOException {
|
||||
final int value1 = 2000;
|
||||
final int value2 = 5000;
|
||||
FileOutputStream writer = new FileOutputStream("src/test/resources/test_read.txt");
|
||||
writer.write(value1);
|
||||
writer.close();
|
||||
writer = new FileOutputStream("src/test/resources/test_read1.txt");
|
||||
writer.write(value2);
|
||||
writer.close();
|
||||
final int expected_value1 = 2000;
|
||||
final int expected_value2 = 5000;
|
||||
|
||||
final FileInputStream stream1 = new FileInputStream("src/test/resources/test_read.txt");
|
||||
final FileInputStream stream2 = new FileInputStream("src/test/resources/test_read1.txt");
|
||||
final FileInputStream stream1 = new FileInputStream("src/test/resources/test_read5.txt");
|
||||
final FileInputStream stream2 = new FileInputStream("src/test/resources/test_read6.txt");
|
||||
|
||||
final SequenceInputStream sequence = new SequenceInputStream(stream1, stream2);
|
||||
final DataInputStream reader = new DataInputStream(sequence);
|
||||
|
||||
assertEquals(value1, reader.readInt());
|
||||
assertEquals(value2, reader.readInt());
|
||||
assertEquals(expected_value1, reader.readInt());
|
||||
assertEquals(expected_value2, reader.readInt());
|
||||
|
||||
reader.close();
|
||||
stream2.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadAllFilesInFolder_thenCorrect() throws IOException {
|
||||
|
||||
final File dir = new File("src/test/resources");
|
||||
final File[] allFiles = dir.listFiles();
|
||||
final int noOfFiles = allFiles.length;
|
||||
|
||||
final Vector<FileInputStream> allStreams = new Vector<FileInputStream>();
|
||||
|
||||
for (int i = 0; i < noOfFiles; i++) {
|
||||
allStreams.add(new FileInputStream(allFiles[i]));
|
||||
}
|
||||
|
||||
final Enumeration<FileInputStream> enu = allStreams.elements();
|
||||
final SequenceInputStream sequence = new SequenceInputStream(enu);
|
||||
|
||||
final BufferedReader reader = new BufferedReader(new InputStreamReader(sequence));
|
||||
|
||||
String line = reader.readLine();
|
||||
while (line != null) {
|
||||
line = reader.readLine();
|
||||
}
|
||||
reader.close();
|
||||
sequence.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadUTFEncodedFile_thenCorrect() throws IOException {
|
||||
final String str = "青空";
|
||||
final BufferedWriter writer = new BufferedWriter(new FileWriter("src/test/resources/test_read.txt"));
|
||||
writer.write(str);
|
||||
writer.close();
|
||||
|
||||
final BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("src/test/resources/test_read.txt"), "UTF-8"));
|
||||
final String expected_value = "青空";
|
||||
final BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("src/test/resources/test_read7.txt"), "UTF-8"));
|
||||
final String currentLine = reader.readLine();
|
||||
reader.close();
|
||||
|
||||
assertEquals(str, currentLine);
|
||||
assertEquals(expected_value, currentLine);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadFileContentsIntoString_thenCorrect() throws IOException {
|
||||
final String str = "Hello world \n Test line \n";
|
||||
final BufferedWriter writer = new BufferedWriter(new FileWriter("src/test/resources/test_read.txt"));
|
||||
writer.write(str);
|
||||
writer.close();
|
||||
|
||||
final BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/test_read.txt"));
|
||||
final String expected_value = "Hello world \n Test line \n";
|
||||
final BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/test_read8.txt"));
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
String currentLine = reader.readLine();
|
||||
while (currentLine != null) {
|
||||
|
@ -195,16 +138,12 @@ public class TestReader {
|
|||
|
||||
reader.close();
|
||||
|
||||
assertEquals(str, builder.toString());
|
||||
assertEquals(expected_value, builder.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadWithFileChannel_thenCorrect() throws IOException {
|
||||
final String str = "Hello world";
|
||||
final BufferedWriter writer = new BufferedWriter(new FileWriter("src/test/resources/test_read.txt"));
|
||||
writer.write(str);
|
||||
writer.close();
|
||||
|
||||
final String expected_value = "Hello world";
|
||||
final RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.txt", "r");
|
||||
final FileChannel channel = reader.getChannel();
|
||||
|
||||
|
@ -214,34 +153,29 @@ public class TestReader {
|
|||
final ByteBuffer buff = ByteBuffer.allocate(bufferSize);
|
||||
channel.read(buff);
|
||||
buff.flip();
|
||||
assertEquals(str, new String(buff.array()));
|
||||
assertEquals(expected_value, new String(buff.array()));
|
||||
channel.close();
|
||||
reader.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadSmallFileJava7_thenCorrect() throws IOException {
|
||||
final String str = "Hello world";
|
||||
|
||||
final String expected_value = "Hello world";
|
||||
final Path path = Paths.get("src/test/resources/test_read.txt");
|
||||
final byte[] strToBytes = str.getBytes();
|
||||
Files.write(path, strToBytes);
|
||||
|
||||
final String read = Files.readAllLines(path).get(0);
|
||||
assertEquals(str, read);
|
||||
assertEquals(expected_value, read);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenReadLargeFileJava7_thenCorrect() throws IOException {
|
||||
final String str = "Hello world";
|
||||
final String expected_value = "Hello world";
|
||||
|
||||
final Path path = Paths.get("src/test/resources/test_read.txt");
|
||||
final byte[] strToBytes = str.getBytes();
|
||||
Files.write(path, strToBytes);
|
||||
|
||||
final BufferedReader reader = Files.newBufferedReader(path);
|
||||
final String line = reader.readLine();
|
||||
assertEquals(str, line);
|
||||
assertEquals(expected_value, line);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue