Skip to content

Commit

Permalink
Use an enum in the parent class to define the resilience.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed Oct 11, 2023
1 parent c25b72e commit a654daa
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
38 changes: 38 additions & 0 deletions src/main/java/edu/hm/hafner/coverage/CoverageParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,47 @@
* @author Ullrich Hafner
*/
public abstract class CoverageParser implements Serializable {
/**
* Defines how to handle fatal errors during parsing.
*/
public enum ProcessingMode {
/** All fatal errors will be ignored and logged. */
IGNORE_ERRORS,
/** An exception will be thrown if a fatal error is detected. */
FAIL_FAST
}

private static final long serialVersionUID = 3941742254762282096L;
private transient TreeStringBuilder treeStringBuilder = new TreeStringBuilder();

private final ProcessingMode processingMode; // since 0.26.0

/**
* Creates a new instance of {@link CoverageParser}.
*
* @param processingMode
* determines whether to ignore errors
*/
protected CoverageParser(final ProcessingMode processingMode) {
this.processingMode = processingMode;
}

/**
* Creates a new instance of {@link CoverageParser} that will fail on all errors.
*/
protected CoverageParser() {
this(ProcessingMode.FAIL_FAST);
}

/**
* Returns whether to ignore errors or to fail fast.
*
* @return true if errors should be ignored, false if an exception should be thrown on errors
*/
protected boolean ignoreErrors() {
return processingMode == ProcessingMode.IGNORE_ERRORS;
}

/**
* Parses a report provided by the given reader.
*
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/edu/hm/hafner/coverage/parser/CoberturaParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,21 @@ public class CoberturaParser extends CoverageParser {
private static final QName BRANCH = new QName("branch");
private static final QName CONDITION_COVERAGE = new QName("condition-coverage");

private final boolean ignoreErrors; // since 0.26.0

/**
* Creates a new instance of {@link CoberturaParser}.
*/
public CoberturaParser() {
this(false);
this(ProcessingMode.FAIL_FAST);
}

/**
* Creates a new instance of {@link CoberturaParser}.
*
* @param ignoreErrors
* @param processingMode
* determines whether to ignore errors
*/
public CoberturaParser(final boolean ignoreErrors) {
super();

this.ignoreErrors = ignoreErrors;
public CoberturaParser(final ProcessingMode processingMode) {
super(processingMode);
}

@Override
Expand Down Expand Up @@ -182,7 +178,7 @@ private Node readClassOrMethod(final XMLEventReader reader, final FileNode fileN
}
else if (METHOD.equals(nextElement.getName())) {
Node methodNode = readClassOrMethod(reader, fileNode, nextElement, log);
if (node.hasChild(methodNode.getName()) && ignoreErrors) {
if (node.hasChild(methodNode.getName()) && ignoreErrors()) {
log.logError("Skipping duplicate method '%s' for class '%s'", node.getName(), methodNode.getName());
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import edu.hm.hafner.coverage.Coverage;
import edu.hm.hafner.coverage.Coverage.CoverageBuilder;
import edu.hm.hafner.coverage.CoverageParser.ProcessingMode;
import edu.hm.hafner.coverage.CyclomaticComplexity;
import edu.hm.hafner.coverage.FileNode;
import edu.hm.hafner.coverage.FractionValue;
Expand Down Expand Up @@ -46,7 +47,8 @@ private void verifyBranchCoverageOfLine61(final Node duplicateMethods) {

@Test
void shouldIgnoreDuplicateMethods() {
Node duplicateMethods = readReport("cobertura-duplicate-methods.xml", new CoberturaParser(true));
Node duplicateMethods = readReport("cobertura-duplicate-methods.xml",
new CoberturaParser(ProcessingMode.IGNORE_ERRORS));

verifySmallTree(duplicateMethods);
assertThat(getLog().hasErrors()).isTrue();
Expand Down

0 comments on commit a654daa

Please sign in to comment.