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.pool;
28  
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertTrue;
31  
32  import java.net.ServerSocket;
33  
34  import javax.net.ssl.SSLServerSocket;
35  import javax.net.ssl.SSLServerSocketFactory;
36  import javax.net.ssl.SSLSocketFactory;
37  
38  import org.apache.http.HttpClientConnection;
39  import org.apache.http.HttpHost;
40  import org.apache.http.config.ConnectionConfig;
41  import org.apache.http.config.SocketConfig;
42  import org.junit.After;
43  import org.junit.Before;
44  import org.junit.Test;
45  
46  public class TestBasicConnPool {
47  
48      private BasicConnFactory connFactory;
49      private BasicConnPool pool;
50      private HttpHost host;
51      private HttpClientConnection conn;
52  
53      private ServerSocket server;
54      private int serverPort;
55  
56      private int sslServerPort;
57  
58      @Before
59      public void setUp() throws Exception {
60          // setup an "http" server
61          server = new ServerSocket(0);
62          serverPort = server.getLocalPort();
63  
64          // setup an "https" server
65          final SSLServerSocket sslServer = (SSLServerSocket) SSLServerSocketFactory.getDefault().createServerSocket(0);
66          sslServerPort = sslServer.getLocalPort();
67  
68          final SocketConfig sconfig = SocketConfig.custom().setSoTimeout(100).build();
69          connFactory = new BasicConnFactory(sconfig, ConnectionConfig.DEFAULT);
70          pool = new BasicConnPool(connFactory);
71      }
72  
73      @After
74      public void tearDown() throws Exception {
75          server.close();
76          if(conn != null) {
77              conn.close();
78          }
79      }
80  
81      @Test(expected=IllegalArgumentException.class)
82      public void testNullConstructor2() throws Exception {
83          new BasicConnPool((BasicConnFactory) null);
84      }
85  
86      @Test
87      public void testHttpCreateConnection() throws Exception {
88          host = new HttpHost("localhost", serverPort, "http");
89          conn = connFactory.create(host);
90  
91          assertTrue(conn.isOpen());
92          assertEquals(100, conn.getSocketTimeout());
93      }
94  
95      @Test
96      public void testHttpsCreateConnection() throws Exception {
97          final SocketConfig sconfig = SocketConfig.custom().setSoTimeout(100).build();
98          connFactory = new BasicConnFactory(
99                  null,
100                 (SSLSocketFactory)SSLSocketFactory.getDefault(),
101                 0, sconfig, ConnectionConfig.DEFAULT);
102         host = new HttpHost("localhost", sslServerPort, "https");
103         conn = connFactory.create(host);
104 
105         assertTrue(conn.isOpen());
106         assertEquals(100, conn.getSocketTimeout());
107     }
108 
109     @Test
110     public void testHttpCreateEntry() throws Exception {
111         host = new HttpHost("localhost", serverPort, "http");
112         conn = connFactory.create(host);
113 
114         final BasicPoolEntry entry = pool.createEntry(host, conn);
115 
116         assertEquals(conn, entry.getConnection());
117         assertEquals("localhost", entry.getRoute().getHostName());
118 
119         entry.close();
120     }
121 
122 }