Search in sources :

Example 1 with IProjectProperties

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

the class PMDPropertyPageController method getPropertyPageBean.

/**
 * populates teh property page bean from the loaded properties
 *
 * @return Returns the propertyPageBean.
 */
public PMDPropertyPageBean getPropertyPageBean() {
    if (this.propertyPageBean == null) {
        LOG.debug("Building a property page bean");
        try {
            final IProjectProperties properties = PMDPlugin.getDefault().loadProjectProperties(this.project);
            propertyPageBean = new PMDPropertyPageBean();
            propertyPageBean.setPmdEnabled(properties.isPmdEnabled());
            propertyPageBean.setProjectWorkingSet(properties.getProjectWorkingSet());
            propertyPageBean.setProjectRuleSet(properties.getProjectRuleSet());
            propertyPageBean.setRuleSetStoredInProject(properties.isRuleSetStoredInProject());
            propertyPageBean.setRuleSetFile(properties.getRuleSetFile());
            propertyPageBean.setIncludeDerivedFiles(properties.isIncludeDerivedFiles());
            propertyPageBean.setFullBuildEnabled(properties.isFullBuildEnabled());
            propertyPageBean.setViolationsAsErrors(properties.violationsAsErrors());
            pmdAlreadyActivated = properties.isPmdEnabled();
        } catch (PropertiesException e) {
            PMDPlugin.getDefault().showError(e.getMessage(), e);
        }
    }
    return this.propertyPageBean;
}
Also used : IProjectProperties(net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties) PropertiesException(net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException)

Example 2 with IProjectProperties

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

the class UpdateProjectPropertiesCmd method execute.

/**
 * @see name.herlin.command.AbstractProcessableCommand#execute()
 */
public void execute() throws CommandException {
    try {
        final IProjectProperties properties = projectProperties();
        properties.setPmdEnabled(pmdEnabled);
        properties.setProjectRuleSet(projectRuleSet);
        properties.setProjectWorkingSet(projectWorkingSet);
        properties.setRuleSetStoredInProject(ruleSetStoredInProject);
        properties.setRuleSetFile(ruleSetFile);
        properties.setIncludeDerivedFiles(includeDerivedFiles);
        properties.setFullBuildEnabled(fullBuildEnabled);
        properties.setViolationsAsErrors(violationsAsErrors);
        properties.sync();
        needRebuild = properties.isNeedRebuild();
        ruleSetFileExists = !properties.isRuleSetFileExist();
    } catch (PropertiesException e) {
        throw new CommandException(e.getMessage(), e);
    } finally {
        setTerminated(true);
    }
}
Also used : IProjectProperties(net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties) PropertiesException(net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException) CommandException(name.herlin.command.CommandException)

Example 3 with IProjectProperties

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

the class ReviewCmdTest method testProjectBuildPath.

/**
 * https://sourceforge.net/p/pmd/bugs/1145/
 */
@Test
public void testProjectBuildPath() throws Exception {
    IProjectProperties properties = PMDPlugin.getDefault().getPropertiesManager().loadProjectProperties(testProject);
    Rule compareObjectsWithEquals = properties.getProjectRuleSet().getRuleByName("CompareObjectsWithEquals");
    RuleSet projectRuleSet = RuleSetUtil.newSingle(compareObjectsWithEquals);
    properties.setProjectRuleSet(projectRuleSet);
    boolean oldSetting = PMDPlugin.getDefault().getPreferencesManager().loadPreferences().isProjectBuildPathEnabled();
    try {
        PMDPlugin.getDefault().getPreferencesManager().loadPreferences().setProjectBuildPathEnabled(true);
        EclipseUtils.createTestSourceFile(testProject, "/src/MyEnum.java", "public enum MyEnum { A, B }");
        IFile sourceFile = EclipseUtils.createTestSourceFile(testProject, "/src/Foo.java", // line 3
        "class Foo {\n" + "  boolean bar(MyEnum a, MyEnum b) {\n" + "    return a == b;\n" + "  }\n" + "}");
        testProject.build(IncrementalProjectBuilder.FULL_BUILD, null);
        testProject.refreshLocal(IResource.DEPTH_INFINITE, null);
        ReviewCodeCmd cmd = new ReviewCodeCmd();
        cmd.addResource(testProject);
        cmd.performExecute();
        cmd.join();
        Map<IFile, Set<MarkerInfo2>> markers = cmd.getMarkers();
        // with type resolution, this comparison is ok, as MyEnum is a enum
        Assert.assertTrue("Type Resolution didn't work", markers.get(sourceFile).isEmpty());
        // without type resolution, there is a violation
        PMDPlugin.getDefault().getPreferencesManager().loadPreferences().setProjectBuildPathEnabled(false);
        cmd = new ReviewCodeCmd();
        cmd.addResource(testProject);
        cmd.performExecute();
        cmd.join();
        markers = cmd.getMarkers();
        // there is a violation expected without type resolution
        Assert.assertFalse(markers.get(sourceFile).isEmpty());
    } finally {
        PMDPlugin.getDefault().getPreferencesManager().loadPreferences().setProjectBuildPathEnabled(oldSetting);
    }
}
Also used : RuleSet(net.sourceforge.pmd.RuleSet) IProjectProperties(net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties) IFile(org.eclipse.core.resources.IFile) Set(java.util.Set) RuleSet(net.sourceforge.pmd.RuleSet) Rule(net.sourceforge.pmd.Rule) Test(org.junit.Test)

