Skip to content

Commit

Permalink
Merge pull request #120 from jenkinsci/xml-exception
Browse files Browse the repository at this point in the history
Move the parsing exception to the model
  • Loading branch information
uhafner committed Aug 18, 2024
2 parents 152c925 + f6fb048 commit 34d7701
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 15 deletions.
79 changes: 71 additions & 8 deletions src/main/java/edu/hm/hafner/coverage/CoverageParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

import org.apache.commons.lang3.exception.ExceptionUtils;

import com.google.errorprone.annotations.FormatMethod;

import edu.hm.hafner.util.FilteredLog;
import edu.hm.hafner.util.SecureXmlParserFactory.ParsingException;
import edu.hm.hafner.util.TreeStringBuilder;

/**
Expand Down Expand Up @@ -75,7 +78,7 @@ protected boolean ignoreErrors() {
*
* @return the root of the created tree
* @throws ParsingException
* if the XML content cannot be read
* if the content cannot be read by the parser
*/
public ModuleNode parse(final Reader reader, final String fileName, final FilteredLog log) {
var moduleNode = parseReport(reader, fileName, log);
Expand All @@ -92,14 +95,12 @@ public ModuleNode parse(final Reader reader, final String fileName, final Filter
* @return the name
*/
protected QName getElementName(final XMLEvent event) {
QName name;
if (event.isStartElement()) {
name = event.asStartElement().getName();
return event.asStartElement().getName();
}
else {
name = event.asEndElement().getName();
return event.asEndElement().getName();
}
return name;
}

/**
Expand All @@ -110,7 +111,7 @@ protected QName getElementName(final XMLEvent event) {
* @param log
* the log
* @param isEmpty
* determines whether the results are empty
* set this flag to {@code true} to indicate that the results are empty
*
* @throws NoSuchElementException
* if the results are empty and errors should not be ignored
Expand Down Expand Up @@ -167,7 +168,7 @@ public final TreeStringBuilder getTreeStringBuilder() {
*
* @return the root of the created tree
* @throws ParsingException
* if the XML content cannot be read
* if the content cannot be read by the parser
*/
protected abstract ModuleNode parseReport(Reader reader, String fileName, FilteredLog log);

Expand Down Expand Up @@ -207,4 +208,66 @@ protected static int parseInteger(final String value) {
protected static ParsingException createEofException(final String fileName) {
return new ParsingException("Unexpected end of file '%s'", fileName);

Check warning on line 209 in src/main/java/edu/hm/hafner/coverage/CoverageParser.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Not covered line

Line 209 is not covered by tests

Check warning on line 209 in src/main/java/edu/hm/hafner/coverage/CoverageParser.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Not covered line

Line 209 is not covered by tests
}

/**
* Indicates that during parsing a non-recoverable error has been occurred.
*
* @author Ullrich Hafner
*/
public static class ParsingException extends RuntimeException {
private static final long serialVersionUID = -9016364685084958944L;

/**
* Constructs a new {@link ParsingException} with the specified cause.
*
* @param cause
* the cause (which is saved for later retrieval by the {@link #getCause()} method).
*/
public ParsingException(final Throwable cause) {
super(createMessage(cause, "Exception occurred during parsing"), cause);
}

/**
* Constructs a new {@link ParsingException} with the specified cause and message.
*
* @param cause
* the cause (which is saved for later retrieval by the {@link #getCause()} method).
* @param messageFormat
* the message as a format string as described in <a href="../util/Formatter.html#syntax">Format string
* syntax</a>
* @param args
* Arguments referenced by the format specifiers in the format string. If there are more arguments than
* format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero.
* The maximum number of arguments is limited by the maximum dimension of a Java array as defined by
* <cite>The Java&trade; Virtual Machine Specification</cite>. The behaviour on a {@code null} argument
* depends on the <a href="../util/Formatter.html#syntax">conversion</a>.
*/
@FormatMethod
public ParsingException(final Throwable cause, final String messageFormat, final Object... args) {
super(createMessage(cause, String.format(messageFormat, args)), cause);
}

/**
* Constructs a new {@link ParsingException} with the specified message.
*
* @param messageFormat
* the message as a format string as described in <a href="../util/Formatter.html#syntax">Format string
* syntax</a>
* @param args
* Arguments referenced by the format specifiers in the format string. If there are more arguments than
* format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero.
* The maximum number of arguments is limited by the maximum dimension of a Java array as defined by
* <cite>The Java&trade; Virtual Machine Specification</cite>. The behavior on a {@code null} argument
* depends on the <a href="../util/Formatter.html#syntax">conversion</a>.
*/
@FormatMethod
public ParsingException(final String messageFormat, final Object... args) {
super(String.format(messageFormat, args));
}

Check warning on line 266 in src/main/java/edu/hm/hafner/coverage/CoverageParser.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Not covered lines

Lines 247-266 are not covered by tests

private static String createMessage(final Throwable cause, final String message) {
return String.format("%s%n%s%n%s", message,

Check warning on line 269 in src/main/java/edu/hm/hafner/coverage/CoverageParser.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Mutation survived

One mutation survived in line 269 (EmptyObjectReturnValsMutator)
Raw output
Survived mutations:
- replaced return value with "" for edu/hm/hafner/coverage/CoverageParser$ParsingException::createMessage (org.pitest.mutationtest.engine.gregor.mutators.returns.EmptyObjectReturnValsMutator)
ExceptionUtils.getMessage(cause), ExceptionUtils.getStackTrace(cause));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import edu.hm.hafner.coverage.TestCase;
import edu.hm.hafner.util.FilteredLog;
import edu.hm.hafner.util.SecureXmlParserFactory;
import edu.hm.hafner.util.SecureXmlParserFactory.ParsingException;

/**
* Baseclass for test result parsers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import edu.hm.hafner.util.FilteredLog;
import edu.hm.hafner.util.PathUtil;
import edu.hm.hafner.util.SecureXmlParserFactory;
import edu.hm.hafner.util.SecureXmlParserFactory.ParsingException;

/**
* Parses Cobertura reports into a hierarchical Java Object Model.
Expand Down Expand Up @@ -218,7 +217,7 @@ protected Node createNode(final Node parentNode, final StartElement element, fin
if (CLASS.equals(element.getName())) {

Check warning on line 217 in src/main/java/edu/hm/hafner/coverage/parser/CoberturaParser.java

View workflow job for this annotation

GitHub Actions / Quality Monitor

Error Prone: YodaCondition

The non-constant portion of an equals check generally comes first. Prefer e.equals(CONSTANT) if e is non-null or Objects.equals(e, CONSTANT) if e may be.
return createClassNode(parentNode, log, name);
}

return createMethodNode(parentNode, element, log, name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import edu.hm.hafner.util.FilteredLog;
import edu.hm.hafner.util.PathUtil;
import edu.hm.hafner.util.SecureXmlParserFactory;
import edu.hm.hafner.util.SecureXmlParserFactory.ParsingException;
import edu.hm.hafner.util.TreeString;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import edu.hm.hafner.util.FilteredLog;
import edu.hm.hafner.util.PathUtil;
import edu.hm.hafner.util.SecureXmlParserFactory;
import edu.hm.hafner.util.SecureXmlParserFactory.ParsingException;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import edu.hm.hafner.coverage.MutationStatus;
import edu.hm.hafner.util.FilteredLog;
import edu.hm.hafner.util.SecureXmlParserFactory;
import edu.hm.hafner.util.SecureXmlParserFactory.ParsingException;

/**
* Parses reports created by PITest into a Java object model.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
import com.google.errorprone.annotations.MustBeClosed;

import edu.hm.hafner.coverage.CoverageParser;
import edu.hm.hafner.coverage.CoverageParser.ParsingException;
import edu.hm.hafner.coverage.CoverageParser.ProcessingMode;
import edu.hm.hafner.coverage.ModuleNode;
import edu.hm.hafner.util.FilteredLog;
import edu.hm.hafner.util.SecureXmlParserFactory.ParsingException;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import static edu.hm.hafner.coverage.assertions.Assertions.*;
Expand Down

0 comments on commit 34d7701

Please sign in to comment.