Skip to content

Commit a654daa

Browse files
committed
Use an enum in the parent class to define the resilience.
1 parent c25b72e commit a654daa

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

src/main/java/edu/hm/hafner/coverage/CoverageParser.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,47 @@
1818
* @author Ullrich Hafner
1919
*/
2020
public abstract class CoverageParser implements Serializable {
21+
/**
22+
* Defines how to handle fatal errors during parsing.
23+
*/
24+
public enum ProcessingMode {
25+
/** All fatal errors will be ignored and logged. */
26+
IGNORE_ERRORS,
27+
/** An exception will be thrown if a fatal error is detected. */
28+
FAIL_FAST
29+
}
30+
2131
private static final long serialVersionUID = 3941742254762282096L;
2232
private transient TreeStringBuilder treeStringBuilder = new TreeStringBuilder();
2333

34+
private final ProcessingMode processingMode; // since 0.26.0
35+
36+
/**
37+
* Creates a new instance of {@link CoverageParser}.
38+
*
39+
* @param processingMode
40+
* determines whether to ignore errors
41+
*/
42+
protected CoverageParser(final ProcessingMode processingMode) {
43+
this.processingMode = processingMode;
44+
}
45+
46+
/**
47+
* Creates a new instance of {@link CoverageParser} that will fail on all errors.
48+
*/
49+
protected CoverageParser() {
50+
this(ProcessingMode.FAIL_FAST);
51+
}
52+
53+
/**
54+
* Returns whether to ignore errors or to fail fast.
55+
*
56+
* @return true if errors should be ignored, false if an exception should be thrown on errors
57+
*/
58+
protected boolean ignoreErrors() {
59+
return processingMode == ProcessingMode.IGNORE_ERRORS;
60+
}
61+
2462
/**
2563
* Parses a report provided by the given reader.
2664
*

src/main/java/edu/hm/hafner/coverage/parser/CoberturaParser.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,21 @@ public class CoberturaParser extends CoverageParser {
6363
private static final QName BRANCH = new QName("branch");
6464
private static final QName CONDITION_COVERAGE = new QName("condition-coverage");
6565

66-
private final boolean ignoreErrors; // since 0.26.0
67-
6866
/**
6967
* Creates a new instance of {@link CoberturaParser}.
7068
*/
7169
public CoberturaParser() {
72-
this(false);
70+
this(ProcessingMode.FAIL_FAST);
7371
}
7472

7573
/**
7674
* Creates a new instance of {@link CoberturaParser}.
7775
*
78-
* @param ignoreErrors
76+
* @param processingMode
7977
* determines whether to ignore errors
8078
*/
81-
public CoberturaParser(final boolean ignoreErrors) {
82-
super();
83-
84-
this.ignoreErrors = ignoreErrors;
79+
public CoberturaParser(final ProcessingMode processingMode) {
80+
super(processingMode);
8581
}
8682

8783
@Override
@@ -182,7 +178,7 @@ private Node readClassOrMethod(final XMLEventReader reader, final FileNode fileN
182178
}
183179
else if (METHOD.equals(nextElement.getName())) {
184180
Node methodNode = readClassOrMethod(reader, fileNode, nextElement, log);
185-
if (node.hasChild(methodNode.getName()) && ignoreErrors) {
181+
if (node.hasChild(methodNode.getName()) && ignoreErrors()) {
186182
log.logError("Skipping duplicate method '%s' for class '%s'", node.getName(), methodNode.getName());
187183
}
188184
else {

src/test/java/edu/hm/hafner/coverage/parser/CoberturaParserTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import edu.hm.hafner.coverage.Coverage;
99
import edu.hm.hafner.coverage.Coverage.CoverageBuilder;
10+
import edu.hm.hafner.coverage.CoverageParser.ProcessingMode;
1011
import edu.hm.hafner.coverage.CyclomaticComplexity;
1112
import edu.hm.hafner.coverage.FileNode;
1213
import edu.hm.hafner.coverage.FractionValue;
@@ -46,7 +47,8 @@ private void verifyBranchCoverageOfLine61(final Node duplicateMethods) {
4647

4748
@Test
4849
void shouldIgnoreDuplicateMethods() {
49-
Node duplicateMethods = readReport("cobertura-duplicate-methods.xml", new CoberturaParser(true));
50+
Node duplicateMethods = readReport("cobertura-duplicate-methods.xml",
51+
new CoberturaParser(ProcessingMode.IGNORE_ERRORS));
5052

5153
verifySmallTree(duplicateMethods);
5254
assertThat(getLog().hasErrors()).isTrue();

0 commit comments

Comments
 (0)