File leak detector: Close streams in some tests

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1592315 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2014-05-04 08:05:32 +00:00
parent ee0cddaf9a
commit 4112a8441e
3 changed files with 46 additions and 18 deletions

View File

@ -269,8 +269,11 @@ public class MutablePropertySet extends PropertySet
throws IOException, WritingNotSupportedException throws IOException, WritingNotSupportedException
{ {
final ByteArrayOutputStream psStream = new ByteArrayOutputStream(); final ByteArrayOutputStream psStream = new ByteArrayOutputStream();
try {
write(psStream); write(psStream);
} finally {
psStream.close(); psStream.close();
}
final byte[] streamData = psStream.toByteArray(); final byte[] streamData = psStream.toByteArray();
return new ByteArrayInputStream(streamData); return new ByteArrayInputStream(streamData);
} }

View File

@ -189,7 +189,12 @@ public class TestWrite
final POIFSReader r = new POIFSReader(); final POIFSReader r = new POIFSReader();
r.registerListener(new MyPOIFSReaderListener(), r.registerListener(new MyPOIFSReaderListener(),
SummaryInformation.DEFAULT_STREAM_NAME); SummaryInformation.DEFAULT_STREAM_NAME);
r.read(new FileInputStream(filename)); FileInputStream stream = new FileInputStream(filename);
try {
r.read(stream);
} finally {
stream.close();
}
} }
@ -251,7 +256,13 @@ public class TestWrite
}, },
SummaryInformation.DEFAULT_STREAM_NAME); SummaryInformation.DEFAULT_STREAM_NAME);
r.read(new FileInputStream(filename));
InputStream stream = new FileInputStream(filename);
try {
r.read(stream);
} finally {
stream.close();
}
assertNotNull(psa[0]); assertNotNull(psa[0]);
assertTrue(psa[0].isSummaryInformation()); assertTrue(psa[0].isSummaryInformation());
@ -329,7 +340,12 @@ public class TestWrite
} }
}, },
STREAM_NAME); STREAM_NAME);
r.read(new FileInputStream(filename)); FileInputStream stream = new FileInputStream(filename);
try {
r.read(stream);
} finally {
stream.close();
}
assertNotNull(psa[0]); assertNotNull(psa[0]);
Section s = (Section) (psa[0].getSections().get(0)); Section s = (Section) (psa[0].getSections().get(0));
assertEquals(s.getFormatID(), formatID); assertEquals(s.getFormatID(), formatID);
@ -995,14 +1011,13 @@ public class TestWrite
*/ */
@Test @Test
public void dictionaryWithInvalidCodepage() throws IOException, HPSFException public void dictionaryWithInvalidCodepage() throws IOException, HPSFException
{
try
{ {
final File copy = TempFile.createTempFile("Test-HPSF", "ole2"); final File copy = TempFile.createTempFile("Test-HPSF", "ole2");
copy.deleteOnExit(); copy.deleteOnExit();
/* Write: */ /* Write: */
final OutputStream out = new FileOutputStream(copy); final OutputStream out = new FileOutputStream(copy);
final POIFSFileSystem poiFs = new POIFSFileSystem(); final POIFSFileSystem poiFs = new POIFSFileSystem();
final MutablePropertySet ps1 = new MutablePropertySet(); final MutablePropertySet ps1 = new MutablePropertySet();
final MutableSection s = (MutableSection) ps1.getSections().get(0); final MutableSection s = (MutableSection) ps1.getSections().get(0);
@ -1010,6 +1025,9 @@ public class TestWrite
m.put(Long.valueOf(1), "String 1"); m.put(Long.valueOf(1), "String 1");
m.put(Long.valueOf(2), "String 2"); m.put(Long.valueOf(2), "String 2");
m.put(Long.valueOf(3), "String 3"); m.put(Long.valueOf(3), "String 3");
try
{
s.setDictionary(m); s.setDictionary(m);
s.setFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID[0]); s.setFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID[0]);
int codepage = 12345; int codepage = 12345;
@ -1022,6 +1040,7 @@ public class TestWrite
} }
catch (IllegalPropertySetDataException ex) catch (IllegalPropertySetDataException ex)
{ {
out.close();
assertTrue(true); assertTrue(true);
} }
} }

View File

@ -229,7 +229,13 @@ final class Util {
r.registerListener(pfl); r.registerListener(pfl);
/* Read the POI filesystem. */ /* Read the POI filesystem. */
r.read(new FileInputStream(poiFs)); FileInputStream stream = new FileInputStream(poiFs);
try {
r.read(stream);
} finally {
stream.close();
}
POIFile[] result = new POIFile[files.size()]; POIFile[] result = new POIFile[files.size()];
for (int i = 0; i < result.length; i++) for (int i = 0; i < result.length; i++)
result[i] = (POIFile) files.get(i); result[i] = (POIFile) files.get(i);