Skip to content

Commit 53c0134

Browse files
committed
Implemented tag filtering
1 parent 4e7f946 commit 53c0134

File tree

6 files changed

+116
-25
lines changed

6 files changed

+116
-25
lines changed

src/main/java/org/pitest/junit5/JUnit5Configuration.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.pitest.help.PitHelpError;
2121
import org.pitest.testapi.Configuration;
22+
import org.pitest.testapi.TestGroupConfig;
2223
import org.pitest.testapi.TestSuiteFinder;
2324
import org.pitest.testapi.TestUnitFinder;
2425

@@ -28,15 +29,18 @@
2829
*/
2930
public class JUnit5Configuration implements Configuration {
3031

32+
private final TestGroupConfig testGroupConfig;
33+
3134
private final Collection<String> includedTestMethods;
3235

33-
public JUnit5Configuration(Collection<String> includedTestMethods) {
36+
public JUnit5Configuration(TestGroupConfig testGroupConfig, Collection<String> includedTestMethods) {
37+
this.testGroupConfig = testGroupConfig;
3438
this.includedTestMethods = includedTestMethods;
3539
}
3640

3741
@Override
3842
public TestUnitFinder testUnitFinder() {
39-
return new JUnit5TestUnitFinder(includedTestMethods);
43+
return new JUnit5TestUnitFinder(testGroupConfig, includedTestMethods);
4044
}
4145

4246
@Override

src/main/java/org/pitest/junit5/JUnit5TestPluginFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public Configuration createTestFrameworkConfiguration(TestGroupConfig config,
3131
ClassByteArraySource source,
3232
Collection<String> excludedRunners,
3333
Collection<String> includedTestMethods) {
34-
return new JUnit5Configuration(includedTestMethods);
34+
return new JUnit5Configuration(config, includedTestMethods);
3535
}
3636

3737
@Override

src/main/java/org/pitest/junit5/JUnit5TestUnitFinder.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,24 @@
1414
*/
1515
package org.pitest.junit5;
1616

17+
import java.util.ArrayList;
1718
import java.util.Collection;
1819
import java.util.List;
1920
import java.util.Set;
2021

2122
import static java.util.Collections.emptyList;
2223
import static java.util.stream.Collectors.toList;
24+
25+
import org.junit.platform.commons.util.PreconditionViolationException;
26+
import org.junit.platform.engine.Filter;
2327
import org.junit.platform.engine.discovery.DiscoverySelectors;
2428
import org.junit.platform.engine.support.descriptor.MethodSource;
2529
import org.junit.platform.launcher.Launcher;
30+
import org.junit.platform.launcher.TagFilter;
2631
import org.junit.platform.launcher.TestPlan;
2732
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
2833
import org.junit.platform.launcher.core.LauncherFactory;
34+
import org.pitest.testapi.TestGroupConfig;
2935
import org.pitest.testapi.TestUnit;
3036
import org.pitest.testapi.TestUnitFinder;
3137

@@ -35,11 +41,14 @@
3541
*/
3642
public class JUnit5TestUnitFinder implements TestUnitFinder {
3743

44+
private final TestGroupConfig testGroupConfig;
45+
3846
private final Collection<String> includedTestMethods;
3947

4048
private final Launcher launcher;
4149

42-
public JUnit5TestUnitFinder(Collection<String> includedTestMethods) {
50+
public JUnit5TestUnitFinder(TestGroupConfig testGroupConfig, Collection<String> includedTestMethods) {
51+
this.testGroupConfig = testGroupConfig;
4352
this.includedTestMethods = includedTestMethods;
4453
this.launcher = LauncherFactory.create();
4554
}
@@ -50,9 +59,25 @@ public List<TestUnit> findTestUnits(Class<?> clazz) {
5059
return emptyList();
5160
}
5261

62+
List<Filter> filters = new ArrayList<>(2);
63+
try {
64+
List<String> excludedGroups = testGroupConfig.getExcludedGroups();
65+
if(excludedGroups != null && !excludedGroups.isEmpty()) {
66+
filters.add(TagFilter.excludeTags(excludedGroups));
67+
}
68+
69+
List<String> includedGroups = testGroupConfig.getIncludedGroups();
70+
if(includedGroups != null && !includedGroups.isEmpty()) {
71+
filters.add(TagFilter.includeTags(includedGroups));
72+
}
73+
} catch(PreconditionViolationException e) {
74+
throw new IllegalArgumentException("Error creating tag filter", e);
75+
}
76+
5377
TestPlan testPlan = launcher.discover(LauncherDiscoveryRequestBuilder
5478
.request()
5579
.selectors(DiscoverySelectors.selectClass(clazz))
80+
.filters(filters.toArray(new Filter[filters.size()]))
5681
.build());
5782

5883
return testPlan.getRoots()

src/test/java/org/pitest/junit5/JUnit5TestUnitFinderTest.java

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828
import org.pitest.junit5.repository.TestClassWithNestedClassWithNestedAnnotationAndNestedTestFactoryAnnotation;
2929
import org.pitest.junit5.repository.TestClassWithParameterizedTestAnnotation;
3030
import org.pitest.junit5.repository.TestClassWithRepeatedTestAnnotation;
31+
import org.pitest.junit5.repository.TestClassWithTags;
3132
import org.pitest.junit5.repository.TestClassWithTestAnnotation;
3233
import org.pitest.junit5.repository.TestClassWithTestFactoryAnnotation;
3334
import org.pitest.junit5.repository.TestClassWithTestTemplateAnnotation;
3435
import org.pitest.junit5.repository.TestClassWithoutAnnotations;
36+
import org.pitest.testapi.TestGroupConfig;
3537

3638
/**
3739
*
@@ -44,77 +46,87 @@ public JUnit5TestUnitFinderTest() {
4446

4547
@Test
4648
public void testTestClassWithParameterizedTestAnnotation() {
47-
assertThat(new JUnit5TestUnitFinder(emptyList()).findTestUnits(TestClassWithParameterizedTestAnnotation.class)).hasSize(1);
49+
assertThat(new JUnit5TestUnitFinder(new TestGroupConfig(), emptyList()).findTestUnits(TestClassWithParameterizedTestAnnotation.class)).hasSize(1);
4850
}
4951

5052
@Test
5153
public void testTestClassWithRepeatedTestAnnotation() {
52-
assertThat(new JUnit5TestUnitFinder(emptyList()).findTestUnits(TestClassWithRepeatedTestAnnotation.class)).hasSize(1);
54+
assertThat(new JUnit5TestUnitFinder(new TestGroupConfig(), emptyList()).findTestUnits(TestClassWithRepeatedTestAnnotation.class)).hasSize(1);
5355
}
5456

5557
@Test
5658
public void testTestClassWithTestAnnotation() {
57-
assertThat(new JUnit5TestUnitFinder(emptyList()).findTestUnits(TestClassWithTestAnnotation.class)).hasSize(1);
59+
assertThat(new JUnit5TestUnitFinder(new TestGroupConfig(), emptyList()).findTestUnits(TestClassWithTestAnnotation.class)).hasSize(1);
5860
}
5961

6062
@Test
6163
public void testTestClassWithTestFactoryAnnotation() {
62-
assertThat(new JUnit5TestUnitFinder(emptyList()).findTestUnits(TestClassWithTestFactoryAnnotation.class)).hasSize(1);
64+
assertThat(new JUnit5TestUnitFinder(new TestGroupConfig(), emptyList()).findTestUnits(TestClassWithTestFactoryAnnotation.class)).hasSize(1);
6365
}
6466

6567
@Test
6668
public void testTestClassWithTestTemplateAnnotation() {
67-
assertThat(new JUnit5TestUnitFinder(emptyList()).findTestUnits(TestClassWithTestTemplateAnnotation.class)).hasSize(1);
69+
assertThat(new JUnit5TestUnitFinder(new TestGroupConfig(), emptyList()).findTestUnits(TestClassWithTestTemplateAnnotation.class)).hasSize(1);
6870
}
6971

7072
@Test
7173
public void testTestClassWithNestedAnnotationAndNestedTestAnnotation() {
72-
assertThat(new JUnit5TestUnitFinder(emptyList()).findTestUnits(TestClassWithNestedAnnotationAndNestedTestAnnotation.class)).hasSize(1);
73-
assertThat(new JUnit5TestUnitFinder(emptyList()).findTestUnits(TestClassWithNestedAnnotationAndNestedTestAnnotation.NestedClass.class)).isEmpty();
74+
assertThat(new JUnit5TestUnitFinder(new TestGroupConfig(), emptyList()).findTestUnits(TestClassWithNestedAnnotationAndNestedTestAnnotation.class)).hasSize(1);
75+
assertThat(new JUnit5TestUnitFinder(new TestGroupConfig(), emptyList()).findTestUnits(TestClassWithNestedAnnotationAndNestedTestAnnotation.NestedClass.class)).isEmpty();
7476
}
7577

7678
@Test
7779
public void testTestClassWithNestedAnnotationAndNestedTestFactoryAnnotation() {
78-
assertThat(new JUnit5TestUnitFinder(emptyList()).findTestUnits(TestClassWithNestedAnnotationAndNestedTestFactoryAnnotation.class)).hasSize(1);
79-
assertThat(new JUnit5TestUnitFinder(emptyList()).findTestUnits(TestClassWithNestedAnnotationAndNestedTestFactoryAnnotation.NestedClass.class)).isEmpty();
80+
assertThat(new JUnit5TestUnitFinder(new TestGroupConfig(), emptyList()).findTestUnits(TestClassWithNestedAnnotationAndNestedTestFactoryAnnotation.class)).hasSize(1);
81+
assertThat(new JUnit5TestUnitFinder(new TestGroupConfig(), emptyList()).findTestUnits(TestClassWithNestedAnnotationAndNestedTestFactoryAnnotation.NestedClass.class)).isEmpty();
8082
}
8183

8284
@Test
8385
public void testTestClassWithNestedAnnotationWithNestedAnnotationAndNestedTestAnnotation() {
84-
assertThat(new JUnit5TestUnitFinder(emptyList()).findTestUnits(TestClassWithNestedAnnotationWithNestedAnnotationAndNestedTestAnnotation.class)).hasSize(1);
85-
assertThat(new JUnit5TestUnitFinder(emptyList()).findTestUnits(TestClassWithNestedAnnotationWithNestedAnnotationAndNestedTestAnnotation.NestedClass.class)).isEmpty();
86-
assertThat(new JUnit5TestUnitFinder(emptyList()).findTestUnits(TestClassWithNestedAnnotationWithNestedAnnotationAndNestedTestAnnotation.NestedClass.NestedNestedClass.class)).isEmpty();
86+
assertThat(new JUnit5TestUnitFinder(new TestGroupConfig(), emptyList()).findTestUnits(TestClassWithNestedAnnotationWithNestedAnnotationAndNestedTestAnnotation.class)).hasSize(1);
87+
assertThat(new JUnit5TestUnitFinder(new TestGroupConfig(), emptyList()).findTestUnits(TestClassWithNestedAnnotationWithNestedAnnotationAndNestedTestAnnotation.NestedClass.class)).isEmpty();
88+
assertThat(new JUnit5TestUnitFinder(new TestGroupConfig(), emptyList()).findTestUnits(TestClassWithNestedAnnotationWithNestedAnnotationAndNestedTestAnnotation.NestedClass.NestedNestedClass.class)).isEmpty();
8789
}
8890

8991
@Test
9092
public void testTestClassWithNestedAnnotationWithNestedAnnotationAndNestedTestFactoryAnnotation() {
91-
assertThat(new JUnit5TestUnitFinder(emptyList()).findTestUnits(TestClassWithNestedAnnotationWithNestedAnnotationAndNestedTestFactoryAnnotation.class)).hasSize(1);
92-
assertThat(new JUnit5TestUnitFinder(emptyList()).findTestUnits(TestClassWithNestedAnnotationWithNestedAnnotationAndNestedTestFactoryAnnotation.NestedClass.class)).isEmpty();
93-
assertThat(new JUnit5TestUnitFinder(emptyList()).findTestUnits(TestClassWithNestedAnnotationWithNestedAnnotationAndNestedTestFactoryAnnotation.NestedClass.NestedNestedClass.class)).isEmpty();
93+
assertThat(new JUnit5TestUnitFinder(new TestGroupConfig(), emptyList()).findTestUnits(TestClassWithNestedAnnotationWithNestedAnnotationAndNestedTestFactoryAnnotation.class)).hasSize(1);
94+
assertThat(new JUnit5TestUnitFinder(new TestGroupConfig(), emptyList()).findTestUnits(TestClassWithNestedAnnotationWithNestedAnnotationAndNestedTestFactoryAnnotation.NestedClass.class)).isEmpty();
95+
assertThat(new JUnit5TestUnitFinder(new TestGroupConfig(), emptyList()).findTestUnits(TestClassWithNestedAnnotationWithNestedAnnotationAndNestedTestFactoryAnnotation.NestedClass.NestedNestedClass.class)).isEmpty();
9496
}
9597

9698
@Test
9799
public void testTestClassWithoutAnnotations() {
98-
assertThat(new JUnit5TestUnitFinder(emptyList()).findTestUnits(TestClassWithoutAnnotations.class)).isEmpty();
100+
assertThat(new JUnit5TestUnitFinder(new TestGroupConfig(), emptyList()).findTestUnits(TestClassWithoutAnnotations.class)).isEmpty();
99101
}
100102

101103
@Test
102104
public void testTestClassWithNestedClassWithNestedAnnotationAndNestedTestAnnotation() {
103-
assertThat(new JUnit5TestUnitFinder(emptyList()).findTestUnits(TestClassWithNestedClassWithNestedAnnotationAndNestedTestAnnotation.class)).isEmpty();
105+
assertThat(new JUnit5TestUnitFinder(new TestGroupConfig(), emptyList()).findTestUnits(TestClassWithNestedClassWithNestedAnnotationAndNestedTestAnnotation.class)).isEmpty();
104106
}
105107

106108
@Test
107109
public void testTestClassWithNestedClassWithNestedAnnotationAndNestedTestFactoryAnnotation() {
108-
assertThat(new JUnit5TestUnitFinder(emptyList()).findTestUnits(TestClassWithNestedClassWithNestedAnnotationAndNestedTestFactoryAnnotation.class)).isEmpty();
110+
assertThat(new JUnit5TestUnitFinder(new TestGroupConfig(), emptyList()).findTestUnits(TestClassWithNestedClassWithNestedAnnotationAndNestedTestFactoryAnnotation.class)).isEmpty();
109111
}
110112

111113
@Test
112114
public void testTestClassWithInheritedTestMethod() {
113-
assertThat(new JUnit5TestUnitFinder(emptyList()).findTestUnits(TestClassWithInheritedTestMethod.class)).hasSize(1);
115+
assertThat(new JUnit5TestUnitFinder(new TestGroupConfig(), emptyList()).findTestUnits(TestClassWithInheritedTestMethod.class)).hasSize(1);
114116
}
115117

116118
@Test
117119
public void testTestClassWithIncludedTestMethod() {
118-
assertThat(new JUnit5TestUnitFinder(singletonList("included")).findTestUnits(TestClassWithIncludedTestMethod.class)).hasSize(1);
120+
assertThat(new JUnit5TestUnitFinder(new TestGroupConfig(), singletonList("included")).findTestUnits(TestClassWithIncludedTestMethod.class)).hasSize(1);
121+
}
122+
123+
@Test
124+
public void testTestClassWithExcludedTag() {
125+
assertThat(new JUnit5TestUnitFinder(new TestGroupConfig().withExcludedGroups("excluded"), emptyList()).findTestUnits(TestClassWithTags.class)).hasSize(3);
126+
}
127+
128+
@Test
129+
public void testTestClassWithIncludedTag() {
130+
assertThat(new JUnit5TestUnitFinder(new TestGroupConfig().withIncludedGroups("included"), emptyList()).findTestUnits(TestClassWithTags.class)).hasSize(1);
119131
}
120132
}

src/test/java/org/pitest/junit5/JUnit5TestUnitTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.pitest.junit5.repository.TestClassWithTestFactoryAnnotation;
3434
import org.pitest.testapi.Description;
3535
import org.pitest.testapi.ResultCollector;
36+
import org.pitest.testapi.TestGroupConfig;
3637

3738
/**
3839
*
@@ -131,7 +132,7 @@ public void testTestClassWithAbortingTest() {
131132

132133
private TestResultCollector findTestsIn(Class<?> clazz) {
133134
TestResultCollector resultCollector = new TestResultCollector();
134-
new JUnit5TestUnitFinder(emptyList()).findTestUnits(clazz)
135+
new JUnit5TestUnitFinder(new TestGroupConfig(), emptyList()).findTestUnits(clazz)
135136
.stream()
136137
.forEach(testUnit -> testUnit.execute(resultCollector));
137138
return resultCollector;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2019 Tobias Stadler
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing,
11+
* software distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and limitations under the License.
14+
*/
15+
package org.pitest.junit5.repository;
16+
17+
import org.junit.jupiter.api.Tag;
18+
import org.junit.jupiter.api.Test;
19+
20+
/**
21+
*
22+
* @author Tobias Stadler
23+
*/
24+
public class TestClassWithTags {
25+
26+
@Test
27+
public void testWithoutTag() {
28+
29+
}
30+
31+
@Test
32+
@Tag("foo")
33+
public void testWithTag() {
34+
35+
}
36+
37+
@Test
38+
@Tag("included")
39+
public void testWithIncludedTag() {
40+
41+
}
42+
43+
@Test
44+
@Tag("excluded")
45+
public void testWithExcludedTag() {
46+
47+
}
48+
49+
}

0 commit comments

Comments
 (0)