View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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 }