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.nio.extension;
29  
30  import org.apache.hc.core5.http.URIScheme;
31  import org.apache.hc.core5.reactor.IOReactorConfig;
32  import org.apache.hc.core5.testing.SSLTestContexts;
33  import org.apache.hc.core5.testing.nio.H2TestClient;
34  import org.apache.hc.core5.testing.nio.H2TestServer;
35  import org.apache.hc.core5.util.TimeValue;
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 H2TestResources implements BeforeEachCallback, AfterEachCallback {
45  
46      private static final Logger LOG = LoggerFactory.getLogger(H2TestResources.class);
47  
48      private final URIScheme scheme;
49      private final Timeout socketTimeout;
50  
51      private H2TestServer server;
52      private H2TestClient client;
53  
54      public H2TestResources(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 H2TestServer(
63                  IOReactorConfig.custom()
64                          .setSoTimeout(socketTimeout)
65                          .build(),
66                  scheme == URIScheme.HTTPS ? SSLTestContexts.createServerSSLContext() : null, null, null);
67  
68          LOG.debug("Starting up test client");
69          client = new H2TestClient(
70                  IOReactorConfig.custom()
71                          .setSoTimeout(socketTimeout)
72                          .build(),
73                  scheme == URIScheme.HTTPS ? SSLTestContexts.createClientSSLContext() : null, null, null);
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(TimeValue.ofSeconds(5));
81          }
82  
83          LOG.debug("Shutting down test server");
84          if (server != null) {
85              server.shutdown(TimeValue.ofSeconds(5));
86          }
87      }
88  
89      public H2TestClient client() {
90          Assertions.assertNotNull(client);
91          return client;
92      }
93  
94      public H2TestServer server() {
95          Assertions.assertNotNull(server);
96          return server;
97      }
98  
99  }