Skip to content

Commit f46363f

Browse files
committed
test: remote requests are now replayed
Instead of hitting remote servers and possibly getting failed tests becauase servers are down or becuase wer're getting rate limited, we now record the responses and replay them. PS: This is not done for Maven artifacts/MIMA.
1 parent 332a94e commit f46363f

File tree

40 files changed

+1409
-4
lines changed

40 files changed

+1409
-4
lines changed

build.gradle

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ dependencies {
114114
testRuntimeOnly "org.junit.platform:junit-platform-launcher"
115115
testImplementation "com.github.stefanbirkner:system-rules:1.17.2"
116116
testImplementation "org.hamcrest:hamcrest-library:2.2"
117-
testImplementation "com.github.tomakehurst:wiremock-jre8:2.32.0"
117+
testImplementation "org.wiremock:wiremock:3.12.1"
118118

119119
//testImplementation 'com.github.maxandersen.karate:karate-junit:19e06766'
120120
//testImplementation 'com.github.maxandersen.karate:karate-apache:19e06766'
@@ -222,18 +222,21 @@ jar {
222222
compileJava {
223223
options.encoding = 'UTF-8'
224224
options.compilerArgs << "-Xlint:unchecked"
225+
options.compilerArgs.addAll(['--release', '8'])
225226
}
226227

227228
compileTestJava {
228229
options.encoding = 'UTF-8'
229230
options.compilerArgs << "-Xlint:unchecked"
231+
options.compilerArgs.addAll(['--release', '11'])
230232
}
231233

232234
compileJava9Java {
233235
sourceCompatibility = 9
234236
targetCompatibility = 9
235237
options.encoding = 'UTF-8'
236238
options.compilerArgs << "-Xlint:unchecked"
239+
options.compilerArgs.addAll(['--release', '9'])
237240
}
238241

239242
shadowJar {
@@ -437,5 +440,3 @@ tasks.named("spotlessXml").configure { dependsOn("chocolatey") }
437440
tasks.named("spotlessXml").configure { dependsOn("copyITests") }
438441

439442
group = "dev.jbang"
440-
sourceCompatibility = '8'
441-
targetCompatibility = '8'

src/main/java/dev/jbang/net/JdkManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ private static List<JdkProvider> providers() {
102102
return providers;
103103
}
104104

105+
public static void resetProviders() {
106+
providers = null;
107+
}
108+
105109
@Nonnull
106110
private static List<JdkProvider> updatableProviders() {
107111
return providers().stream().filter(JdkProvider::canUpdate).collect(Collectors.toList());

src/test/java/dev/jbang/BaseTest.java

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package dev.jbang;
22

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;
37
import java.io.File;
48
import java.io.IOException;
59
import java.io.PrintStream;
@@ -10,25 +14,37 @@
1014
import java.nio.file.Files;
1115
import java.nio.file.Path;
1216
import java.nio.file.Paths;
17+
import java.security.KeyManagementException;
18+
import java.security.NoSuchAlgorithmException;
1319
import java.util.concurrent.Callable;
1420
import java.util.function.Function;
1521

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+
1727
import org.junit.Rule;
1828
import org.junit.contrib.java.lang.system.EnvironmentVariables;
1929
import org.junit.jupiter.api.AfterAll;
30+
import org.junit.jupiter.api.AfterEach;
2031
import org.junit.jupiter.api.BeforeAll;
2132
import org.junit.jupiter.api.BeforeEach;
2233
import org.junit.jupiter.api.io.TempDir;
2334

35+
import com.github.tomakehurst.wiremock.WireMockServer;
36+
import com.github.tomakehurst.wiremock.http.JvmProxyConfigurer;
37+
2438
import dev.jbang.cli.BaseCommand;
2539
import dev.jbang.cli.JBang;
2640
import dev.jbang.dependencies.DependencyCache;
41+
import dev.jbang.net.JdkManager;
2742
import dev.jbang.util.Util;
2843

2944
import picocli.CommandLine;
3045

3146
public abstract class BaseTest {
47+
WireMockServer globalwms;
3248

3349
@BeforeEach
3450
void initEnv(@TempDir Path tempPath) throws IOException {
@@ -54,6 +70,31 @@ void initEnv(@TempDir Path tempPath) throws IOException {
5470
}
5571
Configuration.instance(null);
5672
DependencyCache.clear();
73+
JdkManager.resetProviders();
74+
75+
// Start a WireMock server to capture and replay any remote
76+
// requests JBang makes (any new code that results in additional
77+
// requests will result in new recordings being added to the
78+
// `src/test/resources/mappings` folder which can then be added
79+
// to the git repository. Future requests will then be replayed
80+
// from the recordings instead of hitting the real server.)
81+
globalwms = new WireMockServer(options()
82+
.enableBrowserProxying(true)
83+
.dynamicPort());
84+
globalwms.start();
85+
JvmProxyConfigurer.configureFor(globalwms);
86+
disableSSL();
87+
88+
// This forces MIMA to use the WireMock server as a proxy
89+
// System.setProperty("aether.connector.http.useSystemProperties", "true");
90+
// System.setProperty("aether.connector.https.securityMode", "insecure");
91+
}
92+
93+
@AfterEach
94+
public void cleanupEnv() {
95+
globalwms.stop();
96+
globalwms.snapshotRecord(recordSpec().ignoreRepeatRequests());
97+
JvmProxyConfigurer.restorePrevious();
5798
}
5899

59100
public static final String EXAMPLES_FOLDER = "itests";
@@ -156,4 +197,35 @@ public String normalizedErr() {
156197
}
157198
}
158199

200+
static void disableSSL() {
201+
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
202+
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
203+
return new java.security.cert.X509Certificate[] {};
204+
}
205+
206+
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
207+
}
208+
209+
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
210+
}
211+
} };
212+
213+
try {
214+
SSLContext sc = SSLContext.getInstance("SSL");
215+
sc.init(null, trustAllCerts, new java.security.SecureRandom());
216+
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
217+
} catch (KeyManagementException | NoSuchAlgorithmException e) {
218+
throw new RuntimeException(e);
219+
}
220+
}
221+
222+
protected static void wiremockRequestPrinter(com.github.tomakehurst.wiremock.http.Request inRequest,
223+
com.github.tomakehurst.wiremock.http.Response inResponse) {
224+
System.err.printf("WireMock request at URL: %s%n", inRequest.getAbsoluteUrl());
225+
System.err.printf("WireMock request headers: %s%n", inRequest.getHeaders());
226+
System.err.printf("WireMock response status: %d%n", inResponse.getStatus());
227+
System.err.printf("WireMock response body: %s%n", inResponse.getBodyAsString());
228+
System.err.printf("WireMock response headers: %s%n", inResponse.getHeaders());
229+
}
230+
159231
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"url":"https://api.github.com/gists/503c88fb5123b1000c37a3f2832d4773","forks_url":"https://api.github.com/gists/503c88fb5123b1000c37a3f2832d4773/forks","commits_url":"https://api.github.com/gists/503c88fb5123b1000c37a3f2832d4773/commits","id":"503c88fb5123b1000c37a3f2832d4773","node_id":"MDQ6R2lzdDUwM2M4OGZiNTEyM2IxMDAwYzM3YTNmMjgzMmQ0Nzcz","git_pull_url":"https://gist.github.com/503c88fb5123b1000c37a3f2832d4773.git","git_push_url":"https://gist.github.com/503c88fb5123b1000c37a3f2832d4773.git","html_url":"https://gist.github.com/tivrfoa/503c88fb5123b1000c37a3f2832d4773","files":{"a#a.java":{"filename":"a#a.java","type":"text/x-java-source","language":"Java","raw_url":"https://gist.githubusercontent.com/tivrfoa/503c88fb5123b1000c37a3f2832d4773/raw/812e78353fa3d1e7c487a0543bd42e8747472152/a%23a.java","size":109,"truncated":false,"content":"class file4 {\n\n public static void main(String[] args) {\n System.out.println(\"I'm file 4\");\n }\n}","encoding":"utf-8"},"dash-test.java":{"filename":"dash-test.java","type":"text/x-java-source","language":"Java","raw_url":"https://gist.githubusercontent.com/tivrfoa/503c88fb5123b1000c37a3f2832d4773/raw/eb3e4714f83475500c7cd68951126fc6f3bb2a94/dash-test.java","size":150,"truncated":false,"content":"class file5 {\n\n public static void main(String[] args) {\n System.out.println(\"I'm file 5\");\n System.out.println(\"dash-test\");\n }\n}","encoding":"utf-8"},"file1.java":{"filename":"file1.java","type":"text/x-java-source","language":"Java","raw_url":"https://gist.githubusercontent.com/tivrfoa/503c88fb5123b1000c37a3f2832d4773/raw/496ef86adbbcdd585c4b5ee37719ce778350a4c3/file1.java","size":151,"truncated":false,"content":"///usr/bin/env jbang \"$0\" \"$@\" ; exit $?\n\nclass file1 {\n\n public static void main(String[] args) {\n System.out.println(\"I'm file 1\");\n }\n}","encoding":"utf-8"},"file2.java":{"filename":"file2.java","type":"text/x-java-source","language":"Java","raw_url":"https://gist.githubusercontent.com/tivrfoa/503c88fb5123b1000c37a3f2832d4773/raw/7fbadcb144c7d12fbfdc1acc0c3c6613dec64ea3/file2.java","size":151,"truncated":false,"content":"///usr/bin/env jbang \"$0\" \"$@\" ; exit $?\n\nclass file2 {\n\n public static void main(String[] args) {\n System.out.println(\"I'm file 2\");\n }\n}","encoding":"utf-8"},"file3.jsh":{"filename":"file3.jsh","type":"text/plain","language":"Java","raw_url":"https://gist.githubusercontent.com/tivrfoa/503c88fb5123b1000c37a3f2832d4773/raw/f716d5c44751acb0ea3d10c07886eadfdeb62b99/file3.jsh","size":75,"truncated":false,"content":"System.out.println(\"Test jsh extension\");\nSystem.out.println(\"I'm file 3\");","encoding":"utf-8"},"java-shell-script.jsh":{"filename":"java-shell-script.jsh","type":"text/plain","language":"Java","raw_url":"https://gist.githubusercontent.com/tivrfoa/503c88fb5123b1000c37a3f2832d4773/raw/8122df8e6de01d5b94a90d0ba696360f81c70aa9/java-shell-script.jsh","size":94,"truncated":false,"content":"System.out.println(\"I have a dash in filename.\");\nSystem.out.println(\"I'm java shell script\");","encoding":"utf-8"}},"public":true,"created_at":"2020-10-13T13:57:57Z","updated_at":"2020-10-14T07:15:19Z","description":"Tests for JBang - Multiple Files","comments":0,"user":null,"comments_enabled":true,"comments_url":"https://api.github.com/gists/503c88fb5123b1000c37a3f2832d4773/comments","owner":{"login":"tivrfoa","id":30683,"node_id":"MDQ6VXNlcjMwNjgz","avatar_url":"https://avatars.githubusercontent.com/u/30683?v=4","gravatar_id":"","url":"https://api.github.com/users/tivrfoa","html_url":"https://github.com/tivrfoa","followers_url":"https://api.github.com/users/tivrfoa/followers","following_url":"https://api.github.com/users/tivrfoa/following{/other_user}","gists_url":"https://api.github.com/users/tivrfoa/gists{/gist_id}","starred_url":"https://api.github.com/users/tivrfoa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tivrfoa/subscriptions","organizations_url":"https://api.github.com/users/tivrfoa/orgs","repos_url":"https://api.github.com/users/tivrfoa/repos","events_url":"https://api.github.com/users/tivrfoa/events{/privacy}","received_events_url":"https://api.github.com/users/tivrfoa/received_events","type":"User","user_view_type":"public","site_admin":false},"forks":[{"url":"https://api.github.com/gists/00db676cc6ebc677074b98f460764142","user":{"login":"maxandersen","id":54129,"node_id":"MDQ6VXNlcjU0MTI5","avatar_url":"https://avatars.githubusercontent.com/u/54129?v=4","gravatar_id":"","url":"https://api.github.com/users/maxandersen","html_url":"https://github.com/maxandersen","followers_url":"https://api.github.com/users/maxandersen/followers","following_url":"https://api.github.com/users/maxandersen/following{/other_user}","gists_url":"https://api.github.com/users/maxandersen/gists{/gist_id}","starred_url":"https://api.github.com/users/maxandersen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/maxandersen/subscriptions","organizations_url":"https://api.github.com/users/maxandersen/orgs","repos_url":"https://api.github.com/users/maxandersen/repos","events_url":"https://api.github.com/users/maxandersen/events{/privacy}","received_events_url":"https://api.github.com/users/maxandersen/received_events","type":"User","user_view_type":"public","site_admin":false,"name":"Max Rydahl Andersen","company":"Red Hat","blog":"https://xam.dk","location":"Denmark","email":"max@xam.dk","hireable":true,"bio":"Danish guy enjoying working on open source software. Making things happen at Red Hat.","twitter_username":"maxandersen","public_repos":596,"public_gists":294,"followers":354,"following":2,"created_at":"2009-02-13T01:28:57Z","updated_at":"2025-03-24T22:19:44Z"},"id":"00db676cc6ebc677074b98f460764142","created_at":"2020-10-14T07:15:19Z","updated_at":"2020-10-14T07:15:20Z"}],"history":[{"user":{"login":"tivrfoa","id":30683,"node_id":"MDQ6VXNlcjMwNjgz","avatar_url":"https://avatars.githubusercontent.com/u/30683?v=4","gravatar_id":"","url":"https://api.github.com/users/tivrfoa","html_url":"https://github.com/tivrfoa","followers_url":"https://api.github.com/users/tivrfoa/followers","following_url":"https://api.github.com/users/tivrfoa/following{/other_user}","gists_url":"https://api.github.com/users/tivrfoa/gists{/gist_id}","starred_url":"https://api.github.com/users/tivrfoa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tivrfoa/subscriptions","organizations_url":"https://api.github.com/users/tivrfoa/orgs","repos_url":"https://api.github.com/users/tivrfoa/repos","events_url":"https://api.github.com/users/tivrfoa/events{/privacy}","received_events_url":"https://api.github.com/users/tivrfoa/received_events","type":"User","user_view_type":"public","site_admin":false},"version":"2f93c8c5136989ccc51c7282eef678178df6f862","committed_at":"2020-10-14T02:02:54Z","change_status":{"total":0,"additions":0,"deletions":0},"url":"https://api.github.com/gists/503c88fb5123b1000c37a3f2832d4773/2f93c8c5136989ccc51c7282eef678178df6f862"},{"user":{"login":"tivrfoa","id":30683,"node_id":"MDQ6VXNlcjMwNjgz","avatar_url":"https://avatars.githubusercontent.com/u/30683?v=4","gravatar_id":"","url":"https://api.github.com/users/tivrfoa","html_url":"https://github.com/tivrfoa","followers_url":"https://api.github.com/users/tivrfoa/followers","following_url":"https://api.github.com/users/tivrfoa/following{/other_user}","gists_url":"https://api.github.com/users/tivrfoa/gists{/gist_id}","starred_url":"https://api.github.com/users/tivrfoa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tivrfoa/subscriptions","organizations_url":"https://api.github.com/users/tivrfoa/orgs","repos_url":"https://api.github.com/users/tivrfoa/repos","events_url":"https://api.github.com/users/tivrfoa/events{/privacy}","received_events_url":"https://api.github.com/users/tivrfoa/received_events","type":"User","user_view_type":"public","site_admin":false},"version":"04499a8dc8d9af46ae4ab6a59793a3edd9bd25ac","committed_at":"2020-10-14T01:53:36Z","change_status":{"total":2,"additions":2,"deletions":0},"url":"https://api.github.com/gists/503c88fb5123b1000c37a3f2832d4773/04499a8dc8d9af46ae4ab6a59793a3edd9bd25ac"},{"user":{"login":"tivrfoa","id":30683,"node_id":"MDQ6VXNlcjMwNjgz","avatar_url":"https://avatars.githubusercontent.com/u/30683?v=4","gravatar_id":"","url":"https://api.github.com/users/tivrfoa","html_url":"https://github.com/tivrfoa","followers_url":"https://api.github.com/users/tivrfoa/followers","following_url":"https://api.github.com/users/tivrfoa/following{/other_user}","gists_url":"https://api.github.com/users/tivrfoa/gists{/gist_id}","starred_url":"https://api.github.com/users/tivrfoa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tivrfoa/subscriptions","organizations_url":"https://api.github.com/users/tivrfoa/orgs","repos_url":"https://api.github.com/users/tivrfoa/repos","events_url":"https://api.github.com/users/tivrfoa/events{/privacy}","received_events_url":"https://api.github.com/users/tivrfoa/received_events","type":"User","user_view_type":"public","site_admin":false},"version":"36244984070fa7969c6652f795851f2832302a09","committed_at":"2020-10-13T15:18:59Z","change_status":{"total":13,"additions":13,"deletions":0},"url":"https://api.github.com/gists/503c88fb5123b1000c37a3f2832d4773/36244984070fa7969c6652f795851f2832302a09"},{"user":{"login":"tivrfoa","id":30683,"node_id":"MDQ6VXNlcjMwNjgz","avatar_url":"https://avatars.githubusercontent.com/u/30683?v=4","gravatar_id":"","url":"https://api.github.com/users/tivrfoa","html_url":"https://github.com/tivrfoa","followers_url":"https://api.github.com/users/tivrfoa/followers","following_url":"https://api.github.com/users/tivrfoa/following{/other_user}","gists_url":"https://api.github.com/users/tivrfoa/gists{/gist_id}","starred_url":"https://api.github.com/users/tivrfoa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tivrfoa/subscriptions","organizations_url":"https://api.github.com/users/tivrfoa/orgs","repos_url":"https://api.github.com/users/tivrfoa/repos","events_url":"https://api.github.com/users/tivrfoa/events{/privacy}","received_events_url":"https://api.github.com/users/tivrfoa/received_events","type":"User","user_view_type":"public","site_admin":false},"version":"69a0e67f01f6e273ab5433ced687927b751c2181","committed_at":"2020-10-13T14:36:43Z","change_status":{"total":2,"additions":2,"deletions":0},"url":"https://api.github.com/gists/503c88fb5123b1000c37a3f2832d4773/69a0e67f01f6e273ab5433ced687927b751c2181"},{"user":{"login":"tivrfoa","id":30683,"node_id":"MDQ6VXNlcjMwNjgz","avatar_url":"https://avatars.githubusercontent.com/u/30683?v=4","gravatar_id":"","url":"https://api.github.com/users/tivrfoa","html_url":"https://github.com/tivrfoa","followers_url":"https://api.github.com/users/tivrfoa/followers","following_url":"https://api.github.com/users/tivrfoa/following{/other_user}","gists_url":"https://api.github.com/users/tivrfoa/gists{/gist_id}","starred_url":"https://api.github.com/users/tivrfoa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tivrfoa/subscriptions","organizations_url":"https://api.github.com/users/tivrfoa/orgs","repos_url":"https://api.github.com/users/tivrfoa/repos","events_url":"https://api.github.com/users/tivrfoa/events{/privacy}","received_events_url":"https://api.github.com/users/tivrfoa/received_events","type":"User","user_view_type":"public","site_admin":false},"version":"477e07636d43cec1ae50d91417684262c2a7ed3a","committed_at":"2020-10-13T13:57:57Z","change_status":{"total":16,"additions":16,"deletions":0},"url":"https://api.github.com/gists/503c88fb5123b1000c37a3f2832d4773/477e07636d43cec1ae50d91417684262c2a7ed3a"}],"truncated":false}

0 commit comments

Comments
 (0)