Search in sources :

Example 1 with RuleSetNotFoundException

use of net.sourceforge.pmd.RuleSetNotFoundException in project eclipse-pmd by acanda.

the class AnalyzerTest method analyze.

/**
 * Prepares the arguments, calls {@link Analyzer#analyze(IFile, RuleSets, ViolationProcessor), and verifies that it
 * invokes {@link ViolationProcessor#annotate(IFile, Iterable) with the correct rule violations and that it invokes
 * {@link RuleSets#start(RuleContext)} as well as {@link RuleSets#end(RuleContext)} if the file is valid.
 */
public void analyze(final IFile file, final String ruleSetRefId, final String... violatedRules) {
    try {
        final ViolationProcessor violationProcessor = mock(ViolationProcessor.class);
        final RuleSets ruleSets = spy(new RuleSetFactory().createRuleSets(ruleSetRefId));
        new Analyzer().analyze(file, ruleSets, violationProcessor);
        verify(violationProcessor).annotate(same(file), violations(violatedRules));
        final boolean isValidFile = violatedRules.length > 0;
        if (isValidFile) {
            verify(ruleSets).start(any(RuleContext.class));
            verify(ruleSets).end(any(RuleContext.class));
        }
    } catch (final RuleSetNotFoundException e) {
        throw new AssertionError("Failed to create rule sets", e);
    } catch (CoreException | IOException e) {
        throw new AssertionError("Failed to annotate file", e);
    }
}
Also used : RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) RuleContext(net.sourceforge.pmd.RuleContext) CoreException(org.eclipse.core.runtime.CoreException) RuleSets(net.sourceforge.pmd.RuleSets) RuleSetNotFoundException(net.sourceforge.pmd.RuleSetNotFoundException) IOException(java.io.IOException)

Example 2 with RuleSetNotFoundException

use of net.sourceforge.pmd.RuleSetNotFoundException in project eclipse-pmd by acanda.

the class PMDProjectSettings method getActiveRuleSets.

public RuleSets getActiveRuleSets() {
    RuleSets ruleSets = null;
    try {
        ruleSets = getActiveRuleSetsFromCache();
        if (ruleSets == null) {
            final PMDWorkspaceSettings workspaceSettings = new PMDWorkspaceSettings(PMDPlugin.getDefault().getPreferenceStore());
            final ImmutableList<RuleSetConfiguration> configs = workspaceSettings.getRuleSetsConfigurations();
            final ImmutableList<RuleSetConfiguration> activeConfigs = ImmutableList.copyOf(getActiveRuleSetConfigurations(configs));
            ruleSets = new RuleSetFactory().createRuleSets(Lists.transform(activeConfigs, toReferenceId));
            putActiveRuleSetsIntoCache(ruleSets);
        }
    } catch (final RuleSetNotFoundException e) {
        PMDPlugin.getDefault().error("Could not load PMD rule sets.", e);
    }
    if (ruleSets == null) {
        ruleSets = new RuleSets();
        putActiveRuleSetsIntoCache(ruleSets);
    }
    return ruleSets;
}
Also used : RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) RuleSets(net.sourceforge.pmd.RuleSets) RuleSetNotFoundException(net.sourceforge.pmd.RuleSetNotFoundException)

Example 3 with RuleSetNotFoundException

use of net.sourceforge.pmd.RuleSetNotFoundException in project Gargoyle by callakrsos.

the class RulesetsFactoryUtils method getRuleSets.

/**
	 * Creates a new rulesets with the given string. The resulting rulesets will contain all referenced rulesets.
	 * 
	 * @param rulesets
	 *            the string with the rulesets to load
	 * @param factory
	 *            the ruleset factory
	 * @return the rulesets
	 * @throws IllegalArgumentException
	 *             if rulesets is empty (means, no rules have been found) or if a ruleset couldn't be found.
	 */
public static RuleSets getRuleSets(String rulesets, RuleSetFactory factory) {
    RuleSets ruleSets = null;
    try {
        factory.setWarnDeprecated(true);
        ruleSets = factory.createRuleSets(rulesets);
        factory.setWarnDeprecated(false);
        printRuleNamesInDebug(ruleSets);
        if (ruleSets.ruleCount() == 0) {
            String msg = "No rules found. Maybe you mispelled a rule name? (" + rulesets + ")";
            LOG.log(Level.SEVERE, msg);
            throw new IllegalArgumentException(msg);
        }
    } catch (RuleSetNotFoundException rsnfe) {
        LOG.log(Level.SEVERE, "Ruleset not found", rsnfe);
        throw new IllegalArgumentException(rsnfe);
    }
    return ruleSets;
}
Also used : RuleSets(net.sourceforge.pmd.RuleSets) RuleSetNotFoundException(net.sourceforge.pmd.RuleSetNotFoundException)

