Skip to content

Commit 70b6e22

Browse files
Migrate tests to JUnit5
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup
1 parent 50ffb78 commit 70b6e22

39 files changed

+1499
-1382
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<jenkins.baseline>2.504</jenkins.baseline>
3838
<jenkins.version>${jenkins.baseline}.1</jenkins.version>
3939
<spotless.check.skip>false</spotless.check.skip>
40+
<ban-junit4-imports.skip>false</ban-junit4-imports.skip>
4041
</properties>
4142

4243
<dependencyManagement>
@@ -135,6 +136,12 @@
135136
<artifactId>workflow-multibranch</artifactId>
136137
<scope>test</scope>
137138
</dependency>
139+
<dependency>
140+
<groupId>org.junit-pioneer</groupId>
141+
<artifactId>junit-pioneer</artifactId>
142+
<version>2.3.0</version>
143+
<scope>test</scope>
144+
</dependency>
138145
<dependency>
139146
<groupId>org.mockito</groupId>
140147
<artifactId>mockito-core</artifactId>
Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,40 @@
11
package org.jenkinsci.plugins.github_branch_source;
22

33
import static com.github.tomakehurst.wiremock.client.WireMock.*;
4-
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
54

65
import com.github.tomakehurst.wiremock.common.FileSource;
7-
import com.github.tomakehurst.wiremock.common.SingleRootFileSource;
86
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
97
import com.github.tomakehurst.wiremock.extension.Parameters;
108
import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
119
import com.github.tomakehurst.wiremock.http.Request;
1210
import com.github.tomakehurst.wiremock.http.Response;
13-
import com.github.tomakehurst.wiremock.junit.WireMockRule;
14-
import java.io.File;
15-
import org.junit.Before;
16-
import org.junit.ClassRule;
17-
import org.junit.Rule;
11+
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
12+
import org.junit.jupiter.api.BeforeAll;
13+
import org.junit.jupiter.api.BeforeEach;
14+
import org.junit.jupiter.api.extension.RegisterExtension;
1815
import org.jvnet.hudson.test.JenkinsRule;
16+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
1917

2018
/** @author Liam Newman */
19+
@WithJenkins
2120
public abstract class AbstractGitHubWireMockTest {
2221

2322
// By default the wiremock tests will run without proxy
2423
// The tests will use only the stubbed data and will fail if requests are made for missing data.
2524
// You can use the proxy while writing and debugging tests.
26-
private static final boolean useProxy =
25+
private static final boolean USE_PROXY =
2726
!System.getProperty("test.github.useProxy", "false").equals("false");
2827

29-
@ClassRule
30-
public static JenkinsRule r = new JenkinsRule();
28+
protected static JenkinsRule r;
3129

32-
public static WireMockRuleFactory factory = new WireMockRuleFactory();
30+
private static final WireMockExtensionFactory FACTORY = new WireMockExtensionFactory();
3331

34-
@Rule
35-
public WireMockRule githubRaw =
36-
factory.getRule(WireMockConfiguration.options().dynamicPort().usingFilesUnderClasspath("raw"));
32+
@RegisterExtension
33+
protected final WireMockExtension githubRaw =
34+
FACTORY.getExtension(WireMockConfiguration.options().dynamicPort().usingFilesUnderClasspath("raw"));
3735

38-
@Rule
39-
public WireMockRule githubApi = factory.getRule(WireMockConfiguration.options()
36+
@RegisterExtension
37+
protected final WireMockExtension githubApi = FACTORY.getExtension(WireMockConfiguration.options()
4038
.dynamicPort()
4139
.usingFilesUnderClasspath("api")
4240
.extensions(new ResponseTransformer() {
@@ -48,10 +46,11 @@ public Response transform(Request request, Response response, FileSource files,
4846
.but()
4947
.body(response.getBodyAsString()
5048
.replace(
51-
"https://api.github.com/", "http://localhost:" + githubApi.port() + "/")
49+
"https://api.github.com/",
50+
"http://localhost:" + githubApi.getPort() + "/")
5251
.replace(
5352
"https://raw.githubusercontent.com/",
54-
"http://localhost:" + githubRaw.port() + "/"))
53+
"http://localhost:" + githubRaw.getPort() + "/"))
5554
.build();
5655
}
5756
return response;
@@ -63,11 +62,14 @@ public String getName() {
6362
}
6463
}));
6564

