parent
f32730e8a3
commit
800cf141cf
|
@ -1,30 +1,32 @@
|
||||||
package test;
|
package test;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
|
||||||
import java.io.BufferedOutputStream;
|
import java.io.BufferedOutputStream;
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.FileReader;
|
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.ObjectInputStream;
|
import java.io.ObjectInputStream;
|
||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
|
import java.io.PrintWriter;
|
||||||
import java.io.RandomAccessFile;
|
import java.io.RandomAccessFile;
|
||||||
import java.util.List;
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.channels.FileChannel;
|
||||||
|
import java.nio.channels.FileLock;
|
||||||
|
|
||||||
import au.com.bytecode.opencsv.CSVReader;
|
|
||||||
import au.com.bytecode.opencsv.CSVWriter;
|
|
||||||
import main.Foo;
|
import main.Foo;
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
public class TestWriter extends TestCase {
|
public class TestWriter extends TestCase {
|
||||||
|
|
||||||
|
private String fileName = "test.txt";
|
||||||
private String fileName1 = "test1.txt";
|
private String fileName1 = "test1.txt";
|
||||||
private String fileName2 = "test2.txt";
|
private String fileName2 = "test2.txt";
|
||||||
private String fileName3 = "test3.txt";
|
private String fileName3 = "test3.txt";
|
||||||
private String fileName4 = "test4.csv";
|
private String fileName4 = "test4.txt";
|
||||||
|
private String fileName5 = "test5.txt";
|
||||||
|
|
||||||
public TestWriter(String name) {
|
public TestWriter(String name) {
|
||||||
super(name);
|
super(name);
|
||||||
|
@ -37,11 +39,28 @@ public class TestWriter extends TestCase {
|
||||||
protected void tearDown() throws Exception {
|
protected void tearDown() throws Exception {
|
||||||
super.tearDown();
|
super.tearDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testWriteFormattedStringUsingPrintWriter() 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);
|
||||||
|
printWriter.close();
|
||||||
|
}
|
||||||
|
|
||||||
public void testWriteDouble_thenReadIt_shouldBeTheSame() {
|
public void testWriteStringWithDataOutputStream_thenReadIt_shouldBeTheSame() throws IOException {
|
||||||
double value = 2.5;
|
String value = "Hello";
|
||||||
testWriteDouble(value);
|
FileOutputStream fos = new FileOutputStream(fileName1);
|
||||||
assertEquals(value, testReadDouble(), 0.0000001);
|
DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos));
|
||||||
|
outStream.writeUTF(value);
|
||||||
|
outStream.close();
|
||||||
|
|
||||||
|
String result;
|
||||||
|
FileInputStream fis = new FileInputStream(fileName1);
|
||||||
|
DataInputStream reader = new DataInputStream(fis);
|
||||||
|
result = reader.readUTF();
|
||||||
|
reader.close();
|
||||||
|
|
||||||
|
assertEquals(value, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testWriteToPosition_editValueIfSpecificPos_shouldChange() {
|
public void testWriteToPosition_editValueIfSpecificPos_shouldChange() {
|
||||||
|
@ -53,39 +72,62 @@ public class TestWriter extends TestCase {
|
||||||
assertEquals(data2, testReadFromPosition(4));
|
assertEquals(data2, testReadFromPosition(4));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testWriteObject_thenReadIt_instanceVariableValuesShouldBeTheSame() {
|
public void testWriteObject_thenReadIt_instanceVariableValuesShouldBeTheSame() throws Exception {
|
||||||
Foo foo = new Foo(1, "John");
|
Foo foo = new Foo(1, "John");
|
||||||
testWriteObject(foo);
|
FileOutputStream fos = new FileOutputStream(fileName3);
|
||||||
Foo read = testReadObject();
|
ObjectOutputStream writer = new ObjectOutputStream(fos);
|
||||||
assertEquals(foo.getId(), read.getId());
|
writer.writeObject(foo);
|
||||||
assertEquals(foo.getName(), read.getName());
|
writer.close();
|
||||||
|
Foo result = null;
|
||||||
|
|
||||||
|
FileInputStream fis = new FileInputStream(fileName3);
|
||||||
|
ObjectInputStream reader = new ObjectInputStream(fis);
|
||||||
|
result = (Foo) reader.readObject();
|
||||||
|
reader.close();
|
||||||
|
|
||||||
|
assertEquals(foo.getId(), result.getId());
|
||||||
|
assertEquals(foo.getName(), result.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testWriteCSVReport_thenReadAllData_shouldBeTheSame() {
|
public void testFileLock() throws IOException {
|
||||||
Foo[] arr = new Foo[3];
|
RandomAccessFile stream = new RandomAccessFile(fileName4, "rw");
|
||||||
arr[0] = new Foo(1, "John");
|
FileChannel channel = stream.getChannel();
|
||||||
arr[1] = new Foo(2, "Adam");
|
|
||||||
arr[2] = new Foo(3, "Jane");
|
FileLock lock = channel.tryLock();
|
||||||
testWriteCSVReport(arr);
|
stream.writeChars("test lock");
|
||||||
Foo[] read = testReadCSVReport();
|
lock.release();
|
||||||
assertEquals(arr[0].getId(), read[0].getId());
|
|
||||||
assertEquals(arr[2].getName(), read[2].getName());
|
stream.close();
|
||||||
assertEquals(arr[1].getId(), read[1].getId());
|
channel.close();
|
||||||
}
|
|
||||||
//================= writer methods========
|
|
||||||
// use DataOutputStream to write primitive data types
|
|
||||||
public void testWriteDouble(double value) {
|
|
||||||
DataOutputStream stream;
|
|
||||||
try {
|
|
||||||
FileOutputStream fos = new FileOutputStream(fileName1);
|
|
||||||
stream = new DataOutputStream(new BufferedOutputStream(fos));
|
|
||||||
stream.writeDouble(value);
|
|
||||||
stream.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println(e.getLocalizedMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void testCreateFile_thenCheckIfItExists_thenDeleteAndCheckIfExist() throws IOException{
|
||||||
|
File file = new File("test_create.txt");
|
||||||
|
file.createNewFile();
|
||||||
|
assertTrue(file.exists());
|
||||||
|
file.delete();
|
||||||
|
assertFalse(file.exists());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testWriteStringUsingFileChannel() 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);
|
||||||
|
buffer.put(strBytes);
|
||||||
|
buffer.flip();
|
||||||
|
channel.write(buffer);
|
||||||
|
stream.close();
|
||||||
|
channel.close();
|
||||||
|
|
||||||
|
RandomAccessFile reader = new RandomAccessFile(fileName5, "r");
|
||||||
|
assertEquals(value , reader.readLine());
|
||||||
|
reader.close();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// 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) {
|
public void testWriteToPosition(int data, long position) {
|
||||||
try {
|
try {
|
||||||
|
@ -98,50 +140,7 @@ public class TestWriter extends TestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// use ObjectOutputStream to write object
|
|
||||||
public void testWriteObject(Foo foo) {
|
|
||||||
FileOutputStream fos;
|
|
||||||
try {
|
|
||||||
fos = new FileOutputStream(fileName3);
|
|
||||||
ObjectOutputStream writer = new ObjectOutputStream(fos);
|
|
||||||
writer.writeObject(foo);
|
|
||||||
writer.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println(e.getLocalizedMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// use CSVWriter to write CSV data
|
|
||||||
public void testWriteCSVReport(Foo[] array) {
|
|
||||||
try {
|
|
||||||
CSVWriter writer = new CSVWriter(new FileWriter(fileName4, true), ',');
|
|
||||||
int len = array.length;
|
|
||||||
for (int i = 0; i < len; i++) {
|
|
||||||
writer.writeNext(array[i].toStringArray());
|
|
||||||
}
|
|
||||||
writer.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
System.out.println(e.getLocalizedMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//=============== read methods ======
|
|
||||||
// use DataInputStream
|
|
||||||
public double testReadDouble() {
|
|
||||||
DataInputStream stream;
|
|
||||||
double result = 0;
|
|
||||||
try {
|
|
||||||
FileInputStream fis = new FileInputStream(fileName1);
|
|
||||||
stream = new DataInputStream(new BufferedInputStream(fis));
|
|
||||||
result = stream.readDouble();
|
|
||||||
stream.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println(e.getLocalizedMessage());
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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) {
|
public int testReadFromPosition(long position) {
|
||||||
|
@ -157,46 +156,8 @@ public class TestWriter extends TestCase {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// use ObjectInputStream to read object
|
|
||||||
public Foo testReadObject() {
|
|
||||||
FileInputStream fis;
|
|
||||||
Foo result = null;
|
|
||||||
try {
|
|
||||||
fis = new FileInputStream(fileName3);
|
|
||||||
ObjectInputStream reader = new ObjectInputStream(fis);
|
|
||||||
result = (Foo) reader.readObject();
|
|
||||||
reader.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println(e.getLocalizedMessage());
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
// use CSVReader to read CSV data
|
|
||||||
public Foo[] testReadCSVReport() {
|
|
||||||
CSVReader reader;
|
|
||||||
Foo[] result = null;
|
|
||||||
try {
|
|
||||||
reader = new CSVReader(new FileReader(fileName4));
|
|
||||||
List list = reader.readAll();
|
|
||||||
int size = list.size();
|
|
||||||
result = new Foo[size];
|
|
||||||
Foo temp;
|
|
||||||
String[] currentItem;
|
|
||||||
for (int i = 0; i < size; i++) {
|
|
||||||
temp = new Foo();
|
|
||||||
currentItem = (String[]) list.get(i);
|
|
||||||
temp.setId(Long.parseLong(currentItem[0]));
|
|
||||||
temp.setName(currentItem[1]);
|
|
||||||
result[i] = temp;
|
|
||||||
}
|
|
||||||
reader.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println(e.getLocalizedMessage());
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue