Skip to content

Commit

Permalink
Issue checkstyle#3962: Extracted method to close stream on finally
Browse files Browse the repository at this point in the history
  • Loading branch information
gaganis committed Mar 20, 2017
1 parent 28e6ae8 commit 49e4c01
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
Expand Up @@ -30,12 +30,10 @@

import javax.xml.parsers.ParserConfigurationException;

import com.sun.tools.javac.comp.Check;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.google.common.io.Closeables;
import com.puppycrawl.tools.checkstyle.api.AbstractLoader;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;

Expand Down Expand Up @@ -177,13 +175,7 @@ public static ImportControl load(final URI uri) throws CheckstyleException {
throw new CheckstyleException("unable to find " + uri, ex);
}
finally {
if( inputStream != null ) {
try {
inputStream.close();
} catch (IOException e) {
throw new CheckstyleException("unable to close input stream", e);
}
}
closeStream(inputStream);
}
}

Expand All @@ -210,6 +202,22 @@ private static ImportControl load(final InputSource source,
}
}

/**
* This method exists only due to bug in cobertura library
* https://github.com/cobertura/cobertura/issues/170
* @param inputStream the InputStream to close
* @throws CheckstyleException if an error occurs.
*/
private static void closeStream(InputStream inputStream) throws CheckstyleException {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ex) {
throw new CheckstyleException("unable to close input stream", ex);
}
}
}

/**
* @return the root {@link ImportControl} object loaded.
*/
Expand Down
Expand Up @@ -128,7 +128,7 @@ public void testLoadThrowsException() throws Exception {
}

@Test
public void testAutoCloseable() throws Exception {
public void testInputStreamThatFailsOnClose() throws Exception {
final InputStream inputStream = PowerMockito.mock(InputStream.class);
Mockito.doThrow(IOException.class).when(inputStream).close();
Mockito.doThrow(IOException.class).when(inputStream).available();
Expand Down Expand Up @@ -162,7 +162,7 @@ public void testInputStreamNull() throws Exception {
}

@Test
public void testAutoCloseable2() throws Exception {
public void testInputStreamFailsOnRead() throws Exception {
final InputStream inputStream = PowerMockito.mock(InputStream.class);
Mockito.doThrow(IOException.class).when(inputStream).available();

Expand Down

0 comments on commit 49e4c01

Please sign in to comment.