Skip to content

Commit

Permalink
Added OnlyEngine filter
Browse files Browse the repository at this point in the history
  • Loading branch information
jlink committed Nov 8, 2015
1 parent e1184f5 commit 4ea5e69
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
Expand Up @@ -48,6 +48,10 @@ public static Predicate<TestDescriptor> filterByTags(String... tagNames) {
// @formatter:on
}

public static Predicate<TestDescriptor> filterByEngine(String engineId) {
return (TestDescriptor descriptor) -> descriptor.getUniqueId().startsWith(engineId);
}

public static TestPlanSpecification build(TestPlanSpecificationElement... elements) {
return build(Arrays.asList(elements));
}
Expand All @@ -70,7 +74,7 @@ public Iterator<TestPlanSpecificationElement> iterator() {
}

public void filterWith(Predicate<TestDescriptor> filter) {
descriptorFilter = filter;
descriptorFilter = descriptorFilter.and(filter);
}

public boolean acceptDescriptor(TestDescriptor descriptor) {
Expand Down
Expand Up @@ -25,6 +25,7 @@
import java.util.Map;
import java.util.function.Predicate;

import org.junit.gen5.commons.util.StringUtils;
import org.junit.gen5.engine.TestDescriptor;
import org.junit.gen5.engine.TestPlanSpecification;
import org.junit.gen5.engine.TestPlanSpecificationElement;
Expand Down Expand Up @@ -134,6 +135,29 @@ private static String[] getAnnotatedOnlyIncludeTags(Class<?> testClass) throws I
return annotation.value();
}

/**
* The <code>Packages</code> annotation specifies names of packages to be run when a class
* annotated with <code>@RunWith(JUnit5.class)</code> is run.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
public @interface OnlyEngine {

/**
* @return engineId
*/
String value();
}

private static String getAnnotatedOnlyEngine(Class<?> testClass) throws InitializationError {
OnlyEngine annotation = testClass.getAnnotation(OnlyEngine.class);
if (annotation == null) {
return "";
}
return annotation.value();
}

private final Class<?> testClass;
private TestPlanSpecification specification = null;
private Description description;
Expand All @@ -159,18 +183,31 @@ private TestPlanSpecification createSpecification() throws InitializationError {
specs.add(TestPlanSpecification.forClassName(testClass.getName()));
}
TestPlanSpecification plan = TestPlanSpecification.build(specs);
String[] onlyIncludeTags = getAnnotatedOnlyIncludeTags(testClass);
if (onlyIncludeTags.length > 0) {
Predicate<TestDescriptor> tagNamesFilter = TestPlanSpecification.filterByTags(onlyIncludeTags);
plan.filterWith(tagNamesFilter);
}
addOnlyIncludeTagsFilter(plan);
addOnlyIncludeEngineFilter(plan);
return plan;
}
catch (Exception e) {
throw new InitializationError(e);
}
}

private void addOnlyIncludeTagsFilter(TestPlanSpecification plan) throws InitializationError {
String[] onlyIncludeTags = getAnnotatedOnlyIncludeTags(testClass);
if (onlyIncludeTags.length > 0) {
Predicate<TestDescriptor> tagNamesFilter = TestPlanSpecification.filterByTags(onlyIncludeTags);
plan.filterWith(tagNamesFilter);
}
}

private void addOnlyIncludeEngineFilter(TestPlanSpecification plan) throws InitializationError {
String onlyIncludeEngine = getAnnotatedOnlyEngine(testClass);
if (!StringUtils.isBlank(onlyIncludeEngine)) {
Predicate<TestDescriptor> engineFilter = TestPlanSpecification.filterByEngine(onlyIncludeEngine);
plan.filterWith(engineFilter);
}
}

private Collection<TestPlanSpecificationElement> getPackagesSpecificationElements() throws InitializationError {
String[] packages = getAnnotatedPackages(testClass);
return stream(packages).map(TestPlanSpecification::forPackage).collect(toList());
Expand Down
Expand Up @@ -11,20 +11,20 @@
package com.example;

import static org.junit.gen5.junit4runner.JUnit5.*;
import static org.junit.gen5.junit4runner.JUnit5.Packages;

import org.junit.gen5.junit4runner.JUnit5;
import org.junit.gen5.junit4runner.JUnit5.Classes;
import org.junit.gen5.junit4runner.JUnit5.Packages;
import org.junit.gen5.junit4runner.JUnit5.UniqueIds;
import org.junit.runner.RunWith;

@RunWith(JUnit5.class)
@Classes({ SucceedingTestCase.class })
@Classes({ SampleTestCase.class, SucceedingTestCase.class, JUnit4TestCase.class })
@UniqueIds({ "junit5:com.example.SampleTestCase#assertAllTest()",
"junit5:com.example.SampleTestCase#assertAllFailingTest()" })
@Packages({ "com.example.subpackage" })
@OnlyIncludeTags({ "fast" })
//@Classes({ SampleTestCase.class, SucceedingTestCase.class, JUnit4TestCase.class })
@OnlyEngine("junit5")
public class JUnit4SamplesSuite {

// When you have the following method, it overrides the Classes annotation
Expand Down
2 changes: 2 additions & 0 deletions sample-project/src/test/java/com/example/SampleTestCase.java
Expand Up @@ -19,6 +19,7 @@
import org.junit.gen5.api.After;
import org.junit.gen5.api.Before;
import org.junit.gen5.api.Name;
import org.junit.gen5.api.Tag;
import org.junit.gen5.api.Test;
import org.opentestalliance.TestSkippedException;

Expand Down Expand Up @@ -71,6 +72,7 @@ void abortedTest() {
}

@Test
@Tag("fast")
void failingTest() {
fail("This test will always fail");
}
Expand Down

0 comments on commit 4ea5e69

Please sign in to comment.