Skip to content

Commit 722a5af

Browse files
TrailerTest: replace usages of IOUtils#closeQuietly with try-with-resources statements
(side effects: close #7, close #21, close #20)
1 parent f5c46b9 commit 722a5af

1 file changed

Lines changed: 7 additions & 25 deletions

File tree

src/test/java/org/apache/commons/io/input/TailerTest.java

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,9 @@ public void testMultiByteBreak() throws Exception {
125125
final Thread thread = new Thread(tailer);
126126
thread.start();
127127

128-
Writer out = new OutputStreamWriter(new FileOutputStream(file), charsetUTF8);
129-
BufferedReader reader = null;
130-
try{
128+
try (Writer out = new OutputStreamWriter(new FileOutputStream(file), charsetUTF8);
129+
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(origin), charsetUTF8))) {
131130
List<String> lines = new ArrayList<>();
132-
reader = new BufferedReader(new InputStreamReader(new FileInputStream(origin), charsetUTF8));
133131
String line;
134132
while((line = reader.readLine()) != null){
135133
out.write(line);
@@ -151,10 +149,8 @@ public void testMultiByteBreak() throws Exception {
151149
+ "\nAct: (" + actual.length() + ") "+ actual);
152150
}
153151
}
154-
}finally{
152+
} finally{
155153
tailer.stop();
156-
IOUtils.closeQuietly(reader);
157-
IOUtils.closeQuietly(out);
158154
}
159155
}
160156

@@ -170,7 +166,6 @@ public void testTailerEof() throws Exception {
170166
thread.start();
171167

172168
// Write some lines to the file
173-
final FileWriter writer = null;
174169
try {
175170
writeString(file, "Line");
176171

@@ -189,7 +184,6 @@ public void testTailerEof() throws Exception {
189184
} finally {
190185
tailer.stop();
191186
TestUtils.sleep(delay * 2);
192-
IOUtils.closeQuietly(writer);
193187
}
194188
}
195189

@@ -298,12 +292,9 @@ protected void createFile(final File file, final long size)
298292
throw new IOException("Cannot create file " + file
299293
+ " as the parent directory does not exist");
300294
}
301-
final BufferedOutputStream output =
302-
new BufferedOutputStream(new FileOutputStream(file));
303-
try {
295+
try (final BufferedOutputStream output =
296+
new BufferedOutputStream(new FileOutputStream(file))) {
304297
TestUtils.generateTestData(output, size);
305-
} finally {
306-
IOUtils.closeQuietly(output);
307298
}
308299

309300
// try to make sure file is found
@@ -328,31 +319,22 @@ protected void createFile(final File file, final long size)
328319

329320
/** Append some lines to a file */
330321
private void write(final File file, final String... lines) throws Exception {
331-
FileWriter writer = null;
332-
try {
333-
writer = new FileWriter(file, true);
322+
try (FileWriter writer = new FileWriter(file, true)) {
334323
for (final String line : lines) {
335324
writer.write(line + "\n");
336325
}
337-
} finally {
338-
IOUtils.closeQuietly(writer);
339326
}
340327
}
341328

342329
/** Append a string to a file */
343330
private void writeString(final File file, final String ... strings) throws Exception {
344-
FileWriter writer = null;
345-
try {
346-
writer = new FileWriter(file, true);
331+
try (FileWriter writer = new FileWriter(file, true)) {
347332
for (final String string : strings) {
348333
writer.write(string);
349334
}
350-
} finally {
351-
IOUtils.closeQuietly(writer);
352335
}
353336
}
354337

355-
356338
@Test
357339
public void testStopWithNoFile() throws Exception {
358340
final File file = new File(getTestDirectory(),"nosuchfile");

0 commit comments

Comments
 (0)