66-
@Before
67-
public void prepareMockGitHub() {
68-
prepareMockGitHubFileMappings();
65+
@BeforeAll
66+
static void beforeAll(JenkinsRule rule) {
67+
r = rule;
68+
}
6969

70-
if (useProxy) {
70+
@BeforeEach
71+
void beforeEach() throws Exception {
72+
if (USE_PROXY) {
7173
githubApi.stubFor(get(urlMatching(".*"))
7274
.atPriority(10)
7375
.willReturn(aResponse().proxiedFrom("https://api.github.com/")));
@@ -76,17 +78,4 @@ public void prepareMockGitHub() {
7678
.willReturn(aResponse().proxiedFrom("https://raw.githubusercontent.com/")));
7779
}
7880
}
79-
80-
void prepareMockGitHubFileMappings() {
81-
new File("src/test/resources/api/mappings").mkdirs();
82-
new File("src/test/resources/api/__files").mkdirs();
83-
new File("src/test/resources/raw/mappings").mkdirs();
84-
new File("src/test/resources/raw/__files").mkdirs();
85-
githubApi.enableRecordMappings(
86-
new SingleRootFileSource("src/test/resources/api/mappings"),
87-
new SingleRootFileSource("src/test/resources/api/__files"));
88-
githubRaw.enableRecordMappings(
89-
new SingleRootFileSource("src/test/resources/raw/mappings"),
90-
new SingleRootFileSource("src/test/resources/raw/__files"));
91-
}
9281
}

src/test/java/org/jenkinsci/plugins/github_branch_source/ApiRateLimitCheckerTest.java

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22

33
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
44
import static com.github.tomakehurst.wiremock.client.WireMock.get;
5-
import static com.github.tomakehurst.wiremock.client.WireMock.resetAllScenarios;
65
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
7-
import static org.junit.Assert.assertEquals;
8-
import static org.junit.Assert.assertFalse;
9-
import static org.junit.Assert.assertNotEquals;
10-
import static org.junit.Assert.assertTrue;
6+
import static org.junit.jupiter.api.Assertions.*;
117

12-
import com.github.tomakehurst.wiremock.WireMockServer;
138
import com.github.tomakehurst.wiremock.client.ScenarioMappingBuilder;
9+
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
1410
import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder;
1511
import com.github.tomakehurst.wiremock.stubbing.Scenario;
1612
import hudson.util.LogTaskListener;
@@ -25,14 +21,14 @@
2521
import java.util.logging.Logger;
2622
import java.util.stream.Stream;
2723
import org.jenkinsci.plugins.github.config.GitHubServerConfig;
28-
import org.junit.After;
29-
import org.junit.Before;
30-
import org.junit.Test;
24+
import org.junit.jupiter.api.AfterEach;
25+
import org.junit.jupiter.api.BeforeEach;
26+
import org.junit.jupiter.api.Test;
3127
import org.kohsuke.github.GHRateLimit;
3228
import org.kohsuke.github.GitHub;
3329
import org.mockito.Mockito;
3430

35-
public class ApiRateLimitCheckerTest extends AbstractGitHubWireMockTest {
31+
class ApiRateLimitCheckerTest extends AbstractGitHubWireMockTest {
3632

3733
private RingBufferLogHandler handler;
3834
private LogTaskListener listener;
@@ -57,26 +53,21 @@ private long countOfOutputLinesContaining(String substring) {
5753
return countOfOutputLines(m -> m.contains(substring));
5854
}
5955

60-
public static int getRequestCount(WireMockServer server) {
61-
return server.countRequestsMatching(RequestPatternBuilder.allRequests().build())
56+
private static int getRequestCount(WireMockExtension extension) {
57+
return extension
58+
.countRequestsMatching(RequestPatternBuilder.allRequests().build())
6259
.getCount();
6360
}
6461

65-
private class RateLimit {
66-
final int remaining;
67-
final int limit;
68-
final Date reset;
62+
private record RateLimit(int limit, int remaining, Date reset) {}
6963

70-
RateLimit(int limit, int remaining, Date reset) {
71-
this.limit = limit;
72-
this.remaining = remaining;
73-
this.reset = reset;
74-
}
75-
}
64+
@Override
65+
@BeforeEach
66+
void beforeEach() throws Exception {
67+
super.beforeEach();
7668

77-
@Before
78-
public void setUp() throws Exception {
79-
resetAllScenarios();
69+
githubApi.resetScenarios();
70+
githubRaw.resetScenarios();
8071

8172
handler = new RingBufferLogHandler(1000);
8273
final Logger logger = Logger.getLogger(getClass().getName());
@@ -98,13 +89,12 @@ public void setUp() throws Exception {
9889
ApiRateLimitChecker.resetLocalChecker();
9990
}
10091

101-
@After
102-
public void tearDown() throws Exception {
92+
@AfterEach
93+
void afterEach() {
10394
GitHubConfiguration.get().setEndpoints(new ArrayList<>());
10495
}
10596

10697
private void setupStubs(List<RateLimit> scenarios) throws Exception {
107-
10898
githubApi.stubFor(get(urlEqualTo("/meta"))
10999
.willReturn(aResponse().withStatus(200).withBody("{\"verifiable_password_authentication\": false}")));
110100

@@ -159,13 +149,13 @@ private void setupStubs(List<RateLimit> scenarios) throws Exception {
159149
githubApi.stubFor(scenario);
160150
}
161151

162-
github = Connector.connect("http://localhost:" + githubApi.port(), null);
152+
github = Connector.connect("http://localhost:" + githubApi.getPort(), null);
163153
initialRequestCount = getRequestCount(githubApi);
164154
assertEquals(2, initialRequestCount);
165155
}
166156

167157
@Test
168-
public void NoCheckerConfigured() throws Exception {
158+
void NoCheckerConfigured() throws Exception {
169159
// set up scenarios
170160
List<RateLimit> scenarios = new ArrayList<>();
171161
long now = System.currentTimeMillis();
@@ -204,7 +194,7 @@ public void NoCheckerConfigured() throws Exception {
204194
}
205195

206196
@Test
207-
public void NoCheckerConfiguredWithEndpoint() throws Exception {
197+
void NoCheckerConfiguredWithEndpoint() throws Exception {
208198
// set up scenarios
209199
List<RateLimit> scenarios = new ArrayList<>();
210200
long now = System.currentTimeMillis();
@@ -240,7 +230,7 @@ public void NoCheckerConfiguredWithEndpoint() throws Exception {
240230
* @author Julian V. Modesto
241231
*/
242232
@Test
243-
public void ThrottleOnOverTestWithQuota() throws Exception {
233+
void ThrottleOnOverTestWithQuota() throws Exception {
244234
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleOnOver);
245235

246236
// set up scenarios
@@ -269,7 +259,7 @@ public void ThrottleOnOverTestWithQuota() throws Exception {
269259
* @author Julian V. Modesto
270260
*/
271261
@Test
272-
public void ThrottleOnNormalizeTestWithQuota() throws Exception {
262+
void ThrottleOnNormalizeTestWithQuota() throws Exception {
273263
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleForNormalize);
274264

275265
// set up scenarios
@@ -296,7 +286,7 @@ public void ThrottleOnNormalizeTestWithQuota() throws Exception {
296286
* @author Marc Salles Navarro
297287
*/
298288
@Test
299-
public void NoThrottleTestShouldNotThrottle() throws Exception {
289+
void NoThrottleTestShouldNotThrottle() throws Exception {
300290
// set up scenarios
301291
List<RateLimit> scenarios = new ArrayList<>();
302292
int limit = 5000;
@@ -324,8 +314,7 @@ public void NoThrottleTestShouldNotThrottle() throws Exception {
324314
* @author Marc Salles Navarro
325315
*/
326316
@Test
327-
public void NoThrottleTestShouldNotThrottle404() throws Exception {
328-
317+
void NoThrottleTestShouldNotThrottle404() throws Exception {
329318
setupStubs(new ArrayList<>());
330319
GHRateLimit.Record initial = github.lastRateLimit().getCore();
331320
assertEquals(2, getRequestCount(githubApi));
@@ -341,7 +330,8 @@ public void NoThrottleTestShouldNotThrottle404() throws Exception {
341330
github.getMeta();
342331

343332
// The core should be unknown, but different from initial
344-
assertTrue(github.rateLimit().getCore() instanceof GHRateLimit.UnknownLimitRecord);
333+
assertInstanceOf(
334+
GHRateLimit.UnknownLimitRecord.class, github.rateLimit().getCore());
345335
assertNotEquals(initial, github.rateLimit().getCore());
346336

347337
// there should be no output
@@ -357,7 +347,7 @@ public void NoThrottleTestShouldNotThrottle404() throws Exception {
357347
* @author Marc Salles Navarro
358348
*/
359349
@Test
360-
public void NoThrottleTestShouldFallbackToThrottleOnOverForGitHubDotCom() throws Exception {
350+
void NoThrottleTestShouldFallbackToThrottleOnOverForGitHubDotCom() throws Exception {
361351
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleOnOver);
362352

363353
// set up scenarios
@@ -391,7 +381,7 @@ public void NoThrottleTestShouldFallbackToThrottleOnOverForGitHubDotCom() throws
391381
* @author Julian V. Modesto
392382
*/
393383
@Test
394-
public void ThrottleOnOverTest() throws Exception {
384+
void ThrottleOnOverTest() throws Exception {
395385
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleOnOver);
396386

397387
// set up scenarios
@@ -462,7 +452,7 @@ public void ThrottleOnOverTest() throws Exception {
462452
* @author Julian V. Modesto
463453
*/
464454
@Test
465-
public void ThrottleForNormalizeTestWithinIdeal() throws Exception {
455+
void ThrottleForNormalizeTestWithinIdeal() throws Exception {
466456
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleForNormalize);
467457

468458
List<RateLimit> scenarios = new ArrayList<>();
@@ -552,7 +542,7 @@ public void ThrottleForNormalizeTestWithinIdeal() throws Exception {
552542
* @author Julian V. Modesto
553543
*/
554544
@Test
555-
public void NormalizeThrottleWithBurnedBuffer() throws Exception {
545+
void NormalizeThrottleWithBurnedBuffer() throws Exception {
556546
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleForNormalize);
557547

558548
long now = System.currentTimeMillis();
@@ -614,7 +604,7 @@ public void NormalizeThrottleWithBurnedBuffer() throws Exception {
614604
* @author Alex Taylor
615605
*/
616606
@Test
617-
public void OnOverThrottleTimingRateLimitCheck() throws Exception {
607+
void OnOverThrottleTimingRateLimitCheck() throws Exception {
618608
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleOnOver);
619609

620610
// Longer timings that test defaults for more consistent measurements.
@@ -695,7 +685,7 @@ public void OnOverThrottleTimingRateLimitCheck() throws Exception {
695685
* @author Alex Taylor
696686
*/
697687
@Test
698-
public void NormalizeThrottleTimingRateLimitCheck() throws Exception {
688+
void NormalizeThrottleTimingRateLimitCheck() throws Exception {
699689
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleForNormalize);
700690

701691
ApiRateLimitChecker.setExpirationWaitMillis(60);
@@ -763,7 +753,7 @@ public void NormalizeThrottleTimingRateLimitCheck() throws Exception {
763753
* @author Alex Taylor
764754
*/
765755
@Test
766-
public void NormalizeExpectedIdealOverTime() throws Exception {
756+
void NormalizeExpectedIdealOverTime() throws Exception {
767757
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleForNormalize);
768758

769759
// Set up scenarios
@@ -840,7 +830,7 @@ public void NormalizeExpectedIdealOverTime() throws Exception {
840830
* @author Alex Taylor
841831
*/
842832
@Test
843-
public void OnOverExpectedIdealOverTime() throws Exception {
833+
void OnOverExpectedIdealOverTime() throws Exception {
844834
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleOnOver);
845835

846836
long start = System.currentTimeMillis();
@@ -894,7 +884,7 @@ public void OnOverExpectedIdealOverTime() throws Exception {
894884
* @author Alex Taylor
895885
*/
896886
@Test
897-
public void ExpectedResetTimingNormalize() throws Exception {
887+
void ExpectedResetTimingNormalize() throws Exception {
898888
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleForNormalize);
899889

900890
// Use a longer notification interval to make the test produce stable output
@@ -940,7 +930,7 @@ public void ExpectedResetTimingNormalize() throws Exception {
940930
* @author Alex Taylor
941931
*/
942932
@Test
943-
public void ExpectedResetTimingOnOver() throws Exception {
933+
void ExpectedResetTimingOnOver() throws Exception {
944934
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleOnOver);
945935

946936
// Use a longer notification interval to make the test produce stable output

0 commit comments

Comments
 (0)