Skip to content

Commit f6a25f3

Browse files
author
Eugen
committed
Merge pull request eugenp#45 from Doha2012/master
Modify test methods
2 parents b8102b9 + a06fead commit f6a25f3

1 file changed

Lines changed: 13 additions & 35 deletions

File tree

core-java/src/test/java/org/baeldung/java/io/TestWriter.java

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,23 @@
33
import java.io.BufferedOutputStream;
44
import java.io.DataInputStream;
55
import java.io.DataOutputStream;
6-
import java.io.File;
76
import java.io.FileInputStream;
87
import java.io.FileOutputStream;
98
import java.io.FileWriter;
109
import java.io.IOException;
11-
import java.io.ObjectInputStream;
12-
import java.io.ObjectOutputStream;
1310
import java.io.PrintWriter;
1411
import java.io.RandomAccessFile;
1512
import java.nio.ByteBuffer;
1613
import java.nio.channels.FileChannel;
1714
import java.nio.channels.FileLock;
18-
19-
import main.Foo;
15+
import java.nio.channels.OverlappingFileLockException;
2016
import junit.framework.TestCase;
2117

2218
public class TestWriter extends TestCase {
2319

2420
private String fileName = "test.txt";
2521
private String fileName1 = "test1.txt";
2622
private String fileName2 = "test2.txt";
27-
private String fileName3 = "test3.txt";
2823
private String fileName4 = "test4.txt";
2924
private String fileName5 = "test5.txt";
3025

@@ -40,14 +35,14 @@ protected void tearDown() throws Exception {
4035
super.tearDown();
4136
}
4237

43-
public void testWriteFormattedStringUsingPrintWriter() throws IOException{
38+
public void whenWriteFormattedStringUsingPrintWriter_thenOutputShouldBeFormatted() throws IOException{
4439
FileWriter fileWriter = new FileWriter(fileName);
4540
PrintWriter printWriter = new PrintWriter(fileWriter);
4641
printWriter.printf("Product name is %s and its price is %d $","iPhone",1000);
4742
printWriter.close();
4843
}
4944

50-
public void testWriteStringWithDataOutputStream_thenReadIt_shouldBeTheSame() throws IOException {
45+
public void whenWriteStringWithDataOutputStream_thenReadShouldBeTheSame() throws IOException {
5146
String value = "Hello";
5247
FileOutputStream fos = new FileOutputStream(fileName1);
5348
DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos));
@@ -63,7 +58,7 @@ public void testWriteStringWithDataOutputStream_thenReadIt_shouldBeTheSame() thr
6358
assertEquals(value, result);
6459
}
6560

66-
public void testWriteToPosition_editValueIfSpecificPos_shouldChange() {
61+
public void whenWriteToPositionAndEditIt_thenItShouldChange() {
6762
int data1 = 2014;
6863
int data2 = 1500;
6964
testWriteToPosition(data1, 4);
@@ -72,28 +67,18 @@ public void testWriteToPosition_editValueIfSpecificPos_shouldChange() {
7267
assertEquals(data2, testReadFromPosition(4));
7368
}
7469

75-
public void testWriteObject_thenReadIt_instanceVariableValuesShouldBeTheSame() throws Exception {
76-
Foo foo = new Foo(1, "John");
77-
FileOutputStream fos = new FileOutputStream(fileName3);
78-
ObjectOutputStream writer = new ObjectOutputStream(fos);
79-
writer.writeObject(foo);
80-
writer.close();
81-
Foo result = null;
82-
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());
90-
}
9170

92-
public void testFileLock() throws IOException {
71+
public void whenTryToLockFile_thenItShouldBeLocked() throws IOException {
9372
RandomAccessFile stream = new RandomAccessFile(fileName4, "rw");
9473
FileChannel channel = stream.getChannel();
9574

96-
FileLock lock = channel.tryLock();
75+
FileLock lock = null;
76+
try {
77+
lock = channel.tryLock();
78+
} catch (OverlappingFileLockException e) {
79+
stream.close();
80+
channel.close();
81+
}
9782
stream.writeChars("test lock");
9883
lock.release();
9984

@@ -102,15 +87,8 @@ public void testFileLock() throws IOException {
10287
}
10388

10489

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-
}
11290

113-
public void testWriteStringUsingFileChannel() throws IOException{
91+
public void whenWriteStringUsingFileChannel_thenReadShouldBeTheSame() throws IOException{
11492
RandomAccessFile stream = new RandomAccessFile(fileName5, "rw");
11593
FileChannel channel = stream.getChannel();
11694
String value = "Hello";

0 commit comments

Comments
 (0)