1
+ /*
2
+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License").
5
+ * You may not use this file except in compliance with the License.
6
+ * A copy of the License is located at
7
+ *
8
+ * http://aws.amazon.com/apache2.0
9
+ *
10
+ * or in the "license" file accompanying this file. This file is distributed
11
+ * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12
+ * express or implied. See the License for the specific language governing
13
+ * permissions and limitations under the License.
14
+ */
15
+
16
+ package software .amazon .awssdk .core ;
17
+
18
+ import static org .assertj .core .api .Assertions .assertThatCode ;
19
+ import static org .mockito .Mockito .never ;
20
+
21
+ import java .io .IOException ;
22
+ import java .io .InputStream ;
23
+ import org .junit .jupiter .api .Test ;
24
+ import org .mockito .Mockito ;
25
+ import software .amazon .awssdk .http .Abortable ;
26
+ import software .amazon .awssdk .http .AbortableInputStream ;
27
+
28
+ class ResponseInputStreamTest {
29
+ @ Test
30
+ void abort_withAbortable_closesUnderlyingStream () throws IOException {
31
+ InputStream stream = Mockito .mock (InputStream .class );
32
+ Abortable abortable = Mockito .mock (Abortable .class );
33
+ AbortableInputStream abortableInputStream = AbortableInputStream .create (stream , abortable );
34
+ ResponseInputStream <Object > responseInputStream = new ResponseInputStream <>(new Object (), abortableInputStream );
35
+
36
+ responseInputStream .abort ();
37
+
38
+ Mockito .verify (abortable ).abort ();
39
+ Mockito .verify (stream ).close ();
40
+ }
41
+
42
+ @ Test
43
+ void failedClose_withinAbort_isIgnored () throws IOException {
44
+ InputStream stream = Mockito .mock (InputStream .class );
45
+ Abortable abortable = Mockito .mock (Abortable .class );
46
+ AbortableInputStream abortableInputStream = AbortableInputStream .create (stream , abortable );
47
+ ResponseInputStream <Object > responseInputStream = new ResponseInputStream <>(new Object (), abortableInputStream );
48
+
49
+ Mockito .doThrow (new IOException ()).when (stream ).close ();
50
+ assertThatCode (responseInputStream ::abort ).doesNotThrowAnyException ();
51
+
52
+ Mockito .verify (abortable ).abort ();
53
+ Mockito .verify (stream ).close ();
54
+ }
55
+
56
+ @ Test
57
+ void abort_withoutAbortable_closesUnderlyingStream () throws IOException {
58
+ InputStream stream = Mockito .mock (InputStream .class );
59
+ ResponseInputStream <Object > responseInputStream = new ResponseInputStream <>(new Object (), stream );
60
+
61
+ responseInputStream .abort ();
62
+
63
+ Mockito .verify (stream ).close ();
64
+ }
65
+
66
+ @ Test
67
+ void close_withAbortable_closesUnderlyingStream () throws IOException {
68
+ InputStream stream = Mockito .mock (InputStream .class );
69
+ Abortable abortable = Mockito .mock (Abortable .class );
70
+ AbortableInputStream abortableInputStream = AbortableInputStream .create (stream , abortable );
71
+ ResponseInputStream <Object > responseInputStream = new ResponseInputStream <>(new Object (), abortableInputStream );
72
+
73
+ responseInputStream .close ();
74
+
75
+ Mockito .verify (abortable , never ()).abort ();
76
+ Mockito .verify (stream ).close ();
77
+ }
78
+ }
0 commit comments