Skip to content

Commit 7191ee0

Browse files
Migrate tests to JUnit5 (test / jenkins / 3)
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup
1 parent b62d8dc commit 7191ee0

File tree

121 files changed

+2505
-1914
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+2505
-1914
lines changed

src/checkstyle/checkstyle-configuration.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<property name="max" value="240"/>
1111
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
1212
</module>
13+
<module name="SuppressWarningsFilter" />
1314
<module name="TreeWalker">
1415
<!--
1516
Annotations: https://checkstyle.sourceforge.io/config_annotation.html
@@ -38,6 +39,7 @@
3839
<module name="StringLiteralEquality"/>
3940
<module name="SuperClone"/>
4041
<module name="SuperFinalize"/>
42+
<module name="SuppressWarningsHolder" />
4143
<module name="UnnecessarySemicolonAfterOuterTypeDeclaration"/>
4244
<module name="UnnecessarySemicolonAfterTypeMemberDeclaration"/>
4345
<module name="UnnecessarySemicolonInEnumeration"/>

test/src/test/java/jenkins/ClassPathTest.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,15 @@
3333
import java.util.TreeMap;
3434
import java.util.jar.JarEntry;
3535
import java.util.jar.JarFile;
36-
import org.junit.Rule;
37-
import org.junit.Test;
38-
import org.junit.rules.ErrorCollector;
36+
import org.junit.jupiter.api.AfterEach;
37+
import org.junit.jupiter.api.Test;
3938
import org.jvnet.hudson.test.Issue;
4039
import org.jvnet.hudson.test.WarExploder;
40+
import org.opentest4j.MultipleFailuresError;
4141

