Skip to content

Commit 085226e

Browse files
author
Gary Gregory
committed
Use try-with-resources in tests and more.
- Don't nest if you do not have to. - Document empty blocks. - Only run FileFilterTestCase#testCanExecute() on Windows to fix Jenkins build.
1 parent a14aa92 commit 085226e

17 files changed

Lines changed: 369 additions & 333 deletions

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,12 @@ file comparators, endian transformation classes, and much more.
232232
<version>4.12</version>
233233
<scope>test</scope>
234234
</dependency>
235+
<dependency>
236+
<groupId>org.apache.commons</groupId>
237+
<artifactId>commons-lang3</artifactId>
238+
<version>3.8.1</version>
239+
<scope>test</scope>
240+
</dependency>
235241
</dependencies>
236242

237243
<properties>

src/test/java/org/apache/commons/io/filefilter/FileFilterTestCase.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
import org.apache.commons.io.IOCase;
4444
import org.apache.commons.io.IOUtils;
4545
import org.apache.commons.io.testtools.TestUtils;
46+
import org.apache.commons.lang3.SystemUtils;
47+
import org.junit.Assert;
48+
import org.junit.Assume;
4649
import org.junit.Rule;
4750
import org.junit.Test;
4851
import org.junit.rules.TemporaryFolder;
@@ -941,12 +944,13 @@ public void testHidden() throws Exception {
941944

942945
@Test
943946
public void testCanExecute() throws Exception {
947+
Assume.assumeTrue(SystemUtils.IS_OS_WINDOWS);
944948
final File executableFile = File.createTempFile(getClass().getSimpleName(), ".temp");
945949
try {
946950
try (final BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(executableFile))) {
947951
TestUtils.generateTestData(output, 32);
948952
}
949-
executableFile.setExecutable(true);
953+
Assert.assertTrue(executableFile.setExecutable(true));
950954
assertFiltering(CanExecuteFileFilter.CAN_EXECUTE, executableFile, true);
951955
executableFile.setExecutable(false);
952956
assertFiltering(CanExecuteFileFilter.CANNOT_EXECUTE, executableFile, false);
@@ -966,7 +970,7 @@ public void testCanRead() throws Exception {
966970
new BufferedOutputStream(new FileOutputStream(readOnlyFile))){
967971
TestUtils.generateTestData(output, 32);
968972
}
969-
readOnlyFile.setReadOnly();
973+
Assert.assertTrue(readOnlyFile.setReadOnly());
970974
assertFiltering(CanReadFileFilter.CAN_READ, readOnlyFile, true);
971975
assertFiltering(CanReadFileFilter.CANNOT_READ, readOnlyFile, false);
972976
assertFiltering(CanReadFileFilter.READ_ONLY, readOnlyFile, true);
@@ -984,7 +988,7 @@ public void testCanWrite() throws Exception {
984988
new BufferedOutputStream(new FileOutputStream(readOnlyFile))){
985989
TestUtils.generateTestData(output, 32);
986990
}
987-
readOnlyFile.setReadOnly();
991+
Assert.assertTrue(readOnlyFile.setReadOnly());
988992
assertFiltering(CanWriteFileFilter.CAN_WRITE, getTestDirectory(), true);
989993
assertFiltering(CanWriteFileFilter.CANNOT_WRITE, getTestDirectory(), false);
990994
assertFiltering(CanWriteFileFilter.CAN_WRITE, readOnlyFile, false);

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

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,9 @@ public void testReadXmlWithBOMUcs2() throws Exception {
609609
// UCS-2 is BE.
610610
Assume.assumeTrue(Charset.isSupported("ISO-10646-UCS-2"));
611611
final byte[] data = "<?xml version=\"1.0\" encoding=\"ISO-10646-UCS-2\"?><X/>".getBytes("ISO-10646-UCS-2");
612-
parseXml(new BOMInputStream(createUtf16BeDataStream(data, true), ByteOrderMark.UTF_16BE));
612+
try (BOMInputStream in = new BOMInputStream(createUtf16BeDataStream(data, true), ByteOrderMark.UTF_16BE)) {
613+
parseXml(in);
614+
}
613615
parseXml(createUtf16BeDataStream(data, true));
614616
}
615617

@@ -620,64 +622,84 @@ public void testReadXmlWithBOMUcs4() throws Exception {
620622
Assume.assumeTrue(Charset.isSupported("ISO-10646-UCS-4"));
621623
final byte[] data = "<?xml version=\"1.0\" encoding=\"ISO-10646-UCS-4\"?><X/>".getBytes("ISO-10646-UCS-4");
622624
// XML parser does not know what to do with UTF-32
623-
parseXml(new BOMInputStream(createUtf32BeDataStream(data, true), ByteOrderMark.UTF_32BE));
624-
// XML parser does not know what to do with UTF-32
625-
Assume.assumeTrue("JVM and SAX need to support UTF_32LE for this", jvmAndSaxBothSupportCharset("UTF_32LE"));
625+
try (BOMInputStream in = new BOMInputStream(createUtf32BeDataStream(data, true), ByteOrderMark.UTF_32BE)) {
626+
parseXml(in);
627+
// XML parser does not know what to do with UTF-32
628+
Assume.assumeTrue("JVM and SAX need to support UTF_32LE for this", jvmAndSaxBothSupportCharset("UTF_32LE"));
629+
}
626630
parseXml(createUtf32BeDataStream(data, true));
627631
}
628632

629633
@Test
630634
public void testReadXmlWithBOMUtf16Be() throws Exception {
631635
final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF-16BE\"?><X/>".getBytes(StandardCharsets.UTF_16BE);
632-
parseXml(new BOMInputStream(createUtf16BeDataStream(data, true), ByteOrderMark.UTF_16BE));
636+
try (BOMInputStream in = new BOMInputStream(createUtf16BeDataStream(data, true), ByteOrderMark.UTF_16BE)) {
637+
parseXml(in);
638+
}
633639
parseXml(createUtf16BeDataStream(data, true));
634640
}
635641

636642
@Test
637643
public void testReadXmlWithBOMUtf16Le() throws Exception {
638644
final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF-16LE\"?><X/>".getBytes(StandardCharsets.UTF_16LE);
639-
parseXml(new BOMInputStream(createUtf16LeDataStream(data, true), ByteOrderMark.UTF_16LE));
645+
try (BOMInputStream in = new BOMInputStream(createUtf16LeDataStream(data, true), ByteOrderMark.UTF_16LE)) {
646+
parseXml(in);
647+
}
640648
parseXml(createUtf16LeDataStream(data, true));
641649
}
642650

643651
@Test
644652
public void testReadXmlWithBOMUtf32Be() throws Exception {
645653
Assume.assumeTrue("JVM and SAX need to support UTF_32BE for this", jvmAndSaxBothSupportCharset("UTF_32BE"));
646654
final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF-32BE\"?><X/>".getBytes("UTF_32BE");
647-
parseXml(new BOMInputStream(createUtf32BeDataStream(data, true), ByteOrderMark.UTF_32BE));
655+
try (BOMInputStream in = new BOMInputStream(createUtf32BeDataStream(data, true), ByteOrderMark.UTF_32BE)) {
656+
parseXml(in);
657+
}
648658
// XML parser does not know what to do with UTF-32, so we warp the input stream with a XmlStreamReader
649-
parseXml(new XmlStreamReader(createUtf32BeDataStream(data, true)));
659+
try (XmlStreamReader in = new XmlStreamReader(createUtf32BeDataStream(data, true))) {
660+
parseXml(in);
661+
}
650662
}
651663

652664
@Test
653665
public void testReadXmlWithBOMUtf32Le() throws Exception {
654666
Assume.assumeTrue("JVM and SAX need to support UTF_32LE for this", jvmAndSaxBothSupportCharset("UTF_32LE"));
655667
final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF-32LE\"?><X/>".getBytes("UTF_32LE");
656-
parseXml(new BOMInputStream(createUtf32LeDataStream(data, true), ByteOrderMark.UTF_32LE));
668+
try (BOMInputStream in = new BOMInputStream(createUtf32LeDataStream(data, true), ByteOrderMark.UTF_32LE)) {
669+
parseXml(in);
670+
}
657671
// XML parser does not know what to do with UTF-32, so we warp the input stream with a XmlStreamReader
658-
parseXml(new XmlStreamReader(createUtf32LeDataStream(data, true)));
672+
try (XmlStreamReader in = new XmlStreamReader(createUtf32LeDataStream(data, true))) {
673+
parseXml(in);
674+
}
659675
}
660676

661677
@Test
662678
public void testReadXmlWithBOMUtf8() throws Exception {
663679
final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><X/>".getBytes(StandardCharsets.UTF_8);
664-
parseXml(new BOMInputStream(createUtf8DataStream(data, true)));
680+
try (BOMInputStream in = new BOMInputStream(createUtf8DataStream(data, true))) {
681+
parseXml(in);
682+
}
665683
parseXml(createUtf8DataStream(data, true));
666684
}
667685

668686
@Test
669687
public void testReadXmlWithoutBOMUtf32Be() throws Exception {
670688
Assume.assumeTrue("JVM and SAX need to support UTF_32BE for this", jvmAndSaxBothSupportCharset("UTF_32BE"));
671689
final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF_32BE\"?><X/>".getBytes("UTF_32BE");
672-
parseXml(new BOMInputStream(createUtf32BeDataStream(data, false)));
690+
try (BOMInputStream in = new BOMInputStream(createUtf32BeDataStream(data, false))) {
691+
parseXml(in);
692+
}
673693
parseXml(createUtf32BeDataStream(data, false));
674694
}
675695

676696
@Test
677697
public void testReadXmlWithoutBOMUtf32Le() throws Exception {
678698
Assume.assumeTrue("JVM and SAX need to support UTF_32LE for this", jvmAndSaxBothSupportCharset("UTF_32LE"));
679699
final byte[] data = "<?xml version=\"1.0\" encoding=\"UTF-32LE\"?><X/>".getBytes("UTF_32LE");
680-
parseXml(new BOMInputStream(createUtf32LeDataStream(data, false)));
700+
try (BOMInputStream in = new BOMInputStream(createUtf32LeDataStream(data, false))) {
701+
parseXml(in);
702+
}
681703
parseXml(createUtf32BeDataStream(data, false));
682704
}
683705

src/test/java/org/apache/commons/io/input/compatibility/XmlStreamReader.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,8 @@ public XmlStreamReader(final InputStream is, final boolean lenient) throws IOExc
182182
} catch (final XmlStreamReaderException ex) {
183183
if (!lenient) {
184184
throw ex;
185-
} else {
186-
doLenientDetection(null, ex);
187185
}
186+
doLenientDetection(null, ex);
188187
}
189188
}
190189

