Skip to content

Commit

Permalink
[junitlauncher] Add support for selecting tests based on JUnit-5 tag …
Browse files Browse the repository at this point in the history
…functionality

This closes apache#93 pull request at github/apache/ant repo
  • Loading branch information
Matthias Gutheil authored and jaikiran committed May 22, 2019
1 parent 1cb2f81 commit b845192
Show file tree
Hide file tree
Showing 13 changed files with 383 additions and 1 deletion.
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ Matthew Warman
Matthew Watson
Matthew Yanos
Matthias Bhend
Matthias Gutheil
Michael Bayne
Michael Clarke
Michael Davey
Expand Down
7 changes: 7 additions & 0 deletions WHATSNEW
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ Fixed bugs:
has now been fixed.
Bugzilla Report 63446

Other changes:
--------------

* junitlauncher task now supports selecting test classes for execution,
based on the JUnit 5 tags, through the new "includeTags" and
"excludeTags" attributes.


Changes from Ant 1.10.5 TO Ant 1.10.6
=====================================
Expand Down
4 changes: 4 additions & 0 deletions contributors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,10 @@
<first>Matthias</first>
<last>Bhend</last>
</name>
<name>
<first>Matthias</first>
<last>Gutheil</last>
</name>
<name>
<first>Michael</first>
<last>Bayne</last>
Expand Down
14 changes: 14 additions & 0 deletions manual/Tasks/junitlauncher.html
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@ <h3>Parameters</h3>
<th scope="col">Description</th>
<th scope="col">Required</th>
</tr>
<tr>
<td>includeTags</td>
<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.
<p><em>Since Ant 1.10.7</em></p>
</td>
<td>No</td>
</tr>
<tr>
<td>excludeTags</td>
<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.
<p><em>Since Ant 1.10.7</em></p>
</td>
<td>No</td>
</tr>
<tr>
<td>haltOnFailure</td>
<td>A value of <q>true</q> implies that build has to stop if any failure occurs in any of
Expand Down
65 changes: 65 additions & 0 deletions src/etc/testcases/taskdefs/optional/junitlauncher.xml
Original file line number Diff line number Diff line change
Expand Up @@ -268,5 +268,70 @@
</testclasses>
</junitlauncher>
</target>

<target name="test-method-with-include-tag" depends="init">
<!-- junitlauncher includeTags="fast, superfast" excludeTags="slow"-->
<junitlauncher includeTags=" fast , superfast" excludeTags=" slow">
<test name="org.example.junitlauncher.jupiter.JupiterSampleTest" outputdir="${output.dir}">
<listener classname="org.example.junitlauncher.Tracker" outputDir="${output.dir}"
resultFile="${test-method-with-include-tag.tracker}" if="test-method-with-include-tag.tracker" />
</test>
</junitlauncher>
</target>

<target name="test-method-with-exclude-tag" depends="init">
<junitlauncher excludeTags="slow">
<test name="org.example.junitlauncher.jupiter.JupiterSampleTest" outputdir="${output.dir}">
<listener classname="org.example.junitlauncher.Tracker" outputDir="${output.dir}"
resultFile="${test-method-with-exclude-tag.tracker}" if="test-method-with-exclude-tag.tracker" />
</test>
</junitlauncher>
</target>

<target name="test-method-with-tag-2-classes" depends="init">
<junitlauncher includeTags="fast">
<test name="org.example.junitlauncher.jupiter.JupiterSampleTest" outputdir="${output.dir}">
<listener classname="org.example.junitlauncher.Tracker" outputDir="${output.dir}"
resultFile="${test-method-with-tag-2-classes1.tracker}" if="test-method-with-tag-2-classes1.tracker" />
</test>
<test name="org.example.junitlauncher.jupiter.JupiterTagSampleTest" outputdir="${output.dir}">
<listener classname="org.example.junitlauncher.Tracker" outputDir="${output.dir}"
resultFile="${test-method-with-tag-2-classes2.tracker}" if="test-method-with-tag-2-classes2.tracker" />
</test>
</junitlauncher>
</target>

<target name="test-method-with-tag-fileset" depends="init">
<property name="junitlauncher.test.tracker.append.file" value="${output.dir}/${test-method-with-tag-fileset.tracker}"/>
<junitlauncher includeTags="fast">
<testclasses outputdir="${output.dir}">
<fileset dir="${build.classes.dir}">
<include name="org/example/junitlauncher/jupiter/*" />
</fileset>
<listener classname="org.example.junitlauncher.Tracker" outputDir="${output.dir}"
if="test-method-with-tag-fileset.tracker" />
</testclasses>
</junitlauncher>
</target>

