Skip to content

Commit bc2746b

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 cb57930 commit bc2746b

File tree

60 files changed

+833
-981
lines changed

Some content is hidden

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

60 files changed

+833
-981
lines changed

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
<dependency>
8282
<groupId>io.jenkins.tools.bom</groupId>
8383
<artifactId>bom-${jenkins.baseline}.x</artifactId>
84-
<version>3613.v584fca_12cf5c</version>
84+
<version>4136.vca_c3202a_7fd1</version>
8585
<type>pom</type>
8686
<scope>import</scope>
8787
</dependency>
@@ -197,7 +197,7 @@
197197
</dependency>
198198
<dependency>
199199
<groupId>org.mockito</groupId>
200-
<artifactId>mockito-core</artifactId>
200+
<artifactId>mockito-junit-jupiter</artifactId>
201201
<scope>test</scope>
202202
</dependency>
203203
<dependency>

src/test/java/com/cloudbees/jenkins/support/BundleFileNameTest.java

+10-12
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,26 @@
99
import java.time.Clock;
1010
import java.time.Instant;
1111
import java.time.ZoneOffset;
12-
import org.junit.Ignore;
13-
import org.junit.Rule;
14-
import org.junit.Test;
12+
import org.junit.jupiter.api.Disabled;
13+
import org.junit.jupiter.api.Test;
1514
import org.jvnet.hudson.test.JenkinsRule;
1615
import org.jvnet.hudson.test.TestExtension;
16+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
1717
import org.jvnet.localizer.Localizable;
1818

