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);
}
}
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;
}
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;
}
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();
}
}
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);
}
}
Aggregations