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.hc.core5.testing.classic.extension;
29  
30  import org.apache.hc.core5.http.URIScheme;
31  import org.apache.hc.core5.http.io.SocketConfig;
32  import org.apache.hc.core5.io.CloseMode;
33  import org.apache.hc.core5.testing.SSLTestContexts;
34  import org.apache.hc.core5.testing.classic.ClassicTestClient;
35  import org.apache.hc.core5.testing.classic.ClassicTestServer;
36  import org.apache.hc.core5.util.Timeout;
37  import org.junit.jupiter.api.Assertions;
38  import org.junit.jupiter.api.extension.AfterEachCallback;
39  import org.junit.jupiter.api.extension.BeforeEachCallback;
40  import org.junit.jupiter.api.extension.ExtensionContext;
41  import org.slf4j.Logger;
42  import org.slf4j.LoggerFactory;
43  
44  public class ClassicTestResources implements BeforeEachCallback, AfterEachCallback {
45  
46      private static final Logger LOG = LoggerFactory.getLogger(ClassicTestResources.class);
47  
48      private final URIScheme scheme;
49      private final Timeout socketTimeout;
50  
51      private ClassicTestServer server;
52      private ClassicTestClient client;
53  
54      public ClassicTestResources(final URIScheme scheme, final Timeout socketTimeout) {
55          this.scheme = scheme;
56          this.socketTimeout = socketTimeout;
57      }
58  
59      @Override
60      public void beforeEach(final ExtensionContext extensionContext) throws Exception {
61          LOG.debug("Starting up test server");
62          server = new ClassicTestServer(
63                  scheme == URIScheme.HTTPS ? SSLTestContexts.createServerSSLContext() : null,
64                  SocketConfig.custom()
65                          .setSoTimeout(socketTimeout)
66                          .build());
67  
68          LOG.debug("Starting up test client");
69          client = new ClassicTestClient(
70                  scheme == URIScheme.HTTPS  ? SSLTestContexts.createClientSSLContext() : null,
71                  SocketConfig.custom()
72                          .setSoTimeout(socketTimeout)
73                          .build());
74      }
75  
76      @Override
77      public void afterEach(final ExtensionContext extensionContext) throws Exception {
78          LOG.debug("Shutting down test client");
79          if (client != null) {
80              client.shutdown(CloseMode.IMMEDIATE);
81          }
82  
83          LOG.debug("Shutting down test server");
84          if (server != null) {
85              server.shutdown(CloseMode.IMMEDIATE);
86          }
87      }
88  
89      public ClassicTestClient client() {
90          Assertions.assertNotNull(client);
91          return client;
92      }
93  
94      public ClassicTestServer server() {
95          Assertions.assertNotNull(server);
96          return server;
97      }
98  
99  }