cleanup work

This commit is contained in:
eugenp 2014-09-30 01:42:55 +03:00
parent 851d7c0ecf
commit 0e3c0f36df
1 changed files with 69 additions and 77 deletions

View File

@ -1,4 +1,4 @@
package test;
package org.baeldung.java.io;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
@ -32,167 +32,159 @@ public class TestWriter extends TestCase {
private String fileName4 = "test4.txt";
private String fileName5 = "test5.txt";
public TestWriter(String name) {
public TestWriter(final String name) {
super(name);
}
@Override
protected void setUp() throws Exception {
super.setUp();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
public void whenWriteStringUsingBufferedWritter_thenCorrect() throws IOException{
String str = "Hello";
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3));
public void whenWriteStringUsingBufferedWritter_thenCorrect() throws IOException {
final String str = "Hello";
final BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3));
writer.write(str);
writer.close();
}
public void whenAppendStringUsingBufferedWritter_thenOldContentShouldExistToo() throws IOException{
String str = "World";
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3 , true));
public void whenAppendStringUsingBufferedWritter_thenOldContentShouldExistToo() throws IOException {
final String str = "World";
final BufferedWriter writer = new BufferedWriter(new FileWriter(fileName3, true));
writer.append(' ');
writer.append(str);
writer.close();
}
public void whenWriteFormattedStringUsingPrintWriter_thenOutputShouldBeFormatted() throws IOException{
FileWriter fileWriter = new FileWriter(fileName);
PrintWriter printWriter = new PrintWriter(fileWriter);
printWriter.printf("Product name is %s and its price is %d $","iPhone",1000);
public void whenWriteFormattedStringUsingPrintWriter_thenOutputShouldBeFormatted() throws IOException {
final FileWriter fileWriter = new FileWriter(fileName);
final PrintWriter printWriter = new PrintWriter(fileWriter);
printWriter.printf("Product name is %s and its price is %d $", "iPhone", 1000);
printWriter.close();
}
public void whenWriteStringUsingFileOutputStream_thenCorrect() throws IOException{
String str = "Hello";
FileOutputStream outputStream = new FileOutputStream(fileName3);
byte[] strToBytes = str.getBytes();
public void whenWriteStringUsingFileOutputStream_thenCorrect() throws IOException {
final String str = "Hello";
final FileOutputStream outputStream = new FileOutputStream(fileName3);
final byte[] strToBytes = str.getBytes();
outputStream.write(strToBytes);
outputStream.close();
}
public void whenWriteStringWithDataOutputStream_thenReadShouldBeTheSame() throws IOException {
String value = "Hello";
FileOutputStream fos = new FileOutputStream(fileName1);
DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos));
final String value = "Hello";
final FileOutputStream fos = new FileOutputStream(fileName1);
final DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos));
outStream.writeUTF(value);
outStream.close();
String result;
FileInputStream fis = new FileInputStream(fileName1);
DataInputStream reader = new DataInputStream(fis);
final FileInputStream fis = new FileInputStream(fileName1);
final DataInputStream reader = new DataInputStream(fis);
result = reader.readUTF();
reader.close();
assertEquals(value, result);
}
public void whenWriteToPositionAndEditIt_thenItShouldChange() {
int data1 = 2014;
int data2 = 1500;
final int data1 = 2014;
final int data2 = 1500;
testWriteToPosition(data1, 4);
assertEquals(data1, testReadFromPosition(4));
testWriteToPosition(data2, 4);
assertEquals(data2, testReadFromPosition(4));
}
public void whenTryToLockFile_thenItShouldBeLocked() throws IOException {
RandomAccessFile stream = new RandomAccessFile(fileName4, "rw");
FileChannel channel = stream.getChannel();
final RandomAccessFile stream = new RandomAccessFile(fileName4, "rw");
final FileChannel channel = stream.getChannel();
FileLock lock = null;
try {
lock = channel.tryLock();
} catch (OverlappingFileLockException e) {
} catch (final OverlappingFileLockException e) {
stream.close();
channel.close();
}
stream.writeChars("test lock");
lock.release();
stream.close();
channel.close();
}
public void whenWriteStringUsingFileChannel_thenReadShouldBeTheSame() throws IOException{
RandomAccessFile stream = new RandomAccessFile(fileName5, "rw");
FileChannel channel = stream.getChannel();
String value = "Hello";
byte[] strBytes = value.getBytes();
ByteBuffer buffer = ByteBuffer.allocate(strBytes.length);
public void whenWriteStringUsingFileChannel_thenReadShouldBeTheSame() throws IOException {
final RandomAccessFile stream = new RandomAccessFile(fileName5, "rw");
final FileChannel channel = stream.getChannel();
final String value = "Hello";
final byte[] strBytes = value.getBytes();
final ByteBuffer buffer = ByteBuffer.allocate(strBytes.length);
buffer.put(strBytes);
buffer.flip();
channel.write(buffer);
channel.write(buffer);
stream.close();
channel.close();
RandomAccessFile reader = new RandomAccessFile(fileName5, "r");
assertEquals(value , reader.readLine());
final RandomAccessFile reader = new RandomAccessFile(fileName5, "r");
assertEquals(value, reader.readLine());
reader.close();
}
public void whenWriteToTmpFile_thenCorrect() throws IOException{
String toWrite = "Hello";
File tmpFile = File.createTempFile("test", ".tmp");
FileWriter writer = new FileWriter(tmpFile);
public void whenWriteToTmpFile_thenCorrect() throws IOException {
final String toWrite = "Hello";
final File tmpFile = File.createTempFile("test", ".tmp");
final FileWriter writer = new FileWriter(tmpFile);
writer.write(toWrite);
writer.close();
BufferedReader reader = new BufferedReader(new FileReader(tmpFile));
final BufferedReader reader = new BufferedReader(new FileReader(tmpFile));
assertEquals(toWrite, reader.readLine());
reader.close();
}
public void whenWriteUsingJava7_thenCorrect() throws IOException{
String str = "Hello";
Path path = Paths.get(fileName3);
byte[] strToBytes = str.getBytes();
public void whenWriteUsingJava7_thenCorrect() throws IOException {
final String str = "Hello";
final Path path = Paths.get(fileName3);
final byte[] strToBytes = str.getBytes();
Files.write(path, strToBytes);
String read = Files.readAllLines(path).get(0);
final String read = Files.readAllLines(path).get(0);
assertEquals(str, read);
}
// use RandomAccessFile to write data at specific position in the file
public void testWriteToPosition(int data, long position) {
public void testWriteToPosition(final int data, final long position) {
try {
RandomAccessFile writer = new RandomAccessFile(fileName2, "rw");
final RandomAccessFile writer = new RandomAccessFile(fileName2, "rw");
writer.seek(position);
writer.writeInt(data);
writer.close();
} catch (Exception e) {
} catch (final Exception e) {
System.out.println(e.getLocalizedMessage());
}
}
// use RandomAccessFile to read data from specific position in the file
public int testReadFromPosition(long position) {
public int testReadFromPosition(final long position) {
int result = 0;
try {
RandomAccessFile reader = new RandomAccessFile(fileName2, "r");
final RandomAccessFile reader = new RandomAccessFile(fileName2, "r");
reader.seek(position);
result = reader.readInt();
reader.close();
} catch (Exception e) {
} catch (final Exception e) {
System.out.println(e.getLocalizedMessage());
}
return result;
}
}