1- package test ;
1+ package org .baeldung .java .io ;
2+
3+ import static org .junit .Assert .assertEquals ;
24
35import java .io .BufferedOutputStream ;
46import java .io .BufferedReader ;
2123import java .nio .file .Path ;
2224import java .nio .file .Paths ;
2325
24- import junit .framework .TestCase ;
25-
26- public class TestWriter extends TestCase {
26+ import org .junit .Test ;
2727
28- private String fileName = "test.txt" ;
29- private String fileName1 = "test1.txt" ;
30- private String fileName2 = "test2.txt" ;
31- private String fileName3 = "test3.txt" ;
32- private String fileName4 = "test4.txt" ;
33- private String fileName5 = "test5.txt" ;
34-
35- public TestWriter (String name ) {
36- super (name );
37- }
28+ public class TestWriter {
3829
39- protected void setUp () throws Exception {
40- super .setUp ();
41- }
30+ private String fileName = "src/test/resources/test_write.txt" ;
31+ private String fileName1 = "src/test/resources/test_write_1.txt" ;
32+ private String fileName2 = "src/test/resources/test_write_2.txt" ;
33+ private String fileName3 = "src/test/resources/test_write_3.txt" ;
34+ private String fileName4 = "src/test/resources/test_write_4.txt" ;
35+ private String fileName5 = "src/test/resources/test_write_5.txt" ;
4236
43- protected void tearDown () throws Exception {
44- super .tearDown ();
45- }
46-
47-
48- public void whenWriteStringUsingBufferedWritter_thenCorrect () throws IOException {
49- String str = "Hello" ;
50- BufferedWriter writer = new BufferedWriter (new FileWriter (fileName3 ));
37+ @ Test
38+ public void whenWriteStringUsingBufferedWritter_thenCorrect () throws IOException {
39+ final String str = "Hello" ;
40+ final BufferedWriter writer = new BufferedWriter (new FileWriter (fileName3 ));
5141 writer .write (str );
5242 writer .close ();
5343 }
54-
55- public void whenAppendStringUsingBufferedWritter_thenOldContentShouldExistToo () throws IOException {
56- String str = "World" ;
57- BufferedWriter writer = new BufferedWriter (new FileWriter (fileName3 , true ));
44+
45+ @ Test
46+ public void whenAppendStringUsingBufferedWritter_thenOldContentShouldExistToo () throws IOException {
47+ final String str = "World" ;
48+ final BufferedWriter writer = new BufferedWriter (new FileWriter (fileName3 , true ));
5849 writer .append (' ' );
5950 writer .append (str );
6051 writer .close ();
6152 }
62-
63- public void whenWriteFormattedStringUsingPrintWriter_thenOutputShouldBeFormatted () throws IOException {
64- FileWriter fileWriter = new FileWriter (fileName );
65- PrintWriter printWriter = new PrintWriter (fileWriter );
66- printWriter .printf ("Product name is %s and its price is %d $" ,"iPhone" ,1000 );
53+
54+ @ Test
55+ public void whenWriteFormattedStringUsingPrintWriter_thenOutputShouldBeFormatted () throws IOException {
56+ final FileWriter fileWriter = new FileWriter (fileName );
57+ final PrintWriter printWriter = new PrintWriter (fileWriter );
58+ printWriter .printf ("Product name is %s and its price is %d $" , "iPhone" , 1000 );
6759 printWriter .close ();
6860 }
69-
70- public void whenWriteStringUsingFileOutputStream_thenCorrect () throws IOException {
71- String str = "Hello" ;
72- FileOutputStream outputStream = new FileOutputStream (fileName3 );
73- byte [] strToBytes = str .getBytes ();
61+
62+ @ Test
63+ public void whenWriteStringUsingFileOutputStream_thenCorrect () throws IOException {
64+ final String str = "Hello" ;
65+ final FileOutputStream outputStream = new FileOutputStream (fileName3 );
66+ final byte [] strToBytes = str .getBytes ();
7467 outputStream .write (strToBytes );
7568 outputStream .close ();
7669 }
7770
71+ @ Test
7872 public void whenWriteStringWithDataOutputStream_thenReadShouldBeTheSame () throws IOException {
79- String value = "Hello" ;
80- FileOutputStream fos = new FileOutputStream (fileName1 );
81- DataOutputStream outStream = new DataOutputStream (new BufferedOutputStream (fos ));
73+ final String value = "Hello" ;
74+ final FileOutputStream fos = new FileOutputStream (fileName1 );
75+ final DataOutputStream outStream = new DataOutputStream (new BufferedOutputStream (fos ));
8276 outStream .writeUTF (value );
8377 outStream .close ();
84-
78+
8579 String result ;
86- FileInputStream fis = new FileInputStream (fileName1 );
87- DataInputStream reader = new DataInputStream (fis );
80+ final FileInputStream fis = new FileInputStream (fileName1 );
81+ final DataInputStream reader = new DataInputStream (fis );
8882 result = reader .readUTF ();
8983 reader .close ();
90-
84+
9185 assertEquals (value , result );
9286 }
9387
94- public void whenWriteToPositionAndEditIt_thenItShouldChange () {
95- int data1 = 2014 ;
96- int data2 = 1500 ;
88+ @ Test
89+ public void whenWriteToPositionAndEditIt_thenItShouldChange () throws IOException {
90+ final int data1 = 2014 ;
91+ final int data2 = 1500 ;
9792 testWriteToPosition (data1 , 4 );
9893 assertEquals (data1 , testReadFromPosition (4 ));
9994 testWriteToPosition (data2 , 4 );
10095 assertEquals (data2 , testReadFromPosition (4 ));
10196 }
10297
103-
98+ @ Test
10499 public void whenTryToLockFile_thenItShouldBeLocked () throws IOException {
105- RandomAccessFile stream = new RandomAccessFile (fileName4 , "rw" );
106- FileChannel channel = stream .getChannel ();
100+ final RandomAccessFile stream = new RandomAccessFile (fileName4 , "rw" );
101+ final FileChannel channel = stream .getChannel ();
107102
108103 FileLock lock = null ;
109104 try {
110105 lock = channel .tryLock ();
111- } catch (OverlappingFileLockException e ) {
106+ } catch (final OverlappingFileLockException e ) {
112107 stream .close ();
113108 channel .close ();
114109 }
115110 stream .writeChars ("test lock" );
116111 lock .release ();
117-
112+
118113 stream .close ();
119114 channel .close ();
120115 }
121116
122-
123-
124- public void whenWriteStringUsingFileChannel_thenReadShouldBeTheSame () throws IOException {
125- RandomAccessFile stream = new RandomAccessFile (fileName5 , "rw" );
126- FileChannel channel = stream .getChannel ();
127- String value = "Hello" ;
128- byte [] strBytes = value .getBytes ();
129- ByteBuffer buffer = ByteBuffer .allocate (strBytes .length );
117+ @ Test
118+ public void whenWriteStringUsingFileChannel_thenReadShouldBeTheSame () throws IOException {
119+ final RandomAccessFile stream = new RandomAccessFile (fileName5 , "rw" );
120+ final FileChannel channel = stream .getChannel ();
121+ final String value = "Hello" ;
122+ final byte [] strBytes = value .getBytes ();
123+ final ByteBuffer buffer = ByteBuffer .allocate (strBytes .length );
130124 buffer .put (strBytes );
131125 buffer .flip ();
132- channel .write (buffer );
126+ channel .write (buffer );
133127 stream .close ();
134128 channel .close ();
135-
136- RandomAccessFile reader = new RandomAccessFile (fileName5 , "r" );
137- assertEquals (value , reader .readLine ());
129+
130+ final RandomAccessFile reader = new RandomAccessFile (fileName5 , "r" );
131+ assertEquals (value , reader .readLine ());
138132 reader .close ();
139-
133+
140134 }
141-
142- public void whenWriteToTmpFile_thenCorrect () throws IOException {
143- String toWrite = "Hello" ;
144- File tmpFile = File .createTempFile ("test" , ".tmp" );
145- FileWriter writer = new FileWriter (tmpFile );
135+
136+ @ Test
137+ public void whenWriteToTmpFile_thenCorrect () throws IOException {
138+ final String toWrite = "Hello" ;
139+ final File tmpFile = File .createTempFile ("test" , ".tmp" );
140+ final FileWriter writer = new FileWriter (tmpFile );
146141 writer .write (toWrite );
147142 writer .close ();
148-
149- BufferedReader reader = new BufferedReader (new FileReader (tmpFile ));
143+
144+ final BufferedReader reader = new BufferedReader (new FileReader (tmpFile ));
150145 assertEquals (toWrite , reader .readLine ());
151146 reader .close ();
152147 }
153-
154- public void whenWriteUsingJava7_thenCorrect () throws IOException {
155- String str = "Hello" ;
156-
157- Path path = Paths .get (fileName3 );
158- byte [] strToBytes = str .getBytes ();
159-
148+
149+ @ Test
150+ public void whenWriteUsingJava7_thenCorrect () throws IOException {
151+ final String str = "Hello" ;
152+
153+ final Path path = Paths .get (fileName3 );
154+ final byte [] strToBytes = str .getBytes ();
155+
160156 Files .write (path , strToBytes );
161-
162- String read = Files .readAllLines (path ).get (0 );
157+
158+ final String read = Files .readAllLines (path ).get (0 );
163159 assertEquals (str , read );
164160 }
165-
161+
166162 // use RandomAccessFile to write data at specific position in the file
167- public void testWriteToPosition (int data , long position ) {
168- try {
169- RandomAccessFile writer = new RandomAccessFile (fileName2 , "rw" );
170- writer .seek (position );
171- writer .writeInt (data );
172- writer .close ();
173- } catch (Exception e ) {
174- System .out .println (e .getLocalizedMessage ());
175- }
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 ();
176168 }
177169
178-
179-
180170 // use RandomAccessFile to read data from specific position in the file
181- public int testReadFromPosition (long position ) {
171+ private int testReadFromPosition (final long position ) throws IOException {
182172 int result = 0 ;
183- try {
184- RandomAccessFile reader = new RandomAccessFile (fileName2 , "r" );
185- reader .seek (position );
186- result = reader .readInt ();
187- reader .close ();
188- } catch (Exception e ) {
189- System .out .println (e .getLocalizedMessage ());
190- }
173+ final RandomAccessFile reader = new RandomAccessFile (fileName2 , "r" );
174+ reader .seek (position );
175+ result = reader .readInt ();
176+ reader .close ();
191177 return result ;
192178 }
193179
194-
195-
196-
197-
198- }
180+ }
0 commit comments