Skip to content

Commit

Permalink
Enable using @LocalData for JUnit5 tests (#900)
Browse files Browse the repository at this point in the history
Co-authored-by: strangelookingnerd <[email protected]>
  • Loading branch information
strangelookingnerd and strangelookingnerd authored Jan 20, 2025
1 parent 3bf8df1 commit 446201c
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,44 @@
import org.junit.runner.Description;
import org.jvnet.hudson.test.JenkinsRecipe;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.recipes.LocalData;

/**
* Provides JUnit 5 compatibility for {@link JenkinsRule}.
*/
class JUnit5JenkinsRule extends JenkinsRule {
private final ParameterContext context;
private final ExtensionContext extensionContext;

JUnit5JenkinsRule(@NonNull ParameterContext context, @NonNull ExtensionContext extensionContext) {
this.context = context;
this.extensionContext = extensionContext;
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;
JenkinsRecipe jenkinsRecipe =
context.findAnnotation(JenkinsRecipe.class).orElse(null);
if (jenkinsRecipe != null) {
@SuppressWarnings("unchecked")
final JenkinsRecipe.Runner<JenkinsRecipe> runner = (JenkinsRecipe.Runner<JenkinsRecipe>)
jenkinsRecipe.value().getDeclaredConstructor().newInstance();
recipes.add(runner);
tearDowns.add(() -> runner.tearDown(this, jenkinsRecipe));
}

Method testMethod = extensionContext.getTestMethod().orElse(null);
if (testMethod != null) {
LocalData localData = testMethod.getAnnotation(LocalData.class);
if (localData != null) {
final JenkinsRecipe.Runner<LocalData> runner = new LocalData.RuleRunnerImpl();
recipes.add(runner);
runner.setup(this, localData);
tearDowns.add(() -> runner.tearDown(this, localData));
}
}
@SuppressWarnings("unchecked")
final JenkinsRecipe.Runner<JenkinsRecipe> runner =
(JenkinsRecipe.Runner<JenkinsRecipe>) jenkinsRecipe.value().getDeclaredConstructor().newInstance();
recipes.add(runner);
tearDowns.add(() -> runner.tearDown(this, jenkinsRecipe));
}
}
21 changes: 16 additions & 5 deletions src/main/java/org/jvnet/hudson/test/recipes/LocalData.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*
* The MIT License
*
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -28,6 +28,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import org.junit.runner.Description;
import org.jvnet.hudson.test.HudsonHomeLoader.Local;
import org.jvnet.hudson.test.HudsonTestCase;
Expand Down Expand Up @@ -92,11 +93,21 @@ public void setup(HudsonTestCase testCase, LocalData recipe) throws Exception {
testCase.with(new Local(testCase.getClass().getMethod(testCase.getName()), recipe.value()));
}
}

class RuleRunnerImpl extends JenkinsRecipe.Runner<LocalData> {
@Override
public void setup(JenkinsRule jenkinsRule, LocalData recipe) throws Exception {
Description desc = jenkinsRule.getTestDescription();
jenkinsRule.with(new Local(desc.getTestClass().getMethod(desc.getMethodName()), recipe.value()));

Method testMethod;

try {
testMethod = desc.getTestClass().getDeclaredMethod(desc.getMethodName());
} catch (NoSuchMethodException ex) {
testMethod = desc.getTestClass().getDeclaredMethod(desc.getMethodName(), JenkinsRule.class);
}

jenkinsRule.with(new Local(testMethod, recipe.value()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.io.IOException;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.recipes.LocalData;

@WithJenkins
class JenkinsRuleResolverTest {
Expand All @@ -17,4 +19,16 @@ void jenkinsRuleIsAccessible(JenkinsRule rule) throws IOException {
rule.createFreeStyleProject("job-0");
assertThat(rule.jenkins.getJobNames(), hasSize(1));
}

@Test
@LocalData
void jenkinsRuleUsesLocalData(JenkinsRule rule) {
assertNotNull(rule.jenkins.getItem("testJob"));
}

@Test
@LocalData
void jenkinsRuleUsesLocalDataZip(JenkinsRule rule) {
assertNotNull(rule.jenkins.getItem("somejob"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.io.IOException;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.recipes.LocalData;

class JenkinsRuleResolverWithMethodScopeTest {

Expand All @@ -17,4 +19,18 @@ void jenkinsRuleIsAccessible(JenkinsRule rule) throws IOException {
rule.createFreeStyleProject("job-0");
assertThat(rule.jenkins.getJobNames(), hasSize(1));
}

@Test
@LocalData
@WithJenkins
void jenkinsRuleUsesLocalData(JenkinsRule rule) {
assertNotNull(rule.jenkins.getItem("testJob"));
}

@Test
@LocalData
@WithJenkins
void jenkinsRuleUsesLocalDataZip(JenkinsRule rule) {
assertNotNull(rule.jenkins.getItem("somejob"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version='1.0' encoding='UTF-8'?>
<project>
<actions/>
<description></description>
<properties>
<hudson.security.AuthorizationMatrixProperty>
<useProjectSecurity>true</useProjectSecurity>
<permission>hudson.model.Item.ExtendedRead:alice</permission>
<permission>hudson.model.Item.Configure:alice</permission>
<permission>hudson.model.Item.Configure:bob</permission>
<permission>hudson.model.Item.ExtendedRead:charlie</permission>
</hudson.security.AuthorizationMatrixProperty>
</properties>
<scm class="hudson.scm.NullSCM"/>
<canRoam>true</canRoam>
<disabled>false</disabled>
<jdk>(Default)</jdk>
<triggers class="vector"/>
<builders/>
<publishers/>
<buildWrappers/>
</project>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version='1.0' encoding='UTF-8'?>
<project>
<actions/>
<description></description>
<properties>
<hudson.security.AuthorizationMatrixProperty>
<useProjectSecurity>true</useProjectSecurity>
<permission>hudson.model.Item.ExtendedRead:alice</permission>
<permission>hudson.model.Item.Configure:alice</permission>
<permission>hudson.model.Item.Configure:bob</permission>
<permission>hudson.model.Item.ExtendedRead:charlie</permission>
</hudson.security.AuthorizationMatrixProperty>
</properties>
<scm class="hudson.scm.NullSCM"/>
<canRoam>true</canRoam>
<disabled>false</disabled>
<jdk>(Default)</jdk>
<triggers class="vector"/>
<builders/>
<publishers/>
<buildWrappers/>
</project>
Binary file not shown.

0 comments on commit 446201c

Please sign in to comment.