pmd: let failing rules fail the unit tests again

so that we can early detect any introduced exceptions (like NPE).
This commit is contained in:
Andreas Dangel committed 2013-04-27 10:03:10 +02:00
1 parent 66fed8d9cc
commit 2aad65138a
3 files changed
+31 -1

No files matched your search

@@ -33,6 +33,7 @@ public class RuleContext {
private String sourceCodeFilename;
private LanguageVersion languageVersion;
private final Map<String, Object> attributes;
private boolean ignoreExceptions = true;
/**
* Default constructor.
@@ -189,4 +190,28 @@ public class RuleContext {
public Object removeAttribute(String name) {
return this.attributes.remove(name);
}
/**
* Configure whether exceptions during applying a rule should be ignored or not.
* If set to <code>true</code> then such exceptions are logged as warnings and
* the processing is continued with the next rule - the failing rule is simply skipped.
* This is the default behavior.
* <br>
* If set to <code>false</code> then the processing will be aborted with the exception.
* This is especially useful during unit tests, in order to not oversee any exceptions.
* @param ignoreExceptions if <code>true</code> simply skip failing rules (default).
*/
public void setIgnoreExceptions(boolean ignoreExceptions) {
this.ignoreExceptions = ignoreExceptions;
}
/**
* Gets the configuration whether to skip failing rules (<code>true</code>)
* or whether to throw a a RuntimeException and abort the processing for the first
* failing rule.
* @return <code>true</code> when failing rules are skipped, <code>false</code> otherwise.
*/
public boolean isIgnoreExceptions() {
return ignoreExceptions;
}
}
@@ -239,7 +239,11 @@ public class RuleSet {
} catch (ThreadDeath td) {
throw td;
} catch (Throwable t) {
LOG.log(Level.WARNING, "Exception applying rule " + rule.getName() + ", continuing with next rule", t);
if (ctx.isIgnoreExceptions()) {
LOG.log(Level.WARNING, "Exception applying rule " + rule.getName() + ", continuing with next rule", t);
} else {
throw new RuntimeException(t);
}
}
}
}
@@ -124,6 +124,7 @@ public abstract class RuleTst {
ctx.setReport(report);
ctx.setSourceCodeFilename("n/a");
ctx.setLanguageVersion(languageVersion);
ctx.setIgnoreExceptions(false);
RuleSet rules = new RuleSet();
rules.addRule(rule);
p.getSourceCodeProcessor().processSourceCode(new StringReader(code), new RuleSets(rules), ctx);