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.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 TestBasicAsyncRequestConsumer {
48  
49      private BasicAsyncRequestConsumer consumer;
50      @Mock private HttpEntityEnclosingRequest request;
51      @Mock private HttpContext context;
52      @Mock private ContentDecoder decoder;
53      @Mock private IOControl ioctrl;
54  
55      @Before
56      public void setUp() throws Exception {
57          MockitoAnnotations.initMocks(this);
58  
59          consumer = Mockito.spy(new BasicAsyncRequestConsumer());
60      }
61  
62      @After
63      public void tearDown() throws Exception {
64      }
65  
66      @Test
67      public void testRequestProcessing() throws Exception {
68          when(request.getEntity()).thenReturn(new StringEntity("stuff"));
69  
70          consumer.requestReceived(request);
71          consumer.consumeContent(decoder, ioctrl);
72          consumer.requestCompleted(context);
73  
74          verify(consumer).releaseResources();
75          verify(consumer).buildResult(context);
76          Assert.assertTrue(consumer.isDone());
77          Assert.assertSame(request, consumer.getResult());
78  
79          consumer.requestCompleted(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(request.getEntity()).thenReturn(new StringEntity("stuff"));
87          final RuntimeException ooopsie = new RuntimeException();
88          when(consumer.buildResult(context)).thenThrow(ooopsie);
89  
90          consumer.requestReceived(request);
91          consumer.consumeContent(decoder, ioctrl);
92          consumer.requestCompleted(context);
93  
94          verify(consumer).releaseResources();
95          Assert.assertTrue(consumer.isDone());
96          Assert.assertSame(ooopsie, consumer.getException());
97      }
98  
99      @Test
100     public void testClose() throws Exception {
101         consumer.close();
102 
103         verify(consumer).releaseResources();
104         Assert.assertTrue(consumer.isDone());
105 
106         consumer.close();
107 
108         verify(consumer, times(1)).releaseResources();
109     }
110 
111 }