use of net.sourceforge.pmd.lang.LanguageVersion in project Gargoyle by callakrsos.
the class PMDCheckComposite method transformParametersIntoConfiguration.
public GargoylePMDConfiguration transformParametersIntoConfiguration(GargoylePMDParameters params) {
// if (null == params.getSourceDir() && null == params.getUri() && null == params.getFileListPath()
// && null == params.getSourceText()) {
// throw new IllegalArgumentException(
// "Please provide a parameter for source root directory (-dir or -d), database URI (-uri or -u), or file list path (-filelist).");
// }
GargoylePMDConfiguration configuration = new GargoylePMDConfiguration();
configuration.setInputPaths(params.getSourceDir());
// configuration.setInputFilePath(params.getFileListPath());
// configuration.setInputUri(params.getUri());
configuration.setReportFormat(REPORT_FILE_FORMAT);
configuration.setSourceFileName(params.getSourceFileName());
configuration.setSourceText(params.getSourceText());
// configuration.setBenchmark(params.isBenchmark());
configuration.setDebug(params.isDebug());
// configuration.setMinimumPriority(params.getMinimumPriority());
configuration.setReportFile(params.getReportfile());
configuration.setReportProperties(params.getProperties());
// configuration.setReportShortNames(params.isShortnames());
configuration.setRuleSets(RESULTSET);
configuration.setRuleSetFactoryCompatibilityEnabled(true);
configuration.setShowSuppressedViolations(params.isShowsuppressed());
configuration.setSourceEncoding("UTF-8");
// configuration.setStressTest(params.isStress());
configuration.setSuppressMarker(params.getSuppressmarker());
// configuration.setThreads(1);
configuration.setFailOnViolation(params.isFailOnViolation());
LanguageVersion languageVersion = LanguageRegistry.findLanguageVersionByTerseName(params.getLanguage() + " " + params.getVersion());
if (languageVersion != null) {
configuration.getLanguageVersionDiscoverer().setDefaultLanguageVersion(languageVersion);
}
try {
configuration.prependClasspath(params.getAuxclasspath());
} catch (IOException e) {
throw new IllegalArgumentException("Invalid auxiliary classpath: " + e.getMessage(), e);
}
return configuration;
}
use of net.sourceforge.pmd.lang.LanguageVersion in project eclipse-pmd by acanda.
the class PMDIntegrationTest method violationRangeAndRuleId.
@Test
public void violationRangeAndRuleId() throws IOException, RuleSetNotFoundException, PMDException {
checkState(params.language.isPresent(), "%s: language is missing", testDataXml);
checkState(params.pmdReferenceId.isPresent(), "%s: pmdReferenceId is missing", testDataXml);
final Matcher languageMatcher = Pattern.compile("(.*)\\s+(\\d+[\\.\\d]+)").matcher(params.language.get());
checkState(languageMatcher.matches(), "%s: language must be formated '<terse-name> <version>'", testDataXml);
final Language language = LanguageRegistry.findLanguageByTerseName(languageMatcher.group(1));
if (language == null) {
failDueToInvalidTerseName(languageMatcher.group(1));
}
final LanguageVersion languageVersion = language.getVersion(languageMatcher.group(2));
final String fileExtension = "." + languageVersion.getLanguage().getExtensions().get(0);
final File sourceFile = File.createTempFile(getFilePrefix(params), fileExtension);
try {
final PMDConfiguration configuration = new PMDConfiguration();
final Reader reader = new StringReader(params.source);
final RuleContext context = PMD.newRuleContext(sourceFile.getName(), sourceFile);
context.setLanguageVersion(languageVersion);
final RuleSets ruleSets = RulesetsFactoryUtils.defaultFactory().createRuleSets(params.pmdReferenceId.get());
new SourceCodeProcessor(configuration).processSourceCode(reader, ruleSets, context);
// handle violations that PMD does not (yet) find.
if (!context.getReport().isEmpty()) {
// PMD might find more than one violation. If there is one with a matching range then the test passes.
final Optional<RuleViolation> violation = Iterators.tryFind(context.getReport().iterator(), new Predicate<RuleViolation>() {
@Override
public boolean apply(final RuleViolation violation) {
final Range range = MarkerUtil.getAbsoluteRange(params.source, violation);
return params.offset == range.getStart() && params.length == range.getEnd() - range.getStart();
}
});
assertTrue(testDataXml + " > " + params.name + ": couldn't find violation with expected range (offset = " + params.offset + ", length = " + params.length + ")", violation.isPresent());
final JavaQuickFixGenerator generator = new JavaQuickFixGenerator();
final PMDMarker marker = mock(PMDMarker.class);
final String ruleId = MarkerUtil.createRuleId(violation.get().getRule());
when(marker.getRuleId()).thenReturn(ruleId);
when(marker.getMarkerText()).thenReturn("");
final JavaQuickFixContext quickFixContext = new JavaQuickFixContext(new Version(languageVersion.getVersion()));
assertTrue("The Java quick fix generator should have quick fixes for " + ruleId, generator.hasQuickFixes(marker, quickFixContext));
assertTrue("The Java quick fix generator should have at least one quick fix besides the SuppressWarningsQuickFix for " + ruleId, generator.getQuickFixes(marker, quickFixContext).size() > 1);
}
} finally {
sourceFile.delete();
}
}
use of net.sourceforge.pmd.lang.LanguageVersion in project pmd by pmd.
the class LanguageVersionDiscovererTest method testJavaFileUsing14.
/**
* Test on Java file with Java version set to 1.4.
*/
@Test
public void testJavaFileUsing14() {
LanguageVersionDiscoverer discoverer = new LanguageVersionDiscoverer();
discoverer.setDefaultLanguageVersion(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.4"));
File javaFile = new File("/path/to/MyClass.java");
LanguageVersion languageVersion = discoverer.getDefaultLanguageVersionForFile(javaFile);
assertEquals("LanguageVersion must be Java 1.4!", LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.4"), languageVersion);
}
use of net.sourceforge.pmd.lang.LanguageVersion 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.LanguageVersion 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