1+ package com .baeldung .close ;
2+
3+ import static org .mockito .ArgumentMatchers .any ;
4+ import static org .mockito .Mockito .verify ;
5+ import static org .mockito .Mockito .when ;
6+
7+ import java .io .*;
8+
9+ import org .apache .commons .io .IOUtils ;
10+ import org .apache .commons .io .input .AutoCloseInputStream ;
11+ import org .junit .Test ;
12+ import org .junit .runner .RunWith ;
13+ import org .mockito .Mock ;
14+ import org .mockito .junit .MockitoJUnitRunner ;
15+ import org .slf4j .Logger ;
16+ import org .slf4j .LoggerFactory ;
17+
18+ @ RunWith (MockitoJUnitRunner .class )
19+ public class StreamCloseUnitTest {
20+
21+ private static final Logger log = LoggerFactory .getLogger (StreamCloseUnitTest .class );
22+
23+ @ Mock
24+ private OutputStream wrappedOutputStream ;
25+
26+ @ Mock
27+ private InputStream wrappedInputStream ;
28+
29+ @ Test
30+ public void whenStreamClosedByFinally_thenIOStreamsCloseCalled () throws IOException {
31+
32+ InputStream inputStream = null ;
33+ OutputStream outputStream = null ;
34+
35+ try {
36+ inputStream = new BufferedInputStream (wrappedInputStream );
37+ outputStream = new BufferedOutputStream (wrappedOutputStream );
38+ }
39+ finally {
40+ try {
41+ if (inputStream != null )
42+ inputStream .close ();
43+ }
44+ catch (IOException ioe1 ) {
45+ log .error ("Cannot close InputStream" );
46+ }
47+ try {
48+ if (outputStream != null )
49+ outputStream .close ();
50+ }
51+ catch (IOException ioe2 ) {
52+ log .error ("Cannot close OutputStream" );
53+ }
54+ }
55+
56+ verify (wrappedInputStream ).close ();
57+ verify (wrappedOutputStream ).close ();
58+ }
59+
60+ @ Test
61+ public void whenStreamClosedByCloseQuietly_thenIOStreamsCloseCalled () throws IOException {
62+
63+ InputStream inputStream = null ;
64+ OutputStream outputStream = null ;
65+
66+ try {
67+ inputStream = new BufferedInputStream (wrappedInputStream );
68+ outputStream = new BufferedOutputStream (wrappedOutputStream );
69+ }
70+ finally {
71+ IOUtils .closeQuietly (inputStream );
72+ IOUtils .closeQuietly (outputStream );
73+ }
74+
75+ verify (wrappedInputStream ).close ();
76+ verify (wrappedOutputStream ).close ();
77+ }
78+
79+ @ Test
80+ public void whenFinishReadOnAutoCloseInputStream_thenInputStreamsCloseCalled () throws IOException {
81+
82+ // Mimic no more data in the InputStream
83+ when (wrappedInputStream .read (any (byte [].class ))).thenReturn (-1 );
84+
85+ InputStream inputStream = AutoCloseInputStream .builder ().setInputStream (wrappedInputStream ).get ();
86+
87+ byte [] buffer = new byte [256 ];
88+ while (inputStream .read (buffer ) != -1 ) {
89+ }
90+
91+ verify (wrappedInputStream ).close ();
92+ }
93+
94+ @ Test
95+ public void whenStreamClosedByWithResources_thenIOStreamsCloseCalled () throws IOException {
96+
97+ try (BufferedInputStream inputStream = new BufferedInputStream (wrappedInputStream );
98+ BufferedOutputStream outputStream = new BufferedOutputStream (wrappedOutputStream )) {
99+ }
100+
101+ verify (wrappedInputStream ).close ();
102+ verify (wrappedOutputStream ).close ();
103+ }
104+
105+ @ Test
106+ public void whenStreamClosedByWithResourcesJava9_thenIOStreamsCloseCalled () throws IOException {
107+
108+ InputStream inputStream = new BufferedInputStream (wrappedInputStream );
109+ OutputStream outputStream = new BufferedOutputStream (wrappedOutputStream );
110+
111+ try (inputStream ; outputStream ) {
112+ }
113+
114+ verify (wrappedInputStream ).close ();
115+ verify (wrappedOutputStream ).close ();
116+ }
117+
118+ }
0 commit comments