Skip to content

Commit

Permalink
[SUREFIRE-2167] Simplify deserialization of elapsed time in TestSuite…
Browse files Browse the repository at this point in the history
…XmlParser
  • Loading branch information
michael-o committed May 28, 2023
1 parent cba039e commit 9e971d2
Showing 1 changed file with 12 additions and 21 deletions.
Expand Up @@ -26,8 +26,6 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -40,15 +38,12 @@
import org.xml.sax.helpers.DefaultHandler;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Locale.ENGLISH;
import static org.apache.maven.shared.utils.StringUtils.isBlank;

/**
*
*/
public final class TestSuiteXmlParser extends DefaultHandler {
private final NumberFormat numberFormat = NumberFormat.getInstance(ENGLISH);

private final ConsoleLogger consoleLogger;

private ReportTestSuite defaultSuite;
Expand Down Expand Up @@ -111,13 +106,11 @@ public void startElement(String uri, String localName, String qName, Attributes
case "testsuite":
defaultSuite = new ReportTestSuite();
currentSuite = defaultSuite;

try {
Number time = numberFormat.parse(attributes.getValue("time"));

defaultSuite.setTimeElapsed(time.floatValue());
} catch (NullPointerException e) {
consoleLogger.error("WARNING: no time attribute found on testsuite element");
String timeStr = attributes.getValue("time");
if (timeStr != null) {
defaultSuite.setTimeElapsed(Float.parseFloat(timeStr));
} else {
consoleLogger.warning("No time attribute found on testsuite element");
}

final String name = attributes.getValue("name");
Expand Down Expand Up @@ -151,13 +144,12 @@ public void startElement(String uri, String localName, String qName, Attributes
}
}

String timeAsString = attributes.getValue("time");
Number time = isBlank(timeAsString) ? 0 : numberFormat.parse(timeAsString);
timeStr = attributes.getValue("time");

testCase.setFullClassName(currentSuite.getFullClassName())
.setClassName(currentSuite.getName())
.setFullName(currentSuite.getFullClassName() + "." + testCase.getName())
.setTime(time.floatValue());
.setTime(timeStr != null ? Float.parseFloat(timeStr) : 0.0f);

if (currentSuite != defaultSuite) {
currentSuite.setTimeElapsed(testCase.getTime() + currentSuite.getTimeElapsed());
Expand Down Expand Up @@ -193,8 +185,8 @@ public void startElement(String uri, String localName, String qName, Attributes
default:
break;
}
} catch (ParseException e) {
throw new SAXException(e.getMessage(), e);
} catch (NumberFormatException e) {
throw new SAXException("Failed to parse time value", e);
}
}
}
Expand All @@ -215,10 +207,9 @@ public void endElement(String uri, String localName, String qName) throws SAXExc
break;
case "time":
try {
defaultSuite.setTimeElapsed(
numberFormat.parse(currentElement.toString()).floatValue());
} catch (ParseException e) {
throw new SAXException(e.getMessage(), e);
defaultSuite.setTimeElapsed(Float.parseFloat(currentElement.toString()));
} catch (NumberFormatException e) {
throw new SAXException("Failed to parse time value", e);
}
break;
default:
Expand Down

0 comments on commit 9e971d2

Please sign in to comment.