Skip to content

Commit b845192

Browse files
Matthias Gutheiljaikiran
authored andcommitted
[junitlauncher] Add support for selecting tests based on JUnit-5 tag functionality
This closes apache#93 pull request at github/apache/ant repo
1 parent 1cb2f81 commit b845192

File tree

13 files changed

+383
-1
lines changed

13 files changed

+383
-1
lines changed

CONTRIBUTORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ Matthew Warman
281281
Matthew Watson
282282
Matthew Yanos
283283
Matthias Bhend
284+
Matthias Gutheil
284285
Michael Bayne
285286
Michael Clarke
286287
Michael Davey

WHATSNEW

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ Fixed bugs:
1212
has now been fixed.
1313
Bugzilla Report 63446
1414

15+
Other changes:
16+
--------------
17+
18+
* junitlauncher task now supports selecting test classes for execution,
19+
based on the JUnit 5 tags, through the new "includeTags" and
20+
"excludeTags" attributes.
21+
1522

1623
Changes from Ant 1.10.5 TO Ant 1.10.6
1724
=====================================

contributors.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,10 @@
11671167
<first>Matthias</first>
11681168
<last>Bhend</last>
11691169
</name>
1170+
<name>
1171+
<first>Matthias</first>
1172+
<last>Gutheil</last>
1173+
</name>
11701174
<name>
11711175
<first>Michael</first>
11721176
<last>Bayne</last>

manual/Tasks/junitlauncher.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,20 @@ <h3>Parameters</h3>
129129
<th scope="col">Description</th>
130130
<th scope="col">Required</th>
131131
</tr>
132+
<tr>
133+
<td>includeTags</td>
134+
<td>A comma separated list of <a href="https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations">JUnit 5 tags</a>, describing the tests to include.
135+
<p><em>Since Ant 1.10.7</em></p>
136+
</td>
137+
<td>No</td>
138+
</tr>
139+
<tr>
140+
<td>excludeTags</td>
141+
<td>A comma separated list of <a href="https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations">JUnit 5 tags</a>, describing the tests to exclude.
142+
<p><em>Since Ant 1.10.7</em></p>
143+
</td>
144+
<td>No</td>
145+
</tr>
132146
<tr>
133147
<td>haltOnFailure</td>
134148
<td>A value of <q>true</q> implies that build has to stop if any failure occurs in any of

