HADOOP-15114. Add closeStreams(...) to IOUtils (addendum).

Contributed by Ajay Kumar.
This commit is contained in:
Steve Loughran 2018-01-19 14:54:13 +00:00
parent e5a1ad6e24
commit d689b2d99c

View File

@ -299,13 +299,14 @@ public void testListDirectory() throws IOException {
} }
@Test @Test
public void testCloseStreams() { public void testCloseStreams() throws IOException {
File tmpFile = new File("deleteMe.txt"); File tmpFile = null;
FileOutputStream fos = null; FileOutputStream fos;
BufferedOutputStream bos = null; BufferedOutputStream bos;
FileOutputStream nullStream = null; FileOutputStream nullStream = null;
try { try {
tmpFile = new File(GenericTestUtils.getTestDir(), "testCloseStreams.txt");
fos = new FileOutputStream(tmpFile) { fos = new FileOutputStream(tmpFile) {
@Override @Override
public void close() throws IOException { public void close() throws IOException {
@ -315,19 +316,15 @@ public void close() throws IOException {
bos = new BufferedOutputStream( bos = new BufferedOutputStream(
new FileOutputStream(tmpFile)) { new FileOutputStream(tmpFile)) {
@Override @Override
public void close() throws IOException { public void close() {
throw new NullPointerException(); throw new NullPointerException();
} }
}; };
} catch (IOException ioe) {
LOG.warn("Exception in TestIOUtils.testCloseStreams: ", ioe);
}
try {
IOUtils.closeStreams(fos, bos, nullStream); IOUtils.closeStreams(fos, bos, nullStream);
IOUtils.closeStreams(); IOUtils.closeStreams();
} catch (Exception ex) { } finally {
LOG.error("Expect IOUtils.closeStreams to close streams quietly.", ex); FileUtils.deleteQuietly(tmpFile);
throw ex;
} }
} }