Class JUnitXmlParser

java.lang.Object
rife.bld.extension.junitreporter.JUnitXmlParser

public final class JUnitXmlParser extends Object
Utility class for parsing JUnit XML test reports.

Extracts test failure and error details from JUnit XML files generated by Maven Surefire, Gradle, or similar test runners. Groups results by class name and supports display names from system-out elements.

Ordering: Results are returned in a LinkedHashMap with classes ordered to match typical JUnit console output: last-executed tests appear first. This makes --index=1 refer to the most recently run failing test class, which aligns with what developers see when scrolling test output.

This parser is intended for local, trusted XML files. Basic XXE protection is enabled, but full hardening is unnecessary for typical build tool output.

Since:
1.0
Author:
Erik C. Thauvin
  • Field Details

  • Method Details

    • createTestFailure

      public static TestFailure createTestFailure(Element testCaseElement, Element failureElement, String defaultType)
      Creates a TestFailure from a test case and failure element.

      Extracts test name, class name, execution time, display name, failure type, message, and stack trace. Uses the provided default type if the failure element lacks a type attribute.

      Parameters:
      testCaseElement - the <testcase> element
      failureElement - the <failure> or <error> element
      defaultType - the failure type to use if not specified in the element
      Returns:
      a populated TestFailure instance
    • extractDisplayName

      public static String extractDisplayName(Element testCaseElement)
      Extracts the JUnit display name from a test case's system-out element.

      Searches for display-name: <name> patterns in the system-out text content. Returns an empty string if no display name is found.

      Parameters:
      testCaseElement - the <testcase> element to search within
      Returns:
      the display name if present, otherwise an empty string
    • extractTestFailuresGrouped

      public static Map<String,TestClassFailures> extractTestFailuresGrouped(String xmlFilePath)
      Parses a JUnit XML report and groups test failures by class name.

      Reads the specified file, extracts all <testcase> elements containing <failure> or <error> children, and groups them by the classname attribute.

      Classes are returned in reverse XML order so that --index=1 refers to the last failing test class in the file. This matches the order developers see in console output, where the most recent failures appear at the bottom.

      Parameters:
      xmlFilePath - the path to the JUnit XML file
      Returns:
      a map of class names to TestClassFailures ordered by last-executed first
      Throws:
      NullPointerException - if xmlFilePath is null
      JUnitXmlParserException - if the file does not exist, is not readable, or parsing fails
    • getAttributeOrDefault

      public static String getAttributeOrDefault(Element element, String attributeName, String defaultValue)
      Returns an attribute value from an element, or a default if blank or missing.
      Parameters:
      element - the XML element
      attributeName - the attribute name to retrieve
      defaultValue - the value to return if the attribute is blank
      Returns:
      the attribute value, or defaultValue if blank
    • getTextContentTrimmed

      public static String getTextContentTrimmed(Element element)
      Returns the trimmed text content of an element.
      Parameters:
      element - the XML element
      Returns:
      the trimmed text content, or an empty string if null
    • parseTestCases

      public static Map<String,TestClassFailures> parseTestCases(NodeList testCases)
      Parses test case nodes and groups failures by class name.

      Iterates through all <testcase> elements, extracts failures and errors, and groups them into TestClassFailures instances. The resulting map is reversed so classes appear in reverse XML order, matching console output where the last executed tests appear last.

      Parameters:
      testCases - a NodeList of <testcase> elements
      Returns:
      a map of class names to grouped failures ordered by last-executed first
    • parseTime

      public static double parseTime(String timeStr)
      Parses a time value from a string.

      Converts the input to a double. Returns 0.0 if the input is null, empty, invalid, or not finite (NaN, Infinity).

      Parameters:
      timeStr - the time value as a string
      Returns:
      the parsed time as a double, or 0.0 if invalid
    • validateFile

      public static void validateFile(Path path) throws JUnitXmlParserException
      Validates that a file exists and is readable.
      Parameters:
      path - the file path to validate
      Throws:
      JUnitXmlParserException - if the file does not exist, is not readable, or cannot be accessed