11package org .baeldung .java .io ;
22
3+ import static org .junit .Assert .assertEquals ;
4+
35import java .io .BufferedOutputStream ;
46import java .io .BufferedReader ;
57import java .io .BufferedWriter ;
2123import java .nio .file .Path ;
2224import java .nio .file .Paths ;
2325
24- import junit .framework . TestCase ;
26+ import org . junit .Test ;
2527
26- public class TestWriter extends TestCase {
28+ public class TestWriter {
2729
2830 private String fileName = "test.txt" ;
2931 private String fileName1 = "test1.txt" ;
@@ -32,27 +34,15 @@ public class TestWriter extends TestCase {
3234 private String fileName4 = "test4.txt" ;
3335 private String fileName5 = "test5.txt" ;
3436
35- public TestWriter (final String name ) {
36- super (name );
37- }
38-
39- @ Override
40- protected void setUp () throws Exception {
41- super .setUp ();
42- }
43-
44- @ Override
45- protected void tearDown () throws Exception {
46- super .tearDown ();
47- }
48-
37+ @ Test
4938 public void whenWriteStringUsingBufferedWritter_thenCorrect () throws IOException {
5039 final String str = "Hello" ;
5140 final BufferedWriter writer = new BufferedWriter (new FileWriter (fileName3 ));
5241 writer .write (str );
5342 writer .close ();
5443 }
5544
45+ @ Test
5646 public void whenAppendStringUsingBufferedWritter_thenOldContentShouldExistToo () throws IOException {
5747 final String str = "World" ;
5848 final BufferedWriter writer = new BufferedWriter (new FileWriter (fileName3 , true ));
@@ -61,13 +51,15 @@ public void whenAppendStringUsingBufferedWritter_thenOldContentShouldExistToo()
6151 writer .close ();
6252 }
6353
54+ @ Test
6455 public void whenWriteFormattedStringUsingPrintWriter_thenOutputShouldBeFormatted () throws IOException {
6556 final FileWriter fileWriter = new FileWriter (fileName );
6657 final PrintWriter printWriter = new PrintWriter (fileWriter );
6758 printWriter .printf ("Product name is %s and its price is %d $" , "iPhone" , 1000 );
6859 printWriter .close ();
6960 }
7061
62+ @ Test
7163 public void whenWriteStringUsingFileOutputStream_thenCorrect () throws IOException {
7264 final String str = "Hello" ;
7365 final FileOutputStream outputStream = new FileOutputStream (fileName3 );
@@ -76,6 +68,7 @@ public void whenWriteStringUsingFileOutputStream_thenCorrect() throws IOExceptio
7668 outputStream .close ();
7769 }
7870
71+ @ Test
7972 public void whenWriteStringWithDataOutputStream_thenReadShouldBeTheSame () throws IOException {
8073 final String value = "Hello" ;
8174 final FileOutputStream fos = new FileOutputStream (fileName1 );
@@ -92,7 +85,8 @@ public void whenWriteStringWithDataOutputStream_thenReadShouldBeTheSame() throws
9285 assertEquals (value , result );
9386 }
9487
95- public void whenWriteToPositionAndEditIt_thenItShouldChange () {
88+ @ Test
89+ public void whenWriteToPositionAndEditIt_thenItShouldChange () throws IOException {
9690 final int data1 = 2014 ;
9791 final int data2 = 1500 ;
9892 testWriteToPosition (data1 , 4 );
@@ -101,6 +95,7 @@ public void whenWriteToPositionAndEditIt_thenItShouldChange() {
10195 assertEquals (data2 , testReadFromPosition (4 ));
10296 }
10397
98+ @ Test
10499 public void whenTryToLockFile_thenItShouldBeLocked () throws IOException {
105100 final RandomAccessFile stream = new RandomAccessFile (fileName4 , "rw" );
106101 final FileChannel channel = stream .getChannel ();
@@ -119,6 +114,7 @@ public void whenTryToLockFile_thenItShouldBeLocked() throws IOException {
119114 channel .close ();
120115 }
121116
117+ @ Test
122118 public void whenWriteStringUsingFileChannel_thenReadShouldBeTheSame () throws IOException {
123119 final RandomAccessFile stream = new RandomAccessFile (fileName5 , "rw" );
124120 final FileChannel channel = stream .getChannel ();
@@ -137,6 +133,7 @@ public void whenWriteStringUsingFileChannel_thenReadShouldBeTheSame() throws IOE
137133
138134 }
139135
136+ @ Test
140137 public void whenWriteToTmpFile_thenCorrect () throws IOException {
141138 final String toWrite = "Hello" ;
142139 final File tmpFile = File .createTempFile ("test" , ".tmp" );
@@ -149,6 +146,7 @@ public void whenWriteToTmpFile_thenCorrect() throws IOException {
149146 reader .close ();
150147 }
151148
149+ @ Test
152150 public void whenWriteUsingJava7_thenCorrect () throws IOException {
153151 final String str = "Hello" ;
154152
@@ -162,28 +160,20 @@ public void whenWriteUsingJava7_thenCorrect() throws IOException {
162160 }
163161
164162 // use RandomAccessFile to write data at specific position in the file
165- public void testWriteToPosition (final int data , final long position ) {
166- try {
167- final RandomAccessFile writer = new RandomAccessFile (fileName2 , "rw" );
168- writer .seek (position );
169- writer .writeInt (data );
170- writer .close ();
171- } catch (final Exception e ) {
172- System .out .println (e .getLocalizedMessage ());
173- }
163+ private void testWriteToPosition (final int data , final long position ) throws IOException {
164+ final RandomAccessFile writer = new RandomAccessFile (fileName2 , "rw" );
165+ writer .seek (position );
166+ writer .writeInt (data );
167+ writer .close ();
174168 }
175169
176170 // use RandomAccessFile to read data from specific position in the file
177- public int testReadFromPosition (final long position ) {
171+ private int testReadFromPosition (final long position ) throws IOException {
178172 int result = 0 ;
179- try {
180- final RandomAccessFile reader = new RandomAccessFile (fileName2 , "r" );
181- reader .seek (position );
182- result = reader .readInt ();
183- reader .close ();
184- } catch (final Exception e ) {
185- System .out .println (e .getLocalizedMessage ());
186- }
173+ final RandomAccessFile reader = new RandomAccessFile (fileName2 , "r" );
174+ reader .seek (position );
175+ result = reader .readInt ();
176+ reader .close ();
187177 return result ;
188178 }
189179
0 commit comments