Skip to content

Commit 3fd3475

Browse files
author
eugenp
committed
small testing modification in core java
1 parent 28bc11e commit 3fd3475

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

core-java/src/test/java/org/baeldung/java/io/TestWriter.java renamed to core-java/src/test/java/org/baeldung/java/io/JavaWriteToFileTest.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
import org.junit.Test;
2727

28-
public class TestWriter {
28+
public class JavaWriteToFileTest {
2929

3030
private String fileName = "src/test/resources/test_write.txt";
3131
private String fileName1 = "src/test/resources/test_write_1.txt";
@@ -68,8 +68,10 @@ public void whenWriteStringUsingFileOutputStream_thenCorrect() throws IOExceptio
6868
outputStream.close();
6969
}
7070

71+
//
72+
7173
@Test
72-
public void whenWriteStringWithDataOutputStream_thenReadShouldBeTheSame() throws IOException {
74+
public void givenWritingToFile_whenUsingDataOutputStream_thenCorrect() throws IOException {
7375
final String value = "Hello";
7476
final FileOutputStream fos = new FileOutputStream(fileName1);
7577
final DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos));
@@ -86,13 +88,13 @@ public void whenWriteStringWithDataOutputStream_thenReadShouldBeTheSame() throws
8688
}
8789

8890
@Test
89-
public void whenWriteToPositionAndEditIt_thenItShouldChange() throws IOException {
91+
public void whenWritingToSpecificPositionInFile_thenCorrect() throws IOException {
9092
final int data1 = 2014;
9193
final int data2 = 1500;
92-
testWriteToPosition(data1, 4);
93-
assertEquals(data1, testReadFromPosition(4));
94-
testWriteToPosition(data2, 4);
95-
assertEquals(data2, testReadFromPosition(4));
94+
writeToPosition(fileName2, data1, 4);
95+
assertEquals(data1, readFromPosition(fileName2, 4));
96+
writeToPosition(fileName2, data2, 4);
97+
assertEquals(data2, readFromPosition(fileName2, 4));
9698
}
9799

98100
@Test
@@ -115,7 +117,7 @@ public void whenTryToLockFile_thenItShouldBeLocked() throws IOException {
115117
}
116118

117119
@Test
118-
public void whenWriteStringUsingFileChannel_thenReadShouldBeTheSame() throws IOException {
120+
public void givenWritingToFile_whenUsingFileChannel_thenCorrect() throws IOException {
119121
final RandomAccessFile stream = new RandomAccessFile(fileName5, "rw");
120122
final FileChannel channel = stream.getChannel();
121123
final String value = "Hello";
@@ -147,7 +149,7 @@ public void whenWriteToTmpFile_thenCorrect() throws IOException {
147149
}
148150

149151
@Test
150-
public void whenWriteUsingJava7_thenCorrect() throws IOException {
152+
public void givenUsingJava7_whenWritingToFile_thenCorrect() throws IOException {
151153
final String str = "Hello";
152154

153155
final Path path = Paths.get(fileName3);
@@ -159,23 +161,24 @@ public void whenWriteUsingJava7_thenCorrect() throws IOException {
159161
assertEquals(str, read);
160162
}
161163

164+
// UTIL
165+
162166
// use RandomAccessFile to write data at specific position in the file
163-
private void testWriteToPosition(final int data, final long position) throws IOException {
164-
final RandomAccessFile writer = new RandomAccessFile(fileName2, "rw");
167+
private void writeToPosition(final String filename, final int data, final long position) throws IOException {
168+
final RandomAccessFile writer = new RandomAccessFile(filename, "rw");
165169
writer.seek(position);
166170
writer.writeInt(data);
167171
writer.close();
168172
}
169173

170174
// use RandomAccessFile to read data from specific position in the file
171-
private int testReadFromPosition(final long position) throws IOException {
175+
private int readFromPosition(final String filename, final long position) throws IOException {
172176
int result = 0;
173-
final RandomAccessFile reader = new RandomAccessFile(fileName2, "r");
177+
final RandomAccessFile reader = new RandomAccessFile(filename, "r");
174178
reader.seek(position);
175179
result = reader.readInt();
176180
reader.close();
177181
return result;
178182
}
179183

180-
181184
}

0 commit comments

Comments
 (0)