change test files location

This commit is contained in:
DOHA 2014-09-30 01:46:29 +02:00
parent 3737584740
commit 2bdfde7795

View File

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