Example 4 with IProjectProperties

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

the class ReviewCodeCmdNonJavaTest method checkCodeForNonJavaProject.

@Test
public void checkCodeForNonJavaProject() throws Exception {
    IProject testProject = EclipseUtils.createProject("TestNonJavaProject");
    Assert.assertTrue("A test project cannot be created; the tests cannot be performed.", testProject != null && testProject.exists() && testProject.isAccessible());
    Assert.assertFalse(testProject.hasNature(JavaCore.NATURE_ID));
    Assert.assertFalse(testProject.hasNature(PMDNature.PMD_NATURE));
    PMDNature.addPMDNature(testProject, null);
    Assert.assertTrue(testProject.hasNature(PMDNature.PMD_NATURE));
    // 2. Create a test source file inside that project
    IFolder testFolder = testProject.getFolder("/src");
    if (testFolder.exists()) {
        testFolder.delete(true, null);
    }
    testFolder.create(true, true, null);
    IFile testFile = testFolder.getFile("somefile.js");
    InputStream is = new ByteArrayInputStream("function() { var s = 'test file content'; }".getBytes("UTF-8"));
    if (testFile.exists() && testFile.isAccessible()) {
        testFile.setContents(is, true, false, null);
    } else {
        testFile.create(is, true, null);
    }
    testProject.refreshLocal(IResource.DEPTH_INFINITE, null);
    is = EclipseUtils.getResourceStream(testProject, "/src/somefile.js");
    Assert.assertNotNull("Cannot find the test source file", is);
    is.close();
    // 3. Enable PMD for the test project
    IProjectProperties properties = PMDPlugin.getDefault().getPropertiesManager().loadProjectProperties(testProject);
    properties.setPmdEnabled(true);
    ReviewCodeCmd cmd = new ReviewCodeCmd();
    cmd.addResource(testProject);
    cmd.performExecute();
    cmd.join();
    // 2 files are there: .project and src/somefile.ext
    Assert.assertEquals(2, cmd.getStepCount());
    // only one file has an extension, that could be mapped to a language, and therefore pmd was executed
    Assert.assertEquals(1, cmd.getFileCount());
    if (testProject.exists() && testProject.isAccessible()) {
        EclipseUtils.removePMDNature(testProject);
        testProject.refreshLocal(IResource.DEPTH_INFINITE, null);
        testProject.delete(true, true, null);
        testProject = null;
    }
}
Also used : IProjectProperties(net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties) IFile(org.eclipse.core.resources.IFile) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IProject(org.eclipse.core.resources.IProject) IFolder(org.eclipse.core.resources.IFolder) Test(org.junit.Test)

Example 5 with IProjectProperties

use of net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties 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

IProjectProperties (net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties)14 PropertiesException (net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException)6 IProject (org.eclipse.core.resources.IProject)6 RuleSet (net.sourceforge.pmd.RuleSet)5 IFile (org.eclipse.core.resources.IFile)4 CommandException (name.herlin.command.CommandException)3 CoreException (org.eclipse.core.runtime.CoreException)3 Test (org.junit.Test)3 InputStream (java.io.InputStream)2 Rule (net.sourceforge.pmd.Rule)2 IResource (org.eclipse.core.resources.IResource)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 Set (java.util.Set)1 RuleSetFactory (net.sourceforge.pmd.RuleSetFactory)1 IProjectPropertiesManager (net.sourceforge.pmd.eclipse.runtime.properties.IProjectPropertiesManager)1 IFolder (org.eclipse.core.resources.IFolder)1 IResourceDeltaVisitor (org.eclipse.core.resources.IResourceDeltaVisitor)1 IResourceVisitor (org.eclipse.core.resources.IResourceVisitor)1 Before (org.junit.Before)1