Search in sources :

Example 1 with PMDMarker

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();
    }
}
Also used : RuleContext(net.sourceforge.pmd.RuleContext) Matcher(java.util.regex.Matcher) SourceCodeProcessor(net.sourceforge.pmd.SourceCodeProcessor) Reader(java.io.Reader) StringReader(java.io.StringReader) RuleViolation(net.sourceforge.pmd.RuleViolation) Range(ch.acanda.eclipse.pmd.marker.MarkerUtil.Range) Language(net.sourceforge.pmd.lang.Language) Version(org.osgi.framework.Version) LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) RuleSets(net.sourceforge.pmd.RuleSets) PMDMarker(ch.acanda.eclipse.pmd.marker.PMDMarker) StringReader(java.io.StringReader) LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) File(java.io.File) PMDConfiguration(net.sourceforge.pmd.PMDConfiguration) Test(org.junit.Test)

Aggregations

Range (ch.acanda.eclipse.pmd.marker.MarkerUtil.Range)1 PMDMarker (ch.acanda.eclipse.pmd.marker.PMDMarker)1 File (java.io.File)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 Matcher (java.util.regex.Matcher)1 PMDConfiguration (net.sourceforge.pmd.PMDConfiguration)1 RuleContext (net.sourceforge.pmd.RuleContext)1 RuleSets (net.sourceforge.pmd.RuleSets)1 RuleViolation (net.sourceforge.pmd.RuleViolation)1 SourceCodeProcessor (net.sourceforge.pmd.SourceCodeProcessor)1 Language (net.sourceforge.pmd.lang.Language)1 LanguageVersion (net.sourceforge.pmd.lang.LanguageVersion)1 Test (org.junit.Test)1 Version (org.osgi.framework.Version)1