Skip to content

Commit

Permalink
Migrate tests to JUnit5
Browse files Browse the repository at this point in the history
* Migrate annotations and imports
* Migrate assertions
* Remove public visibility for test classes and methods
* Minor clean up
  • Loading branch information
strangelookingnerd committed Feb 17, 2025
1 parent eac6b1b commit b43b64a
Show file tree
Hide file tree
Showing 14 changed files with 291 additions and 300 deletions.
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
package org.jvnet.jenkins.plugins.nodelabelparameter;

import static hudson.Functions.isWindows;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;

import hudson.model.FreeStyleProject;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Result;
import hudson.slaves.DumbSlave;
import java.util.List;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.CaptureEnvironmentBuilder;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
import org.jvnet.jenkins.plugins.nodelabelparameter.node.AllNodeEligibility;

public class EnvironmentContributorTest {
@WithJenkins
class EnvironmentContributorTest {

@Rule
public JenkinsRule j = new JenkinsRule();
private JenkinsRule j;

private DumbSlave onlineNode1;

@Before
public void setUp() throws Exception {
@BeforeEach
void setUp(JenkinsRule j) throws Exception {
this.j = j;
onlineNode1 = j.createOnlineSlave();
}

@After
public void tearDown() throws Exception {
@AfterEach
void tearDown() throws Exception {
j.jenkins.removeNode(onlineNode1);
}

/**
* Makes sure 'NODE_NAME=built-in' is still available on the controller.
*/
@Test
public void testProjectScoped() throws Exception {
void testProjectScoped() throws Exception {

final String nodeName = j.jenkins.getSelfLabel().getName();
final List<String> defaultNodeNames = List.of(nodeName);
Expand All @@ -59,11 +59,11 @@ public void testProjectScoped() throws Exception {

j.assertBuildStatusSuccess(p.scheduleBuild2(0));

Assert.assertEquals(c.getEnvVars().get("NODE_NAME"), nodeName);
assertEquals(c.getEnvVars().get("NODE_NAME"), nodeName);
}

@Test
public void jenkins19222() throws Exception {
void jenkins19222() throws Exception {

// set label 'clearcase' on controller
j.jenkins.getComputer("").getNode().setLabelString("clearcase");
Expand All @@ -78,15 +78,15 @@ public void jenkins19222() throws Exception {
final CaptureEnvironmentBuilder cB = new CaptureEnvironmentBuilder();
projectA.getBuildersList().add(cB);

Assert.assertNull(projectB.getLastBuild());
assertNull(projectB.getLastBuild());

j.assertBuildStatusSuccess(projectA.scheduleBuild2(0));
j.waitUntilNoActivityUpTo(isWindows() ? 29000 : 10000); // 10secs on non-Windows, 29secs on Windows

final String nodeName = j.jenkins.getSelfLabel().getName();
Assert.assertEquals(nodeName, cA.getEnvVars().get("NODE_NAME"));
Assert.assertEquals(nodeName, cB.getEnvVars().get("NODE_NAME"));
Assert.assertNotNull(projectB.getLastBuild());
Assert.assertEquals(Result.SUCCESS, projectB.getLastBuild().getResult());
assertEquals(nodeName, cA.getEnvVars().get("NODE_NAME"));
assertEquals(nodeName, cB.getEnvVars().get("NODE_NAME"));
assertNotNull(projectB.getLastBuild());
assertEquals(Result.SUCCESS, projectB.getLastBuild().getResult());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,40 @@
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.is;
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;

import hudson.DescriptorExtensionList;
import hudson.model.Node;
import hudson.slaves.DumbSlave;
import java.util.Random;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
import org.jvnet.jenkins.plugins.nodelabelparameter.node.IgnoreTempOfflineNodeEligibility;
import org.jvnet.jenkins.plugins.nodelabelparameter.node.NodeEligibility;
import org.jvnet.jenkins.plugins.nodelabelparameter.node.NodeEligibility.NodeEligibilityDescriptor;

public class IgnoreTempOfflineNodeEligibilityTest {
@ClassRule
public static JenkinsRule j = new JenkinsRule();
@WithJenkins
class IgnoreTempOfflineNodeEligibilityTest {

private final IgnoreTempOfflineNodeEligibility ignoreTempOfflineNodeEligibility =
new IgnoreTempOfflineNodeEligibility();
private final Random random = new Random();

private static JenkinsRule j;

private static DumbSlave onlineNode1;

@BeforeClass
public static void createAgent() throws Exception {
@BeforeAll
static void setUp(JenkinsRule rule) throws Exception {
j = rule;
onlineNode1 = j.createOnlineSlave();
}

private final IgnoreTempOfflineNodeEligibility ignoreTempOfflineNodeEligibility =
new IgnoreTempOfflineNodeEligibility();
private final Random random = new Random();

@Test
public void testGetComputer() throws Exception {
void testGetComputer() throws Exception {
// Does not matter if executors on the Jenkins controller are enabled
j.jenkins.setNumExecutors(random.nextBoolean() ? 1 : 0);

Expand All @@ -52,7 +54,7 @@ public void testGetComputer() throws Exception {
}

@Test
public void testGetComputerWithControllerExecutor() throws Exception {
void testGetComputerWithControllerExecutor() throws Exception {
// Enable executors on the Jenkins controller
j.jenkins.setNumExecutors(1);

Expand All @@ -65,7 +67,7 @@ public void testGetComputerWithControllerExecutor() throws Exception {
}

@Test
public void testGetComputerWithoutControllerExecutor() throws Exception {
void testGetComputerWithoutControllerExecutor() throws Exception {
// Disable executors on the Jenkins controller
j.jenkins.setNumExecutors(0);

Expand All @@ -78,13 +80,13 @@ public void testGetComputerWithoutControllerExecutor() throws Exception {
}

@Test
public void testGetDescriptor() {
void testGetDescriptor() {
NodeEligibilityDescriptor descriptor = ignoreTempOfflineNodeEligibility.getDescriptor();
assertThat(descriptor.getDisplayName(), is("Ignore Temp Offline Nodes"));
}

@Test
public void testAll() {
void testAll() {
DescriptorExtensionList<NodeEligibility, NodeEligibilityDescriptor> descriptors = NodeEligibility.all();
assertThat(descriptors, hasItem(hasProperty("displayName", is("All Nodes"))));
assertThat(descriptors, hasItem(hasProperty("displayName", is("Ignore Offline Nodes"))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,38 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
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;

import hudson.model.labels.LabelAtom;
import hudson.slaves.DumbSlave;
import hudson.util.FormValidation;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
import org.jvnet.jenkins.plugins.nodelabelparameter.node.AllNodeEligibility;
import org.jvnet.jenkins.plugins.nodelabelparameter.node.IgnoreOfflineNodeEligibility;

public class LabelParameterDefinitionTest {
@WithJenkins
class LabelParameterDefinitionTest {

@ClassRule
public static JenkinsRule j = new JenkinsRule();
private static JenkinsRule j;

private static DumbSlave agent;

private static final String LABEL_NAME = "my-agent-label";
private static final LabelAtom label = new LabelAtom(LABEL_NAME);

@BeforeClass
public static void createAgent() throws Exception {
@BeforeAll
static void setUp(JenkinsRule rule) throws Exception {
j = rule;
agent = j.createOnlineSlave(label);
}

@Test
@Deprecated
public void testNodeParameterDefinitionDeprecated() {
void testNodeParameterDefinitionDeprecated() {
String name = "name";
String description = "The description";
String defaultValue = "built-in || master";
Expand All @@ -55,7 +56,7 @@ public void testNodeParameterDefinitionDeprecated() {

@Test
@Deprecated
public void testNodeParameterDefinitionDeprecated3Arg() {
void testNodeParameterDefinitionDeprecated3Arg() {
String name = "name";
String description = "The description";
String defaultValue = "built-in || master";
Expand All @@ -73,7 +74,7 @@ public void testNodeParameterDefinitionDeprecated3Arg() {
}

@Test
public void testNodeParameterDefinition() {
void testNodeParameterDefinition() {
String name = "name";
String description = "The description";
String defaultValue = "built-in || master";
Expand All @@ -92,7 +93,7 @@ public void testNodeParameterDefinition() {
}

@Test
public void testDoListNodesForAgentLabel() throws Exception {
void testDoListNodesForAgentLabel() throws Exception {
LabelParameterDefinition.DescriptorImpl nodeParameterDefinition = new LabelParameterDefinition.DescriptorImpl();
assertThat(nodeParameterDefinition.getDefaultNodeEligibility(), is(instanceOf(AllNodeEligibility.class)));
FormValidation validation = nodeParameterDefinition.doListNodesForLabel(LABEL_NAME);
Expand All @@ -101,7 +102,7 @@ public void testDoListNodesForAgentLabel() throws Exception {
}

@Test
public void testDoListNodesForControllerLabel() throws Exception {
void testDoListNodesForControllerLabel() throws Exception {
String controllerLabel = "built-in";
LabelParameterDefinition.DescriptorImpl nodeParameterDefinition = new LabelParameterDefinition.DescriptorImpl();
assertThat(nodeParameterDefinition.getDefaultNodeEligibility(), is(instanceOf(AllNodeEligibility.class)));
Expand All @@ -111,7 +112,7 @@ public void testDoListNodesForControllerLabel() throws Exception {
}

@Test
public void testDoListNodesForNonExistentLabel() throws Exception {
void testDoListNodesForNonExistentLabel() throws Exception {
String badLabel = "this-label-does-not-exist";
LabelParameterDefinition.DescriptorImpl nodeParameterDefinition = new LabelParameterDefinition.DescriptorImpl();
assertThat(nodeParameterDefinition.getDefaultNodeEligibility(), is(instanceOf(AllNodeEligibility.class)));
Expand All @@ -126,7 +127,7 @@ public void testDoListNodesForNonExistentLabel() throws Exception {
}

@Test
public void testDoListNodesForBlankLabel() throws Exception {
void testDoListNodesForBlankLabel() throws Exception {
String blankLabel = "";
LabelParameterDefinition.DescriptorImpl nodeParameterDefinition = new LabelParameterDefinition.DescriptorImpl();
FormValidation validation = nodeParameterDefinition.doListNodesForLabel(blankLabel);
Expand All @@ -135,7 +136,7 @@ public void testDoListNodesForBlankLabel() throws Exception {
}

@Test
public void testDoListNodesForInvalidLabelExpression() throws Exception {
void testDoListNodesForInvalidLabelExpression() throws Exception {
String invalidLabel = "a||";
LabelParameterDefinition.DescriptorImpl nodeParameterDefinition = new LabelParameterDefinition.DescriptorImpl();
assertThat(nodeParameterDefinition.getDefaultNodeEligibility(), is(instanceOf(AllNodeEligibility.class)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,30 @@
import java.util.Random;
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
import org.jvnet.jenkins.plugins.nodelabelparameter.node.AllNodeEligibility;
import org.jvnet.jenkins.plugins.nodelabelparameter.node.NodeEligibility;

public class LabelParameterValueTest {
@WithJenkins
class LabelParameterValueTest {

@ClassRule
public static JenkinsRule j = new JenkinsRule();
private final Random random = new Random();

private static JenkinsRule j;

private static DumbSlave agent;

@BeforeClass
public static void createAgent() throws Exception {
@BeforeAll
static void setUp(JenkinsRule rule) throws Exception {
j = rule;
agent = j.createOnlineSlave();
}

private final Random random = new Random();

public LabelParameterValueTest() {}

@Test
public void testEqualsContract() {
void testEqualsContract() {
EqualsVerifier.forClass(LabelParameterValue.class)
.usingGetClass()
.suppress(Warning.NONFINAL_FIELDS)
Expand All @@ -69,7 +68,7 @@ public void testEqualsContract() {

@Test
@Deprecated
public void testLabelParameterValueDeprecated2ArgConstructor() {
void testLabelParameterValueDeprecated2ArgConstructor() {
String value = " my-label "; // Intentionally has leading and trailing spaces
String trimmedValue = value.trim();
// Use either value or trimmedValue randomly, does not change any assertion
Expand All @@ -83,7 +82,7 @@ public void testLabelParameterValueDeprecated2ArgConstructor() {

@Test
@Deprecated
public void testLabelParameterValueDeprecated2ArgConstructorNullName() {
void testLabelParameterValueDeprecated2ArgConstructorNullName() {
String value = " my-label "; // Intentionally has leading and trailing spaces
String trimmedValue = value.trim();
// Use either value or trimmedValue randomly, does not change any assertion
Expand All @@ -97,7 +96,7 @@ public void testLabelParameterValueDeprecated2ArgConstructorNullName() {

@Test
@Deprecated
public void testLabelParameterValueDeprecated3ArgConstructor() {
void testLabelParameterValueDeprecated3ArgConstructor() {
String value = " my-label "; // Intentionally has leading and trailing spaces
String trimmedValue = value.trim();
String name = "my-name";
Expand All @@ -112,7 +111,7 @@ public void testLabelParameterValueDeprecated3ArgConstructor() {
}

@Test
public void testGetNextLabels() {
void testGetNextLabels() {
String name = "my-name";
List<String> extraNodeNames = Arrays.asList("built-in", "not-a-valid-node-name");
List<String> nodeNames = new ArrayList<>();
Expand Down
Loading

0 comments on commit b43b64a

Please sign in to comment.