Add test cases for variable expansion in violation message. Enhance replacement logic to escape variable references to non-existent properties, so that MessageFormat does not barf on them. Not 100% foolproof, but should be good enough considering what limitations folks would already be under because of MessageFormat requirements.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@6908 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Ryan Gustafson committed 2009-03-30 04:21:04 +00:00
1 parent 771cc59624
commit f9a41b44ab
2 files changed
+25

No files matched your search

@@ -16,10 +16,13 @@ import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.RulePriority;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.lang.Language;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.lang.java.ast.DummyJavaNode;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.rule.JavaRuleViolation;
import net.sourceforge.pmd.lang.java.symboltable.SourceFileScope;
import net.sourceforge.pmd.lang.rule.properties.IntegerProperty;
import net.sourceforge.pmd.lang.rule.properties.StringProperty;
import org.junit.Test;
@@ -86,6 +89,25 @@ public class AbstractRuleTest {
assertEquals("Rule description mismatch!", "specificdescription", rv.getDescription());
}
@Test
public void testRuleWithVariableInMessage() {
MyRule r = new MyRule();
r.definePropertyDescriptor(new IntegerProperty("testInt", "description", 0, 100, 10, 0));
r.setMessage("Message ${testInt} ${noSuchProperty}");
RuleContext ctx = new RuleContext();
ctx.setLanguageVersion(Language.JAVA.getDefaultVersion());
ctx.setReport(new Report());
ctx.setSourceCodeFilename("filename");
DummyJavaNode s = new DummyJavaNode(1);
s.testingOnly__setBeginColumn(5);
s.testingOnly__setBeginLine(5);
s.setImage("TestImage");
s.setScope(new SourceFileScope("foo"));
r.addViolation(ctx, s);
RuleViolation rv = ctx.getReport().getViolationTree().iterator().next();
assertEquals("Message 10 ${noSuchProperty}", rv.getDescription());
}
@Test
public void testRuleSuppress() {
MyRule r = new MyRule();
@@ -35,8 +35,11 @@ public abstract class AbstractRuleViolationFactory implements RuleViolationFacto
final PropertyDescriptor<?> propertyDescriptor = rule.getPropertyDescriptor(name);
if (propertyDescriptor != null) {
buf.replace(startIndex, endIndex+1, String.valueOf(rule.getProperty(propertyDescriptor)));
continue;
}
}
// Escape the { in the ${, so MessageFormat doesn't bitch
buf.replace(startIndex, startIndex + 2, "$'{'");
}
return buf.toString();
}