Skip to content

Commit d8ebea1

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 d8ebea1

39 files changed

+1481
-1372
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: 35 additions & 48 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,18 @@ 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-
}
76-
77-
@Before
78-
public void setUp() throws Exception {
79-
resetAllScenarios();
64+
@BeforeEach
65+
void beforeEach() {
66+
githubApi.resetScenarios();
67+
githubRaw.resetScenarios();
8068

8169
handler = new RingBufferLogHandler(1000);
8270
final Logger logger = Logger.getLogger(getClass().getName());
@@ -98,13 +86,12 @@ public void setUp() throws Exception {
9886
ApiRateLimitChecker.resetLocalChecker();
9987
}
10088

101-
@After
102-
public void tearDown() throws Exception {
89+
@AfterEach
90+
void afterEach() {
10391
GitHubConfiguration.get().setEndpoints(new ArrayList<>());
10492
}
10593

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

@@ -159,13 +146,13 @@ private void setupStubs(List<RateLimit> scenarios) throws Exception {
159146
githubApi.stubFor(scenario);
160147
}
161148

162-
github = Connector.connect("http://localhost:" + githubApi.port(), null);
149+
github = Connector.connect("http://localhost:" + githubApi.getPort(), null);
163150
initialRequestCount = getRequestCount(githubApi);
164151
assertEquals(2, initialRequestCount);
165152
}
166153

167154
@Test
168-
public void NoCheckerConfigured() throws Exception {
155+
void NoCheckerConfigured() throws Exception {
169156
// set up scenarios
170157
List<RateLimit> scenarios = new ArrayList<>();
171158
long now = System.currentTimeMillis();
@@ -204,7 +191,7 @@ public void NoCheckerConfigured() throws Exception {
204191
}
205192

206193
@Test
207-
public void NoCheckerConfiguredWithEndpoint() throws Exception {
194+
void NoCheckerConfiguredWithEndpoint() throws Exception {
208195
// set up scenarios
209196
List<RateLimit> scenarios = new ArrayList<>();
210197
long now = System.currentTimeMillis();
@@ -240,7 +227,7 @@ public void NoCheckerConfiguredWithEndpoint() throws Exception {
240227
* @author Julian V. Modesto
241228
*/
242229
@Test
243-
public void ThrottleOnOverTestWithQuota() throws Exception {
230+
void ThrottleOnOverTestWithQuota() throws Exception {
244231
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleOnOver);
245232

246233
// set up scenarios
@@ -269,7 +256,7 @@ public void ThrottleOnOverTestWithQuota() throws Exception {
269256
* @author Julian V. Modesto
270257
*/
271258
@Test
272-
public void ThrottleOnNormalizeTestWithQuota() throws Exception {
259+
void ThrottleOnNormalizeTestWithQuota() throws Exception {
273260
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleForNormalize);
274261

275262
// set up scenarios
@@ -296,7 +283,7 @@ public void ThrottleOnNormalizeTestWithQuota() throws Exception {
296283
* @author Marc Salles Navarro
297284
*/
298285
@Test
299-
public void NoThrottleTestShouldNotThrottle() throws Exception {
286+
void NoThrottleTestShouldNotThrottle() throws Exception {
300287
// set up scenarios
301288
List<RateLimit> scenarios = new ArrayList<>();
302289
int limit = 5000;
@@ -324,8 +311,7 @@ public void NoThrottleTestShouldNotThrottle() throws Exception {
324311
* @author Marc Salles Navarro
325312
*/
326313
@Test
327-
public void NoThrottleTestShouldNotThrottle404() throws Exception {
328-
314+
void NoThrottleTestShouldNotThrottle404() throws Exception {
329315
setupStubs(new ArrayList<>());
330316
GHRateLimit.Record initial = github.lastRateLimit().getCore();
331317
assertEquals(2, getRequestCount(githubApi));
@@ -341,7 +327,8 @@ public void NoThrottleTestShouldNotThrottle404() throws Exception {
341327
github.getMeta();
342328

343329
// The core should be unknown, but different from initial
344-
assertTrue(github.rateLimit().getCore() instanceof GHRateLimit.UnknownLimitRecord);
330+
assertInstanceOf(
331+
GHRateLimit.UnknownLimitRecord.class, github.rateLimit().getCore());
345332
assertNotEquals(initial, github.rateLimit().getCore());
346333

347334
// there should be no output
@@ -357,7 +344,7 @@ public void NoThrottleTestShouldNotThrottle404() throws Exception {
357344
* @author Marc Salles Navarro
358345
*/
359346
@Test
360-
public void NoThrottleTestShouldFallbackToThrottleOnOverForGitHubDotCom() throws Exception {
347+
void NoThrottleTestShouldFallbackToThrottleOnOverForGitHubDotCom() throws Exception {
361348
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleOnOver);
362349

363350
// set up scenarios
@@ -391,7 +378,7 @@ public void NoThrottleTestShouldFallbackToThrottleOnOverForGitHubDotCom() throws
391378
* @author Julian V. Modesto
392379
*/
393380
@Test
394-
public void ThrottleOnOverTest() throws Exception {
381+
void ThrottleOnOverTest() throws Exception {
395382
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleOnOver);
396383

397384
// set up scenarios
@@ -462,7 +449,7 @@ public void ThrottleOnOverTest() throws Exception {
462449
* @author Julian V. Modesto
463450
*/
464451
@Test
465-
public void ThrottleForNormalizeTestWithinIdeal() throws Exception {
452+
void ThrottleForNormalizeTestWithinIdeal() throws Exception {
466453
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleForNormalize);
467454

468455
List<RateLimit> scenarios = new ArrayList<>();
@@ -552,7 +539,7 @@ public void ThrottleForNormalizeTestWithinIdeal() throws Exception {
552539
* @author Julian V. Modesto
553540
*/
554541
@Test
555-
public void NormalizeThrottleWithBurnedBuffer() throws Exception {
542+
void NormalizeThrottleWithBurnedBuffer() throws Exception {
556543
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleForNormalize);
557544

558545
long now = System.currentTimeMillis();
@@ -614,7 +601,7 @@ public void NormalizeThrottleWithBurnedBuffer() throws Exception {
614601
* @author Alex Taylor
615602
*/
616603
@Test
617-
public void OnOverThrottleTimingRateLimitCheck() throws Exception {
604+
void OnOverThrottleTimingRateLimitCheck() throws Exception {
618605
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleOnOver);
619606

620607
// Longer timings that test defaults for more consistent measurements.
@@ -695,7 +682,7 @@ public void OnOverThrottleTimingRateLimitCheck() throws Exception {
695682
* @author Alex Taylor
696683
*/
697684
@Test
698-
public void NormalizeThrottleTimingRateLimitCheck() throws Exception {
685+
void NormalizeThrottleTimingRateLimitCheck() throws Exception {
699686
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleForNormalize);
700687

701688
ApiRateLimitChecker.setExpirationWaitMillis(60);
@@ -763,7 +750,7 @@ public void NormalizeThrottleTimingRateLimitCheck() throws Exception {
763750
* @author Alex Taylor
764751
*/
765752
@Test
766-
public void NormalizeExpectedIdealOverTime() throws Exception {
753+
void NormalizeExpectedIdealOverTime() throws Exception {
767754
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleForNormalize);
768755

769756
// Set up scenarios
@@ -840,7 +827,7 @@ public void NormalizeExpectedIdealOverTime() throws Exception {
840827
* @author Alex Taylor
841828
*/
842829
@Test
843-
public void OnOverExpectedIdealOverTime() throws Exception {
830+
void OnOverExpectedIdealOverTime() throws Exception {
844831
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleOnOver);
845832

846833
long start = System.currentTimeMillis();
@@ -894,7 +881,7 @@ public void OnOverExpectedIdealOverTime() throws Exception {
894881
* @author Alex Taylor
895882
*/
896883
@Test
897-
public void ExpectedResetTimingNormalize() throws Exception {
884+
void ExpectedResetTimingNormalize() throws Exception {
898885
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleForNormalize);
899886

900887
// Use a longer notification interval to make the test produce stable output
@@ -940,7 +927,7 @@ public void ExpectedResetTimingNormalize() throws Exception {
940927
* @author Alex Taylor
941928
*/
942929
@Test
943-
public void ExpectedResetTimingOnOver() throws Exception {
930+
void ExpectedResetTimingOnOver() throws Exception {
944931
GitHubConfiguration.get().setApiRateLimitChecker(ApiRateLimitChecker.ThrottleOnOver);
945932

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

0 commit comments

Comments
 (0)