Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jenkinsci/plugin-util-api-plugin
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 7881613357440c81d1657f0d574b3f10dc501b0f
Choose a base ref
..
head repository: jenkinsci/plugin-util-api-plugin
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 77d2275fc2027f1afe535bb74fdf28a07d62b04a
Choose a head ref
Original file line number Diff line number Diff line change
@@ -9,13 +9,13 @@
*
* @author Ullrich Hafner
*/
@EnableJenkins
public abstract class IntegrationTestWithJenkinsPerSuite extends IntegrationTest {
private static JenkinsRule jenkinsPerSuite;

@BeforeAll @EnableJenkins
static void initializeJenkins(final JenkinsRule jenkins) {

IntegrationTestWithJenkinsPerSuite.jenkinsPerSuite = jenkins;
@BeforeAll
static void initializeJenkins(final JenkinsRule rule) {
jenkinsPerSuite = rule;
}

@Override
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
package io.jenkins.plugins.util;

import org.junit.jupiter.api.BeforeEach;
import org.jvnet.hudson.test.JenkinsRule;
import org.junit.jupiter.api.AfterEach;

/**
* Base class for integration tests in Jenkins. Subclasses will get a new and fresh Jenkins instance for each
* test case.
*
* @author Ullrich Hafner
*/
public abstract class IntegrationTestWithJenkinsPerTest extends IntegrationTest {
private static JenkinsRule jenkinsPerTest;

@BeforeEach
@EnableJenkins
void initializeJenkins(final JenkinsRule jenkins) {
jenkinsPerTest = jenkins;
}

@Override
protected JenkinsRule getJenkins() {
return jenkinsPerTest;
public abstract class IntegrationTestWithJenkinsPerTest extends IntegrationTestWithJenkinsPerSuite {
@SuppressWarnings("checkstyle:IllegalThrows")
@AfterEach
void tearDown() throws Throwable {
getJenkins().after();
getJenkins().before();
}
}
37 changes: 20 additions & 17 deletions src/test/java/io/jenkins/plugins/util/JUnit5JenkinsRule.java
Original file line number Diff line number Diff line change
@@ -14,23 +14,26 @@
* Provides JUnit 5 compatibility for {@link JenkinsRule}.
*/
class JUnit5JenkinsRule extends JenkinsRule {
private final ParameterContext context;
private final ParameterContext context;

JUnit5JenkinsRule(@NonNull final ParameterContext context, @NonNull final ExtensionContext extensionContext) {
this.context = context;
this.testDescription = Description.createTestDescription(extensionContext.getTestClass().map(Class::getName).orElse(null),
extensionContext.getTestMethod().map(Method::getName).orElse(null));
}
JUnit5JenkinsRule(@NonNull final ParameterContext context, @NonNull final ExtensionContext extensionContext) {
super();

@Override
public void recipe() throws Exception {
JenkinsRecipe jenkinsRecipe = context.findAnnotation(JenkinsRecipe.class).orElse(null);
if (jenkinsRecipe == null) {
return;
}
@SuppressWarnings("unchecked") final JenkinsRecipe.Runner<JenkinsRecipe> runner = (JenkinsRecipe.Runner<JenkinsRecipe>) jenkinsRecipe
.value().getDeclaredConstructor().newInstance();
recipes.add(runner);
tearDowns.add(() -> runner.tearDown(this, jenkinsRecipe));
}
this.context = context;
this.testDescription = Description.createTestDescription(
extensionContext.getTestClass().map(Class::getName).orElse(null),
extensionContext.getTestMethod().map(Method::getName).orElse(null));
}

@Override
public void recipe() throws Exception {
JenkinsRecipe jenkinsRecipe = context.findAnnotation(JenkinsRecipe.class).orElse(null);
if (jenkinsRecipe == null) {
return;
}
@SuppressWarnings("unchecked") final JenkinsRecipe.Runner<JenkinsRecipe> runner
= (JenkinsRecipe.Runner<JenkinsRecipe>) jenkinsRecipe.value().getDeclaredConstructor().newInstance();
recipes.add(runner);
tearDowns.add(() -> runner.tearDown(this, jenkinsRecipe));
}
}
12 changes: 8 additions & 4 deletions src/test/java/io/jenkins/plugins/util/JenkinsExtension.java
Original file line number Diff line number Diff line change
@@ -13,10 +13,9 @@
* @see EnableJenkins
*/
class JenkinsExtension implements ParameterResolver, AfterEachCallback {

private static final String KEY = "jenkins-instance";
private static final ExtensionContext.Namespace NAMESPACE = ExtensionContext.Namespace.create(
JenkinsExtension.class);
private static final ExtensionContext.Namespace NAMESPACE
= ExtensionContext.Namespace.create(JenkinsExtension.class);

@Override
public void afterEach(final ExtensionContext context) throws Exception {
@@ -36,9 +35,14 @@ public boolean supportsParameter(final ParameterContext parameterContext, final
@Override
public Object resolveParameter(final ParameterContext parameterContext, final ExtensionContext extensionContext)
throws ParameterResolutionException {
final JenkinsRule rule = extensionContext.getStore(NAMESPACE).getOrComputeIfAbsent(KEY, key
JenkinsRule rule = extensionContext.getStore(NAMESPACE).getOrComputeIfAbsent(KEY, key
-> new JUnit5JenkinsRule(parameterContext, extensionContext), JenkinsRule.class);

return invokeRule(rule);
}

@SuppressWarnings({"PMD.AvoidCatchingThrowable", "checkstyle:IllegalCatch"})
private JenkinsRule invokeRule(final JenkinsRule rule) {
try {
rule.before();
return rule;
22 changes: 13 additions & 9 deletions src/test/java/io/jenkins/plugins/util/JenkinsRuleResolverTest.java
Original file line number Diff line number Diff line change
@@ -5,16 +5,20 @@
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.collection.IsCollectionWithSize.*;
import static org.hamcrest.collection.IsEmptyCollection.*;
import static org.assertj.core.api.Assertions.*;

/**
* Verifies the new rule with method scope.
*/
@EnableJenkins
class JenkinsRuleResolverTest {
@Test
void jenkinsRuleIsAccessible(final JenkinsRule rule) throws IOException {
assertThat(rule.jenkins.getJobNames(), empty());
rule.createFreeStyleProject("job-0");
assertThat(rule.jenkins.getJobNames(), hasSize(1));
}
@Test
void jenkinsRuleIsAccessible(final JenkinsRule rule) throws IOException {
assertThat(rule.jenkins.getJobNames()).isEmpty();

String job = "job-0";
rule.createFreeStyleProject(job);

assertThat(rule.jenkins.getJobNames()).containsExactly(job);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.jenkins.plugins.util;

import java.io.IOException;

import org.junit.jupiter.api.Test;

import jenkins.model.Jenkins;

import static org.assertj.core.api.Assertions.*;

/**
* Verifies the new rule with method scope.
*/
class JenkinsRuleResolverWithJenkinsPerSuiteTest extends IntegrationTestWithJenkinsPerSuite {
@Test
void shouldCreateJenkinsPerSuite() throws IOException {
Jenkins jenkins = getJenkins().jenkins;
assertThat(jenkins.getJobNames()).isEmpty();

String job = "job-0";
getJenkins().createFreeStyleProject(job);

assertThat(jenkins.getJobNames()).containsExactly(job);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.jenkins.plugins.util;

import java.io.IOException;

import org.junit.jupiter.api.Test;

import jenkins.model.Jenkins;

import static org.assertj.core.api.Assertions.*;

/**
* Verifies the new rule with method scope.
*/
class JenkinsRuleResolverWithJenkinsPerTestTest extends IntegrationTestWithJenkinsPerTest {
@Test
void jenkinsRuleIsAccessible() throws IOException {
Jenkins jenkins = getJenkins().jenkins;
assertThat(jenkins.getJobNames()).isEmpty();

String job = "job-0";
getJenkins().createFreeStyleProject(job);

assertThat(jenkins.getJobNames()).containsExactly(job);
}
}
Original file line number Diff line number Diff line change
@@ -9,12 +9,15 @@
import static org.hamcrest.collection.IsCollectionWithSize.*;
import static org.hamcrest.collection.IsEmptyCollection.*;

/**
* Verifies the new rule with method scope.
*/
class JenkinsRuleResolverWithMethodScopeTest {
@EnableJenkins
@Test
void jenkinsRuleIsAccessible(final JenkinsRule rule) throws IOException {
assertThat(rule.jenkins.getJobNames(), empty());
rule.createFreeStyleProject("job-0");
assertThat(rule.jenkins.getJobNames(), hasSize(1));
}
@EnableJenkins
@Test
void jenkinsRuleIsAccessible(final JenkinsRule rule) throws IOException {
assertThat(rule.jenkins.getJobNames(), empty());
rule.createFreeStyleProject("job-0");
assertThat(rule.jenkins.getJobNames(), hasSize(1));
}
}