<target name="test-method-with-tag-fileset-fork" depends="init">
<property name="junitlauncher.test.tracker.append.file" value="${output.dir}/${test-method-with-tag-fileset-fork.tracker}" />
<junitlauncher includeTags="fast">
<classpath refid="junit.engine.jupiter.classpath" />
<classpath>
<pathelement location="${build.classes.dir}" />
</classpath>
<classpath refid="junit.platform.classpath" />
<testclasses outputdir="${output.dir}">
<fileset dir="${build.classes.dir}">
<include name="org/example/junitlauncher/jupiter/*" />
</fileset>
<fork dir="${basedir}" includeJUnitPlatformLibraries="false">
</fork>
<listener classname="org.example.junitlauncher.Tracker" outputDir="${output.dir}"
if="test-method-with-tag-fileset-fork.tracker" />
</testclasses>
</junitlauncher>
</target>
</project>

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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


import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.MagicNames;
import org.apache.tools.ant.Project;
Expand All @@ -34,6 +35,7 @@
import org.junit.platform.launcher.EngineFilter;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.TagFilter;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestPlan;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
Expand Down Expand Up @@ -463,6 +465,13 @@ private void addFilters(final TestRequest testRequest) {
if (enginesToExclude != null && enginesToExclude.length > 0) {
requestBuilder.filters(EngineFilter.excludeEngines(enginesToExclude));
}
// add any tag filters
if (this.launchDefinition.getIncludeTags().size() > 0) {
requestBuilder.filters(TagFilter.includeTags(this.launchDefinition.getIncludeTags()));
}
if (this.launchDefinition.getExcludeTags().size() > 0) {
requestBuilder.filters(TagFilter.excludeTags(this.launchDefinition.getExcludeTags()));
}
}

private enum StreamType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,22 @@
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.stream.Stream;

import static javax.xml.stream.XMLStreamConstants.END_DOCUMENT;
import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
import static javax.xml.stream.XMLStreamConstants.START_DOCUMENT;
import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_EXCLUDE_TAGS;
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_HALT_ON_FAILURE;
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_INCLUDE_TAGS;
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_PRINT_SUMMARY;
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ELM_LAUNCH_DEF;
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ELM_LISTENER;
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ELM_TEST;
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ELM_TEST_CLASSES;


/**
* Used for launching forked tests from the {@link JUnitLauncherTask}.
* <p>
Expand Down Expand Up @@ -140,6 +144,14 @@ private static ForkedLaunch parseLaunchDefinition(final Path pathToLaunchDefXml)
if (haltOnfFailure != null) {
forkedLaunch.setHaltOnFailure(Boolean.parseBoolean(haltOnfFailure));
}
final String includeTags = reader.getAttributeValue(null, LD_XML_ATTR_INCLUDE_TAGS);
if (includeTags != null) {
Stream.of(includeTags.split(",")).forEach(i -> forkedLaunch.addIncludeTag(i));
}
final String excludeTags = reader.getAttributeValue(null, LD_XML_ATTR_EXCLUDE_TAGS);
if (excludeTags != null) {
Stream.of(excludeTags.split(",")).forEach(e -> forkedLaunch.addExcludeTag(e));
}
final String printSummary = reader.getAttributeValue(null, LD_XML_ATTR_PRINT_SUMMARY);
if (printSummary != null) {
forkedLaunch.setPrintSummary(Boolean.parseBoolean(printSummary));
Expand Down Expand Up @@ -204,6 +216,8 @@ private static final class ForkedLaunch implements LaunchDefinition {
private boolean haltOnFailure;
private List<TestDefinition> tests = new ArrayList<>();
private List<ListenerDefinition> listeners = new ArrayList<>();
private List<String> includeTags = new ArrayList<>();
private List<String> excludeTags = new ArrayList<>();

@Override
public List<TestDefinition> getTests() {
Expand Down Expand Up @@ -249,5 +263,23 @@ public ForkedLaunch setHaltOnFailure(final boolean haltOnFailure) {
public ClassLoader getClassLoader() {
return this.getClass().getClassLoader();
}

void addIncludeTag(final String filter) {
this.includeTags.add(filter);
}

@Override
public List<String> getIncludeTags() {
return includeTags;
}

void addExcludeTag(final String filter) {
this.excludeTags.add(filter);
}

@Override
public List<String> getExcludeTags() {
return excludeTags;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public final class Constants {
public static final String LD_XML_ELM_TEST = "test";
public static final String LD_XML_ELM_TEST_CLASSES = "test-classes";
public static final String LD_XML_ATTR_HALT_ON_FAILURE = "haltOnFailure";
public static final String LD_XML_ATTR_INCLUDE_TAGS = "includeTags";
public static final String LD_XML_ATTR_EXCLUDE_TAGS = "excludeTags";
public static final String LD_XML_ATTR_OUTPUT_DIRECTORY = "outDir";
public static final String LD_XML_ATTR_INCLUDE_ENGINES = "includeEngines";
public static final String LD_XML_ATTR_EXCLUDE_ENGINES = "excludeEngines";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,17 @@
import java.util.Hashtable;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;

import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_EXCLUDE_TAGS;
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_HALT_ON_FAILURE;
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_INCLUDE_TAGS;
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_PRINT_SUMMARY;
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ELM_LAUNCH_DEF;


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

public JUnitLauncherTask() {
}
Expand Down Expand Up @@ -158,6 +165,32 @@ public void setPrintSummary(final boolean printSummary) {
this.printSummary = printSummary;
}

/**
* Tags to include. Will trim each tag.
*
* @param includes comma separated list of tags to include while running the tests.
* @since Ant 1.10.7
*/
public void setIncludeTags(final String includes) {
final StringTokenizer tokens = new StringTokenizer(includes, ",");
while (tokens.hasMoreTokens()) {
includeTags.add(tokens.nextToken().trim());
}
}

