Search in sources :

Example 1 with IProjectPropertiesManager

use of net.sourceforge.pmd.eclipse.runtime.properties.IProjectPropertiesManager in project pmd-eclipse-plugin by pmd.

the class UpdateProjectPropertiesCmdTest method testBug.

/**
 * Bug: when a user deselect a project rule it is not saved
 */
@Test
public void testBug() throws CommandException, PropertiesException {
    final RuleSetFactory factory = new RuleSetFactory();
    // First ensure that the plugin initial ruleset is equal to the project
    // ruleset
    final IProjectPropertiesManager mgr = PMDPlugin.getDefault().getPropertiesManager();
    final IProjectProperties model = mgr.loadProjectProperties(this.testProject);
    RuleSet projectRuleSet = model.getProjectRuleSet();
    Assert.assertEquals("The project ruleset is not equal to the plugin ruleset", PMDPlugin.getDefault().getPreferencesManager().getRuleSet().getRules(), projectRuleSet.getRules());
    int ruleCountBefore = projectRuleSet.getRules().size();
    // 2. remove a rule (keep its name for assertion)
    RuleSet newRuleSet = RuleSetUtil.newEmpty("test-ruleset", "ruleset for unit testing");
    newRuleSet = RuleSetUtil.addRules(newRuleSet, projectRuleSet.getRules());
    final Rule removedRule = newRuleSet.getRuleByName("UnnecessaryParentheses");
    newRuleSet = RuleSetUtil.removeRule(newRuleSet, removedRule);
    final UpdateProjectPropertiesCmd cmd = new UpdateProjectPropertiesCmd();
    cmd.setPmdEnabled(true);
    cmd.setProject(this.testProject);
    cmd.setProjectRuleSet(newRuleSet);
    cmd.setProjectWorkingSet(null);
    cmd.setRuleSetStoredInProject(false);
    cmd.execute();
    // 3. test the rule has correctly been removed
    projectRuleSet = model.getProjectRuleSet();
    Assert.assertEquals("Rule count should be 1 less", ruleCountBefore - 1, projectRuleSet.getRules().size());
    for (Rule r : projectRuleSet.getRules()) {
        if (r.getName().equals(removedRule.getName()) && r.getLanguage() == removedRule.getLanguage()) {
            Assert.fail("The rule has not been removed!");
        }
    }
}
Also used : RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) RuleSet(net.sourceforge.pmd.RuleSet) IProjectProperties(net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties) IProjectPropertiesManager(net.sourceforge.pmd.eclipse.runtime.properties.IProjectPropertiesManager) Rule(net.sourceforge.pmd.Rule) Test(org.junit.Test)

Aggregations

Rule (net.sourceforge.pmd.Rule)1 RuleSet (net.sourceforge.pmd.RuleSet)1 RuleSetFactory (net.sourceforge.pmd.RuleSetFactory)1 IProjectProperties (net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties)1 IProjectPropertiesManager (net.sourceforge.pmd.eclipse.runtime.properties.IProjectPropertiesManager)1 Test (org.junit.Test)1