Browse Source

[junitlauncher] - Introduce the ability to let listener/result formatter implementations decide whether to use legacy or new style display names for test identifiers

master
Jaikiran Pai 4 years ago
parent
commit
41c51907a4
10 changed files with 82 additions and 4 deletions
  1. +10
    -0
      WHATSNEW
  2. +8
    -0
      manual/Tasks/junitlauncher.html
  3. +2
    -1
      src/etc/testcases/taskdefs/optional/junitlauncher.xml
  4. +1
    -0
      src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java
  5. +8
    -2
      src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LegacyPlainResultFormatter.java
  6. +8
    -1
      src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LegacyXmlResultFormatter.java
  7. +11
    -0
      src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/TestResultFormatter.java
  8. +1
    -0
      src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/Constants.java
  9. +28
    -0
      src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/ListenerDefinition.java
  10. +5
    -0
      src/tests/junit/org/example/junitlauncher/Tracker.java

+ 10
- 0
WHATSNEW View File

@@ -12,6 +12,16 @@ Other changes:
* javaversion condition now has a new "atmost" attribute. See the javaversion
manual for more details

* The "listener" nested element of the "junitlauncher" task now has a new
"useLegacyReportingName" attribute which can be used to control the test
identifiers names that get reported by the listener. See the junitlauncher
manual for more details.
Note that this change also introduces a new "setUseLegacyReportingName" method
on the org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter
interface. This will break backward compatibility with any of your custom
result formatters which implemented this interface and such implementations
are now expected to implement this new method.

Changes from Ant 1.10.8 TO Ant 1.10.9
=====================================



+ 8
- 0
manual/Tasks/junitlauncher.html View File

@@ -402,6 +402,14 @@
is <strong>not</strong> set</a>.</td>
<td>No</td>
</tr>
<tr>
<td>useLegacyReportingName</td>
<td>Set to true, if the test identifiers reported by this listener should use legacy (JUnit4
style) names. Else set to false. Defaults to true.
<p><em>Since Ant 1.10.10</em></p>
</td>
<td>No</td>
</tr>
</table>

<h4>test</h4>


+ 2
- 1
src/etc/testcases/taskdefs/optional/junitlauncher.xml View File

@@ -161,7 +161,7 @@
<sysproperty key="junitlauncher.test.sysprop.one" value="forked"/>
</fork>

<listener type="legacy-xml" sendSysErr="true" sendSysOut="true"/>
<listener type="legacy-xml" sendSysErr="true" sendSysOut="true" useLegacyReportingName="false"/>
</test>
</junitlauncher>
</target>
@@ -368,6 +368,7 @@
</fork>
</testclasses>
<listener type="legacy-plain" sendSysOut="true" />
<listener type="legacy-brief" sendSysOut="true" useLegacyReportingName="true"/>
</junitlauncher>
</target>
</project>


+ 1
- 0
src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java View File