/**
* Tags to exclude. Will trim each tag.
*
* @param excludes comma separated list of tags to exclude while running the tests.
* @since Ant 1.10.7
*/
public void setExcludeTags(final String excludes) {
final StringTokenizer tokens = new StringTokenizer(excludes, ",");
while (tokens.hasMoreTokens()) {
excludeTags.add(tokens.nextToken().trim());
}
}

private void preConfigure(final TestDefinition test) {
if (test.getHaltOnFailure() == null) {
test.setHaltOnFailure(this.haltOnFailure);
Expand Down Expand Up @@ -233,6 +266,12 @@ private void forkTest(final TestDefinition test) {
if (this.haltOnFailure) {
writer.writeAttribute(LD_XML_ATTR_HALT_ON_FAILURE, "true");
}
if (this.includeTags.size() > 0) {
writer.writeAttribute(LD_XML_ATTR_INCLUDE_TAGS, commaSeparatedListElements(includeTags));
}
if (this.excludeTags.size() > 0) {
writer.writeAttribute(LD_XML_ATTR_EXCLUDE_TAGS, commaSeparatedListElements(excludeTags));
}
// task level listeners
for (final ListenerDefinition listenerDef : this.listeners) {
if (!listenerDef.shouldUse(getProject())) {
Expand Down Expand Up @@ -294,6 +333,12 @@ private void forkTest(final TestDefinition test) {
}
}

private static String commaSeparatedListElements(final List<String> stringList) {
return stringList.stream()
.map(Object::toString)
.collect(Collectors.joining(", "));
}

private int executeForkedTest(final ForkDefinition forkDefinition, final CommandlineJava commandlineJava) {
final LogOutputStream outStream = new LogOutputStream(this, Project.MSG_INFO);
final LogOutputStream errStream = new LogOutputStream(this, Project.MSG_WARN);
Expand Down Expand Up @@ -359,6 +404,16 @@ public boolean isHaltOnFailure() {
return haltOnFailure;
}

@Override
public List<String> getIncludeTags() {
return includeTags;
}

@Override
public List<String> getExcludeTags() {
return excludeTags;
}

@Override
public ClassLoader getClassLoader() {
return this.executionCL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,15 @@ public interface LaunchDefinition {
*/
ClassLoader getClassLoader();

/**
* @return Returns the list of tags which will be used to evaluate tests that need to be included
* in the test execution
*/
List<String> getIncludeTags();

/**
* @return Returns the list of tags which will be used to evaluate tests that need to be excluded
* from the test execution
*/
List<String> getExcludeTags();
}
Loading

0 comments on commit b845192

Please sign in to comment.