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.HttpEntityEnclosingRequest;
35 import org.apache.http.HttpHost;
36 import org.apache.http.HttpRequest;
37 import org.apache.http.nio.ContentEncoder;
38 import org.apache.http.nio.entity.HttpAsyncContentProducer;
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.MockitoAnnotations;
45
46 public class TestBasicAsyncRequestProducer {
47
48 private BasicAsyncRequestProducer producer;
49 private HttpHost target;
50 @Mock private HttpAsyncContentProducer contentProducer;
51 @Mock private HttpEntityEnclosingRequest request;
52 @Mock private ContentEncoder encoder;
53
54 @Before
55 public void setUp() throws Exception {
56 MockitoAnnotations.initMocks(this);
57 target = new HttpHost("localhost");
58 producer = new BasicAsyncRequestProducer(target, request, contentProducer);
59 }
60
61 @After
62 public void tearDown() throws Exception {
63 }
64
65 @Test(expected=IllegalArgumentException.class)
66 public void testNullTarget3ArgConstructor() throws Exception {
67 producer = new BasicAsyncRequestProducer(null, request, contentProducer);
68 }
69
70 @Test(expected=IllegalArgumentException.class)
71 public void testNullRequest3ArgConstructor() throws Exception {
72 producer = new BasicAsyncRequestProducer(target, null, contentProducer);
73 }
74
75 @Test(expected=IllegalArgumentException.class)
76 public void testNullTarget2ArgConstructor() throws Exception {
77 producer = new BasicAsyncRequestProducer(null, request);
78 }
79
80 @Test(expected=IllegalArgumentException.class)
81 public void testNullRequest2ArgConstructor() throws Exception {
82 producer = new BasicAsyncRequestProducer(target, null);
83 }
84
85 @Test
86 public void testGenerateRequest() {
87 final HttpRequest res = producer.generateRequest();
88
89 Assert.assertSame(request, res);
90 }
91
92 @Test
93 public void testGetTarget() {
94 final HttpHost res = producer.getTarget();
95
96 Assert.assertSame(target, res);
97 }
98
99 @SuppressWarnings("boxing")
100 @Test
101 public void testProduceContentEncoderCompleted() throws Exception {
102 when(encoder.isCompleted()).thenReturn(Boolean.TRUE);
103
104 producer.produceContent(encoder, null);
105
106 verify(contentProducer, times(1)).close();
107 }
108
109 @SuppressWarnings("boxing")
110 @Test
111 public void testProduceContentEncoderNotCompleted() throws Exception {
112 when(encoder.isCompleted()).thenReturn(Boolean.FALSE);
113
114 producer.produceContent(encoder, null);
115
116 verify(contentProducer, times(0)).close();
117 }
118
119 @Test
120 public void testResetRequest() throws Exception {
121 producer.resetRequest();
122 verify(contentProducer, times(1)).close();
123 }
124
125 @Test
126 public void testClose() throws Exception {
127 producer.close();
128 verify(contentProducer, times(1)).close();
129 }
130
131 @Test
132 public void testToString() {
133 Assert.assertEquals(target + " " + request + " " + contentProducer, producer.toString());
134 }
135
136 }