|
18 | 18 |
|
19 | 19 | import static org.apache.commons.io.test.TestUtils.checkFile; |
20 | 20 | import static org.junit.jupiter.api.Assertions.assertFalse; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
21 | 22 | import static org.junit.jupiter.api.Assertions.assertTrue; |
22 | 23 | import static org.junit.jupiter.api.Assertions.fail; |
23 | 24 |
|
|
30 | 31 | import java.nio.charset.Charset; |
31 | 32 | import java.nio.charset.CharsetEncoder; |
32 | 33 |
|
33 | | -import org.apache.commons.io.IOUtils; |
34 | 34 | import org.junit.jupiter.api.BeforeEach; |
35 | 35 | import org.junit.jupiter.api.Test; |
36 | 36 | import org.junit.jupiter.api.io.TempDir; |
@@ -171,64 +171,44 @@ private void writeTestPayload(final FileWriter fw1, final FileWriterWithEncoding |
171 | 171 | //----------------------------------------------------------------------- |
172 | 172 | @Test |
173 | 173 | public void constructor_File_encoding_badEncoding() { |
174 | | - Writer writer = null; |
175 | | - try { |
176 | | - writer = new FileWriterWithEncoding(file1, "BAD-ENCODE"); |
177 | | - fail(); |
178 | | - } catch (final IOException ex) { |
179 | | - // expected |
180 | | - assertFalse(file1.exists()); |
181 | | - } finally { |
182 | | - IOUtils.closeQuietly(writer); |
183 | | - } |
| 174 | + assertThrows(IOException.class, () -> { |
| 175 | + try ( |
| 176 | + Writer writer = new FileWriterWithEncoding(file1, "BAD-ENCODE"); |
| 177 | + ){ } |
| 178 | + }); |
184 | 179 | assertFalse(file1.exists()); |
185 | 180 | } |
186 | 181 |
|
187 | 182 | //----------------------------------------------------------------------- |
188 | 183 | @Test |
189 | 184 | public void constructor_File_directory() { |
190 | | - Writer writer = null; |
191 | | - try { |
192 | | - writer = new FileWriterWithEncoding(temporaryFolder, defaultEncoding); |
193 | | - fail(); |
194 | | - } catch (final IOException ex) { |
195 | | - // expected |
196 | | - assertFalse(file1.exists()); |
197 | | - } finally { |
198 | | - IOUtils.closeQuietly(writer); |
199 | | - } |
| 185 | + assertThrows(IOException.class, () -> { |
| 186 | + try ( |
| 187 | + Writer writer = new FileWriterWithEncoding(temporaryFolder, defaultEncoding); |
| 188 | + ){ } |
| 189 | + }); |
200 | 190 | assertFalse(file1.exists()); |
201 | 191 | } |
202 | 192 |
|
203 | 193 | //----------------------------------------------------------------------- |
204 | 194 | @Test |
205 | 195 | public void constructor_File_nullFile() throws IOException { |
206 | | - Writer writer = null; |
207 | | - try { |
208 | | - writer = new FileWriterWithEncoding((File) null, defaultEncoding); |
209 | | - fail(); |
210 | | - } catch (final NullPointerException ex) { |
211 | | - // expected |
212 | | - assertFalse(file1.exists()); |
213 | | - } finally { |
214 | | - IOUtils.closeQuietly(writer); |
215 | | - } |
| 196 | + assertThrows(NullPointerException.class, () -> { |
| 197 | + try ( |
| 198 | + Writer writer = new FileWriterWithEncoding((File) null, defaultEncoding); |
| 199 | + ){ } |
| 200 | + }); |
216 | 201 | assertFalse(file1.exists()); |
217 | 202 | } |
218 | 203 |
|
219 | 204 | //----------------------------------------------------------------------- |
220 | 205 | @Test |
221 | 206 | public void constructor_fileName_nullFile() throws IOException { |
222 | | - Writer writer = null; |
223 | | - try { |
224 | | - writer = new FileWriterWithEncoding((String) null, defaultEncoding); |
225 | | - fail(); |
226 | | - } catch (final NullPointerException ex) { |
227 | | - // expected |
228 | | - assertFalse(file1.exists()); |
229 | | - } finally { |
230 | | - IOUtils.closeQuietly(writer); |
231 | | - } |
| 207 | + assertThrows(NullPointerException.class, () -> { |
| 208 | + try ( |
| 209 | + Writer writer = new FileWriterWithEncoding((String) null, defaultEncoding); |
| 210 | + ){ } |
| 211 | + }); |
232 | 212 | assertFalse(file1.exists()); |
233 | 213 | } |
234 | 214 |
|
|
0 commit comments