From 2022dc2db3ab717499610e5c5df8c8fd6caf5bd7 Mon Sep 17 00:00:00 2001 From: strangelookingnerd <49242855+strangelookingnerd@users.noreply.github.com> Date: Mon, 24 Mar 2025 15:58:11 +0100 Subject: [PATCH] Migrate tests to JUnit5 * Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup --- .../displayurlapi/DisplayURLProviderTest.java | 52 ++++++++++++------- .../JenkinsRuleWithLocalPort.java | 12 ----- .../actions/AbstractActionRedirectTest.java | 18 ++++--- .../actions/ActionRedirectClassicTest.java | 32 +++++++----- .../actions/ActionRedirectExtendedTest.java | 33 +++++++----- 5 files changed, 79 insertions(+), 68 deletions(-) delete mode 100644 src/test/java/org/jenkinsci/plugins/displayurlapi/JenkinsRuleWithLocalPort.java diff --git a/src/test/java/org/jenkinsci/plugins/displayurlapi/DisplayURLProviderTest.java b/src/test/java/org/jenkinsci/plugins/displayurlapi/DisplayURLProviderTest.java index 6e19071..023b4a6 100644 --- a/src/test/java/org/jenkinsci/plugins/displayurlapi/DisplayURLProviderTest.java +++ b/src/test/java/org/jenkinsci/plugins/displayurlapi/DisplayURLProviderTest.java @@ -13,27 +13,39 @@ import jenkins.model.Jenkins; import org.jenkinsci.plugins.displayurlapi.actions.RunDisplayAction; import org.jenkinsci.plugins.displayurlapi.user.PreferredProviderUserProperty; -import org.junit.Rule; -import org.junit.Test; -import org.jvnet.hudson.test.FlagRule; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.jvnet.hudson.test.JenkinsRule; import org.jvnet.hudson.test.MockFolder; import org.jvnet.hudson.test.TestExtension; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import java.util.HashMap; import java.util.Map; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; -public class DisplayURLProviderTest { +@WithJenkins +class DisplayURLProviderTest { + private JenkinsRule rule; + private String flag; - @Rule - public JenkinsRuleWithLocalPort rule = new JenkinsRuleWithLocalPort(); + @BeforeEach + void setUp(JenkinsRule r) { + rule = r; + flag = System.clearProperty(DisplayURLProvider.JENKINS_DISPLAYURL_PROVIDER_PROP); + } - @Rule - public FlagRule flag = FlagRule.systemProperty(DisplayURLProvider.JENKINS_DISPLAYURL_PROVIDER_PROP); + @AfterEach + void tearDown() { + if (flag != null) { + System.setProperty(DisplayURLProvider.JENKINS_DISPLAYURL_PROVIDER_PROP, flag); + } + } @Test - public void urls() throws Exception { + void urls() throws Exception { DefaultDisplayURLProviderGlobalConfiguration.get().setProviderId( ClassicDisplayURLProvider.class.getName() ); @@ -41,7 +53,7 @@ public void urls() throws Exception { MockFolder folder = rule.createFolder("my folder"); FreeStyleProject p = folder.createProject(FreeStyleProject.class, "my job"); Run b = rule.buildAndAssertSuccess(p); - asssertExternalUrls(p, b); + assertExternalUrls(p, b); assertEquals(DisplayURLProvider.get().getRoot() + "job/my%20folder/job/my%20job/1/", b.getAction(RunDisplayAction.class).getDisplayUrl()); } @@ -94,13 +106,13 @@ public String getJobURL( Job project ) } @Test - public void urlsWithSysPropProvider() throws Exception { + void urlsWithSysPropProvider() throws Exception { System.setProperty(DisplayURLProvider.JENKINS_DISPLAYURL_PROVIDER_PROP, TestSysPropDisplayURLProvider.class.getName()); MockFolder folder = rule.createFolder("my folder"); FreeStyleProject p = folder.createProject(FreeStyleProject.class, "my job"); Run b = rule.buildAndAssertSuccess(p); - asssertExternalUrls(p, b); + assertExternalUrls(p, b); assertEquals(DisplayURLProvider.get().getRoot() + "job/my%20folder/job/my%20job/1/" + TestSysPropDisplayURLProvider.EXTRA_CONTENT_IN_URL, b.getAction(RunDisplayAction.class).getDisplayUrl()); } @@ -153,7 +165,7 @@ public String getJobURL( Job project ) } @Test - public void urlsWithUserDefinedProvider() throws Exception { + void urlsWithUserDefinedProvider() throws Exception { rule.jenkins.setSecurityRealm(rule.createDummySecurityRealm()); User foo = User.getById("foo", true); @@ -164,14 +176,14 @@ public void urlsWithUserDefinedProvider() throws Exception { FreeStyleProject p = folder.createProject(FreeStyleProject.class, "my job"); Run b = rule.buildAndAssertSuccess(p); try (ACLContext unused = ACL.as(foo)) { - asssertExternalUrls(p, b); + assertExternalUrls(p, b); assertEquals(DisplayURLProvider.get().getRoot() + "job/my%20folder/job/my%20job/1/" + TestUserDisplayURLProvider.EXTRA_CONTENT_IN_URL, b.getAction(RunDisplayAction.class).getDisplayUrl()); } } @Test - public void decoration() throws Exception { + void decoration() throws Exception { MockFolder folder = rule.createFolder("my folder"); FreeStyleProject project = (FreeStyleProject) folder .createProject(rule.jenkins.getDescriptorByType(FreeStyleProject.DescriptorImpl.class), "my job", @@ -179,7 +191,7 @@ public void decoration() throws Exception { Run run = project.scheduleBuild2(0).get(); String root = DisplayURLProvider.get().getRoot(); - assertEquals("http://localhost:" + rule.getLocalPort() + "/jenkins/", root); + assertEquals("http://localhost:" + rule.getURL().getPort() + "/jenkins/", root); assertEquals(root + "job/my%20folder/job/my%20job/1/display/redirect?utm_campaign=jenkins&utm_source=Jenkins" + "&utm_term=my+folder%2Fmy+job%231", DisplayURLProvider.get().getRunURL(run)); assertEquals(root + "job/my%20folder/job/my%20job/display/redirect?utm_campaign=jenkins&utm_source=Jenkins", @@ -223,7 +235,7 @@ public void decoration() throws Exception { } @Test - public void providerConfigurationPrecedence() throws Exception { + void providerConfigurationPrecedence() throws Exception { rule.jenkins.setSecurityRealm(rule.createDummySecurityRealm()); // user1 does not have a preference, but user2 does. User user1 = User.getById("user1", true); @@ -241,11 +253,11 @@ public void providerConfigurationPrecedence() throws Exception { } } - private void asssertExternalUrls(Job project, Run run) throws Exception { + private void assertExternalUrls(Job project, Run run) throws Exception { // No matter what configuration is being used, this plugin should always produce .../display/redirect URLs. // Configurations should only be applied when resolving a redirect URL. String root = DisplayURLProvider.get().getRoot(); - assertEquals("http://localhost:" + rule.getLocalPort() + "/jenkins/", root); + assertEquals("http://localhost:" + rule.getURL().getPort() + "/jenkins/", root); assertEquals(root + "job/my%20folder/job/my%20job/1/display/redirect", DisplayURLProvider.get().getRunURL(run)); assertEquals(root + "job/my%20folder/job/my%20job/display/redirect", DisplayURLProvider.get().getJobURL(project)); diff --git a/src/test/java/org/jenkinsci/plugins/displayurlapi/JenkinsRuleWithLocalPort.java b/src/test/java/org/jenkinsci/plugins/displayurlapi/JenkinsRuleWithLocalPort.java deleted file mode 100644 index 2622cd2..0000000 --- a/src/test/java/org/jenkinsci/plugins/displayurlapi/JenkinsRuleWithLocalPort.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.jenkinsci.plugins.displayurlapi; - -import org.jvnet.hudson.test.JenkinsRule; - -/** - * {@link JenkinsRule} that exposes the local port number - */ -public class JenkinsRuleWithLocalPort extends JenkinsRule { - public int getLocalPort() { - return this.localPort; - } -} diff --git a/src/test/java/org/jenkinsci/plugins/displayurlapi/actions/AbstractActionRedirectTest.java b/src/test/java/org/jenkinsci/plugins/displayurlapi/actions/AbstractActionRedirectTest.java index fd67e55..4dee5b3 100644 --- a/src/test/java/org/jenkinsci/plugins/displayurlapi/actions/AbstractActionRedirectTest.java +++ b/src/test/java/org/jenkinsci/plugins/displayurlapi/actions/AbstractActionRedirectTest.java @@ -8,24 +8,26 @@ import hudson.model.Run; import hudson.tasks.ArtifactArchiver; import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider; -import org.jenkinsci.plugins.displayurlapi.JenkinsRuleWithLocalPort; -import org.junit.Before; -import org.junit.Rule; +import org.junit.jupiter.api.BeforeEach; +import org.jvnet.hudson.test.JenkinsRule; import org.jvnet.hudson.test.MockFolder; import org.jvnet.hudson.test.TestBuilder; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import java.io.IOException; -public abstract class AbstractActionRedirectTest { +@WithJenkins +abstract class AbstractActionRedirectTest { protected Job job; protected Run run; protected DisplayURLProvider provider; - @Rule - public JenkinsRuleWithLocalPort rule = new JenkinsRuleWithLocalPort(); + protected JenkinsRule rule; + + @BeforeEach + void setUp(JenkinsRule r) throws Exception { + rule = r; - @Before - public void createJobAndRun() throws Exception { MockFolder folder = rule.createFolder("my folder"); FreeStyleProject job = (FreeStyleProject) folder.createProject(rule.jenkins.getDescriptorByType(FreeStyleProject.DescriptorImpl.class), "my job", false); job.getBuildersList().add(new CreateArtifact()); diff --git a/src/test/java/org/jenkinsci/plugins/displayurlapi/actions/ActionRedirectClassicTest.java b/src/test/java/org/jenkinsci/plugins/displayurlapi/actions/ActionRedirectClassicTest.java index e9e9362..3fbb9c4 100644 --- a/src/test/java/org/jenkinsci/plugins/displayurlapi/actions/ActionRedirectClassicTest.java +++ b/src/test/java/org/jenkinsci/plugins/displayurlapi/actions/ActionRedirectClassicTest.java @@ -7,29 +7,33 @@ import org.jenkinsci.plugins.displayurlapi.ClassicDisplayURLProvider; import org.jenkinsci.plugins.displayurlapi.DefaultDisplayURLProviderGlobalConfiguration; import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; import jakarta.servlet.http.HttpServletResponse; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import java.net.HttpURLConnection; import java.net.URL; import static io.restassured.RestAssured.given; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; -public class ActionRedirectClassicTest extends AbstractActionRedirectTest { +@WithJenkins +class ActionRedirectClassicTest extends AbstractActionRedirectTest { - @Before - public void setup() { + @BeforeEach + @Override + void setUp(JenkinsRule r) throws Exception { + super.setUp(r); DefaultDisplayURLProviderGlobalConfiguration.get().setProviderId( ClassicDisplayURLProvider.class.getName() ); } @Test - public void testRedirectForJobURL() throws Exception { + void testRedirectForJobURL() throws Exception { given() .urlEncodingEnabled(false) .redirects().follow(false) @@ -42,11 +46,11 @@ public void testRedirectForJobURL() throws Exception { .withThrowExceptionOnFailingStatusCode(false); WebResponse rsp = wc.getPage(new WebRequest(new URL(provider.getJobURL(job)))).getWebResponse(); - assertEquals(rsp.getContentAsString(), HttpURLConnection.HTTP_OK, rsp.getStatusCode()); + assertEquals(HttpURLConnection.HTTP_OK, rsp.getStatusCode(), rsp.getContentAsString()); } @Test - public void testRedirectForRunURL() throws Exception { + void testRedirectForRunURL() throws Exception { given() .urlEncodingEnabled(false) .redirects().follow(false) @@ -59,11 +63,11 @@ public void testRedirectForRunURL() throws Exception { .withThrowExceptionOnFailingStatusCode(false); WebResponse rsp = wc.getPage(new WebRequest(new URL(provider.getRunURL(run)))).getWebResponse(); - assertEquals(rsp.getContentAsString(), HttpURLConnection.HTTP_OK, rsp.getStatusCode()); + assertEquals(HttpURLConnection.HTTP_OK, rsp.getStatusCode(), rsp.getContentAsString()); } @Test - public void testRedirectForArtifactsURL() throws Exception { + void testRedirectForArtifactsURL() throws Exception { given() .urlEncodingEnabled(false) .redirects().follow(false) @@ -76,11 +80,11 @@ public void testRedirectForArtifactsURL() throws Exception { .withThrowExceptionOnFailingStatusCode(false); WebResponse rsp = wc.getPage(new WebRequest(new URL(provider.getArtifactsURL(run)))).getWebResponse(); - assertEquals(rsp.getContentAsString(), HttpURLConnection.HTTP_OK, rsp.getStatusCode()); + assertEquals(HttpURLConnection.HTTP_OK, rsp.getStatusCode(), rsp.getContentAsString()); } @Test - public void testRedirectForChangesURL() { + void testRedirectForChangesURL() { given() .urlEncodingEnabled(false) .redirects().follow(false) @@ -90,7 +94,7 @@ public void testRedirectForChangesURL() { } @Test - public void testRedirectForTestsURL() { + void testRedirectForTestsURL() { given() .urlEncodingEnabled(false) .redirects().follow(false) diff --git a/src/test/java/org/jenkinsci/plugins/displayurlapi/actions/ActionRedirectExtendedTest.java b/src/test/java/org/jenkinsci/plugins/displayurlapi/actions/ActionRedirectExtendedTest.java index 957ebd2..540cfe3 100644 --- a/src/test/java/org/jenkinsci/plugins/displayurlapi/actions/ActionRedirectExtendedTest.java +++ b/src/test/java/org/jenkinsci/plugins/displayurlapi/actions/ActionRedirectExtendedTest.java @@ -7,26 +7,31 @@ import hudson.model.Run; import org.jenkinsci.plugins.displayurlapi.DefaultDisplayURLProviderGlobalConfiguration; import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.jvnet.hudson.test.JenkinsRule; import org.jvnet.hudson.test.TestExtension; import jakarta.servlet.http.HttpServletResponse; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import static io.restassured.RestAssured.given; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; -public class ActionRedirectExtendedTest extends AbstractActionRedirectTest { +@WithJenkins +class ActionRedirectExtendedTest extends AbstractActionRedirectTest { - @Before - public void setup() { + @BeforeEach + @Override + void setUp(JenkinsRule r) throws Exception { + super.setUp(r); DefaultDisplayURLProviderGlobalConfiguration.get().setProviderId( AnotherDisplayURLProvider.class.getName() ); } @Test - public void testRedirectForJobURL() { + void testRedirectForJobURL() { given() .urlEncodingEnabled(false) .redirects().follow(false) @@ -36,7 +41,7 @@ public void testRedirectForJobURL() { } @Test - public void testRedirectForRunURL() { + void testRedirectForRunURL() { given() .urlEncodingEnabled(false) .redirects().follow(false) @@ -46,7 +51,7 @@ public void testRedirectForRunURL() { } @Test - public void testRedirectForArtifactsURL() { + void testRedirectForArtifactsURL() { given() .urlEncodingEnabled(false) .redirects().follow(false) @@ -56,7 +61,7 @@ public void testRedirectForArtifactsURL() { } @Test - public void testRedirectForChangesURL() { + void testRedirectForChangesURL() { given() .urlEncodingEnabled(false) .redirects().follow(false) @@ -66,7 +71,7 @@ public void testRedirectForChangesURL() { } @Test - public void testRedirectForTestsURL() { + void testRedirectForTestsURL() { given() .urlEncodingEnabled(false) .redirects().follow(false) @@ -76,7 +81,7 @@ public void testRedirectForTestsURL() { } @Test - public void testRedirectForYetAnotherProviderParameter() { + void testRedirectForYetAnotherProviderParameter() { given() .urlEncodingEnabled(false) .redirects().follow(false) @@ -86,9 +91,9 @@ public void testRedirectForYetAnotherProviderParameter() { } @Test - public void testUrls() { + void testUrls() throws Exception { String root = DisplayURLProvider.get().getRoot(); - assertEquals("http://localhost:" + rule.getLocalPort() + "/jenkins/", root); + assertEquals("http://localhost:" + rule.getURL().getPort() + "/jenkins/", root); assertEquals(root + "job/my%20folder/job/my%20job/1/another", getRedirectedProvider().getRunURL(run)); assertEquals(root + "job/my%20folder/job/my%20job/another", getRedirectedProvider().getJobURL(job)); assertEquals(root + "job/my%20folder/job/my%20job/1/artifactanother", getRedirectedProvider().getArtifactsURL(run));