Skip to content

Commit 800cf14

Browse files
committed
Modify TestWriter
Adding some test methods
1 parent f32730e commit 800cf14

1 file changed

Lines changed: 82 additions & 121 deletions

File tree

Lines changed: 82 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
package test;
22

3-
import java.io.BufferedInputStream;
43
import java.io.BufferedOutputStream;
54
import java.io.DataInputStream;
65
import java.io.DataOutputStream;
6+
import java.io.File;
77
import java.io.FileInputStream;
88
import java.io.FileOutputStream;
9-
import java.io.FileReader;
109
import java.io.FileWriter;
1110
import java.io.IOException;
1211
import java.io.ObjectInputStream;
1312
import java.io.ObjectOutputStream;
13+
import java.io.PrintWriter;
1414
import java.io.RandomAccessFile;
15-
import java.util.List;
15+
import java.nio.ByteBuffer;
16+
import java.nio.channels.FileChannel;
17+
import java.nio.channels.FileLock;
1618

17-
import au.com.bytecode.opencsv.CSVReader;
18-
import au.com.bytecode.opencsv.CSVWriter;
1919
import main.Foo;
2020
import junit.framework.TestCase;
2121

2222
public class TestWriter extends TestCase {
2323

24+
private String fileName = "test.txt";
2425
private String fileName1 = "test1.txt";
2526
private String fileName2 = "test2.txt";
2627
private String fileName3 = "test3.txt";
27-
private String fileName4 = "test4.csv";
28+
private String fileName4 = "test4.txt";
29+
private String fileName5 = "test5.txt";
2830

2931
public TestWriter(String name) {
3032
super(name);
@@ -37,11 +39,28 @@ protected void setUp() throws Exception {
3739
protected void tearDown() throws Exception {
3840
super.tearDown();
3941
}
42+
43+
public void testWriteFormattedStringUsingPrintWriter() throws IOException{
44+
FileWriter fileWriter = new FileWriter(fileName);
45+
PrintWriter printWriter = new PrintWriter(fileWriter);
46+
printWriter.printf("Product name is %s and its price is %d $","iPhone",1000);
47+
printWriter.close();
48+
}
4049

41-
public void testWriteDouble_thenReadIt_shouldBeTheSame() {
42-
double value = 2.5;
43-
testWriteDouble(value);
44-
assertEquals(value, testReadDouble(), 0.0000001);
50+
public void testWriteStringWithDataOutputStream_thenReadIt_shouldBeTheSame() throws IOException {
51+
String value = "Hello";
52+
FileOutputStream fos = new FileOutputStream(fileName1);
53+
DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos));
54+
outStream.writeUTF(value);
55+
outStream.close();
56+
57+
String result;
58+
FileInputStream fis = new FileInputStream(fileName1);
59+
DataInputStream reader = new DataInputStream(fis);
60+
result = reader.readUTF();
61+
reader.close();
62+
63+
assertEquals(value, result);
4564
}
4665

4766
public void testWriteToPosition_editValueIfSpecificPos_shouldChange() {
@@ -53,39 +72,62 @@ public void testWriteToPosition_editValueIfSpecificPos_shouldChange() {
5372
assertEquals(data2, testReadFromPosition(4));
5473
}
5574

56-
public void testWriteObject_thenReadIt_instanceVariableValuesShouldBeTheSame() {
75+
public void testWriteObject_thenReadIt_instanceVariableValuesShouldBeTheSame() throws Exception {
5776
Foo foo = new Foo(1, "John");
58-
testWriteObject(foo);
59-
Foo read = testReadObject();
60-
assertEquals(foo.getId(), read.getId());
61-
assertEquals(foo.getName(), read.getName());
62-
}
77+
FileOutputStream fos = new FileOutputStream(fileName3);
78+
ObjectOutputStream writer = new ObjectOutputStream(fos);
79+
writer.writeObject(foo);
80+
writer.close();
81+
Foo result = null;
6382

64-
public void testWriteCSVReport_thenReadAllData_shouldBeTheSame() {
65-
Foo[] arr = new Foo[3];
66-
arr[0] = new Foo(1, "John");
67-
arr[1] = new Foo(2, "Adam");
68-
arr[2] = new Foo(3, "Jane");
69-
testWriteCSVReport(arr);
70-
Foo[] read = testReadCSVReport();
71-
assertEquals(arr[0].getId(), read[0].getId());
72-
assertEquals(arr[2].getName(), read[2].getName());
73-
assertEquals(arr[1].getId(), read[1].getId());
83+
FileInputStream fis = new FileInputStream(fileName3);
84+
ObjectInputStream reader = new ObjectInputStream(fis);
85+
result = (Foo) reader.readObject();
86+
reader.close();
87+
88+
assertEquals(foo.getId(), result.getId());
89+
assertEquals(foo.getName(), result.getName());
7490
}
75-
//================= writer methods========
76-
// use DataOutputStream to write primitive data types
77-
public void testWriteDouble(double value) {
78-
DataOutputStream stream;
79-
try {
80-
FileOutputStream fos = new FileOutputStream(fileName1);
81-
stream = new DataOutputStream(new BufferedOutputStream(fos));
82-
stream.writeDouble(value);
83-
stream.close();
84-
} catch (Exception e) {
85-
System.out.println(e.getLocalizedMessage());
86-
}
91+
92+
public void testFileLock() throws IOException {
93+
RandomAccessFile stream = new RandomAccessFile(fileName4, "rw");
94+
FileChannel channel = stream.getChannel();
95+
96+
FileLock lock = channel.tryLock();
97+
stream.writeChars("test lock");
98+
lock.release();
99+
100+
stream.close();
101+
channel.close();
87102
}
88103

104+
105+
public void testCreateFile_thenCheckIfItExists_thenDeleteAndCheckIfExist() throws IOException{
106+
File file = new File("test_create.txt");
107+
file.createNewFile();
108+
assertTrue(file.exists());
109+
file.delete();
110+
assertFalse(file.exists());
111+
}
112+
113+
public void testWriteStringUsingFileChannel() throws IOException{
114+
RandomAccessFile stream = new RandomAccessFile(fileName5, "rw");
115+
FileChannel channel = stream.getChannel();
116+
String value = "Hello";
117+
byte[] strBytes = value.getBytes();
118+
ByteBuffer buffer = ByteBuffer.allocate(strBytes.length);
119+
buffer.put(strBytes);
120+
buffer.flip();
121+
channel.write(buffer);
122+
stream.close();
123+
channel.close();
124+
125+
RandomAccessFile reader = new RandomAccessFile(fileName5, "r");
126+
assertEquals(value , reader.readLine());
127+
reader.close();
128+
129+
}
130+
89131
// use RandomAccessFile to write data at specific position in the file
90132
public void testWriteToPosition(int data, long position) {
91133
try {
@@ -98,50 +140,7 @@ public void testWriteToPosition(int data, long position) {
98140
}
99141
}
100142

101-
// use ObjectOutputStream to write object
102-
public void testWriteObject(Foo foo) {
103-
FileOutputStream fos;
104-
try {
105-
fos = new FileOutputStream(fileName3);
106-
ObjectOutputStream writer = new ObjectOutputStream(fos);
107-
writer.writeObject(foo);
108-
writer.close();
109-
} catch (Exception e) {
110-
System.out.println(e.getLocalizedMessage());
111-
}
112143

113-
}
114-
115-
// use CSVWriter to write CSV data
116-
public void testWriteCSVReport(Foo[] array) {
117-
try {
118-
CSVWriter writer = new CSVWriter(new FileWriter(fileName4, true), ',');
119-
int len = array.length;
120-
for (int i = 0; i < len; i++) {
121-
writer.writeNext(array[i].toStringArray());
122-
}
123-
writer.close();
124-
} catch (IOException e) {
125-
System.out.println(e.getLocalizedMessage());
126-
}
127-
}
128-
129-
130-
//=============== read methods ======
131-
// use DataInputStream
132-
public double testReadDouble() {
133-
DataInputStream stream;
134-
double result = 0;
135-
try {
136-
FileInputStream fis = new FileInputStream(fileName1);
137-
stream = new DataInputStream(new BufferedInputStream(fis));
138-
result = stream.readDouble();
139-
stream.close();
140-
} catch (Exception e) {
141-
System.out.println(e.getLocalizedMessage());
142-
}
143-
return result;
144-
}
145144

146145
// use RandomAccessFile to read data from specific position in the file
147146
public int testReadFromPosition(long position) {
@@ -157,46 +156,8 @@ public int testReadFromPosition(long position) {
157156
return result;
158157
}
159158

160-
// use ObjectInputStream to read object
161-
public Foo testReadObject() {
162-
FileInputStream fis;
163-
Foo result = null;
164-
try {
165-
fis = new FileInputStream(fileName3);
166-
ObjectInputStream reader = new ObjectInputStream(fis);
167-
result = (Foo) reader.readObject();
168-
reader.close();
169-
} catch (Exception e) {
170-
System.out.println(e.getLocalizedMessage());
171-
}
172-
return result;
173-
}
174-
175-
// use CSVReader to read CSV data
176-
public Foo[] testReadCSVReport() {
177-
CSVReader reader;
178-
Foo[] result = null;
179-
try {
180-
reader = new CSVReader(new FileReader(fileName4));
181-
List list = reader.readAll();
182-
int size = list.size();
183-
result = new Foo[size];
184-
Foo temp;
185-
String[] currentItem;
186-
for (int i = 0; i < size; i++) {
187-
temp = new Foo();
188-
currentItem = (String[]) list.get(i);
189-
temp.setId(Long.parseLong(currentItem[0]));
190-
temp.setName(currentItem[1]);
191-
result[i] = temp;
192-
}
193-
reader.close();
194-
} catch (Exception e) {
195-
System.out.println(e.getLocalizedMessage());
196-
}
197-
return result;
198-
}
199-
159+
160+
200161

201162

202163
}

0 commit comments

Comments
 (0)