Skip to content

Commit ab2c985

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 d77eae5 commit ab2c985

File tree

40 files changed

+1433
-20
lines changed

40 files changed

+1433
-20
lines changed

build.gradle

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ dependencies {
111111

112112
testImplementation platform('org.junit:junit-bom:5.12.1')
113113
testImplementation "org.junit.jupiter:junit-jupiter"
114-
testRuntimeOnly "org.junit.platform:junit-platform-launcher"
114+
testImplementation "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: 86 additions & 16 deletions
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,44 @@
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;
19-
import org.junit.jupiter.api.AfterAll;
20-
import org.junit.jupiter.api.BeforeAll;
29+
import org.junit.jupiter.api.AfterEach;
2130
import org.junit.jupiter.api.BeforeEach;
2231
import org.junit.jupiter.api.io.TempDir;
2332

33+
import com.github.tomakehurst.wiremock.WireMockServer;
34+
import com.github.tomakehurst.wiremock.http.JvmProxyConfigurer;
35+
2436
import dev.jbang.cli.BaseCommand;
2537
import dev.jbang.cli.JBang;
2638
import dev.jbang.dependencies.DependencyCache;
39+
import dev.jbang.net.JdkManager;
2740
import dev.jbang.util.Util;
2841

2942
import picocli.CommandLine;
3043

3144
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();
3255

3356
@BeforeEach
3457
void initEnv(@TempDir Path tempPath) throws IOException {
@@ -54,15 +77,40 @@ void initEnv(@TempDir Path tempPath) throws IOException {
5477
}
5578
Configuration.instance(null);
5679
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();
57105
}
58106

59107
public static final String EXAMPLES_FOLDER = "itests";
60-
public static Path examplesTestFolder;
61108

62-
@BeforeAll
63-
static void init() throws URISyntaxException, IOException {
109+
// @BeforeAll
110+
public static void initBeforeAll() throws URISyntaxException, IOException {
64111
mavenTempDir = Files.createTempDirectory("jbang_tests_maven");
65112
jdksTempDir = Files.createTempDirectory("jbang_tests_jdks");
113+
System.err.println("## INIT BEFORE ALL TESTS " + mavenTempDir);
66114
URL examplesUrl = BaseTest.class.getClassLoader().getResource(EXAMPLES_FOLDER);
67115
if (examplesUrl == null) {
68116
examplesTestFolder = Paths.get(EXAMPLES_FOLDER).toAbsolutePath();
@@ -71,20 +119,12 @@ static void init() throws URISyntaxException, IOException {
71119
}
72120
}
73121

74-
@AfterAll
75-
static void cleanup() {
122+
// @AfterAll
123+
public static void cleanupAfterAll() {
76124
Util.deletePath(mavenTempDir, true);
77125
Util.deletePath(jdksTempDir, true);
78126
}
79127

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-
88128
protected <T> CaptureResult checkedRun(Function<T, Integer> commandRunner, String... args) throws Exception {
89129
CommandLine.ParseResult pr = JBang.getCommandLine().parseArgs(args);
90130
while (pr.subcommand() != null) {
@@ -156,4 +196,34 @@ public String normalizedErr() {
156196
}
157197
}
158198

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+
}
159229
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package dev.jbang;
2+
3+
import org.junit.platform.launcher.TestExecutionListener;
4+
import org.junit.platform.launcher.TestPlan;
5+
6+
public class JBangTestExecutionListener implements TestExecutionListener {
7+
8+
@Override
9+
public void testPlanExecutionStarted(TestPlan testPlan) {
10+
System.err.println("##############################################");
11+
System.err.println("## BEFORE ALL TESTS");
12+
try {
13+
BaseTest.initBeforeAll();
14+
} catch (Exception e) {
15+
throw new RuntimeException(e);
16+
}
17+
System.err.println("##############################################");
18+
TestExecutionListener.super.testPlanExecutionStarted(testPlan);
19+
}
20+
21+
@Override
22+
public void testPlanExecutionFinished(TestPlan testPlan) {
23+
System.err.println("##############################################");
24+
System.err.println("## AFTER ALL TESTS");
25+
TestExecutionListener.super.testPlanExecutionFinished(testPlan);
26+
try {
27+
BaseTest.cleanupAfterAll();
28+
} catch (Exception e) {
29+
throw new RuntimeException(e);
30+
}
31+
System.err.println("##############################################");
32+
}
33+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dev.jbang.JBangTestExecutionListener

0 commit comments

Comments
 (0)