Example 4 with RuleSetNotFoundException

use of net.sourceforge.pmd.RuleSetNotFoundException in project eclipse-pmd by acanda.

the class RuleSetsCacheLoader method load.

@Override
public RuleSets load(final String projectName) {
    PMDPlugin.getDefault().info("RuleSetsCache: loading rule sets for project " + projectName);
    try {
        final ProjectModel projectModel = repository.load(projectName).or(new ProjectModel(projectName));
        final ImmutableSortedSet<RuleSetModel> ruleSetModels = projectModel.getRuleSets();
        final Iterable<RuleSetReferenceId> ids = presentInstances(transform(ruleSetModels, new ToReferenceId(projectName)));
        return new RuleSetFactory().createRuleSets(ImmutableList.copyOf(ids));
    } catch (final RuleSetNotFoundException e) {
        PMDPlugin.getDefault().error("Cannot load rule sets for project " + projectName, e);
        return new RuleSets();
    }
}
Also used : RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) RuleSetModel(ch.acanda.eclipse.pmd.domain.RuleSetModel) RuleSetReferenceId(net.sourceforge.pmd.RuleSetReferenceId) RuleSets(net.sourceforge.pmd.RuleSets) RuleSetNotFoundException(net.sourceforge.pmd.RuleSetNotFoundException) ProjectModel(ch.acanda.eclipse.pmd.domain.ProjectModel)

Example 5 with RuleSetNotFoundException

use of net.sourceforge.pmd.RuleSetNotFoundException in project eclipse-pmd by acanda.

the class AddRuleSetConfigurationModel method validateLocation.

/**
 * Validates the location of the rule set configuration and sets or resets the property {@link #rules} depending on
 * whether {@link #location} contains a valid rule set configuration location or not.
 */
@SuppressWarnings("PMD.AvoidCatchingGenericException")
private void validateLocation(final String propertyName, final ValidationResult result) {
    final Builder<Rule> rules = ImmutableList.builder();
    String ruleSetName = null;
    if (!errorIfBlank(LOCATION, location, "Please enter the location of the rule set configuration", result)) {
        RuleSet ruleSet = null;
        try {
            final String referenceId;
            if (isRemoteTypeSelected) {
                referenceId = validateRemoteLocation(result);
            } else {
                referenceId = validateLocalLocation(result);
            }
            if (referenceId != null) {
                ruleSet = new RuleSetFactory().createRuleSet(referenceId);
                ruleSetName = ruleSet.getName();
                rules.addAll(ruleSet.getRules());
            }
        } catch (final RuleSetNotFoundException | RuntimeException e) {
        // the rule set location is invalid - the validation problem will be added below
        }
        if (ruleSet == null || ruleSet.getRules().isEmpty()) {
            result.add(new ValidationProblem(LOCATION, Severity.ERROR, "The rule set configuration at the given location is invalid"));
        }
    }
    if (LOCATION.equals(propertyName)) {
        setRules(rules.build());
        setName(ruleSetName == null ? "" : ruleSetName);
    }
}
Also used : RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) RuleSet(net.sourceforge.pmd.RuleSet) RuleSetNotFoundException(net.sourceforge.pmd.RuleSetNotFoundException) ValidationProblem(ch.acanda.eclipse.pmd.ui.model.ValidationProblem) Rule(net.sourceforge.pmd.Rule)

Aggregations

RuleSetNotFoundException (net.sourceforge.pmd.RuleSetNotFoundException)5 RuleSetFactory (net.sourceforge.pmd.RuleSetFactory)4 RuleSets (net.sourceforge.pmd.RuleSets)4 ProjectModel (ch.acanda.eclipse.pmd.domain.ProjectModel)1 RuleSetModel (ch.acanda.eclipse.pmd.domain.RuleSetModel)1 ValidationProblem (ch.acanda.eclipse.pmd.ui.model.ValidationProblem)1 IOException (java.io.IOException)1 Rule (net.sourceforge.pmd.Rule)1 RuleContext (net.sourceforge.pmd.RuleContext)1 RuleSet (net.sourceforge.pmd.RuleSet)1 RuleSetReferenceId (net.sourceforge.pmd.RuleSetReferenceId)1 CoreException (org.eclipse.core.runtime.CoreException)1