@@ -226,6 +226,7 @@ public class LauncherSupport {
testRequest.closeUponCompletion(resultFormatter);
// set the execution context
resultFormatter.setContext(this.testExecutionContext);
resultFormatter.setUseLegacyReportingName(formatterDefinition.isUseLegacyReportingName());
// set the destination output stream for writing out the formatted result
final java.nio.file.Path resultOutputFile = getListenerOutputFile(testRequest, formatterDefinition);
try {


+ 8
- 2
src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LegacyPlainResultFormatter.java View File

@@ -47,6 +47,7 @@ class LegacyPlainResultFormatter extends AbstractJUnitResultFormatter implements
private final Map<TestIdentifier, Stats> testIds = new ConcurrentHashMap<>();
private TestPlan testPlan;
private BufferedWriter writer;
private boolean useLegacyReportingName = true;

@Override
public void testPlanExecutionStarted(final TestPlan testPlan) {
@@ -111,7 +112,7 @@ class LegacyPlainResultFormatter extends AbstractJUnitResultFormatter implements
if (testIdentifier.isTest()) {
final StringBuilder sb = new StringBuilder();
sb.append("Test: ");
sb.append(testIdentifier.getLegacyReportingName());
sb.append(this.useLegacyReportingName ? testIdentifier.getLegacyReportingName() : testIdentifier.getDisplayName());
sb.append(" took ");
stats.appendElapsed(sb);
sb.append(" SKIPPED");
@@ -175,7 +176,7 @@ class LegacyPlainResultFormatter extends AbstractJUnitResultFormatter implements
if (testIdentifier.isTest() && shouldReportExecutionFinished(testIdentifier, testExecutionResult)) {
final StringBuilder sb = new StringBuilder();
sb.append("Test: ");
sb.append(testIdentifier.getLegacyReportingName());
sb.append(this.useLegacyReportingName ? testIdentifier.getLegacyReportingName() : testIdentifier.getDisplayName());
if (stats != null) {
sb.append(" took ");
stats.appendElapsed(sb);
@@ -230,6 +231,11 @@ class LegacyPlainResultFormatter extends AbstractJUnitResultFormatter implements
this.writer = new BufferedWriter(new OutputStreamWriter(this.outputStream, StandardCharsets.UTF_8));
}

@Override
public void setUseLegacyReportingName(final boolean useLegacyReportingName) {
this.useLegacyReportingName = useLegacyReportingName;
}

protected boolean shouldReportExecutionFinished(final TestIdentifier testIdentifier, final TestExecutionResult testExecutionResult) {
return true;
}


+ 8
- 1
src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LegacyXmlResultFormatter.java View File

@@ -63,6 +63,7 @@ class LegacyXmlResultFormatter extends AbstractJUnitResultFormatter implements T
private final AtomicLong numTestsFailed = new AtomicLong(0);
private final AtomicLong numTestsSkipped = new AtomicLong(0);
private final AtomicLong numTestsAborted = new AtomicLong(0);
private boolean useLegacyReportingName = true;


@Override
@@ -141,6 +142,11 @@ class LegacyXmlResultFormatter extends AbstractJUnitResultFormatter implements T
this.outputStream = os;
}

@Override
public void setUseLegacyReportingName(final boolean useLegacyReportingName) {
this.useLegacyReportingName = useLegacyReportingName;
}

private final class Stats {
@SuppressWarnings("unused")
private final TestIdentifier testIdentifier;
@@ -252,7 +258,8 @@ class LegacyXmlResultFormatter extends AbstractJUnitResultFormatter implements T
final String classname = (parentClassSource.get()).getClassName();
writer.writeStartElement(ELEM_TESTCASE);
writer.writeAttribute(ATTR_CLASSNAME, classname);
writer.writeAttribute(ATTR_NAME, testId.getLegacyReportingName());
writer.writeAttribute(ATTR_NAME, useLegacyReportingName ? testId.getLegacyReportingName()
: testId.getDisplayName());
final Stats stats = entry.getValue();
writer.writeAttribute(ATTR_TIME, String.valueOf((stats.endedAt - stats.startedAt) / ONE_SECOND));
// skipped element if the test was skipped


+ 11
- 0
src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/TestResultFormatter.java View File

@@ -50,6 +50,17 @@ public interface TestResultFormatter extends TestExecutionListener, Closeable {
*/
void setContext(TestExecutionContext context);

/**
* This method will be invoked by the {@code junitlauncher} to let the result formatter implementation
* know whether or not to use JUnit 4 style, legacy reporting names for test identifiers that get
* displayed in the test reports. Result formatter implementations are allowed to default to a specific
* reporting style for test identifiers, if this method isn't invoked.
* @param useLegacyReportingName {@code true} if legacy reporting name is to be used, {@code false}
* otherwise.
* @since Ant 1.10.10
*/
void setUseLegacyReportingName(boolean useLegacyReportingName);

/**
* This method will be invoked by the <code>junitlauncher</code>, <strong>regularly/multiple times</strong>,
* as and when any content is generated on the standard output stream during the test execution.


+ 1
- 0
src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/Constants.java View File

@@ -48,6 +48,7 @@ public final class Constants {
public static final String LD_XML_ATTR_SEND_SYS_ERR = "sendSysErr";
public static final String LD_XML_ATTR_SEND_SYS_OUT = "sendSysOut";
public static final String LD_XML_ATTR_LISTENER_RESULT_FILE = "resultFile";
public static final String LD_XML_ATTR_LISTENER_USE_LEGACY_REPORTING_NAME = "useLegacyReportingName";


private Constants() {


+ 28
- 0
src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/confined/ListenerDefinition.java View File

@@ -28,6 +28,7 @@ import javax.xml.stream.XMLStreamWriter;

import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_CLASS_NAME;
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_LISTENER_RESULT_FILE;
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_LISTENER_USE_LEGACY_REPORTING_NAME;
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_OUTPUT_DIRECTORY;
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_SEND_SYS_ERR;
import static org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.Constants.LD_XML_ATTR_SEND_SYS_OUT;
@@ -51,6 +52,7 @@ public class ListenerDefinition {
private boolean sendSysOut;
private boolean sendSysErr;
private String outputDir;
private boolean useLegacyReportingName = true;

public ListenerDefinition() {

@@ -135,6 +137,26 @@ public class ListenerDefinition {
return this.outputDir;
}

/**
*
* @return Returns {@code true} if legacy reporting name (JUnit 4 style) is to be used.
* Else returns {@code false}.
* @since Ant 1.10.10
*/
public boolean isUseLegacyReportingName() {
return useLegacyReportingName;
}

/**
* Set the test identifier reporting style
* @param useLegacyReportingName {@code true} if legacy reporting name (JUnit 4 style) is to
* be used. Else {@code false}.
* @since Ant 1.10.10
*/
public void setUseLegacyReportingName(final boolean useLegacyReportingName) {
this.useLegacyReportingName = useLegacyReportingName;
}

public boolean shouldUse(final Project project) {
final PropertyHelper propertyHelper = PropertyHelper.getPropertyHelper(project);
return propertyHelper.testIfCondition(this.ifProperty) && propertyHelper.testUnlessCondition(this.unlessProperty);
@@ -157,6 +179,7 @@ public class ListenerDefinition {
writer.writeAttribute(LD_XML_ATTR_CLASS_NAME, this.className);
writer.writeAttribute(LD_XML_ATTR_SEND_SYS_ERR, Boolean.toString(this.sendSysErr));
writer.writeAttribute(LD_XML_ATTR_SEND_SYS_OUT, Boolean.toString(this.sendSysOut));
writer.writeAttribute(LD_XML_ATTR_LISTENER_USE_LEGACY_REPORTING_NAME, Boolean.toString(this.useLegacyReportingName));
if (this.outputDir != null) {
writer.writeAttribute(LD_XML_ATTR_OUTPUT_DIRECTORY, this.outputDir);
}
@@ -187,6 +210,11 @@ public class ListenerDefinition {
if (resultFile != null) {
listenerDef.setResultFile(resultFile);
}
final String useLegacyReportingName = reader.getAttributeValue(null,
LD_XML_ATTR_LISTENER_USE_LEGACY_REPORTING_NAME);
if (useLegacyReportingName != null) {
listenerDef.setUseLegacyReportingName(Boolean.parseBoolean(useLegacyReportingName));
}
reader.nextTag();
reader.require(XMLStreamConstants.END_ELEMENT, null, LD_XML_ELM_LISTENER);
return listenerDef;


+ 5
- 0
src/tests/junit/org/example/junitlauncher/Tracker.java View File

@@ -73,6 +73,11 @@ public class Tracker implements TestResultFormatter {
this.context = context;
}

@Override
public void setUseLegacyReportingName(final boolean useLegacyReportingName) {
// do nothing
}

@Override
public void close() throws IOException {
this.writer.flush();


Loading…
Cancel
Save