use of net.sourceforge.pmd.lang.rule.xpath.SaxonXPathRuleQuery 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.lang.rule.xpath.SaxonXPathRuleQuery 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());
}
Aggregations