Skip to content

Commit

Permalink
Upgrade tests to run with JUnit5 (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
strangelookingnerd authored May 11, 2024
1 parent a3bb1f2 commit 924feaa
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 168 deletions.
19 changes: 14 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<url>https://github.com/jenkinsci/badge-plugin</url>

<developers>
<developer>
<id>strangelookingnerd</id>
<name>Daniel Krämer</name>
</developer>
<developer>
<id>bakito</id>
<name>Marc Brugger</name>
Expand Down Expand Up @@ -129,6 +133,16 @@
<groupId>io.jenkins.plugins</groupId>
<artifactId>ionicons-api</artifactId>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>jackson2-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>commons-lang3-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
Expand All @@ -150,11 +164,6 @@
<artifactId>workflow-job</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.hubspot.jinjava</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
package com.jenkinsci.plugins.badge.action;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

import static org.junit.jupiter.api.Assertions.*;

@WithJenkins
class AbstractActionTest {

@Test
void isJenkinsSymbolRef() {
void isJenkinsSymbolRef(@SuppressWarnings("unused") JenkinsRule r) {
assertTrue(AbstractAction.isJenkinsSymbolRef("symbol-cube"));
assertFalse(AbstractAction.isJenkinsSymbolRef("info.gif"));
}

@Test
void getJenkinsColorClass() {
void getJenkinsColorClass(@SuppressWarnings("unused") JenkinsRule r) {
assertNull(AbstractAction.getJenkinsColorClass(null));
assertNull(AbstractAction.getJenkinsColorClass(""));
assertNull(AbstractAction.getJenkinsColorClass("teal"));
assertNull(AbstractAction.getJenkinsColorClass("burlywood"));
assertNull(AbstractAction.getJenkinsColorClass("light-"));
assertNull(AbstractAction.getJenkinsColorClass("dark-"));
assertEquals(AbstractAction.getJenkinsColorClass("blue"), "jenkins-!-color-blue");
assertEquals(AbstractAction.getJenkinsColorClass("light-blue"), "jenkins-!-color-light-blue");
assertEquals(AbstractAction.getJenkinsColorClass("dark-blue"), "jenkins-!-color-dark-blue");
assertEquals(AbstractAction.getJenkinsColorClass("danger"), "jenkins-!-danger-color");
assertEquals("jenkins-!-color-blue", AbstractAction.getJenkinsColorClass("blue"));
assertEquals("jenkins-!-color-light-blue", AbstractAction.getJenkinsColorClass("light-blue"));
assertEquals("jenkins-!-color-dark-blue", AbstractAction.getJenkinsColorClass("dark-blue"));
assertEquals("jenkins-!-danger-color", AbstractAction.getJenkinsColorClass("danger"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@


import hudson.model.Hudson;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

public class BadgeActionTest {

@Rule
public JenkinsRule r = new JenkinsRule();
@WithJenkins
class BadgeActionTest {

@Test
public void getIconPath() {
void getIconPath(@SuppressWarnings("unused") JenkinsRule r) {
assertNull(BadgeAction.getIconPath(null));
assertEquals("/icon.png", BadgeAction.getIconPath("/icon.png"));
assertEquals("http://foo.com/icon.png", BadgeAction.getIconPath("http://foo.com/icon.png"));
Expand All @@ -27,35 +26,35 @@ public void getIconPath() {
}

@Test
public void getIconClass() {
void getIconClass(@SuppressWarnings("unused") JenkinsRule r) {
BadgeAction action = BadgeAction.createBadge("info.gif", "text");
assertEquals("", action.getIconClass());
action = BadgeAction.createBadge("symbol-star", "text");
action = BadgeAction.createBadge("symbol-star plugin-ionicons-api", "text");
assertEquals("icon-sm", action.getIconClass());
action = BadgeAction.createBadge("symbol-star", "#000000", "", null);
action = BadgeAction.createBadge("symbol-star plugin-ionicons-api", "#000000", "", null);
assertEquals("icon-sm", action.getIconClass());
action = BadgeAction.createBadge("symbol-star", "blue", "", null);
action = BadgeAction.createBadge("symbol-star plugin-ionicons-api", "blue", "", null);
assertEquals("icon-sm jenkins-!-color-blue", action.getIconClass());
action = BadgeAction.createBadge("/foo/symbol-star.gif", "blue", "", null);
assertEquals("jenkins-!-color-blue", action.getIconClass());
// teal is not in the palette
action = BadgeAction.createBadge("symbol-star", "teal", "", null);
action = BadgeAction.createBadge("symbol-star plugin-ionicons-api", "teal", "", null);
assertEquals("icon-sm", action.getIconClass());
}

@Test
public void getIconColorStyle() {
void getIconColorStyle(@SuppressWarnings("unused") JenkinsRule r) {
BadgeAction action = BadgeAction.createBadge("info.gif", "text");
assertNull(action.getIconColorStyle());
action = BadgeAction.createBadge("symbol-star", "#000000", "", null);
action = BadgeAction.createBadge("symbol-star plugin-ionicons-api", "#000000", "", null);
assertEquals("#000000", action.getIconColorStyle());
action = BadgeAction.createBadge("symbol-star", "var(--yellow)", "", null);
action = BadgeAction.createBadge("symbol-star plugin-ionicons-api", "var(--yellow)", "", null);
assertEquals("var(--yellow)", action.getIconColorStyle());
action = BadgeAction.createBadge("symbol-star", "jenkins-!-color-blue", "", null);
action = BadgeAction.createBadge("symbol-star plugin-ionicons-api", "jenkins-!-color-blue", "", null);
assertNull(action.getIconColorStyle());
action = BadgeAction.createBadge("symbol-star", "blue", "", null);
action = BadgeAction.createBadge("symbol-star plugin-ionicons-api", "blue", "", null);
assertNull(action.getIconColorStyle());
action = BadgeAction.createBadge("symbol-star", "teal", "", null);
action = BadgeAction.createBadge("symbol-star plugin-ionicons-api", "teal", "", null);
assertEquals("teal", action.getIconColorStyle());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@

import hudson.model.Action;
import hudson.model.BuildBadgeAction;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ClassHierarchyTest {

class ClassHierarchyTest {

@Test
public void badgeSummaryAction() {
void badgeSummaryAction() {
assertTrue(Action.class.isAssignableFrom(BadgeSummaryAction.class));
assertTrue(AbstractAction.class.isAssignableFrom(BadgeSummaryAction.class));

Expand All @@ -42,7 +43,7 @@ public void badgeSummaryAction() {
}

@Test
public void badgeAction() {
void badgeAction() {
assertTrue(Action.class.isAssignableFrom(BadgeAction.class));
assertTrue(AbstractAction.class.isAssignableFrom(BadgeAction.class));

Expand All @@ -51,7 +52,7 @@ public void badgeAction() {
}

@Test
public void htmlBadgeAction() {
void htmlBadgeAction() {
assertTrue(Action.class.isAssignableFrom(HtmlBadgeAction.class));
assertTrue(AbstractAction.class.isAssignableFrom(HtmlBadgeAction.class));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,10 @@
*/
package com.jenkinsci.plugins.badge.dsl;

import org.junit.ClassRule;
import org.junit.Rule;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

public abstract class AbstractBadgeTest {

@ClassRule
public static BuildWatcher buildWatcher = new BuildWatcher();
@Rule
public JenkinsRule r = new JenkinsRule();
@WithJenkins
abstract class AbstractBadgeTest {

}
120 changes: 61 additions & 59 deletions src/test/java/com/jenkinsci/plugins/badge/dsl/AddBadgeStepTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,77 @@
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.util.List;
import java.util.UUID;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;

public class AddBadgeStepTest extends AbstractBadgeTest {

class AddBadgeStepTest extends AbstractBadgeTest {

@Test
void addBadge(JenkinsRule r) throws Exception {
addBadge(r, false);
}

@Test
void addBadge_in_node(JenkinsRule r) throws Exception {
addBadge(r, true);
}

@Test
void addInfoBadge(JenkinsRule r) throws Exception {
addStatusBadge(r, "addInfoBadge", "symbol-information-circle plugin-ionicons-api", false);
addStatusBadge(r,"addInfoBadge", "symbol-information-circle plugin-ionicons-api", true);
}

@Test
public void addBadge() throws Exception {
addBadge(false);
void addWarningBadge(JenkinsRule r) throws Exception {
addStatusBadge(r,"addWarningBadge", "symbol-warning plugin-ionicons-api", false);
addStatusBadge(r,"addWarningBadge", "symbol-warning plugin-ionicons-api", true);
}

@Test
public void addBadge_in_node() throws Exception {
addBadge(true);
void addErrorBadge(JenkinsRule r) throws Exception {
addStatusBadge(r, "addErrorBadge", "symbol-remove-circle plugin-ionicons-api", false);
addStatusBadge(r, "addErrorBadge", "symbol-remove-circle plugin-ionicons-api", true);
}

@Test
void addBadge_invalid_link(JenkinsRule r) throws Exception {
String icon = UUID.randomUUID().toString();
String text = UUID.randomUUID().toString();
String link = "javascript:" + UUID.randomUUID();
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");

String script = "addBadge(icon:\"" + icon + "\", text:\"" + text + "\", link:\"" + link + "\")";
p.setDefinition(new CpsFlowDefinition(script, true));
r.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0));
}

@Test
void addBadge_invalid_text(JenkinsRule r) throws Exception {
String icon = UUID.randomUUID().toString();
String textPrefix = UUID.randomUUID().toString();
String text = textPrefix + "');alert('foo";
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");

String script = "addBadge(icon: '" + icon + "', text:\"" + text + "\")";
p.setDefinition(new CpsFlowDefinition(script, true));
WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));

List<BuildBadgeAction> badgeActions = b.getBadgeActions();
assertEquals(1, badgeActions.size());

BadgeAction action = (BadgeAction) badgeActions.get(0);
assertTrue(action.getIconPath().endsWith(icon));
assertEquals(textPrefix + "&#39;);alert(&#39;foo", action.getText());
}

private void addBadge(boolean inNode) throws Exception {
private void addBadge(JenkinsRule r, boolean inNode) throws Exception {
String icon = UUID.randomUUID().toString();
String text = UUID.randomUUID().toString();
String link = "https://" + UUID.randomUUID();
Expand All @@ -71,25 +122,7 @@ private void addBadge(boolean inNode) throws Exception {
assertEquals(link, action.getLink());
}

@Test
public void addInfoBadge() throws Exception {
addStatusBadge("addInfoBadge", "symbol-information-circle plugin-ionicons-api", false);
addStatusBadge("addInfoBadge", "symbol-information-circle plugin-ionicons-api", true);
}

@Test
public void addWarningBadge() throws Exception {
addStatusBadge("addWarningBadge", "symbol-warning plugin-ionicons-api", false);
addStatusBadge("addWarningBadge", "symbol-warning plugin-ionicons-api", true);
}

@Test
public void addErrorBadge() throws Exception {
addStatusBadge("addErrorBadge", "symbol-remove-circle plugin-ionicons-api", false);
addStatusBadge("addErrorBadge", "symbol-remove-circle plugin-ionicons-api", true);
}

private void addStatusBadge(String functionName, String expectedIcon, boolean withLink) throws Exception {
private void addStatusBadge(JenkinsRule r, String functionName, String expectedIcon, boolean withLink) throws Exception {
String text = UUID.randomUUID().toString();
String link = "mailto://" + UUID.randomUUID();

Expand All @@ -107,42 +140,11 @@ private void addStatusBadge(String functionName, String expectedIcon, boolean wi

BadgeAction action = (BadgeAction) badgeActions.get(0);
assertEquals(text, action.getText());
assertTrue("expect " + action.getIconPath() + " ends with " + expectedIcon, action.getIconPath().endsWith(expectedIcon));
assertTrue(action.getIconPath().endsWith(expectedIcon));
if (withLink) {
assertEquals(link, action.getLink());
} else {
assertNull(action.getLink());
}
}

@Test
public void addBadge_invalid_link() throws Exception {
String icon = UUID.randomUUID().toString();
String text = UUID.randomUUID().toString();
String link = "javascript:" + UUID.randomUUID();
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");

String script = "addBadge(icon:\"" + icon + "\", text:\"" + text + "\", link:\"" + link + "\")";
p.setDefinition(new CpsFlowDefinition(script, true));
r.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0));
}

@Test
public void addBadge_invalid_text() throws Exception {
String icon = UUID.randomUUID().toString();
String textPrefix = UUID.randomUUID().toString();
String text = textPrefix + "');alert('foo";
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");

String script = "addBadge(icon: '" + icon + "', text:\"" + text + "\")";
p.setDefinition(new CpsFlowDefinition(script, true));
WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));

List<BuildBadgeAction> badgeActions = b.getBadgeActions();
assertEquals(1, badgeActions.size());

BadgeAction action = (BadgeAction) badgeActions.get(0);
assertTrue(action.getIconPath().endsWith(icon));
assertEquals(textPrefix + "&#39;);alert(&#39;foo", action.getText());
}
}
Loading

0 comments on commit 924feaa

Please sign in to comment.