src/etc/testcases/taskdefs/optional/junitlauncher.xml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,5 +268,70 @@
268268
</testclasses>
269269
</junitlauncher>
270270
</target>
271+
272+
<target name="test-method-with-include-tag" depends="init">
273+
<!-- junitlauncher includeTags="fast, superfast" excludeTags="slow"-->
274+
<junitlauncher includeTags=" fast , superfast" excludeTags=" slow">
275+
<test name="org.example.junitlauncher.jupiter.JupiterSampleTest" outputdir="${output.dir}">
276+
<listener classname="org.example.junitlauncher.Tracker" outputDir="${output.dir}"
277+
resultFile="${test-method-with-include-tag.tracker}" if="test-method-with-include-tag.tracker" />
278+
</test>
279+
</junitlauncher>
280+
</target>
281+
282+
<target name="test-method-with-exclude-tag" depends="init">
283+
<junitlauncher excludeTags="slow">
284+
<test name="org.example.junitlauncher.jupiter.JupiterSampleTest" outputdir="${output.dir}">
285+
<listener classname="org.example.junitlauncher.Tracker" outputDir="${output.dir}"
286+
resultFile="${test-method-with-exclude-tag.tracker}" if="test-method-with-exclude-tag.tracker" />
287+
</test>
288+
</junitlauncher>
289+
</target>
290+
291+
<target name="test-method-with-tag-2-classes" depends="init">
292+
<junitlauncher includeTags="fast">
293+
<test name="org.example.junitlauncher.jupiter.JupiterSampleTest" outputdir="${output.dir}">
294+
<listener classname="org.example.junitlauncher.Tracker" outputDir="${output.dir}"
295+
resultFile="${test-method-with-tag-2-classes1.tracker}" if="test-method-with-tag-2-classes1.tracker" />
296+
</test>
297+
<test name="org.example.junitlauncher.jupiter.JupiterTagSampleTest" outputdir="${output.dir}">
298+
<listener classname="org.example.junitlauncher.Tracker" outputDir="${output.dir}"
299+
resultFile="${test-method-with-tag-2-classes2.tracker}" if="test-method-with-tag-2-classes2.tracker" />
300+
</test>
301+
</junitlauncher>
302+
</target>
303+
304+
<target name="test-method-with-tag-fileset" depends="init">
305+
<property name="junitlauncher.test.tracker.append.file" value="${output.dir}/${test-method-with-tag-fileset.tracker}"/>
306+
<junitlauncher includeTags="fast">
307+
<testclasses outputdir="${output.dir}">
308+
<fileset dir="${build.classes.dir}">
309+
<include name="org/example/junitlauncher/jupiter/*" />
310+
</fileset>
311+
<listener classname="org.example.junitlauncher.Tracker" outputDir="${output.dir}"
312+
if="test-method-with-tag-fileset.tracker" />
313+
</testclasses>
314+
</junitlauncher>
315+
</target>
316+
317+
<target name="test-method-with-tag-fileset-fork" depends="init">
318+
<property name="junitlauncher.test.tracker.append.file" value="${output.dir}/${test-method-with-tag-fileset-fork.tracker}" />
319+
<junitlauncher includeTags="fast">
320+
<classpath refid="junit.engine.jupiter.classpath" />
321+
<classpath>
322+
<pathelement location="${build.classes.dir}" />
323+
</classpath>
324+
<classpath refid="junit.platform.classpath" />
325+
<testclasses outputdir="${output.dir}">
326+
<fileset dir="${build.classes.dir}">
327+
<include name="org/example/junitlauncher/jupiter/*" />
328+
</fileset>
329+
<fork dir="${basedir}" includeJUnitPlatformLibraries="false">
330+
</fork>
331+
<listener classname="org.example.junitlauncher.Tracker" outputDir="${output.dir}"
332+
if="test-method-with-tag-fileset-fork.tracker" />
333+
</testclasses>
334+
</junitlauncher>
335+
</target>
271336
</project>
272337

src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package org.apache.tools.ant.taskdefs.optional.junitlauncher;
2020

21+
2122
import org.apache.tools.ant.BuildException;
2223
import org.apache.tools.ant.MagicNames;
2324
import org.apache.tools.ant.Project;
@@ -34,6 +35,7 @@
3435
import org.junit.platform.launcher.EngineFilter;
3536
import org.junit.platform.launcher.Launcher;
3637
import org.junit.platform.launcher.LauncherDiscoveryRequest;
38+
import org.junit.platform.launcher.TagFilter;
3739
import org.junit.platform.launcher.TestExecutionListener;
3840
import org.junit.platform.launcher.TestPlan;
3941
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
@@ -463,6 +465,13 @@ private void addFilters(final TestRequest testRequest) {
463465
if (enginesToExclude != null && enginesToExclude.length > 0) {
464466
requestBuilder.filters(EngineFilter.excludeEngines(enginesToExclude));
465467
}
468+
// add any tag filters
469+
if (this.launchDefinition.getIncludeTags().size() > 0) {
470+
requestBuilder.filters(TagFilter.includeTags(this.launchDefinition.getIncludeTags()));
471+
}
472+
if (this.launchDefinition.getExcludeTags().size() > 0) {
473+
requestBuilder.filters(TagFilter.excludeTags(this.launchDefinition.getExcludeTags()));
474+
}
466475
}
467476

