Skip to content

Commit 6a3aad2

Browse files
committed
Ensure streams are closed
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1695021 13f79535-47bb-0310-9956-ffa450edef68
1 parent 03e7adf commit 6a3aad2

6 files changed

Lines changed: 23 additions & 12 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,13 @@ public void testMarkResetBeforeReadWithoutBOM() throws Exception {
367367
public void testNoBoms() throws Exception {
368368
final byte[] data = new byte[] { 'A', 'B', 'C' };
369369
try {
370-
new BOMInputStream(createUtf8DataStream(data, true), false, (ByteOrderMark[])null);
370+
(new BOMInputStream(createUtf8DataStream(data, true), false, (ByteOrderMark[])null)).close();;
371371
fail("Null BOMs, expected IllegalArgumentException");
372372
} catch (final IllegalArgumentException e) {
373373
// expected
374374
}
375375
try {
376-
new BOMInputStream(createUtf8DataStream(data, true), false, new ByteOrderMark[0]);
376+
(new BOMInputStream(createUtf8DataStream(data, true), false, new ByteOrderMark[0])).close();;
377377
fail("Null BOMs, expected IllegalArgumentException");
378378
} catch (final IllegalArgumentException e) {
379379
// expected

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected void _testRawNoBomValid(final String encoding) throws Exception {
6868
protected void _testRawNoBomInvalid(final String encoding) throws Exception {
6969
final InputStream is = getXmlStream("no-bom", XML3, encoding, encoding);
7070
try {
71-
new XmlStreamReader(is, false);
71+
(new XmlStreamReader(is, false)).close();;
7272
fail("It should have failed");
7373
} catch (final IOException ex) {
7474
assertTrue(ex.getMessage().contains("Invalid encoding,"));
@@ -337,7 +337,7 @@ protected void _testHttpInvalid(final String cT, final String bomEnc, final Stri
337337
final InputStream is = getXmlStream(bomEnc,
338338
prologEnc == null ? XML2 : XML3, streamEnc, prologEnc);
339339
try {
340-
new XmlStreamReader(is, cT, false);
340+
(new XmlStreamReader(is, cT, false)).close();;
341341
fail("It should have failed for HTTP Content-type " + cT + ", BOM "
342342
+ bomEnc + ", streamEnc " + streamEnc + " and prologEnc "
343343
+ prologEnc);

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ public void write_four_chunks() throws Exception {
3535
ChunkedOutputStream chunked = new ChunkedOutputStream(baos, 10);
3636
chunked.write("0123456789012345678901234567891".getBytes());
3737
assertEquals(4, numWrites.get());
38+
chunked.close();
3839
}
3940

4041
@Test(expected = IllegalArgumentException.class)
41-
public void negative_chunksize_not_permitted() {
42-
new ChunkedOutputStream(new ByteArrayOutputStream(), 0);
42+
public void negative_chunksize_not_permitted() throws Exception{
43+
ChunkedOutputStream chunked = new ChunkedOutputStream(new ByteArrayOutputStream(), 0);
44+
chunked.close();
4345
}
4446

4547
@Test
@@ -49,6 +51,7 @@ public void defaultConstructor() throws IOException {
4951
ChunkedOutputStream chunked = new ChunkedOutputStream(baos);
5052
chunked.write(new byte[1024 * 4 + 1]);
5153
assertEquals(2, numWrites.get());
54+
chunked.close();
5255
}
5356

5457
private ByteArrayOutputStream getByteArrayOutputStream(final AtomicInteger numWrites) {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public void write_four_chunks() throws Exception {
3434
chunked.write("0123456789012345678901234567891".toCharArray());
3535
chunked.flush();
3636
assertEquals(4, numWrites.get());
37+
chunked.close();
3738
}
3839

3940
@Test
@@ -45,6 +46,7 @@ public void write_two_chunks_default_constructor() throws Exception {
4546
chunked.write(new char[1024 * 4 + 1]);
4647
chunked.flush();
4748
assertEquals(2, numWrites.get());
49+
chunked.close();
4850
}
4951

5052
private OutputStreamWriter getOutputStreamWriter(final AtomicInteger numWrites) {
@@ -59,7 +61,7 @@ public void write(char[] cbuf, int off, int len) throws IOException {
5961
}
6062

6163
@Test(expected = IllegalArgumentException.class)
62-
public void negative_chunksize_not_permitted() {
63-
new ChunkedWriter(new OutputStreamWriter(new ByteArrayOutputStream()), 0);
64+
public void negative_chunksize_not_permitted() throws Exception {
65+
(new ChunkedWriter(new OutputStreamWriter(new ByteArrayOutputStream()), 0)).close();;
6466
}
6567
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,18 @@ public class ClosedOutputStreamTest extends TestCase {
2727

2828
/**
2929
* Test the <code>write(b)</code> method.
30+
* @throws Exception
3031
*/
31-
public void testRead() {
32+
public void testRead() throws Exception {
33+
ClosedOutputStream cos = null;
3234
try {
33-
new ClosedOutputStream().write('x');
35+
cos = new ClosedOutputStream();
36+
cos.write('x');
3437
fail("write(b)");
3538
} catch (final IOException e) {
3639
// expected
40+
} finally {
41+
cos.close();
3742
}
3843
}
3944

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,15 +324,16 @@ public void testTempFileAboveThresholdPrefixOnly() {
324324

325325
/**
326326
* Test specifying a temporary file and the threshold is reached.
327+
* @throws Exception
327328
*/
328-
public void testTempFileError() {
329+
public void testTempFileError() throws Exception {
329330

330331
final String prefix = null;
331332
final String suffix = ".out";
332333
final File tempDir = new File(".");
333334
try
334335
{
335-
new DeferredFileOutputStream(testBytes.length - 5, prefix, suffix, tempDir);
336+
(new DeferredFileOutputStream(testBytes.length - 5, prefix, suffix, tempDir)).close();
336337
fail("Expected IllegalArgumentException ");
337338
}
338339
catch (final IllegalArgumentException e) {

0 commit comments

Comments
 (0)