1
1
package dev .jbang ;
2
2
3
+ import static com .github .tomakehurst .wiremock .client .WireMock .recordSpec ;
4
+ import static com .github .tomakehurst .wiremock .core .WireMockConfiguration .options ;
5
+
6
+ import java .io .ByteArrayOutputStream ;
3
7
import java .io .File ;
4
8
import java .io .IOException ;
5
9
import java .io .PrintStream ;
10
14
import java .nio .file .Files ;
11
15
import java .nio .file .Path ;
12
16
import java .nio .file .Paths ;
17
+ import java .security .KeyManagementException ;
18
+ import java .security .NoSuchAlgorithmException ;
13
19
import java .util .concurrent .Callable ;
14
20
import java .util .function .Function ;
15
21
16
- import org .apache .commons .io .output .ByteArrayOutputStream ;
22
+ import javax .net .ssl .HttpsURLConnection ;
23
+ import javax .net .ssl .SSLContext ;
24
+ import javax .net .ssl .TrustManager ;
25
+ import javax .net .ssl .X509TrustManager ;
26
+
17
27
import org .junit .Rule ;
18
28
import org .junit .contrib .java .lang .system .EnvironmentVariables ;
19
- import org .junit .jupiter .api .AfterAll ;
20
- import org .junit .jupiter .api .BeforeAll ;
29
+ import org .junit .jupiter .api .AfterEach ;
21
30
import org .junit .jupiter .api .BeforeEach ;
22
31
import org .junit .jupiter .api .io .TempDir ;
23
32
33
+ import com .github .tomakehurst .wiremock .WireMockServer ;
34
+ import com .github .tomakehurst .wiremock .http .JvmProxyConfigurer ;
35
+
24
36
import dev .jbang .cli .BaseCommand ;
25
37
import dev .jbang .cli .JBang ;
26
38
import dev .jbang .dependencies .DependencyCache ;
39
+ import dev .jbang .net .JdkManager ;
27
40
import dev .jbang .util .Util ;
28
41
29
42
import picocli .CommandLine ;
30
43
31
44
public abstract class BaseTest {
45
+ public Path jbangTempDir ;
46
+ public Path cwdDir ;
47
+ public WireMockServer globalwms ;
48
+
49
+ public static Path mavenTempDir ;
50
+ public static Path jdksTempDir ;
51
+ public static Path examplesTestFolder ;
52
+
53
+ @ Rule
54
+ public final EnvironmentVariables environmentVariables = new EnvironmentVariables ();
32
55
33
56
@ BeforeEach
34
57
void initEnv (@ TempDir Path tempPath ) throws IOException {
@@ -54,15 +77,40 @@ void initEnv(@TempDir Path tempPath) throws IOException {
54
77
}
55
78
Configuration .instance (null );
56
79
DependencyCache .clear ();
80
+ JdkManager .resetProviders ();
81
+
82
+ // Start a WireMock server to capture and replay any remote
83
+ // requests JBang makes (any new code that results in additional
84
+ // requests will result in new recordings being added to the
85
+ // `src/test/resources/mappings` folder which can then be added
86
+ // to the git repository. Future requests will then be replayed
87
+ // from the recordings instead of hitting the real server.)
88
+ globalwms = new WireMockServer (options ()
89
+ .enableBrowserProxying (true )
90
+ .dynamicPort ());
91
+ globalwms .start ();
92
+ JvmProxyConfigurer .configureFor (globalwms );
93
+ disableSSL ();
94
+
95
+ // This forces MIMA to use the WireMock server as a proxy
96
+ // System.setProperty("aether.connector.http.useSystemProperties", "true");
97
+ // System.setProperty("aether.connector.https.securityMode", "insecure");
98
+ }
99
+
100
+ @ AfterEach
101
+ public void cleanupEnv () {
102
+ globalwms .stop ();
103
+ globalwms .snapshotRecord (recordSpec ().ignoreRepeatRequests ());
104
+ JvmProxyConfigurer .restorePrevious ();
57
105
}
58
106
59
107
public static final String EXAMPLES_FOLDER = "itests" ;
60
- public static Path examplesTestFolder ;
61
108
62
- @ BeforeAll
63
- static void init () throws URISyntaxException , IOException {
109
+ // @BeforeAll
110
+ public static void initBeforeAll () throws URISyntaxException , IOException {
64
111
mavenTempDir = Files .createTempDirectory ("jbang_tests_maven" );
65
112
jdksTempDir = Files .createTempDirectory ("jbang_tests_jdks" );
113
+ System .err .println ("## INIT BEFORE ALL TESTS " + mavenTempDir );
66
114
URL examplesUrl = BaseTest .class .getClassLoader ().getResource (EXAMPLES_FOLDER );
67
115
if (examplesUrl == null ) {
68
116
examplesTestFolder = Paths .get (EXAMPLES_FOLDER ).toAbsolutePath ();
@@ -71,20 +119,12 @@ static void init() throws URISyntaxException, IOException {
71
119
}
72
120
}
73
121
74
- @ AfterAll
75
- static void cleanup () {
122
+ // @AfterAll
123
+ public static void cleanupAfterAll () {
76
124
Util .deletePath (mavenTempDir , true );
77
125
Util .deletePath (jdksTempDir , true );
78
126
}
79
127
80
- @ Rule
81
- public final EnvironmentVariables environmentVariables = new EnvironmentVariables ();
82
-
83
- public static Path mavenTempDir ;
84
- public static Path jdksTempDir ;
85
- public Path jbangTempDir ;
86
- public Path cwdDir ;
87
-
88
128
protected <T > CaptureResult checkedRun (Function <T , Integer > commandRunner , String ... args ) throws Exception {
89
129
CommandLine .ParseResult pr = JBang .getCommandLine ().parseArgs (args );
90
130
while (pr .subcommand () != null ) {
@@ -156,4 +196,34 @@ public String normalizedErr() {
156
196
}
157
197
}
158
198
199
+ static void disableSSL () {
200
+ TrustManager [] trustAllCerts = new TrustManager [] { new X509TrustManager () {
201
+ public java .security .cert .X509Certificate [] getAcceptedIssuers () {
202
+ return new java .security .cert .X509Certificate [] {};
203
+ }
204
+
205
+ public void checkClientTrusted (java .security .cert .X509Certificate [] certs , String authType ) {
206
+ }
207
+
208
+ public void checkServerTrusted (java .security .cert .X509Certificate [] certs , String authType ) {
209
+ }
210
+ } };
211
+
212
+ try {
213
+ SSLContext sc = SSLContext .getInstance ("SSL" );
214
+ sc .init (null , trustAllCerts , new java .security .SecureRandom ());
215
+ HttpsURLConnection .setDefaultSSLSocketFactory (sc .getSocketFactory ());
216
+ } catch (KeyManagementException | NoSuchAlgorithmException e ) {
217
+ throw new RuntimeException (e );
218
+ }
219
+ }
220
+
221
+ protected static void wiremockRequestPrinter (com .github .tomakehurst .wiremock .http .Request inRequest ,
222
+ com .github .tomakehurst .wiremock .http .Response inResponse ) {
223
+ System .err .printf ("WireMock request at URL: %s%n" , inRequest .getAbsoluteUrl ());
224
+ System .err .printf ("WireMock request headers: %s%n" , inRequest .getHeaders ());
225
+ System .err .printf ("WireMock response status: %d%n" , inResponse .getStatus ());
226
+ System .err .printf ("WireMock response body: %s%n" , inResponse .getBodyAsString ());
227
+ System .err .printf ("WireMock response headers: %s%n" , inResponse .getHeaders ());
228
+ }
159
229
}
0 commit comments