2323import java .nio .charset .UnsupportedCharsetException ;
2424
2525import org .apache .commons .io .FileUtils ;
26- import org .apache .commons .io .IOUtils ;
2726import org .apache .commons .io .testtools .FileBasedTestCase ;
2827import org .junit .After ;
2928import org .junit .Before ;
@@ -65,18 +64,14 @@ public void tearDown() throws IOException {
6564
6665 //-----------------------------------------------------------------------
6766 @ Test public void testFileLocked () throws IOException {
68- LockableFileWriter lfw1 = null ;
69- LockableFileWriter lfw2 = null ;
70- LockableFileWriter lfw3 = null ;
71- try {
72- // open a valid locakable writer
73- lfw1 = new LockableFileWriter (file );
67+
68+ // open a valid locakable writer
69+ try (LockableFileWriter lfw1 = new LockableFileWriter (file )) {
7470 assertTrue (file .exists ());
7571 assertTrue (lockFile .exists ());
7672
7773 // try to open a second writer
78- try {
79- lfw2 = new LockableFileWriter (file );
74+ try (LockableFileWriter lfw2 = new LockableFileWriter (file )) {
8075 fail ("Somehow able to open a locked file. " );
8176 } catch (final IOException ioe ) {
8277 final String msg = ioe .getMessage ();
@@ -87,8 +82,7 @@ public void tearDown() throws IOException {
8782 }
8883
8984 // try to open a third writer
90- try {
91- lfw3 = new LockableFileWriter (file );
85+ try (LockableFileWriter lfw3 = new LockableFileWriter (file )) {
9286 fail ("Somehow able to open a locked file. " );
9387 } catch (final IOException ioe ) {
9488 final String msg = ioe .getMessage ();
@@ -97,29 +91,20 @@ public void tearDown() throws IOException {
9791 assertTrue (file .exists ());
9892 assertTrue (lockFile .exists ());
9993 }
100-
101- } finally {
102- IOUtils .closeQuietly (lfw1 );
103- IOUtils .closeQuietly (lfw2 );
104- IOUtils .closeQuietly (lfw3 );
10594 }
10695 assertTrue (file .exists ());
10796 assertFalse (lockFile .exists ());
10897 }
10998
11099 //-----------------------------------------------------------------------
111100 @ Test public void testAlternateLockDir () throws IOException {
112- LockableFileWriter lfw1 = null ;
113- LockableFileWriter lfw2 = null ;
114- try {
115- // open a valid locakable writer
116- lfw1 = new LockableFileWriter (file , "UTF-8" ,true , altLockDir .getAbsolutePath ());
101+ // open a valid locakable writer
102+ try (LockableFileWriter lfw1 = new LockableFileWriter (file , "UTF-8" ,true , altLockDir .getAbsolutePath ())){
117103 assertTrue (file .exists ());
118104 assertTrue (altLockFile .exists ());
119105
120106 // try to open a second writer
121- try {
122- lfw2 = new LockableFileWriter (file , StandardCharsets .UTF_8 , true , altLockDir .getAbsolutePath ());
107+ try (LockableFileWriter lfw2 = new LockableFileWriter (file , StandardCharsets .UTF_8 , true , altLockDir .getAbsolutePath ())){
123108 fail ("Somehow able to open a locked file. " );
124109 } catch (final IOException ioe ) {
125110 final String msg = ioe .getMessage ();
@@ -128,105 +113,77 @@ public void tearDown() throws IOException {
128113 assertTrue (file .exists ());
129114 assertTrue (altLockFile .exists ());
130115 }
131-
132- } finally {
133- IOUtils .closeQuietly (lfw1 );
134- IOUtils .closeQuietly (lfw2 );
135116 }
136117 assertTrue (file .exists ());
137118 assertFalse (altLockFile .exists ());
138119 }
139120
140121 //-----------------------------------------------------------------------
141122 @ Test public void testFileNotLocked () throws IOException {
142- // open a valid locakable writer
143- LockableFileWriter lfw1 = null ;
144- try {
145- lfw1 = new LockableFileWriter (file );
123+ // open a valid lockable writer
124+ try (LockableFileWriter lfw1 = new LockableFileWriter (file )) {
146125 assertTrue (file .exists ());
147126 assertTrue (lockFile .exists ());
148- } finally {
149- IOUtils .closeQuietly (lfw1 );
150127 }
151128 assertTrue (file .exists ());
152129 assertFalse (lockFile .exists ());
153130
154131 // open a second valid writer on the same file
155- LockableFileWriter lfw2 = null ;
156- try {
157- lfw2 = new LockableFileWriter (file );
132+ try (LockableFileWriter lfw2 = new LockableFileWriter (file )) {
158133 assertTrue (file .exists ());
159134 assertTrue (lockFile .exists ());
160- } finally {
161- IOUtils .closeQuietly (lfw2 );
162135 }
163136 assertTrue (file .exists ());
164137 assertFalse (lockFile .exists ());
165138 }
166139
167140 //-----------------------------------------------------------------------
168141 @ Test public void testConstructor_File_encoding_badEncoding () throws IOException {
169- Writer writer = null ;
170- try {
171- writer = new LockableFileWriter (file , "BAD-ENCODE" );
142+ try (Writer writer = new LockableFileWriter (file , "BAD-ENCODE" )) {
172143 fail ();
173144 } catch (final UnsupportedCharsetException ex ) {
174145 // expected
175146 assertFalse (file .exists ());
176147 assertFalse (lockFile .exists ());
177- } finally {
178- IOUtils .closeQuietly (writer );
179148 }
180149 assertFalse (file .exists ());
181150 assertFalse (lockFile .exists ());
182151 }
183152
184153 //-----------------------------------------------------------------------
185154 @ Test public void testConstructor_File_directory () {
186- Writer writer = null ;
187- try {
188- writer = new LockableFileWriter (getTestDirectory ());
155+ try (Writer writer = new LockableFileWriter (getTestDirectory ())) {
189156 fail ();
190157 } catch (final IOException ex ) {
191158 // expected
192159 assertFalse (file .exists ());
193160 assertFalse (lockFile .exists ());
194- } finally {
195- IOUtils .closeQuietly (writer );
196161 }
197162 assertFalse (file .exists ());
198163 assertFalse (lockFile .exists ());
199164 }
200165
201166 //-----------------------------------------------------------------------
202167 @ Test public void testConstructor_File_nullFile () throws IOException {
203- Writer writer = null ;
204- try {
205- writer = new LockableFileWriter ((File ) null );
168+ try (Writer writer = new LockableFileWriter ((File ) null )) {
206169 fail ();
207170 } catch (final NullPointerException ex ) {
208171 // expected
209172 assertFalse (file .exists ());
210173 assertFalse (lockFile .exists ());
211- } finally {
212- IOUtils .closeQuietly (writer );
213174 }
214175 assertFalse (file .exists ());
215176 assertFalse (lockFile .exists ());
216177 }
217178
218179 //-----------------------------------------------------------------------
219180 @ Test public void testConstructor_fileName_nullFile () throws IOException {
220- Writer writer = null ;
221- try {
222- writer = new LockableFileWriter ((String ) null );
181+ try (Writer writer = new LockableFileWriter ((String ) null )) {
223182 fail ();
224183 } catch (final NullPointerException ex ) {
225184 // expected
226185 assertFalse (file .exists ());
227186 assertFalse (lockFile .exists ());
228- } finally {
229- IOUtils .closeQuietly (writer );
230187 }
231188 assertFalse (file .exists ());
232189 assertFalse (lockFile .exists ());
0 commit comments