Merge pull request #14670 from manfred106/BAEL-6894-close-iostream

BAEL-6894: Closing Java IO Streams
This commit is contained in:
davidmartinezbarua 2023-09-05 14:15:27 -03:00 committed by GitHub
commit 41facdad5f
2 changed files with 125 additions and 0 deletions

View File

@ -54,6 +54,13 @@
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>

View File

@ -0,0 +1,118 @@
package com.baeldung.close;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.*;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.AutoCloseInputStream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@RunWith(MockitoJUnitRunner.class)
public class StreamCloseUnitTest {
private static final Logger log = LoggerFactory.getLogger(StreamCloseUnitTest.class);
@Mock
private OutputStream wrappedOutputStream;
@Mock
private InputStream wrappedInputStream;
@Test
public void whenStreamClosedByFinally_thenIOStreamsCloseCalled() throws IOException {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = new BufferedInputStream(wrappedInputStream);
outputStream = new BufferedOutputStream(wrappedOutputStream);
}
finally {
try {
if (inputStream != null)
inputStream.close();
}
catch (IOException ioe1) {
log.error("Cannot close InputStream");
}
try {
if (outputStream != null)
outputStream.close();
}
catch (IOException ioe2) {
log.error("Cannot close OutputStream");
}
}
verify(wrappedInputStream).close();
verify(wrappedOutputStream).close();
}
@Test
public void whenStreamClosedByCloseQuietly_thenIOStreamsCloseCalled() throws IOException {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = new BufferedInputStream(wrappedInputStream);
outputStream = new BufferedOutputStream(wrappedOutputStream);
}
finally {
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
}
verify(wrappedInputStream).close();
verify(wrappedOutputStream).close();
}
@Test
public void whenFinishReadOnAutoCloseInputStream_thenInputStreamsCloseCalled() throws IOException {
// Mimic no more data in the InputStream
when(wrappedInputStream.read(any(byte[].class))).thenReturn(-1);
InputStream inputStream = AutoCloseInputStream.builder().setInputStream(wrappedInputStream).get();
byte[] buffer = new byte[256];
while (inputStream.read(buffer) != -1) {
}
verify(wrappedInputStream).close();
}
@Test
public void whenStreamClosedByWithResources_thenIOStreamsCloseCalled() throws IOException {
try (BufferedInputStream inputStream = new BufferedInputStream(wrappedInputStream);
BufferedOutputStream outputStream = new BufferedOutputStream(wrappedOutputStream)) {
}
verify(wrappedInputStream).close();
verify(wrappedOutputStream).close();
}
@Test
public void whenStreamClosedByWithResourcesJava9_thenIOStreamsCloseCalled() throws IOException {
InputStream inputStream = new BufferedInputStream(wrappedInputStream);
OutputStream outputStream = new BufferedOutputStream(wrappedOutputStream);
try (inputStream; outputStream) {
}
verify(wrappedInputStream).close();
verify(wrappedOutputStream).close();
}
}