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  package org.apache.http.impl.execchain;
28  
29  import java.io.IOException;
30  import java.util.concurrent.TimeUnit;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.http.HttpClientConnection;
34  import org.apache.http.conn.HttpClientConnectionManager;
35  import org.junit.Assert;
36  import org.junit.Before;
37  import org.junit.Test;
38  import org.mockito.Mockito;
39  
40  @SuppressWarnings({"static-access"}) // test code
41  public class TestConnectionHolder {
42  
43      private Log log;
44      private HttpClientConnectionManager mgr;
45      private HttpClientConnection conn;
46      private ConnectionHolder connHolder;
47  
48      @Before
49      public void setup() {
50          log = Mockito.mock(Log.class);
51          mgr = Mockito.mock(HttpClientConnectionManager.class);
52          conn = Mockito.mock(HttpClientConnection.class);
53          connHolder = new ConnectionHolder(log, mgr, conn);
54      }
55  
56      @Test
57      public void testAbortConnection() throws Exception {
58          connHolder.abortConnection();
59  
60          Assert.assertTrue(connHolder.isReleased());
61  
62          Mockito.verify(conn).shutdown();
63          Mockito.verify(mgr).releaseConnection(conn, null, 0, TimeUnit.MILLISECONDS);
64  
65          connHolder.abortConnection();
66  
67          Mockito.verify(conn, Mockito.times(1)).shutdown();
68          Mockito.verify(mgr, Mockito.times(1)).releaseConnection(
69                  Mockito.<HttpClientConnection>any(),
70                  Mockito.anyObject(),
71                  Mockito.anyLong(),
72                  Mockito.<TimeUnit>any());
73      }
74  
75      @Test
76      public void testAbortConnectionIOError() throws Exception {
77          Mockito.doThrow(new IOException()).when(conn).shutdown();
78  
79          connHolder.abortConnection();
80  
81          Assert.assertTrue(connHolder.isReleased());
82  
83          Mockito.verify(conn).shutdown();
84          Mockito.verify(mgr).releaseConnection(conn, null, 0, TimeUnit.MILLISECONDS);
85      }
86  
87      @Test
88      public void testCancell() throws Exception {
89          Assert.assertTrue(connHolder.cancel());
90  
91          Assert.assertTrue(connHolder.isReleased());
92  
93          Mockito.verify(conn).shutdown();
94          Mockito.verify(mgr).releaseConnection(conn, null, 0, TimeUnit.MILLISECONDS);
95  
96          Assert.assertFalse(connHolder.cancel());
97  
98          Mockito.verify(conn, Mockito.times(1)).shutdown();
99          Mockito.verify(mgr, Mockito.times(1)).releaseConnection(
100                 Mockito.<HttpClientConnection>any(),
101                 Mockito.anyObject(),
102                 Mockito.anyLong(),
103                 Mockito.<TimeUnit>any());
104     }
105 
106     @Test
107     public void testReleaseConnectionReusable() throws Exception {
108         connHolder.setState("some state");
109         connHolder.setValidFor(100, TimeUnit.SECONDS);
110         connHolder.markReusable();
111 
112         connHolder.releaseConnection();
113 
114         Assert.assertTrue(connHolder.isReleased());
115 
116         Mockito.verify(conn, Mockito.never()).close();
117         Mockito.verify(mgr).releaseConnection(conn, "some state", 100, TimeUnit.SECONDS);
118 
119         connHolder.releaseConnection();
120 
121         Mockito.verify(mgr, Mockito.times(1)).releaseConnection(
122                 Mockito.<HttpClientConnection>any(),
123                 Mockito.anyObject(),
124                 Mockito.anyLong(),
125                 Mockito.<TimeUnit>any());
126     }
127 
128     @Test
129     public void testReleaseConnectionNonReusable() throws Exception {
130         connHolder.setState("some state");
131         connHolder.setValidFor(100, TimeUnit.SECONDS);
132         connHolder.markNonReusable();
133 
134         connHolder.releaseConnection();
135 
136         Assert.assertTrue(connHolder.isReleased());
137 
138         Mockito.verify(conn, Mockito.times(1)).close();
139         Mockito.verify(mgr).releaseConnection(conn, null, 0, TimeUnit.MILLISECONDS);
140 
141         connHolder.releaseConnection();
142 
143         Mockito.verify(mgr, Mockito.times(1)).releaseConnection(
144                 Mockito.<HttpClientConnection>any(),
145                 Mockito.anyObject(),
146                 Mockito.anyLong(),
147                 Mockito.<TimeUnit>any());
148     }
149 
150 }