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.client5.testing.extension.sync;
29  
30  import java.util.function.Consumer;
31  
32  import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
33  import org.apache.hc.core5.http.URIScheme;
34  import org.apache.hc.core5.io.CloseMode;
35  import org.apache.hc.core5.util.Asserts;
36  import org.apache.hc.core5.util.Timeout;
37  import org.junit.jupiter.api.extension.AfterEachCallback;
38  import org.junit.jupiter.api.extension.ExtensionContext;
39  import org.slf4j.Logger;
40  import org.slf4j.LoggerFactory;
41  
42  public class TestClientResources implements AfterEachCallback {
43  
44      private static final Logger LOG = LoggerFactory.getLogger(TestClientResources.class);
45  
46      private final URIScheme scheme;
47      private final Timeout timeout;
48      private final ClientProtocolLevel clientProtocolLevel;
49      private final TestServerBootstrap serverBootstrap;
50      private final TestClientBuilder clientBuilder;
51  
52      private TestServer server;
53      private PoolingHttpClientConnectionManager connManager;
54      private TestClient client;
55  
56      public TestClientResources(final URIScheme scheme, final ClientProtocolLevel clientProtocolLevel, final Timeout timeout) {
57          this.scheme = scheme != null ? scheme : URIScheme.HTTP;
58          this.timeout = timeout;
59          this.clientProtocolLevel = clientProtocolLevel != null ? clientProtocolLevel : ClientProtocolLevel.STANDARD;
60          this.serverBootstrap = new TestServerBootstrap(this.scheme)
61                  .setTimeout(this.timeout);
62          switch (this.clientProtocolLevel) {
63              case MINIMAL:
64                  this.clientBuilder = new MinimalTestClientBuilder();
65                  break;
66              default:
67                  this.clientBuilder = new StandardTestClientBuilder();
68          }
69          this.clientBuilder.setTimeout(timeout);
70      }
71  
72      @Override
73      public void afterEach(final ExtensionContext extensionContext) throws Exception {
74          LOG.debug("Shutting down test server");
75  
76          if (client != null) {
77              client.close(CloseMode.GRACEFUL);
78          }
79  
80          if (connManager != null) {
81              connManager.close(CloseMode.IMMEDIATE);
82          }
83  
84          if (server != null) {
85              server.shutdown(CloseMode.IMMEDIATE);
86          }
87      }
88  
89      public URIScheme scheme() {
90          return this.scheme;
91      }
92  
93      public ClientProtocolLevel getClientProtocolLevel() {
94          return clientProtocolLevel;
95      }
96  
97      public void configureServer(final Consumer<TestServerBootstrap> serverCustomizer) {
98          Asserts.check(server == null, "Server is already running and cannot be changed");
99          serverCustomizer.accept(serverBootstrap);
100     }
101 
102     public TestServer server() throws Exception {
103         if (server == null) {
104             server = serverBootstrap.build();
105         }
106         return server;
107     }
108 
109     public void configureClient(final Consumer<TestClientBuilder> clientCustomizer) {
110         Asserts.check(client == null, "Client is already running and cannot be changed");
111         clientCustomizer.accept(clientBuilder);
112     }
113 
114     public TestClient client() throws Exception {
115         if (client == null) {
116             client = clientBuilder.build();
117         }
118         return client;
119     }
120 
121 }