Skip to content

Commit 6b57d2a

Browse files
fix some warnings in tests
1 parent 6e33d3d commit 6b57d2a

6 files changed

Lines changed: 31 additions & 93 deletions

File tree

src/test/java/org/apache/commons/io/IOUtilsTestCase.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
* </ul>
5151
* Due to interdependencies in IOUtils and IOUtilsTestlet, one bug may cause multiple tests to fail.
5252
*/
53+
@SuppressWarnings("deprecation") // deliberately testing deprecated code
5354
public class IOUtilsTestCase extends FileBasedTestCase {
5455

5556
private static final int FILE_SIZE = 1024 * 4 + 1;
@@ -284,8 +285,6 @@ public synchronized void close() throws IOException {
284285
assertTrue(IOUtils.contentEqualsIgnoreEOL(r1, r2));
285286
}
286287

287-
@SuppressWarnings("deprecation")
288-
// testing deprecated method
289288
@Test public void testCopy_ByteArray_OutputStream() throws Exception {
290289
final File destination = TestUtils.newFile(getTestDirectory(), "copy8.txt");
291290
byte[] in;
@@ -305,8 +304,6 @@ public synchronized void close() throws IOException {
305304
TestUtils.deleteFile(destination);
306305
}
307306

308-
@SuppressWarnings("deprecation")
309-
// testing deprecated method
310307
@Test public void testCopy_ByteArray_Writer() throws Exception {
311308
final File destination = TestUtils.newFile(getTestDirectory(), "copy7.txt");
312309
byte[] in;
@@ -324,8 +321,6 @@ public synchronized void close() throws IOException {
324321
TestUtils.deleteFile(destination);
325322
}
326323

327-
@SuppressWarnings("deprecation")
328-
// testing deprecated method
329324
@Test public void testCopy_String_Writer() throws Exception {
330325
final File destination = TestUtils.newFile(getTestDirectory(), "copy6.txt");
331326
String str;
@@ -703,7 +698,6 @@ public synchronized void close() throws IOException {
703698
IOUtils.closeQuietly(reader);
704699
}
705700

706-
@SuppressWarnings("deprecation") // deliberately testing deprecated method
707701
@Test public void testReadLines_InputStream() throws Exception {
708702
final File file = TestUtils.newFile(getTestDirectory(), "lines.txt");
709703
InputStream in = null;
@@ -849,8 +843,6 @@ public synchronized void close() throws IOException {
849843
IOUtils.closeQuietly(input);
850844
}
851845

852-
@SuppressWarnings("deprecation")
853-
// testing deprecated method
854846
@Test public void testStringToOutputStream() throws Exception {
855847
final File destination = TestUtils.newFile(getTestDirectory(), "copy5.txt");
856848
String str;
@@ -961,7 +953,6 @@ public synchronized void close() throws IOException {
961953
}
962954
}
963955

964-
@SuppressWarnings("deprecation") // deliberately testing deprecated method
965956
@Test public void testToByteArray_Reader() throws IOException {
966957
final String charsetName = "UTF-8";
967958
final byte[] expecteds = charsetName.getBytes(charsetName);
@@ -971,8 +962,6 @@ public synchronized void close() throws IOException {
971962
Assert.assertArrayEquals(expecteds, actuals);
972963
}
973964

974-
@SuppressWarnings("deprecation")
975-
// testing deprecated method
976965
@Test public void testToByteArray_String() throws Exception {
977966
try (FileReader fin = new FileReader(m_testFile)) {
978967
// Create our String. Rely on testReaderToString() to make sure this is valid.
@@ -1006,7 +995,6 @@ public synchronized void close() throws IOException {
1006995
assertEquals(FILE_SIZE, actual.length);
1007996
}
1008997

1009-
@SuppressWarnings("deprecation") // deliberately testing deprecated method
1010998
@Test public void testToCharArray_InputStream() throws Exception {
1011999
try (FileInputStream fin = new FileInputStream(m_testFile)) {
10121000
final char[] out = IOUtils.toCharArray(fin);
@@ -1044,10 +1032,8 @@ public synchronized void close() throws IOException {
10441032
* @throws Exception
10451033
* on error
10461034
*/
1047-
@SuppressWarnings("javadoc") // deliberately testing deprecated method
10481035
@Test public void testToInputStream_CharSequence() throws Exception {
10491036
final CharSequence csq = new StringBuilder("Abc123Xyz!");
1050-
@SuppressWarnings("deprecation")
10511037
InputStream inStream = IOUtils.toInputStream(csq); // deliberately testing deprecated method
10521038
byte[] bytes = IOUtils.toByteArray(inStream);
10531039
assertEqualContent(csq.toString().getBytes(), bytes);
@@ -1069,10 +1055,8 @@ public synchronized void close() throws IOException {
10691055
* @throws Exception
10701056
* on error
10711057
*/
1072-
@SuppressWarnings("javadoc") // deliberately testing deprecated method
10731058
@Test public void testToInputStream_String() throws Exception {
10741059
final String str = "Abc123Xyz!";
1075-
@SuppressWarnings("deprecation") // deliberately testing deprecated method
10761060
InputStream inStream = IOUtils.toInputStream(str);
10771061
byte[] bytes = IOUtils.toByteArray(inStream);
10781062
assertEqualContent(str.getBytes(), bytes);
@@ -1084,8 +1068,6 @@ public synchronized void close() throws IOException {
10841068
assertEqualContent(str.getBytes("UTF-8"), bytes);
10851069
}
10861070

1087-
@SuppressWarnings("deprecation")
1088-
// testing deprecated method
10891071
@Test public void testToString_ByteArray() throws Exception {
10901072
try (FileInputStream fin = new FileInputStream(m_testFile)) {
10911073
final byte[] in = IOUtils.toByteArray(fin);
@@ -1095,7 +1077,6 @@ public synchronized void close() throws IOException {
10951077
}
10961078
}
10971079

1098-
@SuppressWarnings("deprecation") // deliberately testing deprecated method
10991080
@Test public void testToString_InputStream() throws Exception {
11001081
try (FileInputStream fin = new FileInputStream(m_testFile)) {
11011082
final String out = IOUtils.toString(fin);
@@ -1113,7 +1094,6 @@ public synchronized void close() throws IOException {
11131094
}
11141095
}
11151096

1116-
@SuppressWarnings("deprecation") // deliberately testing deprecated method
11171097
@Test public void testToString_URI() throws Exception {
11181098
final URI url = m_testFile.toURI();
11191099
final String out = IOUtils.toString(url);
@@ -1136,7 +1116,6 @@ private void testToString_URI(final String encoding) throws Exception {
11361116
testToString_URI(null);
11371117
}
11381118

1139-
@SuppressWarnings("deprecation") // deliberately testing deprecated method
11401119
@Test public void testToString_URL() throws Exception {
11411120
final URL url = m_testFile.toURI().toURL();
11421121
final String out = IOUtils.toString(url);

src/test/java/org/apache/commons/io/comparator/ComparatorAbstractTestCase.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.apache.commons.io.FileUtils;
2525
import org.apache.commons.io.testtools.FileBasedTestCase;
2626
import org.junit.After;
27-
import org.junit.Before;
2827
import org.junit.Test;
2928

3029
import static org.junit.Assert.assertEquals;

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,9 @@ public void close()
185185
@Test(timeout = 5000)
186186
public void testReadBytesEOF() throws IOException {
187187
BoundedReader mr = new BoundedReader( sr, 3 );
188-
BufferedReader br = new BufferedReader( mr );
189-
br.readLine();
190-
br.readLine();
188+
try ( BufferedReader br = new BufferedReader( mr ) ) {
189+
br.readLine();
190+
br.readLine();
191+
}
191192
}
192193
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ public void test() throws Exception {
3838
final byte[] buffer = generateRandomByteStream(i);
3939
final MessageDigest md5Sum = MessageDigest.getInstance("MD5");
4040
final byte[] expect = md5Sum.digest(buffer);
41-
final MessageDigestCalculatingInputStream md5InputStream = new MessageDigestCalculatingInputStream(new ByteArrayInputStream(buffer));
42-
md5InputStream.consume();
43-
final byte[] got = md5InputStream.getMessageDigest().digest();
44-
assertArrayEquals(expect, got);
41+
try (final MessageDigestCalculatingInputStream md5InputStream =
42+
new MessageDigestCalculatingInputStream(new ByteArrayInputStream(buffer))) {
43+
md5InputStream.consume();
44+
final byte[] got = md5InputStream.getMessageDigest().digest();
45+
assertArrayEquals(expect, got);
46+
}
4547
}
4648
}
4749

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ public void testLongFile() throws Exception {
6363

6464
final File file = new File(getTestDirectory(), "testLongFile.txt");
6565
createFile(file, 0);
66-
final Writer writer = new FileWriter(file, true);
67-
for (int i = 0; i < 100000; i++) {
68-
writer.write("LineLineLineLineLineLineLineLineLineLine\n");
66+
try (final Writer writer = new FileWriter(file, true)) {
67+
for (int i = 0; i < 100000; i++) {
68+
writer.write("LineLineLineLineLineLineLineLineLineLine\n");
69+
}
70+
writer.write("SBTOURIST\n");
6971
}
70-
writer.write("SBTOURIST\n");
71-
IOUtils.closeQuietly(writer);
7272

7373
final TestTailerListener listener = new TestTailerListener();
7474
tailer = new Tailer(file, listener, delay, false);

src/test/java/org/apache/commons/io/output/LockableFileWriterTest.java

Lines changed: 15 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.nio.charset.UnsupportedCharsetException;
2424

2525
import org.apache.commons.io.FileUtils;
26-
import org.apache.commons.io.IOUtils;
2726
import org.apache.commons.io.testtools.FileBasedTestCase;
2827
import org.junit.After;
2928
import 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

Comments
 (0)