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.HttpResponse;
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 TestBasicAsyncResponseConsumer {
48  
49      private BasicAsyncResponseConsumer consumer;
50      @Mock private HttpResponse response;
51      @Mock private HttpContext context;
52      @Mock private ContentDecoder decoder;
53      @Mock private IOControl ioControl;
54  
55      @Before
56      public void setUp() throws Exception {
57          MockitoAnnotations.initMocks(this);
58  
59          consumer = Mockito.spy(new BasicAsyncResponseConsumer());
60      }
61  
62      @After
63      public void tearDown() throws Exception {
64      }
65  
66      @Test
67      public void testResponseProcessing() throws Exception {
68          when(response.getEntity()).thenReturn(new StringEntity("stuff"));
69  
70          consumer.responseReceived(response);
71          consumer.consumeContent(decoder, ioControl);
72          consumer.responseCompleted(context);
73  
74          verify(consumer).releaseResources();
75          verify(consumer).buildResult(context);
76          Assert.assertTrue(consumer.isDone());
77          Assert.assertSame(response, consumer.getResult());
78  
79          consumer.responseCompleted(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(response.getEntity()).thenReturn(new StringEntity("stuff"));
87          final RuntimeException ooopsie = new RuntimeException();
88          when(consumer.buildResult(context)).thenThrow(ooopsie);
89  
90          consumer.responseReceived(response);
91          consumer.consumeContent(decoder, ioControl);
92          consumer.responseCompleted(context);
93  
94          verify(consumer).releaseResources();
95          Assert.assertTrue(consumer.isDone());
96          Assert.assertSame(ooopsie, consumer.getException());
97      }
98  
99      @Test
100     public void testCancel() throws Exception {
101         Assert.assertTrue(consumer.cancel());
102 
103         verify(consumer).releaseResources();
104         Assert.assertTrue(consumer.isDone());
105 
106         Assert.assertFalse(consumer.cancel());
107         verify(consumer, times(1)).releaseResources();
108     }
109 
110     @Test
111     public void testFailed() throws Exception {
112         final RuntimeException ooopsie = new RuntimeException();
113 
114         consumer.failed(ooopsie);
115 
116         verify(consumer).releaseResources();
117         Assert.assertTrue(consumer.isDone());
118         Assert.assertSame(ooopsie, consumer.getException());
119     }
120 
121     @Test
122     public void testFailedAfterDone() throws Exception {
123         final RuntimeException ooopsie = new RuntimeException();
124 
125         consumer.cancel();
126         consumer.failed(ooopsie);
127 
128         verify(consumer, times(1)).releaseResources();
129         Assert.assertTrue(consumer.isDone());
130         Assert.assertNull(consumer.getException());
131     }
132 
133     @Test
134     public void testClose() throws Exception {
135         consumer.close();
136 
137         verify(consumer).releaseResources();
138         Assert.assertTrue(consumer.isDone());
139 
140         consumer.close();
141 
142         verify(consumer, times(1)).releaseResources();
143     }
144 
145 }