forked from jenkinsci/git-plugin
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-72993] Add a junit5 extension to wrap the GitSampleRepoRule (j…
- Loading branch information
Showing
5 changed files
with
151 additions
and
2 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
45 changes: 45 additions & 0 deletions
45
src/test/java/jenkins/plugins/git/junit/jupiter/GitSampleRepoExtension.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,45 @@ | ||
package jenkins.plugins.git.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 jenkins.plugins.git.GitSampleRepoRule; | ||
|
||
/** | ||
* A Junit5 extension to use {@link GitSampleRepoRule} with Junit5 | ||
* See {@link WithGitSampleRepo} for details | ||
*/ | ||
public class GitSampleRepoExtension implements ParameterResolver, AfterEachCallback { | ||
|
||
private static final String KEY = "git-sample-repo"; | ||
private static final ExtensionContext.Namespace NAMESPACE = ExtensionContext.Namespace.create(GitSampleRepoExtension.class); | ||
|
||
@Override | ||
public void afterEach(ExtensionContext context) { | ||
var rule = context.getStore(NAMESPACE).remove(KEY, GitSampleRepoRule.class); | ||
if (rule != null) { | ||
rule.after(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { | ||
return parameterContext.getParameter().getType().equals(GitSampleRepoRule.class); | ||
} | ||
|
||
@Override | ||
public GitSampleRepoRule resolveParameter(ParameterContext parameterContext, ExtensionContext context) { | ||
var rule = context.getStore(NAMESPACE).getOrComputeIfAbsent(KEY, key -> new GitSampleRepoRule(), GitSampleRepoRule.class); | ||
if (rule != null) { | ||
try { | ||
rule.before(); | ||
} catch (Throwable t) { | ||
throw new ParameterResolutionException(t.getMessage(), t); | ||
} | ||
} | ||
return rule; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/test/java/jenkins/plugins/git/junit/jupiter/GitSampleRepoExtensionClassTest.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 jenkins.plugins.git.junit.jupiter; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import jenkins.plugins.git.GitSampleRepoRule; | ||
|
||
@WithGitSampleRepo | ||
class GitSampleRepoExtensionClassTest { | ||
|
||
@Test | ||
void gitSampleRepoIsInjected(GitSampleRepoRule rule) throws Exception { | ||
Assertions.assertNotNull(rule); | ||
// somehow testing initialization | ||
var root = rule.getRoot(); | ||
Assertions.assertNotNull(root); | ||
rule.init(); | ||
Assertions.assertNotNull(rule.head()); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/test/java/jenkins/plugins/git/junit/jupiter/GitSampleRepoExtensionMethodTest.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 jenkins.plugins.git.junit.jupiter; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import jenkins.plugins.git.GitSampleRepoRule; | ||
|
||
class GitSampleRepoExtensionMethodTest { | ||
|
||
@WithGitSampleRepo | ||
@Test | ||
void gitSampleRepoIsInjected(GitSampleRepoRule rule) throws Exception { | ||
Assertions.assertNotNull(rule); | ||
// somehow testing initialization | ||
var root = rule.getRoot(); | ||
Assertions.assertNotNull(root); | ||
rule.init(); | ||
Assertions.assertNotNull(rule.head()); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/test/java/jenkins/plugins/git/junit/jupiter/WithGitSampleRepo.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,62 @@ | ||
package jenkins.plugins.git.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 jenkins.plugins.git.GitSampleRepoRule; | ||
|
||
/** | ||
* A meta annotation for the {@link GitSampleRepoExtension} to use {@link GitSampleRepoRule} with Junit5. | ||
* Two possible usages: | ||
* <p> | ||
* 1. Class annotation: each method of the class can have the rule injected as a parameter: | ||
* | ||
* <blockquote> | ||
* <pre> | ||
* @WithGitSampleRepo | ||
* class ClassLevelAnnotationTest { | ||
* | ||
* @Test | ||
* void usingRule(GitSampleRepoRule rule) { | ||
* // ... | ||
* } | ||
* | ||
* @Test | ||
* void notUsingRule() { | ||
* // ... | ||
* } | ||
* } | ||
* </pre> | ||
* </blockquote> | ||
* <p> | ||
* 2. Method annotation: only the annotated method can have the rule injected as a parameter: | ||
* | ||
* <blockquote> | ||
* <pre> | ||
* class MethodLevelAnnotationTest { | ||
* | ||
* @WithGitSampleRepo | ||
* @Test | ||
* void usingRule(GitSampleRepoRule rule) { | ||
* // ... | ||
* } | ||
* | ||
* @Test | ||
* void notUsingRule() { | ||
* // ... | ||
* } | ||
* } | ||
* </pre> | ||
* </blockquote> | ||
*/ | ||
@Target({ ElementType.TYPE, ElementType.METHOD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@ExtendWith(GitSampleRepoExtension.class) | ||
public @interface WithGitSampleRepo { | ||
} |