468477
private enum StreamType {

src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/StandaloneLauncher.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,22 @@
3939
import java.util.List;
4040
import java.util.Optional;
4141
import java.util.Properties;
42+
import java.util.stream.Stream;
4243

4344
import static javax.xml.stream.XMLStreamConstants.END_DOCUMENT;
4445
import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
4546
import static javax.xml.stream.XMLStreamConstants.START_DOCUMENT;
4647
import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;
48+
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_EXCLUDE_TAGS;
4749
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_HALT_ON_FAILURE;
50+
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_INCLUDE_TAGS;
4851
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_PRINT_SUMMARY;
4952
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ELM_LAUNCH_DEF;
5053
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ELM_LISTENER;
5154
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ELM_TEST;
5255
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ELM_TEST_CLASSES;
5356

57+
5458
/**
5559
* Used for launching forked tests from the {@link JUnitLauncherTask}.
5660
* <p>
@@ -140,6 +144,14 @@ private static ForkedLaunch parseLaunchDefinition(final Path pathToLaunchDefXml)
140144
if (haltOnfFailure != null) {
141145
forkedLaunch.setHaltOnFailure(Boolean.parseBoolean(haltOnfFailure));
142146
}
147+
final String includeTags = reader.getAttributeValue(null, LD_XML_ATTR_INCLUDE_TAGS);
148+
if (includeTags != null) {
149+
Stream.of(includeTags.split(",")).forEach(i -> forkedLaunch.addIncludeTag(i));
150+
}
151+
final String excludeTags = reader.getAttributeValue(null, LD_XML_ATTR_EXCLUDE_TAGS);
152+
if (excludeTags != null) {
153+
Stream.of(excludeTags.split(",")).forEach(e -> forkedLaunch.addExcludeTag(e));
154+
}
143155
final String printSummary = reader.getAttributeValue(null, LD_XML_ATTR_PRINT_SUMMARY);
144156
if (printSummary != null) {
145157
forkedLaunch.setPrintSummary(Boolean.parseBoolean(printSummary));
@@ -204,6 +216,8 @@ private static final class ForkedLaunch implements LaunchDefinition {
204216
private boolean haltOnFailure;
205217
private List<TestDefinition> tests = new ArrayList<>();
206218
private List<ListenerDefinition> listeners = new ArrayList<>();
219+
private List<String> includeTags = new ArrayList<>();
220+
private List<String> excludeTags = new ArrayList<>();
207221

208222
@Override
209223
public List<TestDefinition> getTests() {
@@ -249,5 +263,23 @@ public ForkedLaunch setHaltOnFailure(final boolean haltOnFailure) {
249263
public ClassLoader getClassLoader() {
250264
return this.getClass().getClassLoader();
251265
}
266+
267+
void addIncludeTag(final String filter) {
268+
this.includeTags.add(filter);
269+
}
270+
271+
@Override
272+
public List<String> getIncludeTags() {
273+
return includeTags;
274+
}
275+
276+
void addExcludeTag(final String filter) {
277+
this.excludeTags.add(filter);
278+
}
279+
280+
@Override
281+
public List<String> getExcludeTags() {
282+
return excludeTags;
283+
}
252284
}
253285
}

src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public final class Constants {
3636
public static final String LD_XML_ELM_TEST = "test";
3737
public static final String LD_XML_ELM_TEST_CLASSES = "test-classes";
3838
public static final String LD_XML_ATTR_HALT_ON_FAILURE = "haltOnFailure";
39+
public static final String LD_XML_ATTR_INCLUDE_TAGS = "includeTags";
40+
public static final String LD_XML_ATTR_EXCLUDE_TAGS = "excludeTags";
3941
public static final String LD_XML_ATTR_OUTPUT_DIRECTORY = "outDir";
4042
public static final String LD_XML_ATTR_INCLUDE_ENGINES = "includeEngines";
4143
public static final String LD_XML_ATTR_EXCLUDE_ENGINES = "excludeEngines";

src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/JUnitLauncherTask.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,17 @@
4141
import java.util.Hashtable;
4242
import java.util.List;
4343
import java.util.Properties;
44+
import java.util.StringTokenizer;
4445
import java.util.concurrent.TimeoutException;
46+
import java.util.stream.Collectors;
4547

48+
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_EXCLUDE_TAGS;
4649
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_HALT_ON_FAILURE;
50+
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_INCLUDE_TAGS;
4751
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_PRINT_SUMMARY;
4852
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ELM_LAUNCH_DEF;
4953

54+
5055
/**
5156
* An Ant {@link Task} responsible for launching the JUnit platform for running tests.
5257
* This requires a minimum of JUnit 5, since that's the version in which the JUnit platform launcher
@@ -75,6 +80,8 @@ public class JUnitLauncherTask extends Task {
7580
private boolean printSummary;
7681
private final List<TestDefinition> tests = new ArrayList<>();
7782
private final List<ListenerDefinition> listeners = new ArrayList<>();
83+
private List<String> includeTags = new ArrayList<>();
84+
private List<String> excludeTags = new ArrayList<>();
7885

7986
public JUnitLauncherTask() {
8087
}
@@ -158,6 +165,32 @@ public void setPrintSummary(final boolean printSummary) {
158165
this.printSummary = printSummary;
159166
}
160167

168+
/**
169+
* Tags to include. Will trim each tag.
170+
*
171+
* @param includes comma separated list of tags to include while running the tests.
172+
* @since Ant 1.10.7
173+
*/
174+
public void setIncludeTags(final String includes) {
175+
final StringTokenizer tokens = new StringTokenizer(includes, ",");
176+
while (tokens.hasMoreTokens()) {
177+
includeTags.add(tokens.nextToken().trim());
178+
}
179+
}
180+
181+
/**
182+
* Tags to exclude. Will trim each tag.
183+
*
184+
* @param excludes comma separated list of tags to exclude while running the tests.
185+
* @since Ant 1.10.7
186+
*/
187+
public void setExcludeTags(final String excludes) {
188+
final StringTokenizer tokens = new StringTokenizer(excludes, ",");
189+
while (tokens.hasMoreTokens()) {
190+
excludeTags.add(tokens.nextToken().trim());
191+
}
192+
}
193+
161194
private void preConfigure(final TestDefinition test) {
162195
if (test.getHaltOnFailure() == null) {
163196
test.setHaltOnFailure(this.haltOnFailure);
@@ -233,6 +266,12 @@ private void forkTest(final TestDefinition test) {
233266
if (this.haltOnFailure) {
234267
writer.writeAttribute(LD_XML_ATTR_HALT_ON_FAILURE, "true");
235268
}
269+
if (this.includeTags.size() > 0) {
270+
writer.writeAttribute(LD_XML_ATTR_INCLUDE_TAGS, commaSeparatedListElements(includeTags));
271+
}
272+
if (this.excludeTags.size() > 0) {
273+
writer.writeAttribute(LD_XML_ATTR_EXCLUDE_TAGS, commaSeparatedListElements(excludeTags));
274+
}
236275
// task level listeners
237276
for (final ListenerDefinition listenerDef : this.listeners) {
238277
if (!listenerDef.shouldUse(getProject())) {
@@ -294,6 +333,12 @@ private void forkTest(final TestDefinition test) {
294333
}
295334
}
296335

336+
private static String commaSeparatedListElements(final List<String> stringList) {
337+
return stringList.stream()
338+
.map(Object::toString)
339+
.collect(Collectors.joining(", "));
340+
}
341+
297342
private int executeForkedTest(final ForkDefinition forkDefinition, final CommandlineJava commandlineJava) {
298343
final LogOutputStream outStream = new LogOutputStream(this, Project.MSG_INFO);
299344
final LogOutputStream errStream = new LogOutputStream(this, Project.MSG_WARN);
@@ -359,6 +404,16 @@ public boolean isHaltOnFailure() {
359404
return haltOnFailure;
360405
}
361406

407+
@Override
408+
public List<String> getIncludeTags() {
409+
return includeTags;
410+
}
411+
412+
@Override
413+
public List<String> getExcludeTags() {
414+
return excludeTags;
415+
}
416+
362417
@Override
363418
public ClassLoader getClassLoader() {
364419
return this.executionCL;

src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/LaunchDefinition.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,15 @@ public interface LaunchDefinition {
5555
*/
5656
ClassLoader getClassLoader();
5757

58+
/**
59+
* @return Returns the list of tags which will be used to evaluate tests that need to be included
60+
* in the test execution
61+
*/
62+
List<String> getIncludeTags();
63+
64+
/**
65+
* @return Returns the list of tags which will be used to evaluate tests that need to be excluded
66+
* from the test execution
67+
*/
68+
List<String> getExcludeTags();
5869
}

0 commit comments

Comments
 (0)