use of net.sourceforge.pmd.properties.PropertyDescriptor in project pmd by pmd.
the class XPathRuleTest method testFollowingSibling.
/**
* Following sibling check: See https://sourceforge.net/p/pmd/bugs/1209/
*
* @throws Exception
* any error
*/
@Test
public void testFollowingSibling() throws Exception {
final String SOURCE = "public class dummy {\n" + " public String toString() {\n" + " String test = \"bad example\";\n" + " test = \"a\";\n" + " return test;\n" + " }\n" + "}";
LanguageVersion language = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion();
ParserOptions parserOptions = language.getLanguageVersionHandler().getDefaultParserOptions();
Parser parser = language.getLanguageVersionHandler().getParser(parserOptions);
ASTCompilationUnit cu = (ASTCompilationUnit) parser.parse("test", new StringReader(SOURCE));
RuleContext ruleContext = new RuleContext();
ruleContext.setLanguageVersion(language);
String xpath = "//Block/BlockStatement/following-sibling::BlockStatement";
// XPATH version 1.0
XPathRuleQuery xpathRuleQuery = new JaxenXPathRuleQuery();
xpathRuleQuery.setXPath(xpath);
xpathRuleQuery.setProperties(new HashMap<PropertyDescriptor<?>, Object>());
xpathRuleQuery.setVersion(XPathRuleQuery.XPATH_1_0);
List<Node> nodes = xpathRuleQuery.evaluate(cu, ruleContext);
assertEquals(2, nodes.size());
assertEquals(4, nodes.get(0).getBeginLine());
assertEquals(5, nodes.get(1).getBeginLine());
// XPATH version 2.0
xpathRuleQuery = new SaxonXPathRuleQuery();
xpathRuleQuery.setXPath(xpath);
xpathRuleQuery.setProperties(new HashMap<PropertyDescriptor<?>, Object>());
xpathRuleQuery.setVersion(XPathRuleQuery.XPATH_2_0);
nodes = xpathRuleQuery.evaluate(cu, ruleContext);
assertEquals(2, nodes.size());
assertEquals(4, nodes.get(0).getBeginLine());
assertEquals(5, nodes.get(1).getBeginLine());
}
use of net.sourceforge.pmd.properties.PropertyDescriptor in project pmd by pmd.
the class XPathRuleTest method testImageOfPrimarySuffix.
/**
* Test for problem reported in bug #1219 PrimarySuffix/@Image does not work
* in some cases in xpath 2.0
*
* @throws Exception
* any error
*/
@Test
public void testImageOfPrimarySuffix() throws Exception {
final String SUFFIX = "import java.io.File;\n" + "\n" + "public class TestSuffix {\n" + " public static void main(String args[]) {\n" + " new File(\"subdirectory\").list();\n" + " }\n" + "}";
LanguageVersion language = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion();
ParserOptions parserOptions = language.getLanguageVersionHandler().getDefaultParserOptions();
Parser parser = language.getLanguageVersionHandler().getParser(parserOptions);
ASTCompilationUnit cu = (ASTCompilationUnit) parser.parse("test", new StringReader(SUFFIX));
RuleContext ruleContext = new RuleContext();
ruleContext.setLanguageVersion(language);
String xpath = "//PrimarySuffix[@Image='list']";
// XPATH version 1.0
XPathRuleQuery xpathRuleQuery = new JaxenXPathRuleQuery();
xpathRuleQuery.setXPath(xpath);
xpathRuleQuery.setProperties(new HashMap<PropertyDescriptor<?>, Object>());
xpathRuleQuery.setVersion(XPathRuleQuery.XPATH_1_0);
List<Node> nodes = xpathRuleQuery.evaluate(cu, ruleContext);
assertEquals(1, nodes.size());
// XPATH version 1.0 Compatibility
xpathRuleQuery = new SaxonXPathRuleQuery();
xpathRuleQuery.setXPath(xpath);
xpathRuleQuery.setProperties(new HashMap<PropertyDescriptor<?>, Object>());
xpathRuleQuery.setVersion(XPathRuleQuery.XPATH_1_0_COMPATIBILITY);
nodes = xpathRuleQuery.evaluate(cu, ruleContext);
assertEquals(1, nodes.size());
// XPATH version 2.0
xpathRuleQuery = new SaxonXPathRuleQuery();
xpathRuleQuery.setXPath(xpath);
xpathRuleQuery.setProperties(new HashMap<PropertyDescriptor<?>, Object>());
xpathRuleQuery.setVersion(XPathRuleQuery.XPATH_2_0);
nodes = xpathRuleQuery.evaluate(cu, ruleContext);
assertEquals(1, nodes.size());
}
use of net.sourceforge.pmd.properties.PropertyDescriptor in project pmd by pmd.
the class RuleSetWriter method createRuleElement.
private Element createRuleElement(Rule rule) {
if (rule instanceof RuleReference) {
RuleReference ruleReference = (RuleReference) rule;
RuleSetReference ruleSetReference = ruleReference.getRuleSetReference();
if (ruleSetReference.isAllRules()) {
if (!ruleSetFileNames.contains(ruleSetReference.getRuleSetFileName())) {
ruleSetFileNames.add(ruleSetReference.getRuleSetFileName());
return createRuleSetReferenceElement(ruleSetReference);
} else {
return null;
}
} else {
Language language = ruleReference.getOverriddenLanguage();
LanguageVersion minimumLanguageVersion = ruleReference.getOverriddenMinimumLanguageVersion();
LanguageVersion maximumLanguageVersion = ruleReference.getOverriddenMaximumLanguageVersion();
Boolean deprecated = ruleReference.isOverriddenDeprecated();
String name = ruleReference.getOverriddenName();
String ref = ruleReference.getRuleSetReference().getRuleSetFileName() + '/' + ruleReference.getRule().getName();
String message = ruleReference.getOverriddenMessage();
String externalInfoUrl = ruleReference.getOverriddenExternalInfoUrl();
String description = ruleReference.getOverriddenDescription();
RulePriority priority = ruleReference.getOverriddenPriority();
List<PropertyDescriptor<?>> propertyDescriptors = ruleReference.getOverriddenPropertyDescriptors();
Map<PropertyDescriptor<?>, Object> propertiesByPropertyDescriptor = ruleReference.getOverriddenPropertiesByPropertyDescriptor();
List<String> examples = ruleReference.getOverriddenExamples();
return createSingleRuleElement(language, minimumLanguageVersion, maximumLanguageVersion, deprecated, name, null, ref, message, externalInfoUrl, null, null, null, null, description, priority, propertyDescriptors, propertiesByPropertyDescriptor, examples);
}
} else {
return createSingleRuleElement(rule instanceof ImmutableLanguage ? null : rule.getLanguage(), rule.getMinimumLanguageVersion(), rule.getMaximumLanguageVersion(), rule.isDeprecated(), rule.getName(), rule.getSince(), null, rule.getMessage(), rule.getExternalInfoUrl(), rule.getRuleClass(), rule.isDfa(), rule.isTypeResolution(), rule.isMultifile(), rule.getDescription(), rule.getPriority(), rule.getPropertyDescriptors(), rule.getPropertiesByPropertyDescriptor(), rule.getExamples());
}
}
use of net.sourceforge.pmd.properties.PropertyDescriptor in project pmd by pmd.
the class RuleTst method runTest.
/**
* Run the rule on the given code, and check the expected number of
* violations.
*/
@SuppressWarnings("unchecked")
public void runTest(TestDescriptor test) {
Rule rule = test.getRule();
if (test.getReinitializeRule()) {
rule = reinitializeRule(rule);
}
Map<PropertyDescriptor<?>, Object> oldProperties = rule.getPropertiesByPropertyDescriptor();
try {
int res;
Report report;
try {
// Set test specific properties onto the Rule
if (test.getProperties() != null) {
for (Map.Entry<Object, Object> entry : test.getProperties().entrySet()) {
String propertyName = (String) entry.getKey();
PropertyDescriptor propertyDescriptor = rule.getPropertyDescriptor(propertyName);
if (propertyDescriptor == null) {
throw new IllegalArgumentException("No such property '" + propertyName + "' on Rule " + rule.getName());
}
Object value = propertyDescriptor.valueFrom((String) entry.getValue());
rule.setProperty(propertyDescriptor, value);
}
}
report = processUsingStringReader(test, rule);
res = report.size();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException('"' + test.getDescription() + "\" failed", e);
}
if (test.getNumberOfProblemsExpected() != res) {
printReport(test, report);
}
assertEquals('"' + test.getDescription() + "\" resulted in wrong number of failures,", test.getNumberOfProblemsExpected(), res);
assertMessages(report, test);
assertLineNumbers(report, test);
} finally {
// Restore old properties
for (Map.Entry<PropertyDescriptor<?>, Object> entry : oldProperties.entrySet()) {
rule.setProperty((PropertyDescriptor) entry.getKey(), entry.getValue());
}
}
}
use of net.sourceforge.pmd.properties.PropertyDescriptor in project pmd by pmd.
the class AbstractRuleSetFactoryTest method assertEqualsRuleSet.
private void assertEqualsRuleSet(String message, RuleSet ruleSet1, RuleSet ruleSet2) {
assertEquals(message + ", RuleSet name", ruleSet1.getName(), ruleSet2.getName());
assertEquals(message + ", RuleSet description", ruleSet1.getDescription(), ruleSet2.getDescription());
assertEquals(message + ", RuleSet exclude patterns", ruleSet1.getExcludePatterns(), ruleSet2.getExcludePatterns());
assertEquals(message + ", RuleSet include patterns", ruleSet1.getIncludePatterns(), ruleSet2.getIncludePatterns());
assertEquals(message + ", RuleSet rule count", ruleSet1.getRules().size(), ruleSet2.getRules().size());
for (int i = 0; i < ruleSet1.getRules().size(); i++) {
Rule rule1 = ((List<Rule>) ruleSet1.getRules()).get(i);
Rule rule2 = ((List<Rule>) ruleSet2.getRules()).get(i);
assertFalse(message + ", Different RuleReference", rule1 instanceof RuleReference != rule2 instanceof RuleReference);
if (rule1 instanceof RuleReference) {
RuleReference ruleReference1 = (RuleReference) rule1;
RuleReference ruleReference2 = (RuleReference) rule2;
assertEquals(message + ", RuleReference overridden language", ruleReference1.getOverriddenLanguage(), ruleReference2.getOverriddenLanguage());
assertEquals(message + ", RuleReference overridden minimum language version", ruleReference1.getOverriddenMinimumLanguageVersion(), ruleReference2.getOverriddenMinimumLanguageVersion());
assertEquals(message + ", RuleReference overridden maximum language version", ruleReference1.getOverriddenMaximumLanguageVersion(), ruleReference2.getOverriddenMaximumLanguageVersion());
assertEquals(message + ", RuleReference overridden deprecated", ruleReference1.isOverriddenDeprecated(), ruleReference2.isOverriddenDeprecated());
assertEquals(message + ", RuleReference overridden name", ruleReference1.getOverriddenName(), ruleReference2.getOverriddenName());
assertEquals(message + ", RuleReference overridden description", ruleReference1.getOverriddenDescription(), ruleReference2.getOverriddenDescription());
assertEquals(message + ", RuleReference overridden message", ruleReference1.getOverriddenMessage(), ruleReference2.getOverriddenMessage());
assertEquals(message + ", RuleReference overridden external info url", ruleReference1.getOverriddenExternalInfoUrl(), ruleReference2.getOverriddenExternalInfoUrl());
assertEquals(message + ", RuleReference overridden priority", ruleReference1.getOverriddenPriority(), ruleReference2.getOverriddenPriority());
assertEquals(message + ", RuleReference overridden examples", ruleReference1.getOverriddenExamples(), ruleReference2.getOverriddenExamples());
}
assertEquals(message + ", Rule name", rule1.getName(), rule2.getName());
assertEquals(message + ", Rule class", rule1.getRuleClass(), rule2.getRuleClass());
assertEquals(message + ", Rule description " + rule1.getName(), rule1.getDescription(), rule2.getDescription());
assertEquals(message + ", Rule message", rule1.getMessage(), rule2.getMessage());
assertEquals(message + ", Rule external info url", rule1.getExternalInfoUrl(), rule2.getExternalInfoUrl());
assertEquals(message + ", Rule priority", rule1.getPriority(), rule2.getPriority());
assertEquals(message + ", Rule examples", rule1.getExamples(), rule2.getExamples());
List<PropertyDescriptor<?>> propertyDescriptors1 = rule1.getPropertyDescriptors();
List<PropertyDescriptor<?>> propertyDescriptors2 = rule2.getPropertyDescriptors();
assertEquals(message + ", Rule property descriptor ", propertyDescriptors1, propertyDescriptors2);
for (int j = 0; j < propertyDescriptors1.size(); j++) {
assertEquals(message + ", Rule property value " + j, rule1.getProperty(propertyDescriptors1.get(j)), rule2.getProperty(propertyDescriptors2.get(j)));
}
assertEquals(message + ", Rule property descriptor count", propertyDescriptors1.size(), propertyDescriptors2.size());
}
}
Aggregations