1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.http.nio.protocol;
29
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33
34 import org.apache.http.HttpResponse;
35 import org.apache.http.entity.StringEntity;
36 import org.apache.http.nio.ContentDecoder;
37 import org.apache.http.nio.IOControl;
38 import org.apache.http.protocol.HttpContext;
39 import org.junit.After;
40 import org.junit.Assert;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.mockito.Mock;
44 import org.mockito.Mockito;
45 import org.mockito.MockitoAnnotations;
46
47 public class TestBasicAsyncResponseConsumer {
48
49 private BasicAsyncResponseConsumer consumer;
50 @Mock private HttpResponse response;
51 @Mock private HttpContext context;
52 @Mock private ContentDecoder decoder;
53 @Mock private IOControl ioControl;
54
55 @Before
56 public void setUp() throws Exception {
57 MockitoAnnotations.initMocks(this);
58
59 consumer = Mockito.spy(new BasicAsyncResponseConsumer());
60 }
61
62 @After
63 public void tearDown() throws Exception {
64 }
65
66 @Test
67 public void testResponseProcessing() throws Exception {
68 when(response.getEntity()).thenReturn(new StringEntity("stuff"));
69
70 consumer.responseReceived(response);
71 consumer.consumeContent(decoder, ioControl);
72 consumer.responseCompleted(context);
73
74 verify(consumer).releaseResources();
75 verify(consumer).buildResult(context);
76 Assert.assertTrue(consumer.isDone());
77 Assert.assertSame(response, consumer.getResult());
78
79 consumer.responseCompleted(context);
80 verify(consumer, times(1)).releaseResources();
81 verify(consumer, times(1)).buildResult(context);
82 }
83
84 @Test
85 public void testResponseProcessingWithException() throws Exception {
86 when(response.getEntity()).thenReturn(new StringEntity("stuff"));
87 final RuntimeException ooopsie = new RuntimeException();
88 when(consumer.buildResult(context)).thenThrow(ooopsie);
89
90 consumer.responseReceived(response);
91 consumer.consumeContent(decoder, ioControl);
92 consumer.responseCompleted(context);
93
94 verify(consumer).releaseResources();
95 Assert.assertTrue(consumer.isDone());
96 Assert.assertSame(ooopsie, consumer.getException());
97 }
98
99 @Test
100 public void testCancel() throws Exception {
101 Assert.assertTrue(consumer.cancel());
102
103 verify(consumer).releaseResources();
104 Assert.assertTrue(consumer.isDone());
105
106 Assert.assertFalse(consumer.cancel());
107 verify(consumer, times(1)).releaseResources();
108 }
109
110 @Test
111 public void testFailed() throws Exception {
112 final RuntimeException ooopsie = new RuntimeException();
113
114 consumer.failed(ooopsie);
115
116 verify(consumer).releaseResources();
117 Assert.assertTrue(consumer.isDone());
118 Assert.assertSame(ooopsie, consumer.getException());
119 }
120
121 @Test
122 public void testFailedAfterDone() throws Exception {
123 final RuntimeException ooopsie = new RuntimeException();
124
125 consumer.cancel();
126 consumer.failed(ooopsie);
127
128 verify(consumer, times(1)).releaseResources();
129 Assert.assertTrue(consumer.isDone());
130 Assert.assertNull(consumer.getException());
131 }
132
133 @Test
134 public void testClose() throws Exception {
135 consumer.close();
136
137 verify(consumer).releaseResources();
138 Assert.assertTrue(consumer.isDone());
139
140 consumer.close();
141
142 verify(consumer, times(1)).releaseResources();
143 }
144
145 }