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.testserver;
29  
30  import java.net.URL;
31  
32  import javax.net.ssl.SSLContext;
33  
34  import org.apache.http.impl.nio.pool.BasicNIOConnFactory;
35  import org.apache.http.ssl.SSLContextBuilder;
36  import org.junit.After;
37  
38  /**
39   * Base class for all HttpCore NIO integration tests
40   *
41   */
42  public abstract class HttpCoreNIOTestBase {
43  
44      public enum ProtocolScheme { http, https }
45  
46      private final ProtocolScheme scheme;
47  
48      protected HttpServerNio server;
49      protected HttpClientNio client;
50  
51      public HttpCoreNIOTestBase(final ProtocolScheme scheme) {
52          this.scheme = scheme;
53      }
54  
55      public HttpCoreNIOTestBase() {
56          this(ProtocolScheme.http);
57      }
58  
59      public ProtocolScheme getScheme() {
60          return this.scheme;
61      }
62  
63      protected SSLContext createServerSSLContext() throws Exception {
64          final URL keyStoreURL = getClass().getResource("/test.keystore");
65          final String storePassword = "nopassword";
66          return SSLContextBuilder.create()
67                  .loadTrustMaterial(keyStoreURL, storePassword.toCharArray())
68                  .loadKeyMaterial(keyStoreURL, storePassword.toCharArray(), storePassword.toCharArray())
69                  .build();
70      }
71  
72      protected SSLContext createClientSSLContext() throws Exception {
73          final URL keyStoreURL = getClass().getResource("/test.keystore");
74          final String storePassword = "nopassword";
75          return SSLContextBuilder.create()
76                  .loadTrustMaterial(keyStoreURL, storePassword.toCharArray())
77                  .build();
78      }
79  
80      protected ServerConnectionFactory createServerConnectionFactory() throws Exception {
81          return new ServerConnectionFactory(
82                  this.scheme.equals(ProtocolScheme.https) ? createServerSSLContext() : null);
83      }
84  
85      protected BasicNIOConnFactory createClientConnectionFactory() throws Exception {
86          return new BasicNIOConnFactory(
87                  new ClientConnectionFactory(),
88                  this.scheme.equals(ProtocolScheme.https) ? new ClientConnectionFactory(createClientSSLContext()) : null);
89  
90      }
91  
92      public void initServer() throws Exception {
93          this.server = new HttpServerNio();
94          this.server.setConnectionFactory(createServerConnectionFactory());
95          this.server.setTimeout(5000);
96      }
97  
98      public void initClient() throws Exception {
99          this.client = new HttpClientNio(createClientConnectionFactory());
100         this.client.setTimeout(5000);
101     }
102 
103     @After
104     public void shutDownClient() throws Exception {
105         if (this.client != null) {
106             this.client.shutdown();
107             this.client = null;
108         }
109     }
110 
111     @After
112     public void shutDownServer() throws Exception {
113         if (this.server != null) {
114             this.server.shutdown();
115             this.server = null;
116         }
117     }
118 
119 }