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.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 }