@@ -319,9 +318,8 @@ public XmlStreamReader(final InputStream is, final String httpContentType,
319318
} catch (final XmlStreamReaderException ex) {
320319
if (!lenient) {
321320
throw ex;
322-
} else {
323-
doLenientDetection(httpContentType, ex);
324321
}
322+
doLenientDetection(httpContentType, ex);
325323
}
326324
}
327325

@@ -681,11 +679,10 @@ private static String getXmlProlog(final BufferedInputStream is, final String gu
681679
if (firstGT == -1) {
682680
if (c == -1) {
683681
throw new IOException("Unexpected end of XML stream");
684-
} else {
685-
throw new IOException(
686-
"XML prolog or ROOT element not found on first "
687-
+ offset + " bytes");
688682
}
683+
throw new IOException(
684+
"XML prolog or ROOT element not found on first "
685+
+ offset + " bytes");
689686
}
690687
final int bytesRead = offset;
691688
if (bytesRead > 0) {

src/test/java/org/apache/commons/io/input/compatibility/XmlStreamReaderUtilitiesCompatibilityTest.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,19 @@ public class XmlStreamReaderUtilitiesCompatibilityTest extends XmlStreamReaderUt
2929
@Override
3030
protected String calculateRawEncoding(final String bomEnc, final String xmlGuessEnc, final String xmlEnc,
3131
final String defaultEncoding) throws IOException {
32-
final MockXmlStreamReader mock = new MockXmlStreamReader(defaultEncoding);
33-
final String enc = mock.calculateRawEncoding(bomEnc, xmlGuessEnc, xmlEnc, null);
34-
mock.close();
35-
return enc;
32+
try (final MockXmlStreamReader mock = new MockXmlStreamReader(defaultEncoding)) {
33+
return mock.calculateRawEncoding(bomEnc, xmlGuessEnc, xmlEnc, null);
34+
}
3635
}
3736
@Override
3837
protected String calculateHttpEncoding(final String httpContentType, final String bomEnc, final String xmlGuessEnc,
3938
final String xmlEnc, final boolean lenient, final String defaultEncoding) throws IOException {
40-
final MockXmlStreamReader mock = new MockXmlStreamReader(defaultEncoding);
41-
final String enc = mock.calculateHttpEncoding(
42-
XmlStreamReader.getContentTypeMime(httpContentType),
43-
XmlStreamReader.getContentTypeEncoding(httpContentType),
44-
bomEnc, xmlGuessEnc, xmlEnc, null, lenient);
45-
mock.close();
46-
return enc;
39+
try (final MockXmlStreamReader mock = new MockXmlStreamReader(defaultEncoding)) {
40+
return mock.calculateHttpEncoding(
41+
XmlStreamReader.getContentTypeMime(httpContentType),
42+
XmlStreamReader.getContentTypeEncoding(httpContentType),
43+
bomEnc, xmlGuessEnc, xmlEnc, null, lenient);
44+
}
4745
}
4846

4947
/** Mock {@link XmlStreamReader} implementation */

src/test/java/org/apache/commons/io/monitor/CollectionFileListener.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ public void onFileDelete(final File file) {
190190
*/
191191
@Override
192192
public void onStop(final FileAlterationObserver observer) {
193+
// noop
193194
}
194195

195196
}

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,25 @@ public class ChunkedWriterTest {
2828
@Test
2929
public void write_four_chunks() throws Exception {
3030
final AtomicInteger numWrites = new AtomicInteger();
31-
final OutputStreamWriter osw = getOutputStreamWriter(numWrites);
32-
33-
final ChunkedWriter chunked = new ChunkedWriter(osw, 10);
34-
chunked.write("0123456789012345678901234567891".toCharArray());
35-
chunked.flush();
36-
assertEquals(4, numWrites.get());
37-
chunked.close();
31+
try (final OutputStreamWriter osw = getOutputStreamWriter(numWrites)) {
32+
try (final ChunkedWriter chunked = new ChunkedWriter(osw, 10)) {
33+
chunked.write("0123456789012345678901234567891".toCharArray());
34+
chunked.flush();
35+
assertEquals(4, numWrites.get());
36+
}
37+
}
3838
}
3939

4040
@Test
4141
public void write_two_chunks_default_constructor() throws Exception {
4242
final AtomicInteger numWrites = new AtomicInteger();
43-
final OutputStreamWriter osw = getOutputStreamWriter(numWrites);
44-
45-
final ChunkedWriter chunked = new ChunkedWriter(osw);
46-
chunked.write(new char[1024 * 4 + 1]);
47-
chunked.flush();
48-
assertEquals(2, numWrites.get());
49-
chunked.close();
43+
try (final OutputStreamWriter osw = getOutputStreamWriter(numWrites)) {
44+
try (final ChunkedWriter chunked = new ChunkedWriter(osw)) {
45+
chunked.write(new char[1024 * 4 + 1]);
46+
chunked.flush();
47+
assertEquals(2, numWrites.get());
48+
}
49+
}
5050
}
5151

5252
private OutputStreamWriter getOutputStreamWriter(final AtomicInteger numWrites) {
@@ -62,6 +62,6 @@ public void write(final char[] cbuf, final int off, final int len) throws IOExce
6262

6363
@Test(expected = IllegalArgumentException.class)
6464
public void negative_chunksize_not_permitted() throws Exception {
65-
(new ChunkedWriter(new OutputStreamWriter(new ByteArrayOutputStream()), 0)).close();;
65+
(new ChunkedWriter(new OutputStreamWriter(new ByteArrayOutputStream()), 0)).close();
6666
}
6767
}

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

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,39 +39,38 @@ public class CountingOutputStreamTest {
3939
@Test
4040
public void testCounting() throws IOException {
4141
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
42-
final CountingOutputStream cos = new CountingOutputStream(baos);
43-
44-
for(int i = 0; i < 20; i++) {
45-
cos.write(i);
46-
}
47-
assertByteArrayEquals("CountingOutputStream.write(int)", baos.toByteArray(), 0, 20);
48-
assertEquals("CountingOutputStream.getCount()", cos.getCount(), 20);
49-
50-
final byte[] array = new byte[10];
51-
for(int i = 20; i < 30; i++) {
52-
array[i-20] = (byte)i;
53-
}
54-
cos.write(array);
55-
assertByteArrayEquals("CountingOutputStream.write(byte[])", baos.toByteArray(), 0, 30);
56-
assertEquals("CountingOutputStream.getCount()", cos.getCount(), 30);
57-
58-
for(int i = 25; i < 35; i++) {
59-
array[i-25] = (byte)i;
42+
try (final CountingOutputStream cos = new CountingOutputStream(baos)) {
43+
44+
for (int i = 0; i < 20; i++) {
45+
cos.write(i);
46+
}
47+
assertByteArrayEquals("CountingOutputStream.write(int)", baos.toByteArray(), 0, 20);
48+
assertEquals("CountingOutputStream.getCount()", cos.getCount(), 20);
49+
50+
final byte[] array = new byte[10];
51+
for (int i = 20; i < 30; i++) {
52+
array[i - 20] = (byte) i;
53+
}
54+
cos.write(array);
55+
assertByteArrayEquals("CountingOutputStream.write(byte[])", baos.toByteArray(), 0, 30);
56+
assertEquals("CountingOutputStream.getCount()", cos.getCount(), 30);
57+
58+
for (int i = 25; i < 35; i++) {
59+
array[i - 25] = (byte) i;
60+
}
61+
cos.write(array, 5, 5);
62+
assertByteArrayEquals("CountingOutputStream.write(byte[], int, int)", baos.toByteArray(), 0, 35);
63+
assertEquals("CountingOutputStream.getCount()", cos.getCount(), 35);
64+
65+
final int count = cos.resetCount();
66+
assertEquals("CountingOutputStream.resetCount()", count, 35);
67+
68+
for (int i = 0; i < 10; i++) {
69+
cos.write(i);
70+
}
71+
assertByteArrayEquals("CountingOutputStream.write(int)", baos.toByteArray(), 35, 45);
72+
assertEquals("CountingOutputStream.getCount()", cos.getCount(), 10);
6073
}
61-
cos.write(array, 5, 5);
62-
assertByteArrayEquals("CountingOutputStream.write(byte[], int, int)", baos.toByteArray(), 0, 35);
63-
assertEquals("CountingOutputStream.getCount()", cos.getCount(), 35);
64-
65-
final int count = cos.resetCount();
66-
assertEquals("CountingOutputStream.resetCount()", count, 35);
67-
68-
for(int i = 0; i < 10; i++) {
69-
cos.write(i);
70-
}
71-
assertByteArrayEquals("CountingOutputStream.write(int)", baos.toByteArray(), 35, 45);
72-
assertEquals("CountingOutputStream.getCount()", cos.getCount(), 10);
73-
74-
cos.close();
7574
}
7675

7776
/*

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,16 @@ public class NullOutputStreamTest {
3232

3333
@Test
3434
public void testNull() throws IOException {
35-
final NullOutputStream nos = new NullOutputStream();
36-
nos.write("string".getBytes());
37-
nos.write("some string".getBytes(), 3, 5);
38-
nos.write(1);
39-
nos.write(0x0f);
40-
nos.flush();
41-
nos.close();
42-
nos.write("allowed".getBytes());
43-
nos.write(255);
35+
try (final NullOutputStream nos = new NullOutputStream()) {
36+
nos.write("string".getBytes());
37+
nos.write("some string".getBytes(), 3, 5);
38+
nos.write(1);
39+
nos.write(0x0f);
40+
nos.flush();
41+
nos.close();
42+
nos.write("allowed".getBytes());
43+
nos.write(255);
44+
}
4445
}
4546

4647
}

0 commit comments

Comments
 (0)