From bf05275598d93a8aaeedb7271f26e7b42eb22c4f Mon Sep 17 00:00:00 2001
From: strangelookingnerd
<49242855+strangelookingnerd@users.noreply.github.com>
Date: Mon, 17 Nov 2025 16:14:46 +0100
Subject: [PATCH] Migrate tests to JUnit Jupiter
* Migrate annotations and imports
* Migrate assertions
* Remove public visibility for test classes and methods
* Minor code cleanup
* Ban JUnit4 imports
---
pom.xml | 1 +
.../hudson/model/DownloadServiceTest.java | 8 +--
.../hudson/tools/JDKInstallerCascTest.java | 15 ++--
.../java/hudson/tools/JDKInstallerTest.java | 72 ++++++++++---------
4 files changed, 51 insertions(+), 45 deletions(-)
diff --git a/pom.xml b/pom.xml
index 0678ed8..1388165 100644
--- a/pom.xml
+++ b/pom.xml
@@ -16,6 +16,7 @@
2.479
${jenkins.baseline}.3
jenkinsci/${project.artifactId}-plugin
+ false
Oracle Java SE Development Kit Installer Plugin
https://github.com/jenkinsci/${project.artifactId}-plugin
diff --git a/src/test/java/hudson/model/DownloadServiceTest.java b/src/test/java/hudson/model/DownloadServiceTest.java
index ab3df9f..d0574c8 100644
--- a/src/test/java/hudson/model/DownloadServiceTest.java
+++ b/src/test/java/hudson/model/DownloadServiceTest.java
@@ -6,17 +6,17 @@
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONObject;
-import org.junit.Test;
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import org.junit.jupiter.api.Test;
/**
* @author Kohsuke Kawaguchi
*/
-public class DownloadServiceTest {
+class DownloadServiceTest {
@Test
- public void testReduceFunctionWithJDKJsons() throws Exception {
+ void testReduceFunctionWithJDKJsons() throws Exception {
URL resource1 = DownloadServiceTest.class.getResource("hudson.tools.JDKInstaller1.json");
URL resource2 = DownloadServiceTest.class.getResource("hudson.tools.JDKInstaller2.json");
URL resource3 = DownloadServiceTest.class.getResource("hudson.tools.JDKInstaller3.json");
diff --git a/src/test/java/hudson/tools/JDKInstallerCascTest.java b/src/test/java/hudson/tools/JDKInstallerCascTest.java
index 5850b46..48956f6 100644
--- a/src/test/java/hudson/tools/JDKInstallerCascTest.java
+++ b/src/test/java/hudson/tools/JDKInstallerCascTest.java
@@ -4,19 +4,18 @@
import hudson.model.JDK;
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
-import org.junit.Rule;
-import org.junit.Test;
+import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
+import org.junit.jupiter.api.Test;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
-public class JDKInstallerCascTest {
- @Rule
- public JenkinsConfiguredWithCodeRule r = new JenkinsConfiguredWithCodeRule();
+@WithJenkinsConfiguredWithCode
+class JDKInstallerCascTest {
@ConfiguredWithCode("casc.yaml")
@Test
- public void configuredByCasC() {
+ void configuredByCasC(JenkinsConfiguredWithCodeRule r) {
final JDK.DescriptorImpl descriptor = ExtensionList.lookupSingleton(JDK.DescriptorImpl.class);
assertEquals(1, descriptor.getInstallations().length);
diff --git a/src/test/java/hudson/tools/JDKInstallerTest.java b/src/test/java/hudson/tools/JDKInstallerTest.java
index bc5675a..a6011d8 100644
--- a/src/test/java/hudson/tools/JDKInstallerTest.java
+++ b/src/test/java/hudson/tools/JDKInstallerTest.java
@@ -1,7 +1,8 @@
package hudson.tools;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assumptions.assumeFalse;
import org.htmlunit.html.HtmlForm;
import org.htmlunit.html.HtmlFormUtil;
@@ -10,10 +11,10 @@
import java.io.InputStream;
import java.nio.file.Files;
-import org.junit.Assume;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
import hudson.model.JDK;
import hudson.model.TaskListener;
import hudson.util.StreamTaskListener;
@@ -23,37 +24,41 @@
import hudson.Launcher.LocalLauncher;
import java.io.File;
-import java.util.Arrays;
+import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;
-import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.JenkinsRule;
+import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
/**
* @author Kohsuke Kawaguchi
*/
-public class JDKInstallerTest {
+@WithJenkins
+class JDKInstallerTest {
+
+ private static final Logger LOGGER = Logger.getLogger(JDKInstallerTest.class.getName());
- @Rule
- public JenkinsRule j = new JenkinsRule();
+ private JenkinsRule j;
- @Rule
- public TemporaryFolder tmp = new TemporaryFolder();
+ @TempDir
+ private File tmp;
+
+ @BeforeEach
+ void beforeEach(JenkinsRule rule) throws Exception {
+ j = rule;
- @Before
- public void setUp() throws Exception {
File f = new File(new File(System.getProperty("user.home")),".jenkins-ci.org");
if (!f.exists()) {
- LOGGER.warning(f+" doesn't exist. Skipping JDK installation tests");
+ LOGGER.warning(f + " doesn't exist. Skipping JDK installation tests");
} else {
Properties prop = new Properties();
try (InputStream in = Files.newInputStream(f.toPath())) {
prop.load(in);
String u = prop.getProperty("oracle.userName");
String p = prop.getProperty("oracle.password");
- if (u==null || p==null) {
- LOGGER.warning(f+" doesn't contain oracle.userName and oracle.password. Skipping JDK installation tests.");
+ if (u == null || p == null) {
+ LOGGER.warning(f + " doesn't contain oracle.userName and oracle.password. Skipping JDK installation tests.");
} else {
DescriptorImpl d = j.jenkins.getDescriptorByType(DescriptorImpl.class);
d.doPostCredential(u,p);
@@ -63,7 +68,7 @@ public void setUp() throws Exception {
}
@Test
- public void enterCredential() throws Exception {
+ void enterCredential() throws Exception {
HtmlPage p = j.createWebClient().goTo("descriptorByName/hudson.tools.JDKInstaller/enterCredential");
HtmlForm form = p.getFormByName("postCredential");
form.getInputByName("username").setValue("foo");
@@ -79,17 +84,17 @@ public void enterCredential() throws Exception {
* Tests the configuration round trip.
*/
@Test
- public void configRoundtrip() throws Exception {
+ void configRoundtrip() throws Exception {
JDKInstaller installer = new JDKInstaller("jdk-6u13-oth-JPR@CDS-CDS_Developer", true);
- j.jenkins.getJDKs().add(new JDK("test",tmp.getRoot().getAbsolutePath(), Arrays.asList(
- new InstallSourceProperty(Arrays.asList(installer)))));
+ j.jenkins.getJDKs().add(new JDK("test", tmp.getAbsolutePath(), List.of(
+ new InstallSourceProperty(List.of(installer)))));
j.submit(j.createWebClient().goTo("configureTools").getFormByName("config"));
JDK jdk = j.jenkins.getJDK("test");
InstallSourceProperty isp = jdk.getProperties().get(InstallSourceProperty.class);
- assertEquals(1,isp.installers.size());
+ assertEquals(1, isp.installers.size());
j.assertEqualBeans(installer, isp.installers.get(JDKInstaller.class), "id,acceptLicense");
}
@@ -97,25 +102,26 @@ public void configRoundtrip() throws Exception {
* Fake installation on Unix.
*/
@Test
- public void fakeUnixInstall() throws Exception {
- Assume.assumeFalse("If we're on Windows, don't bother doing this", Functions.isWindows());
+ void fakeUnixInstall() throws Exception {
+ assumeFalse(Functions.isWindows(), "If we're on Windows, don't bother doing this");
File bundle = File.createTempFile("fake-jdk-by-hudson","sh");
try {
new FilePath(bundle).write(
- "#!/bin/bash -ex\n" +
- "mkdir -p jdk1.6.0_dummy/bin\n" +
- "touch jdk1.6.0_dummy/bin/java","ASCII");
+ """
+ #!/bin/bash -ex
+ mkdir -p jdk1.6.0_dummy/bin
+ touch jdk1.6.0_dummy/bin/java
+ """,
+ "ASCII");
TaskListener l = StreamTaskListener.fromStdout();
- new JDKInstaller("",true).install(new LocalLauncher(l),Platform.LINUX,
- new JDKInstaller.FilePathFileSystem(j.jenkins),l,tmp.getRoot().getPath(),bundle.getPath());
+ new JDKInstaller("",true).install(new LocalLauncher(l), Platform.LINUX,
+ new JDKInstaller.FilePathFileSystem(j.jenkins), l, tmp.getPath(), bundle.getPath());
- assertTrue(new File(tmp.getRoot(),"bin/java").exists());
+ assertTrue(new File(tmp,"bin/java").exists());
} finally {
bundle.delete();
}
}
-
- private static final Logger LOGGER = Logger.getLogger(JDKInstallerTest.class.getName());
}