use of ch.acanda.eclipse.pmd.marker.PMDMarker 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();
}
}
Aggregations