19-
public class BundleFileNameTest {
19+
@WithJenkins
20+
class BundleFileNameTest {
2021

2122
private static final Clock TEST_CLOCK = Clock.fixed(Instant.parse("2020-10-01T10:20:30Z"), ZoneOffset.UTC);
2223

23-
@Rule
24-
public JenkinsRule j = new JenkinsRule();
25-
2624
@Test
27-
public void testGenerate_Default() {
25+
void testGenerate_Default(JenkinsRule j) {
2826
assertThat(BundleFileName.generate(TEST_CLOCK, null), equalTo("support_2020-10-01_10.20.30.zip"));
2927
}
3028

31-
@Ignore("Relies on SupportPlugin object which is not instantiated by TestPluginManager")
29+
@Disabled("Relies on SupportPlugin object which is not instantiated by TestPluginManager")
3230
@Test
33-
public void testGenerate_WithSupportProvider() {
31+
void testGenerate_WithSupportProvider(JenkinsRule j) {
3432
assertThat(BundleFileName.generate(TEST_CLOCK, null), equalTo("amazing-support_2020-10-01_10.20.30.zip"));
3533
}
3634

@@ -61,13 +59,13 @@ public void printAboutJenkins(PrintWriter out) {}
6159
}
6260

6361
@Test
64-
public void testGenerate_WithQualifier() {
62+
void testGenerate_WithQualifier(JenkinsRule j) {
6563
assertThat(
6664
BundleFileName.generate(TEST_CLOCK, "qualifier"), equalTo("support_qualifier_2020-10-01_10.20.30.zip"));
6765
}
6866

6967
@Test
70-
public void testGenerate_WithQualifierAndInstanceType() {
68+
void testGenerate_WithQualifierAndInstanceType(JenkinsRule j) {
7169
assertThat(
7270
BundleFileName.generate(TEST_CLOCK, "qualifier"),
7371
equalTo("support_qualifier_instance_type_2020-10-01_10.20.30.zip"));

src/test/java/com/cloudbees/jenkins/support/BundleNamePrefixTest.java

+8-10
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,33 @@
66
import edu.umd.cs.findbugs.annotations.NonNull;
77
import java.text.SimpleDateFormat;
88
import java.util.Date;
9-
import org.junit.Rule;
10-
import org.junit.Test;
9+
import org.junit.jupiter.api.Test;
1110
import org.jvnet.hudson.test.JenkinsRule;
1211
import org.jvnet.hudson.test.TestExtension;
12+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
1313

14-
public class BundleNamePrefixTest {
14+
@WithJenkins
15+
class BundleNamePrefixTest {
1516

1617
private static final String CURRENT_YEAR = new SimpleDateFormat("yyyy").format(new Date());
1718

18-
@Rule
19-
public JenkinsRule j = new JenkinsRule();
20-
2119
@Test
22-
public void checkOriginalBehaviour() {
20+
void checkOriginalBehaviour(JenkinsRule j) {
2321
assertThat(BundleFileName.generate(), startsWith("support_" + CURRENT_YEAR));
2422
}
2523

2624
@Test
27-
public void checkWithOneProvider() {
25+
void checkWithOneProvider(JenkinsRule j) {
2826
assertThat(BundleFileName.generate(), startsWith("support_pouet_" + CURRENT_YEAR));
2927
}
3028

3129
@Test
32-
public void tooManyProviders() {
30+
void tooManyProviders(JenkinsRule j) {
3331
assertThat(BundleFileName.generate(), startsWith("support_Zis_" + CURRENT_YEAR));
3432
}
3533

3634
@Test
37-
public void withSysProp() {
35+
void withSysProp(JenkinsRule j) {
3836
System.setProperty(BundleNameInstanceTypeProvider.SUPPORT_BUNDLE_NAMING_INSTANCE_SPEC_PROPERTY, "paf");
3937
assertThat(BundleFileName.generate(), startsWith("support_paf_" + CURRENT_YEAR));
4038
System.getProperties().remove(BundleNameInstanceTypeProvider.SUPPORT_BUNDLE_NAMING_INSTANCE_SPEC_PROPERTY);

src/test/java/com/cloudbees/jenkins/support/CasCTest.java

+10-12
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,27 @@
33
import static org.hamcrest.CoreMatchers.is;
44
import static org.hamcrest.MatcherAssert.assertThat;
55
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
6-
import static org.junit.Assert.assertTrue;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
77

88
import com.cloudbees.jenkins.support.config.SupportAutomatedBundleConfiguration;
99
import com.cloudbees.jenkins.support.filter.ContentFilters;
1010
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
1111
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
12-
import org.junit.Rule;
13-
import org.junit.Test;
12+
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
13+
import org.junit.jupiter.api.Test;
1414

15-
public class CasCTest {
16-
17-
@Rule
18-
public JenkinsConfiguredWithCodeRule r = new JenkinsConfiguredWithCodeRule();
15+
@WithJenkinsConfiguredWithCode
16+
class CasCTest {
1917

2018
@Test
2119
@ConfiguredWithCode("configuration-as-code.yaml")
22-
public void assertConfiguredAsExpected() {
20+
void assertConfiguredAsExpected(JenkinsConfiguredWithCodeRule r) {
2321
assertTrue(
24-
"JCasC should have configured support core to anonymize contents, but it didn't",
25-
ContentFilters.get().isEnabled());
22+
ContentFilters.get().isEnabled(),
23+
"JCasC should have configured support core to anonymize contents, but it didn't");
2624
assertTrue(
27-
"JCasC should have configured support period bundle generation enabled, but it didn't",
28-
SupportAutomatedBundleConfiguration.get().isEnabled());
25+
SupportAutomatedBundleConfiguration.get().isEnabled(),
26+
"JCasC should have configured support period bundle generation enabled, but it didn't");
2927
assertThat(
3028
"JCasC should have configured support period bundle generation period, but it didn't",
3129
SupportAutomatedBundleConfiguration.get().getPeriod(),

src/test/java/com/cloudbees/jenkins/support/CheckFilterTest.java

+29-36
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import static org.hamcrest.MatcherAssert.assertThat;
44
import static org.hamcrest.Matchers.empty;
5-
import static org.junit.Assert.fail;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
import static org.junit.jupiter.api.Assertions.fail;
67

78
import com.cloudbees.jenkins.support.api.Component;
89
import com.cloudbees.jenkins.support.configfiles.AgentsConfigFile;
@@ -38,7 +39,6 @@
3839
import java.net.InetAddress;
3940
import java.net.NetworkInterface;
4041
import java.net.SocketException;
41-
import java.net.UnknownHostException;
4242
import java.nio.charset.StandardCharsets;
4343
import java.nio.file.Files;
4444
import java.util.Arrays;
@@ -55,36 +55,29 @@
5555
import java.util.zip.ZipEntry;
5656
import java.util.zip.ZipFile;
5757
import jenkins.model.Jenkins;
58-
import org.junit.Assert;
59-
import org.junit.Rule;
60-
import org.junit.Test;
61-
import org.junit.rules.TemporaryFolder;
58+
import org.junit.jupiter.api.Test;
59+
import org.junit.jupiter.api.io.TempDir;
6260
import org.jvnet.hudson.test.JenkinsRule;
63-
import org.jvnet.hudson.test.LoggerRule;
61+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
6462

65-
public class CheckFilterTest {
63+
@WithJenkins
64+
class CheckFilterTest {
6665
private static final Logger LOGGER = Logger.getLogger(CheckFilterTest.class.getName());
6766

6867
private static final String JOB_NAME = "thejob";
6968
private static final String AGENT_NAME = "agent0"; // it's the name used by createOnlineSlave
7069
private static final String VIEW_ALL_NEW_NAME = "all-view";
7170
private static final String ENV_VAR = getFirstEnvVar();
7271

73-
@Rule
74-
public TemporaryFolder temp = new TemporaryFolder();
75-
76-
@Rule
77-
public JenkinsRule j = new JenkinsRule();
78-
79-
@Rule
80-
public LoggerRule logging = new LoggerRule().record(AsyncResultCache.class, Level.FINER);
72+
@TempDir
73+
private File temp;
8174

8275
@Test
83-
public void checkFilterTest() throws Exception {
76+
void checkFilterTest(JenkinsRule j) throws Exception {
8477
// Create the files to check
8578
FileChecker checker = new FileChecker(j.jenkins);
8679
// Create the objects needed for some contents to be included
87-
QueueTaskFuture<FreeStyleBuild> build = createObjectsWithNames();
80+
QueueTaskFuture<FreeStyleBuild> build = createObjectsWithNames(j);
8881

8982
// Reload the mappings, after the objects were created
9083
ContentFilters.get().setEnabled(true);
@@ -123,7 +116,7 @@ public void checkFilterTest() throws Exception {
123116
j.waitUntilNoActivity();
124117
}
125118

126-
private QueueTaskFuture<FreeStyleBuild> createObjectsWithNames() throws Exception {
119+
private static QueueTaskFuture<FreeStyleBuild> createObjectsWithNames(JenkinsRule j) throws Exception {
127120
// For an environment variable
128121
if (ENV_VAR != null) {
129122
User.getOrCreateByIdOrFullName(ENV_VAR);
@@ -157,7 +150,7 @@ private QueueTaskFuture<FreeStyleBuild> createObjectsWithNames() throws Exceptio
157150

158151
private void assertComponent(List<Class<? extends Component>> componentClasses, FileChecker checker)
159152
throws IOException {
160-
File fileZip = new File(temp.getRoot(), "filteredBundle.zip");
153+
File fileZip = new File(temp, "filteredBundle.zip");
161154
Files.deleteIfExists(fileZip.toPath());
162155

163156
try (FileOutputStream zipOutputStream = new FileOutputStream(fileZip)) {
@@ -191,7 +184,7 @@ private void assertComponent(List<Class<? extends Component>> componentClasses,
191184
}
192185
}
193186

194-
private String getContentFromEntry(ZipFile zip, ZipEntry entry) throws IOException {
187+
private static String getContentFromEntry(ZipFile zip, ZipEntry entry) throws IOException {
195188
if (entry.getSize() == 0) {
196189
return "";
197190
}
@@ -209,7 +202,7 @@ private String getContentFromEntry(ZipFile zip, ZipEntry entry) throws IOExcepti
209202
out.write(data, 0, currentByte);
210203
}
211204
out.flush();
212-
content = new String(out.toByteArray(), StandardCharsets.UTF_8);
205+
content = out.toString(StandardCharsets.UTF_8);
213206
}
214207
return content;
215208
}
@@ -228,11 +221,11 @@ private static String getFirstEnvVar() {
228221
}
229222

230223
private static class FileChecker {
231-
private Set<FileToCheck> fileSet = new HashSet<>();
232-
private Set<FileToCheck> unchecked = new HashSet<>();
233-
private Set<String> words = new HashSet<>();
224+
private final Set<FileToCheck> fileSet = new HashSet<>();
225+
private final Set<FileToCheck> unchecked = new HashSet<>();
226+
private final Set<String> words = new HashSet<>();
234227

235-
private FileChecker(Jenkins jenkins) throws UnknownHostException {
228+
private FileChecker(Jenkins jenkins) {
236229
fileSet.add(of("manifest.md", "about", false));
237230
fileSet.add(of("about.md", "b", false));
238231
// fileSet.add(of("items.md", "jobs", false));
@@ -347,22 +340,22 @@ private FileChecker(Jenkins jenkins) throws UnknownHostException {
347340
unchecked.addAll(fileSet);
348341
}
349342

350-
private String getUpdateCenterURL(Jenkins jenkins) {
343+
private static String getUpdateCenterURL(Jenkins jenkins) {
351344
if (jenkins.getUpdateCenter().getSiteList() != null
352345
&& jenkins.getUpdateCenter().getSiteList().get(0) != null) {
353346
return jenkins.getUpdateCenter().getSiteList().get(0).getUrl();
354347
}
355348
return null;
356349
}
357350

358-
private String getInetAddress() {
351+
private static String getInetAddress() {
359352
try {
360353
Enumeration<NetworkInterface> networkInterfaces = null;
361354
networkInterfaces = NetworkInterface.getNetworkInterfaces();
362355
if (networkInterfaces.hasMoreElements()) {
363356
NetworkInterface ni = networkInterfaces.nextElement();
364357
Enumeration<InetAddress> inetAddresses = ni.getInetAddresses();
365-
if (inetAddresses != null && inetAddresses.hasMoreElements()) {
358+
if (inetAddresses.hasMoreElements()) {
366359
return inetAddresses.nextElement().toString();
367360
}
368361
}
@@ -409,15 +402,15 @@ private void check(String file, String content) {
409402
if (content == null) {
410403
fail(String.format("Error checking the file %s because its content was null", file));
411404
} else {
412-
Assert.assertTrue(
405+
assertTrue(
406+
content.toLowerCase(Locale.ENGLISH)
407+
.contains(value.wordFiltered.toLowerCase(Locale.ENGLISH)),
413408
String.format(
414409
"The file '%s' should have the word '%s'. File content:\n\n----------\n%s\n%s----------\n\n",
415410
file,
416411
value.wordFiltered,
417412
content.substring(0, Math.min(MAX_CONTENT_LENGTH, content.length())),
418-
content.length() > MAX_CONTENT_LENGTH ? "...\n(content cut off)\n" : ""),
419-
content.toLowerCase(Locale.ENGLISH)
420-
.contains(value.wordFiltered.toLowerCase(Locale.ENGLISH)));
413+
content.length() > MAX_CONTENT_LENGTH ? "...\n(content cut off)\n" : ""));
421414
}
422415
return;
423416
}
@@ -427,9 +420,9 @@ private void check(String file, String content) {
427420
}
428421

429422
private static class FileToCheck {
430-
private String filePattern;
431-
private String word;
432-
private boolean fileIsFiltered;
423+
private final String filePattern;
424+
private final String word;
425+
private final boolean fileIsFiltered;
433426
private String wordFiltered;
434427

435428
private FileToCheck(String filePattern, String word, boolean fileIsFiltered) {

0 commit comments

Comments
 (0)