Skip to content

Commit d66df9d

Browse files
Migrate tests to JUnit Jupiter (#363)
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup * Ban JUnit4 imports Co-authored-by: Mark Waite <mark.earl.waite@gmail.com>
1 parent 5215554 commit d66df9d

36 files changed

+1859
-1553
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ THE SOFTWARE.
6363
<jenkins.version>${jenkins.baseline}.3</jenkins.version>
6464
<no-test-jar>false</no-test-jar>
6565
<spotbugs.threshold>High</spotbugs.threshold> <!-- TODO some violations remaining -->
66+
<ban-junit4-imports.skip>false</ban-junit4-imports.skip>
6667
<svnkit.version>1.10.11</svnkit.version>
6768
</properties>
6869

src/test/java/hudson/scm/AbstractSubversionTest.java

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,61 @@
55
import hudson.Launcher.LocalLauncher;
66
import hudson.Proc;
77
import hudson.util.StreamTaskListener;
8-
9-
import static org.mockito.Mockito.mock;
10-
import static org.mockito.Mockito.when;
118
import net.sf.json.JSONObject;
12-
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.extension.RegisterExtension;
11+
import org.junit.jupiter.api.io.TempDir;
12+
import org.jvnet.hudson.test.JenkinsRule;
13+
import org.jvnet.hudson.test.junit.jupiter.BuildWatcherExtension;
14+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
1315
import org.kohsuke.stapler.StaplerRequest2;
16+
import org.opentest4j.TestAbortedException;
1417

1518
import java.io.File;
1619
import java.io.IOException;
1720
import java.io.InputStream;
1821
import java.net.ServerSocket;
1922
import java.net.Socket;
2023
import java.net.URL;
21-
import org.junit.AssumptionViolatedException;
22-
import org.junit.ClassRule;
23-
import org.junit.Rule;
24-
import org.junit.rules.TemporaryFolder;
25-
import org.jvnet.hudson.test.BuildWatcher;
26-
import org.jvnet.hudson.test.JenkinsRule;
24+
25+
import static org.mockito.Mockito.mock;
26+
import static org.mockito.Mockito.when;
2727

2828
/**
2929
* Base class for Subversion related tests.
3030
*
3131
* @author Kohsuke Kawaguchi
3232
*/
3333
// TODO perhaps merge into SubversionSampleRepoRule
34+
@WithJenkins
3435
public abstract class AbstractSubversionTest {
3536

36-
@ClassRule
37-
public static BuildWatcher buildWatcher = new BuildWatcher();
37+
@SuppressWarnings("unused")
38+
@RegisterExtension
39+
private static final BuildWatcherExtension BUILD_WATCHER = new BuildWatcherExtension();
40+
41+
protected JenkinsRule r;
3842

39-
@Rule
40-
public JenkinsRule r = new JenkinsRule();
43+
@TempDir
44+
protected File tmp;
4145

42-
@Rule
43-
public TemporaryFolder tmp = new TemporaryFolder();
46+
@BeforeEach
47+
protected void beforeEach(JenkinsRule rule) throws Exception {
48+
r = rule;
49+
}
4450

4551
/**
4652
* Configure the SVN workspace format - i.e. the format of the local workspace copy.
47-
*
53+
*
4854
* @param format one of the WC constants form SVNAdminAreaFactory or SubversionWorkspaceSelector.WC_FORMAT_17
4955
*/
5056
protected void configureSvnWorkspaceFormat(int format) throws Exception {
51-
StaplerRequest2 req = mock(StaplerRequest2.class);
52-
when(req.getParameter("svn.workspaceFormat")).thenReturn(""+format);
53-
54-
JSONObject formData = new JSONObject();
55-
56-
r.jenkins.getDescriptorByType(SubversionSCM.DescriptorImpl.class).configure(req, formData);
57+
StaplerRequest2 req = mock(StaplerRequest2.class);
58+
when(req.getParameter("svn.workspaceFormat")).thenReturn("" + format);
59+
60+
JSONObject formData = new JSONObject();
61+
62+
r.jenkins.getDescriptorByType(SubversionSCM.DescriptorImpl.class).configure(req, formData);
5763
}
5864

5965
protected void configureSvnWorkspaceFormat2(int format) throws Exception {
@@ -69,19 +75,19 @@ protected void configureSvnWorkspaceFormat2(int format) throws Exception {
6975
public static void checkForSvnServe() throws InterruptedException {
7076
LocalLauncher launcher = new LocalLauncher(StreamTaskListener.fromStdout());
7177
try {
72-
launcher.launch().cmds("svnserve","--help").start().join();
78+
launcher.launch().cmds("svnserve", "--help").start().join();
7379
} catch (IOException e) {
7480
// TODO better to add a docker-fixtures test dep so CI builds can run these tests
75-
throw new AssumptionViolatedException("svnserve apparently not installed", e);
81+
throw new TestAbortedException("svnserve apparently not installed", e);
7682
}
7783
}
7884

7985
public Proc runSvnServe(URL zip) throws Exception {
8086
return runSvnServe(tmp, zip);
8187
}
8288

83-
public static Proc runSvnServe(TemporaryFolder tmp, URL zip) throws Exception {
84-
File target = tmp.newFolder();
89+
public static Proc runSvnServe(File tmp, URL zip) throws Exception {
90+
File target = newFolder(tmp, "junit-" + System.currentTimeMillis());
8591
try (InputStream is = zip.openStream()) {
8692
new FilePath(target).unzipFrom(is);
8793
}
@@ -126,7 +132,7 @@ public static Proc runSvnServe(File repo) throws Exception {
126132
private static void waitForSvnServer(int port) throws InterruptedException {
127133
boolean serverReady = false;
128134
int retries = 0;
129-
while(serverReady == false && retries < 6){
135+
while (!serverReady && retries < 6) {
130136
try (Socket s = new Socket("localhost", port)) {
131137
// Server is up
132138
serverReady = true;
@@ -138,4 +144,13 @@ private static void waitForSvnServer(int port) throws InterruptedException {
138144
}
139145
}
140146
}
147+
148+
private static File newFolder(File root, String... subDirs) throws IOException {
149+
String subFolder = String.join("/", subDirs);
150+
File result = new File(root, subFolder);
151+
if (!result.mkdirs()) {
152+
throw new IOException("Couldn't create folders " + root);
153+
}
154+
return result;
155+
}
141156
}
Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,53 @@
11
package hudson.scm;
22

3-
import static java.util.regex.Pattern.compile;
43
import hudson.scm.SubversionSCM.SVNLogHandler;
54
import hudson.util.StreamTaskListener;
5+
import org.apache.commons.io.output.NullOutputStream;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
import org.jvnet.hudson.test.JenkinsRule;
9+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
610

711
import java.io.IOException;
812
import java.io.ObjectOutputStream;
913
import java.util.Collections;
1014
import java.util.regex.Pattern;
1115

12-
import org.junit.Rule;
13-
import org.junit.Test;
14-
import org.jvnet.hudson.test.JenkinsRule;
16+
import static java.util.regex.Pattern.compile;
1517

16-
import org.apache.commons.io.output.NullOutputStream;
18+
@WithJenkins
19+
class CompareAgainstBaselineCallableTest {
1720

18-
public class CompareAgainstBaselineCallableTest {
21+
private JenkinsRule r;
1922

20-
@Rule
21-
public JenkinsRule r = new JenkinsRule();
22-
2323
private CompareAgainstBaselineCallable callable;
2424

25+
@BeforeEach
26+
void beforeEach(JenkinsRule rule) {
27+
r = rule;
28+
}
29+
2530
@Test
26-
public void callableShouldBeRemotable() throws IOException {
31+
void callableShouldBeRemotable() throws IOException {
2732
givenACallable();
28-
33+
2934
// WHEN callable is serialized
30-
ObjectOutputStream oos = new ObjectOutputStream(new NullOutputStream());
35+
ObjectOutputStream oos = new ObjectOutputStream(NullOutputStream.INSTANCE);
3136
oos.writeObject(callable);
3237
// THEN no NotSerializableException should have been thrown
3338
oos.close();
3439
}
3540

3641
private void givenACallable() {
3742
@SuppressWarnings("unchecked")
38-
SVNLogFilter filter = new DefaultSVNLogFilter(new Pattern[] {compile("excludes")}, new Pattern[] {compile("includes")},
43+
SVNLogFilter filter = new DefaultSVNLogFilter(new Pattern[]{compile("excludes")}, new Pattern[]{compile("includes")},
3944
Collections.EMPTY_SET, "", new Pattern[0], false);
40-
45+
4146
StreamTaskListener taskListener = null; // this fails with NPE because of static Channel current(): StreamTaskListener.fromStdout();
4247
this.callable = new CompareAgainstBaselineCallable(
4348
new SVNRevisionState(null),
44-
new SVNLogHandler( filter, taskListener),
45-
"projectName", taskListener, null, Collections.emptyMap(), "nodeName");
49+
new SVNLogHandler(filter, taskListener),
50+
"projectName", taskListener, null, Collections.emptyMap(), "nodeName");
4651
}
4752

4853
}

src/test/java/hudson/scm/CredentialsExternalsTest.java

Lines changed: 60 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,49 +29,64 @@
2929
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
3030
import hudson.model.FreeStyleBuild;
3131
import hudson.model.FreeStyleProject;
32+
import jenkins.scm.impl.subversion.SubversionSampleRepoExtension;
33+
import org.junit.jupiter.api.BeforeEach;
34+
import org.junit.jupiter.api.Test;
35+
import org.junit.jupiter.api.extension.RegisterExtension;
36+
import org.jvnet.hudson.test.Issue;
37+
import org.jvnet.hudson.test.JenkinsRule;
38+
import org.jvnet.hudson.test.junit.jupiter.BuildWatcherExtension;
39+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
40+
3241
import java.net.ServerSocket;
3342
import java.util.Arrays;
3443
import java.util.Collections;
3544
import java.util.Set;
3645
import java.util.TreeSet;
37-
import jenkins.scm.impl.subversion.SubversionSampleRepoRule;
38-
import org.junit.ClassRule;
39-
import org.junit.Test;
40-
import static org.junit.Assert.*;
41-
import org.junit.Rule;
42-
import org.jvnet.hudson.test.BuildWatcher;
43-
import org.jvnet.hudson.test.Issue;
44-
import org.jvnet.hudson.test.JenkinsRule;
4546

46-
public class CredentialsExternalsTest {
47+
import static org.junit.jupiter.api.Assertions.assertEquals;
48+
49+
@WithJenkins
50+
class CredentialsExternalsTest {
4751

48-
@ClassRule
49-
public static BuildWatcher buildWatcher = new BuildWatcher();
52+
@SuppressWarnings("unused")
53+
@RegisterExtension
54+
private static final BuildWatcherExtension BUILD_WATCHER = new BuildWatcherExtension();
5055

51-
@Rule
52-
public JenkinsRule r = new JenkinsRule();
56+
private JenkinsRule r;
5357

54-
@Rule
55-
public SubversionSampleRepoRule main = new SubversionSampleRepoRule();
58+
@RegisterExtension
59+
private final SubversionSampleRepoExtension main = new SubversionSampleRepoExtension();
5660

57-
@Rule
58-
public SubversionSampleRepoRule ext = new SubversionSampleRepoRule();
61+
@RegisterExtension
62+
private final SubversionSampleRepoExtension ext = new SubversionSampleRepoExtension();
63+
64+
@BeforeEach
65+
void beforeEach(JenkinsRule rule) {
66+
r = rule;
67+
}
5968

6069
@Issue("JENKINS-32167")
6170
@Test
62-
public void smokes() throws Exception {
71+
void smokes() throws Exception {
6372
main.init();
6473
main.writeConf("svnserve.conf",
65-
"[general]\n" +
66-
"password-db = passwd\n" +
67-
"authz-db = authz\n" +
68-
"anon-access = none\n"); // https://bugzilla.redhat.com/show_bug.cgi?id=556712
74+
"""
75+
[general]
76+
password-db = passwd
77+
authz-db = authz
78+
anon-access = none
79+
"""); // https://bugzilla.redhat.com/show_bug.cgi?id=556712
6980
main.writeConf("passwd",
70-
"[users]\n" +
71-
"alice = alice\n");
81+
"""
82+
[users]
83+
alice = alice
84+
""");
7285
main.writeConf("authz",
73-
"[/]\n" +
74-
"alice = rw\n");
86+
"""
87+
[/]
88+
alice = rw
89+
""");
7590
// Adapted from AbstractSubversionTest.runSvnServe:
7691
int mainPort;
7792
ServerSocket serverSocket = new ServerSocket(0);
@@ -86,16 +101,22 @@ public void smokes() throws Exception {
86101
System.err.println("Running svnserve on <svn://localhost:" + mainPort + "> " + main.uuid());
87102
ext.init();
88103
ext.writeConf("svnserve.conf",
89-
"[general]\n" +
90-
"password-db = passwd\n" +
91-
"authz-db = authz\n" +
92-
"anon-access = none\n");
104+
"""
105+
[general]
106+
password-db = passwd
107+
authz-db = authz
108+
anon-access = none
109+
""");
93110
ext.writeConf("passwd",
94-
"[users]\n" +
95-
"bob = bob\n");
111+
"""
112+
[users]
113+
bob = bob
114+
""");
96115
ext.writeConf("authz",
97-
"[/]\n" +
98-
"bob = rw\n");
116+
"""
117+
[/]
118+
bob = rw
119+
""");
99120
int extPort;
100121
serverSocket = new ServerSocket(0);
101122
try {
@@ -111,12 +132,12 @@ public void smokes() throws Exception {
111132
main.svnkit("commit", "--message=externals", main.wc());
112133
FreeStyleProject p = r.createFreeStyleProject("p");
113134
SystemCredentialsProvider.getInstance().setDomainCredentialsMap(Collections.singletonMap(Domain.global(), Arrays.asList(
114-
new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "main-creds", null, "alice", "alice"),
115-
new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "ext-creds", null, "bob", "bob"))));
135+
new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "main-creds", null, "alice", "alice"),
136+
new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "ext-creds", null, "bob", "bob"))));
116137
p.setScm(new SubversionSCM(
117-
Collections.singletonList(new SubversionSCM.ModuleLocation("svn://localhost:" + mainPort + "/prj/trunk", "main-creds", ".", "", false)),
118-
null, null, null, null, null, null, null, false, false, // WTF was all that?
119-
Collections.singletonList(new SubversionSCM.AdditionalCredentials("<svn://localhost:" + extPort + "> " + ext.uuid(), "ext-creds"))));
138+
Collections.singletonList(new SubversionSCM.ModuleLocation("svn://localhost:" + mainPort + "/prj/trunk", "main-creds", ".", "", false)),
139+
null, null, null, null, null, null, null, false, false, // WTF was all that?
140+
Collections.singletonList(new SubversionSCM.AdditionalCredentials("<svn://localhost:" + extPort + "> " + ext.uuid(), "ext-creds"))));
120141
FreeStyleBuild b = r.buildAndAssertSuccess(p);
121142
assertEquals("", b.getWorkspace().child("file").readToString());
122143
assertEquals("", b.getWorkspace().child("ext/file").readToString());

0 commit comments

Comments
 (0)