Skip to content

Migrate tests to JUnit5 #255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,47 @@
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<String> 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()
);

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());
}
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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);

Expand All @@ -164,22 +176,22 @@ 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",
false);
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",
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -90,7 +94,7 @@ public void testRedirectForChangesURL() {
}

@Test
public void testRedirectForTestsURL() {
void testRedirectForTestsURL() {
given()
.urlEncodingEnabled(false)
.redirects().follow(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -36,7 +41,7 @@ public void testRedirectForJobURL() {
}

@Test
public void testRedirectForRunURL() {
void testRedirectForRunURL() {
given()
.urlEncodingEnabled(false)
.redirects().follow(false)
Expand All @@ -46,7 +51,7 @@ public void testRedirectForRunURL() {
}

@Test
public void testRedirectForArtifactsURL() {
void testRedirectForArtifactsURL() {
given()
.urlEncodingEnabled(false)
.redirects().follow(false)
Expand All @@ -56,7 +61,7 @@ public void testRedirectForArtifactsURL() {
}

@Test
public void testRedirectForChangesURL() {
void testRedirectForChangesURL() {
given()
.urlEncodingEnabled(false)
.redirects().follow(false)
Expand All @@ -66,7 +71,7 @@ public void testRedirectForChangesURL() {
}

@Test
public void testRedirectForTestsURL() {
void testRedirectForTestsURL() {
given()
.urlEncodingEnabled(false)
.redirects().follow(false)
Expand All @@ -76,7 +81,7 @@ public void testRedirectForTestsURL() {
}

@Test
public void testRedirectForYetAnotherProviderParameter() {
void testRedirectForYetAnotherProviderParameter() {
given()
.urlEncodingEnabled(false)
.redirects().follow(false)
Expand All @@ -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));
Expand Down
Loading