Skip to content

Updates for Issue #477 - JUnitFormatter does not put required name attribute #480

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions core/src/main/java/cucumber/runtime/formatter/JUnitFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
Expand Down Expand Up @@ -88,8 +89,11 @@ public void step(Step step) {
@Override
public void done() {
try {
//set up a transformer
// set up a transformer
rootElement.setAttribute("name", JUnitFormatter.class.getName());
rootElement.setAttribute("failures", String.valueOf(rootElement.getElementsByTagName("failure").getLength()));
rootElement.setAttribute("skipped", String.valueOf(rootElement.getElementsByTagName("skipped").getLength()));
rootElement.setAttribute("time", sumTimes(rootElement.getElementsByTagName("testcase")));
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.INDENT, "yes");
Expand Down Expand Up @@ -125,6 +129,24 @@ private void handleHook(Result result) {

}

private String sumTimes(NodeList testCaseNodes) {
double totalDurationSecondsForAllTimes = 0.0d;
for( int i = 0; i < testCaseNodes.getLength(); i++ ) {
try {
double testCaseTime =
Double.parseDouble(testCaseNodes.item(i).getAttributes().getNamedItem("time").getNodeValue());
totalDurationSecondsForAllTimes += testCaseTime;
} catch ( NumberFormatException e ) {
throw new CucumberException(e);
} catch ( NullPointerException e ) {
throw new CucumberException(e);
}
}
DecimalFormat nfmt = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
nfmt.applyPattern("0.######");
return nfmt.format(totalDurationSecondsForAllTimes);
}

private void increaseAttributeValue(Element element, String attribute) {
int value = 0;
if (element.hasAttribute(attribute)) {
Expand Down Expand Up @@ -238,7 +260,7 @@ public void updateElement(Document doc, Element tc) {
child = doc.createElement("system-out");
child.appendChild(doc.createCDATASection(sb.toString()));
}

Node existingChild = tc.getFirstChild();
if (existingChild == null) {
tc.appendChild(child);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<testsuite tests="2" failures="0">
<testsuite tests="2" failures="0" skipped="2" time="0" name="cucumber.runtime.formatter.JUnitFormatter">
<testcase classname="Feature_1" name="Scenario_1" time="0">
<skipped><![CDATA[Given step_1................................................................undefined
When step_2.................................................................undefined
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<testsuite tests="2" failures="0">
<testsuite tests="2" failures="0" skipped="2" time="0" name="cucumber.runtime.formatter.JUnitFormatter">
<testcase classname="Feature_2" name="Scenario_1" time="0">
<skipped><![CDATA[Given bg_1.................................................................undefined
When bg_2..................................................................undefined
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<testsuite failures="0" tests="4">
<testsuite failures="0" tests="4" skipped="4" time="0" name="cucumber.runtime.formatter.JUnitFormatter">
<testcase classname="Feature_3" name="Scenario_1" time="0">
<skipped><![CDATA[Given bg_1.................................................................undefined
When bg_2..................................................................undefined
Expand Down