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