1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 }