42-
public class ClassPathTest {
42+
class ClassPathTest {
4343

44-
@Rule
45-
public ErrorCollector errors = new ErrorCollector();
44+
private final List<Throwable> errors = new ArrayList<>();
4645

4746
private static final Set<String> KNOWN_VIOLATIONS = Set.of(
4847
// TODO duplicated in [jline-2.14.6.jar, jansi-1.11.jar]
@@ -75,9 +74,16 @@ public class ClassPathTest {
7574
"org/fusesource/jansi/internal/WindowsSupport.class",
7675
"org/fusesource/jansi/WindowsAnsiOutputStream.class");
7776

77+
@AfterEach
78+
void tearDown() {
79+
if (!errors.isEmpty()) {
80+
throw new MultipleFailuresError(null, errors);
81+
}
82+
}
83+
7884
@Issue("JENKINS-46754")
7985
@Test
80-
public void uniqueness() throws Exception {
86+
void uniqueness() throws Exception {
8187
Map<String, List<String>> entries = new TreeMap<>();
8288
for (File jar : new File(WarExploder.getExplodedDir(), "WEB-INF/lib").listFiles((dir, name) -> name.endsWith(".jar"))) {
8389
String jarname = jar.getName();
@@ -93,7 +99,7 @@ public void uniqueness() throws Exception {
9399
}
94100
entries.forEach((name, jarnames) -> {
95101
if (jarnames.size() > 1 && !KNOWN_VIOLATIONS.contains(name)) { // Matchers.hasSize unfortunately does not display the collection
96-
errors.addError(new AssertionError(name + " duplicated in " + jarnames));
102+
errors.add(new AssertionError(name + " duplicated in " + jarnames));
97103
}
98104
});
99105
}

test/src/test/java/jenkins/CoreAutomaticTestBuilder.java

Lines changed: 0 additions & 54 deletions
This file was deleted.

test/src/test/java/jenkins/ExtensionFilterTest.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,33 @@
22

33
import static org.hamcrest.MatcherAssert.assertThat;
44
import static org.hamcrest.Matchers.hasSize;
5-
import static org.junit.Assert.assertTrue;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
66

77
import hudson.ExtensionComponent;
88
import hudson.console.ConsoleAnnotatorFactory;
99
import hudson.model.PageDecorator;
1010
import jenkins.install.SetupWizard;
11-
import org.junit.Rule;
12-
import org.junit.Test;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
1313
import org.jvnet.hudson.test.JenkinsRule;
1414
import org.jvnet.hudson.test.TestExtension;
15+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
1516

1617
/**
1718
* @author Kohsuke Kawaguchi
1819
*/
19-
public class ExtensionFilterTest {
20+
@WithJenkins
21+
class ExtensionFilterTest {
2022

21-
@Rule
22-
public JenkinsRule j = new JenkinsRule();
23+
private JenkinsRule j;
24+
25+
@BeforeEach
26+
void setUp(JenkinsRule rule) {
27+
j = rule;
28+
}
2329

2430
@Test
25-
public void filter() {
31+
void filter() {
2632
assertThat(PageDecorator.all(), hasSize(1));
2733
assertTrue(ConsoleAnnotatorFactory.all().isEmpty());
2834
}

test/src/test/java/jenkins/I18nTest.java

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,88 +26,97 @@
2626

2727
import static org.hamcrest.MatcherAssert.assertThat;
2828
import static org.hamcrest.Matchers.startsWith;
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
2930

3031
import java.io.IOException;
3132
import net.sf.json.JSONObject;
32-
import org.junit.Assert;
33-
import org.junit.Rule;
34-
import org.junit.Test;
33+
import org.junit.jupiter.api.BeforeEach;
34+
import org.junit.jupiter.api.Test;
3535
import org.jvnet.hudson.test.Issue;
3636
import org.jvnet.hudson.test.JenkinsRule;
37-
import org.xml.sax.SAXException;
37+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
3838

3939
/**
4040
* @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
4141
*/
42-
public class I18nTest {
42+
@WithJenkins
43+
class I18nTest {
4344

44-
@Rule
45-
public JenkinsRule jenkinsRule = new JenkinsRule();
45+
private JenkinsRule jenkinsRule;
46+
47+
@BeforeEach
48+
void setUp(JenkinsRule rule) {
49+
jenkinsRule = rule;
50+
}
4651

4752
@Test
48-
public void test_baseName_unspecified() throws IOException, SAXException {
53+
void test_baseName_unspecified() throws IOException {
4954
JSONObject response = jenkinsRule.getJSON("i18n/resourceBundle").getJSONObject();
50-
Assert.assertEquals("error", response.getString("status"));
51-
Assert.assertEquals("Mandatory parameter 'baseName' not specified.", response.getString("message"));
55+
assertEquals("error", response.getString("status"));
56+
assertEquals("Mandatory parameter 'baseName' not specified.", response.getString("message"));
5257
}
5358

5459
@Test
55-
public void test_baseName_unknown() throws IOException, SAXException {
60+
void test_baseName_unknown() throws IOException {
5661
JSONObject response = jenkinsRule.getJSON("i18n/resourceBundle?baseName=com.acme.XyzWhatever").getJSONObject();
57-
Assert.assertEquals("error", response.getString("status"));
62+
assertEquals("error", response.getString("status"));
5863
assertThat(response.getString("message"), startsWith("Can't find bundle for base name com.acme.XyzWhatever"));
5964
}
6065

6166
@Issue("JENKINS-35270")
6267
@Test
63-
public void test_baseName_plugin() throws Exception {
68+
void test_baseName_plugin() throws Exception {
6469
JSONObject response = jenkinsRule.getJSON("i18n/resourceBundle?baseName=org.jenkinsci.plugins.matrixauth.Messages").getJSONObject();
65-
Assert.assertEquals(response.toString(), "ok", response.getString("status"));
70+
assertEquals("ok", response.getString("status"), response.toString());
6671
JSONObject data = response.getJSONObject("data");
67-
Assert.assertEquals("Matrix-based security", data.getString("GlobalMatrixAuthorizationStrategy.DisplayName"));
72+
assertEquals("Matrix-based security", data.getString("GlobalMatrixAuthorizationStrategy.DisplayName"));
6873
}
6974

7075
@Test
71-
public void test_valid() throws IOException, SAXException {
76+
void test_valid() throws IOException {
7277
JSONObject response = jenkinsRule.getJSON("i18n/resourceBundle?baseName=hudson.logging.Messages&language=de").getJSONObject();
73-
Assert.assertEquals("ok", response.getString("status"));
78+
assertEquals("ok", response.getString("status"));
7479
JSONObject data = response.getJSONObject("data");
75-
Assert.assertEquals("Initialisiere Log-Rekorder", data.getString("LogRecorderManager.init"));
80+
assertEquals("Initialisiere Log-Rekorder", data.getString("LogRecorderManager.init"));
7681
}
7782

83+
// variant testing
7884
@Issue("JENKINS-39034")
79-
@Test // variant testing
80-
public void test_valid_region_variant() throws IOException, SAXException {
85+
@Test
86+
void test_valid_region_variant() throws IOException {
8187
JSONObject response = jenkinsRule.getJSON("i18n/resourceBundle?baseName=jenkins.i18n.Messages&language=en_AU_variant").getJSONObject();
82-
Assert.assertEquals("ok", response.getString("status"));
88+
assertEquals("ok", response.getString("status"));
8389
JSONObject data = response.getJSONObject("data");
84-
Assert.assertEquals("value_au_variant", data.getString("Key"));
90+
assertEquals("value_au_variant", data.getString("Key"));
8591
}
8692

93+
//country testing with delimiter '-' instead of '_'
8794
@Issue("JENKINS-39034")
88-
@Test //country testing with delimiter '-' instead of '_'
89-
public void test_valid_region() throws IOException, SAXException {
95+
@Test
96+
void test_valid_region() throws IOException {
9097
JSONObject response = jenkinsRule.getJSON("i18n/resourceBundle?baseName=jenkins.i18n.Messages&language=en-AU").getJSONObject();
91-
Assert.assertEquals("ok", response.getString("status"));
98+
assertEquals("ok", response.getString("status"));
9299
JSONObject data = response.getJSONObject("data");
93-
Assert.assertEquals("value_au", data.getString("Key"));
100+
assertEquals("value_au", data.getString("Key"));
94101
}
95102

103+
//fallthrough to default language if variant does not exit
96104
@Issue("JENKINS-39034")
97-
@Test //fallthrough to default language if variant does not exit
98-
public void test_valid_fallback() throws IOException, SAXException {
105+
@Test
106+
void test_valid_fallback() throws IOException {
99107
JSONObject response = jenkinsRule.getJSON("i18n/resourceBundle?baseName=jenkins.i18n.Messages&language=en_NZ_variant").getJSONObject();
100-
Assert.assertEquals("ok", response.getString("status"));
108+
assertEquals("ok", response.getString("status"));
101109
JSONObject data = response.getJSONObject("data");
102-
Assert.assertEquals("value", data.getString("Key"));
110+
assertEquals("value", data.getString("Key"));
103111
}
104112

105-
@Test // testing with unknown language falls through to default language
106-
public void test_unsupported_language() throws IOException, SAXException {
113+
// testing with unknown language falls through to default language
114+
@Test
115+
void test_unsupported_language() throws IOException {
107116
JSONObject response = jenkinsRule.getJSON("i18n/resourceBundle?baseName=jenkins.i18n.Messages&language=xyz").getJSONObject();
108-
Assert.assertEquals("ok", response.getString("status"));
117+
assertEquals("ok", response.getString("status"));
109118
JSONObject data = response.getJSONObject("data");
110-
Assert.assertEquals("value", data.getString("Key"));
119+
assertEquals("value", data.getString("Key"));
111120
}
112121

113122
}

test/src/test/java/jenkins/bugs/BridgeMethodsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
import java.util.List;
1515
import java.util.concurrent.Future;
1616
import java.util.stream.Collectors;
17-
import org.junit.Test;
17+
import org.junit.jupiter.api.Test;
1818
import org.jvnet.hudson.test.Issue;
1919

20-
public class BridgeMethodsTest {
20+
class BridgeMethodsTest {
2121

2222
@Test
2323
@Issue("JENKINS-65605")
24-
public void checkBridgeMethod() {
24+
void checkBridgeMethod() {
2525
/*
2626
* we should have 2 methods getFuture() in hudson.model.Queue$WaitingItem but with different return types :
2727
* hudson.model.Queue$WaitingItem.getFuture()Ljava/util/concurrent/Future

0 commit comments

Comments
 (0)