-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: offa <[email protected]> Co-authored-by: Réda Housni Alaoui <[email protected]>
- Loading branch information
1 parent
6d4e97d
commit 14f5dba
Showing
7 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/org/jvnet/hudson/test/junit/jupiter/JUnit5JenkinsRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.jvnet.hudson.test.junit.jupiter; | ||
|
||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import java.lang.reflect.Method; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.ParameterContext; | ||
import org.junit.runner.Description; | ||
import org.jvnet.hudson.test.JenkinsRecipe; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
/** | ||
* Provides JUnit 5 compatibility for {@link JenkinsRule}. | ||
*/ | ||
class JUnit5JenkinsRule extends JenkinsRule { | ||
private final ParameterContext context; | ||
|
||
JUnit5JenkinsRule(@NonNull ParameterContext context, @NonNull ExtensionContext extensionContext) { | ||
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)); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/org/jvnet/hudson/test/junit/jupiter/JenkinsExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.jvnet.hudson.test.junit.jupiter; | ||
|
||
import org.junit.jupiter.api.extension.AfterEachCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.ParameterContext; | ||
import org.junit.jupiter.api.extension.ParameterResolutionException; | ||
import org.junit.jupiter.api.extension.ParameterResolver; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
/** | ||
* JUnit 5 extension providing {@link JenkinsRule} integration. | ||
* | ||
* @see WithJenkins | ||
*/ | ||
class JenkinsExtension implements ParameterResolver, AfterEachCallback { | ||
|
||
private static final String KEY = "jenkins-instance"; | ||
private static final ExtensionContext.Namespace NAMESPACE = ExtensionContext.Namespace.create(JenkinsExtension.class); | ||
|
||
@Override | ||
public void afterEach(ExtensionContext context) throws Exception { | ||
final JenkinsRule rule = context.getStore(NAMESPACE).remove(KEY, JenkinsRule.class); | ||
if (rule == null) { | ||
return; | ||
} | ||
rule.after(); | ||
} | ||
|
||
@Override | ||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { | ||
return parameterContext.getParameter().getType().equals(JenkinsRule.class); | ||
} | ||
|
||
@Override | ||
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { | ||
final JenkinsRule rule = | ||
extensionContext | ||
.getStore(NAMESPACE) | ||
.getOrComputeIfAbsent( | ||
KEY, | ||
key -> new JUnit5JenkinsRule(parameterContext, extensionContext), | ||
JenkinsRule.class); | ||
|
||
try { | ||
rule.before(); | ||
return rule; | ||
} catch (Throwable t) { | ||
throw new ParameterResolutionException(t.getMessage(), t); | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/org/jvnet/hudson/test/junit/jupiter/WithJenkins.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package org.jvnet.hudson.test.junit.jupiter; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
/** | ||
* JUnit 5 meta annotation providing {@link JenkinsRule JenkinsRule} integration. | ||
* | ||
* <p>Test methods using the rule extension need to accept it by {@link JenkinsRule JenkinsRule} parameter; | ||
* each test case gets a new rule object. | ||
* An annotated method without a {@link JenkinsRule JenkinsRule} parameter behaves as if it were not annotated. | ||
* | ||
* <p>Annotating a <em>class</em> provides access for all of its tests. | ||
* Unrelated test cases can omit the parameter. | ||
* | ||
* <blockquote> | ||
* | ||
* <pre> | ||
* @WithJenkins | ||
* class ExampleJUnit5Test { | ||
* | ||
* @Test | ||
* public void example(JenkinsRule r) { | ||
* // use 'r' ... | ||
* } | ||
* | ||
* @Test | ||
* public void exampleNotUsingRule() { | ||
* // ... | ||
* } | ||
* } | ||
* </pre> | ||
* | ||
* </blockquote> | ||
* | ||
* <p>Annotating a <i>method</i> limits access to the method. | ||
* | ||
* <blockquote> | ||
* | ||
* <pre> | ||
* class ExampleJUnit5Test { | ||
* | ||
* @WithJenkins | ||
* @Test | ||
* public void example(JenkinsRule r) { | ||
* // use 'r' ... | ||
* } | ||
* } | ||
* </pre> | ||
* | ||
* </blockquote> | ||
* | ||
* @see JenkinsExtension | ||
* @see org.junit.jupiter.api.extension.ExtendWith | ||
*/ | ||
@Target({ElementType.TYPE, ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@ExtendWith(JenkinsExtension.class) | ||
public @interface WithJenkins {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/test/java/org/jvnet/hudson/test/junit/jupiter/JenkinsRuleResolverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.jvnet.hudson.test.junit.jupiter; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.collection.IsCollectionWithSize.hasSize; | ||
import static org.hamcrest.collection.IsEmptyCollection.empty; | ||
|
||
import java.io.IOException; | ||
import org.junit.jupiter.api.Test; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
@WithJenkins | ||
class JenkinsRuleResolverTest { | ||
|
||
@Test | ||
void jenkinsRuleIsAccessible(JenkinsRule rule) throws IOException { | ||
assertThat(rule.jenkins.getJobNames(), empty()); | ||
rule.createFreeStyleProject("job-0"); | ||
assertThat(rule.jenkins.getJobNames(), hasSize(1)); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...test/java/org/jvnet/hudson/test/junit/jupiter/JenkinsRuleResolverWithMethodScopeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.jvnet.hudson.test.junit.jupiter; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.collection.IsCollectionWithSize.hasSize; | ||
import static org.hamcrest.collection.IsEmptyCollection.empty; | ||
|
||
import java.io.IOException; | ||
import org.junit.jupiter.api.Test; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
class JenkinsRuleResolverWithMethodScopeTest { | ||
|
||
@Test | ||
@WithJenkins | ||
void jenkinsRuleIsAccessible(JenkinsRule rule) throws IOException { | ||
assertThat(rule.jenkins.getJobNames(), empty()); | ||
rule.createFreeStyleProject("job-0"); | ||
assertThat(rule.jenkins.getJobNames(), hasSize(1)); | ||
} | ||
} |