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.never;
31 import static org.mockito.Mockito.times;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
34
35 import org.apache.http.HttpResponse;
36 import org.apache.http.nio.ContentEncoder;
37 import org.apache.http.nio.entity.HttpAsyncContentProducer;
38 import org.junit.After;
39 import org.junit.Assert;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44
45 public class TestBasicAsyncResponseProducer {
46
47 private BasicAsyncResponseProducer producer;
48 @Mock private HttpAsyncContentProducer contentProducer;
49 @Mock private HttpResponse response;
50 @Mock private ContentEncoder encoder;
51
52 @Before
53 public void setUp() throws Exception {
54 MockitoAnnotations.initMocks(this);
55 producer = new BasicAsyncResponseProducer(response, contentProducer);
56 }
57
58 @After
59 public void tearDown() throws Exception {
60 }
61
62 @Test(expected=IllegalArgumentException.class)
63 public void testNullTargetArgConstructor() throws Exception {
64 producer = new BasicAsyncResponseProducer(null);
65 }
66
67 @Test
68 public void testGenerateRequest() {
69 final HttpResponse res = producer.generateResponse();
70
71 Assert.assertSame(response, res);
72 }
73
74 @SuppressWarnings("boxing")
75 @Test
76 public void testProduceContentEncoderCompleted() throws Exception {
77 when(encoder.isCompleted()).thenReturn(Boolean.TRUE);
78
79 producer.produceContent(encoder, null);
80
81 verify(contentProducer, times(1)).close();
82 }
83
84 @SuppressWarnings("boxing")
85 @Test
86 public void testProduceContentEncoderNotCompleted() throws Exception {
87 when(encoder.isCompleted()).thenReturn(Boolean.FALSE);
88
89 producer.produceContent(encoder, null);
90
91 verify(contentProducer, never()).close();
92 }
93
94 @Test
95 public void testClose() throws Exception {
96 producer.close();
97 verify(contentProducer, times(1)).close();
98 }
99
100 }