Skip to content

Commit 93f25bb

Browse files
Migrate tests to JUnit5 (#184)
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup
1 parent cc4543a commit 93f25bb

File tree

3 files changed

+46
-49
lines changed

3 files changed

+46
-49
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>org.jenkins-ci.plugins</groupId>
66
<artifactId>plugin</artifactId>
7-
<version>5.3</version>
7+
<version>5.17</version>
88
<relativePath />
99
</parent>
1010

@@ -46,7 +46,7 @@
4646
<changelist>999999-SNAPSHOT</changelist>
4747
<gitHubRepo>jenkinsci/${project.artifactId}</gitHubRepo>
4848
<jenkins.baseline>2.479</jenkins.baseline>
49-
<jenkins.version>${jenkins.baseline}.1</jenkins.version>
49+
<jenkins.version>${jenkins.baseline}.3</jenkins.version>
5050
<spotbugs.effort>Max</spotbugs.effort>
5151
<spotless.check.skip>false</spotless.check.skip>
5252
</properties>
@@ -56,7 +56,7 @@
5656
<dependency>
5757
<groupId>io.jenkins.tools.bom</groupId>
5858
<artifactId>bom-${jenkins.baseline}.x</artifactId>
59-
<version>3790.va_b_a_2d26d2b_69</version>
59+
<version>4770.v9a_2b_7a_9d8b_7f</version>
6060
<type>pom</type>
6161
<scope>import</scope>
6262
</dependency>

src/test/java/org/jenkinsci/plugins/reverse_proxy_auth/ReverseProxySecurityRealmTest.java

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,64 +5,58 @@
55
import static org.hamcrest.CoreMatchers.is;
66
import static org.hamcrest.CoreMatchers.not;
77
import static org.hamcrest.MatcherAssert.assertThat;
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
89

910
import hudson.security.SecurityRealm;
1011
import hudson.util.Secret;
1112
import java.io.File;
1213
import java.io.FileReader;
1314
import java.io.IOException;
1415
import java.util.Collections;
15-
import java.util.concurrent.Callable;
1616
import jenkins.model.Jenkins;
1717
import org.apache.commons.io.IOUtils;
18-
import org.junit.Assert;
19-
import org.junit.Before;
20-
import org.junit.Rule;
21-
import org.junit.Test;
18+
import org.junit.jupiter.api.BeforeEach;
19+
import org.junit.jupiter.api.Test;
2220
import org.jvnet.hudson.test.Issue;
2321
import org.jvnet.hudson.test.JenkinsRule;
22+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
2423
import org.jvnet.hudson.test.recipes.LocalData;
2524
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
2625
import org.springframework.security.core.Authentication;
2726
import org.springframework.security.core.userdetails.UserDetails;
2827

29-
public class ReverseProxySecurityRealmTest {
30-
@Rule
31-
public final JenkinsRule jenkinsRule = new JenkinsRule();
28+
@WithJenkins
29+
class ReverseProxySecurityRealmTest {
3230

33-
private Jenkins jenkins;
31+
private JenkinsRule j;
3432

35-
@Before
36-
public void setUp() {
37-
jenkins = jenkinsRule.jenkins;
33+
@BeforeEach
34+
void setUp(JenkinsRule rule) {
35+
j = rule;
3836
}
3937

4038
@Test
41-
public void basicGetUserDetails() {
39+
void basicGetUserDetails() {
4240
final ReverseProxySecurityRealm realm = createBasicRealm();
4341
final UserDetails userDetails = realm.loadUserByUsername2("[email protected]");
44-
Assert.assertEquals("[email protected]", userDetails.getUsername());
42+
assertEquals("[email protected]", userDetails.getUsername());
4543
}
4644

4745
@Test
4846
@Issue("JENKINS-49274")
49-
public void basicAuthenticate() throws Exception {
47+
void basicAuthenticate() throws Exception {
5048
final ReverseProxySecurityRealm realm = createBasicRealm();
51-
jenkins.setSecurityRealm(realm);
49+
j.jenkins.setSecurityRealm(realm);
5250

53-
final JenkinsRule.WebClient client = jenkinsRule.createWebClient();
54-
client.addRequestHeader(realm.getForwardedUser(), "[email protected]");
55-
final Authentication authentication = client.executeOnServer(new Callable<Authentication>() {
56-
@Override
57-
public Authentication call() {
58-
return Jenkins.getAuthentication2();
59-
}
60-
});
61-
Assert.assertEquals(
62-
"Authentication should match",
63-
new UsernamePasswordAuthenticationToken(
64-
"[email protected]", "", Collections.singleton(SecurityRealm.AUTHENTICATED_AUTHORITY2)),
65-
authentication);
51+
try (JenkinsRule.WebClient client = j.createWebClient()) {
52+
client.addRequestHeader(realm.getForwardedUser(), "[email protected]");
53+
final Authentication authentication = client.executeOnServer(Jenkins::getAuthentication2);
54+
assertEquals(
55+
new UsernamePasswordAuthenticationToken(
56+
"[email protected]", "", Collections.singleton(SecurityRealm.AUTHENTICATED_AUTHORITY2)),
57+
authentication,
58+
"Authentication should match");
59+
}
6660
}
6761

6862
private ReverseProxySecurityRealm createBasicRealm() {
@@ -94,8 +88,8 @@ private ReverseProxySecurityRealm createBasicRealm() {
9488

9589
@Test
9690
@LocalData
97-
public void testPasswordMigration() throws IOException {
98-
final SecurityRealm securityRealm = jenkinsRule.jenkins.getSecurityRealm();
91+
void testPasswordMigration() throws IOException {
92+
final SecurityRealm securityRealm = j.jenkins.getSecurityRealm();
9993
assertThat(securityRealm, instanceOf(ReverseProxySecurityRealm.class));
10094
ReverseProxySecurityRealm reverseProxySecurityRealm = (ReverseProxySecurityRealm) securityRealm;
10195
assertThat(reverseProxySecurityRealm.getManagerPassword().getPlainText(), is("theManagerPassw0rd"));
Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
package org.jenkinsci.plugins.reverse_proxy_auth.data;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
35
import hudson.model.User;
46
import hudson.tasks.Mailer;
57
import jenkins.model.Jenkins;
6-
import org.junit.Assert;
7-
import org.junit.Before;
8-
import org.junit.Rule;
9-
import org.junit.Test;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
1010
import org.jvnet.hudson.test.JenkinsRule;
1111
import org.jvnet.hudson.test.MockAuthorizationStrategy;
12+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
13+
14+
@WithJenkins
15+
class ForwardedUserDataTest {
1216

13-
public class ForwardedUserDataTest {
1417
private ForwardedUserData forwardedUserData;
1518
private User user;
1619

17-
@Rule
18-
public JenkinsRule j = new JenkinsRule();
20+
private JenkinsRule j;
1921

20-
@Before
21-
public void setup() {
22+
@BeforeEach
23+
void setUp(JenkinsRule rule) {
24+
j = rule;
2225
j.jenkins.setAuthorizationStrategy(
2326
new MockAuthorizationStrategy().grant(Jenkins.READ).everywhere().to("Max Mustermann"));
2427

@@ -27,24 +30,24 @@ public void setup() {
2730
}
2831

2932
@Test
30-
public void basicForwardedUserData() {
33+
void basicForwardedUserData() {
3134
forwardedUserData.setEmail("[email protected]");
32-
Assert.assertEquals("[email protected]", forwardedUserData.getEmail());
35+
assertEquals("[email protected]", forwardedUserData.getEmail());
3336

3437
forwardedUserData.setDisplayName("Max Mustermann");
35-
Assert.assertEquals("Max Mustermann", forwardedUserData.getDisplayName());
38+
assertEquals("Max Mustermann", forwardedUserData.getDisplayName());
3639
}
3740

3841
@Test
39-
public void testUpdate() {
42+
void testUpdate() {
4043
user.setFullName("John Doe");
4144
forwardedUserData.setDisplayName("Max Mustermann");
4245
forwardedUserData.update(user);
43-
Assert.assertEquals("Max Mustermann", user.getFullName());
46+
assertEquals("Max Mustermann", user.getFullName());
4447

4548
forwardedUserData.setEmail("[email protected]");
4649
forwardedUserData.update(user);
4750
Mailer.UserProperty emailProp = user.getProperty(Mailer.UserProperty.class);
48-
Assert.assertEquals("[email protected]", emailProp.getAddress());
51+
assertEquals("[email protected]", emailProp.getAddress());
4952
}
5053
}

0 commit comments

Comments
 (0)