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 Jan 27, 2025
1 parent dfdd8d2 commit 69917af
Show file tree
Hide file tree
Showing 18 changed files with 282 additions and 280 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.michelin.cio.hudson.plugins.rolestrategy;

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

import hudson.model.User;
import hudson.security.ACL;
Expand All @@ -11,17 +11,15 @@
import java.util.HashSet;
import java.util.Map;
import jenkins.model.Jenkins;
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;

public class GrantingDisabledPermissionTest {

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

@Test
public void grantDisabledPermissionTest() throws Exception {
void grantDisabledPermissionTest(JenkinsRule r) throws Exception {
HudsonPrivateSecurityRealm realm = new HudsonPrivateSecurityRealm(true, false, null);
realm.createAccount("admin", "admin");
realm.createAccount("alice", "alice");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
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.security.Permission;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class RoleTest {
class RoleTest {

@Test
public void testHasPermission() {
void testHasPermission() {
Role role = new Role("name", new HashSet<>(Arrays.asList(Permission.CREATE, Permission.READ, Permission.DELETE)));
assertTrue(role.hasPermission(Permission.READ));
assertFalse(role.hasPermission(Permission.WRITE));
}

@Test
public void testHasAnyPermissions() {
void testHasAnyPermissions() {
Role role = new Role("name", new HashSet<>((Arrays.asList(Permission.READ, Permission.DELETE))));
assertTrue(role.hasAnyPermission(new HashSet<>(Arrays.asList(Permission.READ, Permission.WRITE))));
assertFalse(role.hasAnyPermission(new HashSet<>(Arrays.asList(Permission.UPDATE, Permission.WRITE))));
}

@Test
public void shouldNotAddNullPermToNewRole() {
void shouldNotAddNullPermToNewRole() {
Permission permission = Permission.CREATE;
Permission nullPerm = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,30 @@

import hudson.model.FreeStyleProject;
import org.htmlunit.html.HtmlPage;
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.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsRule.DummySecurityRealm;
import org.jvnet.hudson.test.JenkinsRule.WebClient;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
import org.jvnet.hudson.test.recipes.LocalData;

public class ContainedInViewTest {
@Rule
public JenkinsRule jenkinsRule = new JenkinsRule();
@WithJenkins
class ContainedInViewTest {

@Before
private JenkinsRule jenkinsRule;

@BeforeEach
@LocalData
public void setup() throws Exception {
void setup(JenkinsRule jenkinsRule) throws Exception {
this.jenkinsRule = jenkinsRule;
DummySecurityRealm sr = jenkinsRule.createDummySecurityRealm();
jenkinsRule.jenkins.setSecurityRealm(sr);
}

@Test
@LocalData
public void userCanAccessJobInRootView() throws Exception {
void userCanAccessJobInRootView() throws Exception {
FreeStyleProject project = jenkinsRule.jenkins.getItemByFullName("testJob", FreeStyleProject.class);
WebClient wc = jenkinsRule.createWebClient();
wc.withThrowExceptionOnFailingStatusCode(false);
Expand All @@ -37,7 +39,7 @@ public void userCanAccessJobInRootView() throws Exception {

@Test
@LocalData
public void userCantAccessJobNotInRootView() throws Exception {
void userCantAccessJobNotInRootView() throws Exception {
FreeStyleProject project = jenkinsRule.jenkins.getItemByFullName("hiddenJob", FreeStyleProject.class);
WebClient wc = jenkinsRule.createWebClient();
wc.withThrowExceptionOnFailingStatusCode(false);
Expand All @@ -48,7 +50,7 @@ public void userCantAccessJobNotInRootView() throws Exception {

@Test
@LocalData
public void userCanAccessJobInFolderView() throws Exception {
void userCanAccessJobInFolderView() throws Exception {
FreeStyleProject project = jenkinsRule.jenkins.getItemByFullName("folder/testjob2", FreeStyleProject.class);
WebClient wc = jenkinsRule.createWebClient();
wc.withThrowExceptionOnFailingStatusCode(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@

package com.synopsys.arc.jenkins.plugins.rolestrategy;

import org.junit.Assert;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.fail;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* Contains tests for Macro.
Expand All @@ -34,10 +38,10 @@
* @since 2.1.0
* @author Oleg Nenashev
*/
public class MacroTest {
class MacroTest {

@Test
public void correctFormatsSanityCheck() {
void correctFormatsSanityCheck() {
parseMacro("@Dummy");
parseMacro("@Dummy:1");
parseMacro("@Dummy(aaa)");
Expand All @@ -47,7 +51,7 @@ public void correctFormatsSanityCheck() {
}

@Test
public void testValidUsers() {
void testValidUsers() {
parseWrongMacro("test", MacroExceptionCode.Not_Macro);
parseWrongMacro("logged_user", MacroExceptionCode.Not_Macro);
parseWrongMacro("anonymous", MacroExceptionCode.Not_Macro);
Expand All @@ -56,7 +60,7 @@ public void testValidUsers() {
}

@Test
public void testWrongBrackets() {
void testWrongBrackets() {
parseWrongMacro("@test:1(aaa,bbb(", MacroExceptionCode.WrongFormat);
parseWrongMacro("@test:1(aaa,bbb(", MacroExceptionCode.WrongFormat);
parseWrongMacro("@test:1()(aaa,bbb)", MacroExceptionCode.WrongFormat);
Expand All @@ -66,12 +70,12 @@ public void testWrongBrackets() {
}

@Test
public void err_WrongEnding() {
void err_WrongEnding() {
parseWrongMacro("@test:1(aaa,bbb)error", MacroExceptionCode.WrongFormat);
}

@Test
public void err_Quotes() {
void err_Quotes() {
parseWrongMacro("@test':1(aaa,bbb)", MacroExceptionCode.WrongFormat);
parseWrongMacro("@'test':1(aaa,bbb)", MacroExceptionCode.WrongFormat);
parseWrongMacro("@test:1('aaa',bbb)", MacroExceptionCode.WrongFormat);
Expand All @@ -85,13 +89,13 @@ public void err_Quotes() {
}

@Test
public void emptyParameters() {
void emptyParameters() {
parseMacro("@Dummy()");
parseMacro("@Dummy:1()");
}

@Test
public void test_MacroSanity() {
void test_MacroSanity() {
testCycle("test1", null, null);
testCycle("test2", 1, null);
testCycle("test3", -1, null);
Expand All @@ -114,13 +118,13 @@ private static void testCycle(String macroName, Integer macroId, String[] parame
}

private static void assertEquals(Macro expected, Macro actual) {
Assert.assertEquals("Wrong name", expected.getName(), expected.getName());
Assert.assertEquals("Wrong index", expected.getIndex(), expected.getIndex());
Assertions.assertEquals(expected.getName(), actual.getName(), "Wrong name");
Assertions.assertEquals(expected.getIndex(), actual.getIndex(), "Wrong index");

if (expected.hasParameters()) {
Assert.assertArrayEquals("Wrong parameters set", expected.getParameters(), actual.getParameters());
assertArrayEquals(expected.getParameters(), actual.getParameters(), "Wrong parameters set");
} else {
Assert.assertFalse("Actual macro shouldn't have parameters", actual.hasParameters());
assertFalse(actual.hasParameters(), "Actual macro shouldn't have parameters");
}
}

Expand All @@ -132,15 +136,15 @@ private static Macro assertParseMacroDriver(String errorMessage, String macroStr
} catch (MacroException ex) {
ex.printStackTrace();
if (expectException) {
Assert.assertEquals(errorMessage + ". Wrong error code", expectedError, ex.getErrorCode());
Assertions.assertEquals(expectedError, ex.getErrorCode(), errorMessage + ". Wrong error code");
} else {
Assert.fail(errorMessage + ". Got Macro Exception: " + ex.getMessage());
fail(errorMessage + ". Got Macro Exception: " + ex.getMessage());
}
return res;
}

if (expectException) {
Assert.fail(errorMessage + ". Haven't got exception. Expected code is " + expectedError);
fail(errorMessage + ". Haven't got exception. Expected code is " + expectedError);
}
return res;
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/jmh/BenchmarkRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

import java.util.concurrent.TimeUnit;
import jenkins.benchmark.jmh.BenchmarkFinder;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.results.format.ResultFormatType;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.ChainedOptionsBuilder;
import org.openjdk.jmh.runner.options.OptionsBuilder;

public final class BenchmarkRunner {
class BenchmarkRunner {
@Test
public void runJmhBenchmarks() throws Exception {
void runJmhBenchmarks() throws Exception {
ChainedOptionsBuilder options = new OptionsBuilder()
.mode(Mode.AverageTime)
.warmupIterations(2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,26 @@
import org.jenkinsci.plugins.authorizeproject.AuthorizeProjectProperty;
import org.jenkinsci.plugins.authorizeproject.ProjectQueueItemAuthenticator;
import org.jenkinsci.plugins.authorizeproject.strategy.TriggeringUsersAuthorizationStrategy;
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.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsRule.DummySecurityRealm;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
import org.jvnet.hudson.test.recipes.LocalData;

public class AuthorizeProjectTest {
@WithJenkins
class AuthorizeProjectTest {

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

private Slave node;
private FreeStyleProject project;
private AuthorizationCheckBuilder checker;

@Before
@BeforeEach
@LocalData
public void setup() throws Exception {
void setup(JenkinsRule jenkinsRule) throws Exception {
this.jenkinsRule = jenkinsRule;
QueueItemAuthenticatorConfiguration.get().getAuthenticators().add(new ProjectQueueItemAuthenticator(Collections.emptyMap()));
node = jenkinsRule.createSlave("TestAgent", null, null);
jenkinsRule.waitOnline(node);
Expand All @@ -62,7 +63,7 @@ public void setup() throws Exception {

@Test
@LocalData
public void agentBuildPermissionsAllowsToBuildOnAgent() throws Exception {
void agentBuildPermissionsAllowsToBuildOnAgent() throws Exception {
try (ACLContext c = ACL.as(User.getById("tester", true))) {
project.scheduleBuild2(0, new Cause.UserIdCause());
}
Expand All @@ -75,7 +76,7 @@ public void agentBuildPermissionsAllowsToBuildOnAgent() throws Exception {

@Test
@LocalData
public void missingAgentBuildPermissionsBlockBuild() throws Exception {
void missingAgentBuildPermissionsBlockBuild() throws Exception {
try (ACLContext c = ACL.as(User.getById("reader", true))) {
project.scheduleBuild2(0, new Cause.UserIdCause());
}
Expand All @@ -102,7 +103,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
}

public static class DescriptorImpl extends BuildStepDescriptor<Builder> {
@SuppressWarnings("rawtypes")

@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
Expand Down
Loading

0 comments on commit 69917af

Please sign in to comment.