Skip to content

Commit 319c1df

Browse files
committed
Fixes #78: Project properties cannot be loaded anymore
1 parent 2a913ff commit 319c1df

File tree

4 files changed

+88
-8
lines changed

4 files changed

+88
-8
lines changed

ReleaseNotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Eclipse Update Site:
1515

1616
* [#70](https://github.com/pmd/pmd-eclipse-plugin/issues/70): UnsupportedOperationException opening Rule Configuration
1717
* [#76](https://github.com/pmd/pmd-eclipse-plugin/issues/76): Global rule management is saved even if cancelled
18+
* [#78](https://github.com/pmd/pmd-eclipse-plugin/issues/78): Project properties cannot be loaded anymore
1819

1920
### External Contributions
2021

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/ProjectPropertiesModelTest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@
3434

3535
package net.sourceforge.pmd.eclipse.runtime.properties;
3636

37+
import java.io.ByteArrayInputStream;
3738
import java.io.ByteArrayOutputStream;
39+
import java.io.InputStream;
3840
import java.io.PrintStream;
41+
import java.nio.charset.StandardCharsets;
3942
import java.util.Collection;
4043
import java.util.Iterator;
4144

@@ -468,6 +471,63 @@ public void testRuleSetStoredInProjectTRUE() throws PropertiesException, RuleSet
468471
// projectRuleSet);
469472
}
470473

474+
/**
475+
* Bug: when the project properties don't contain any rules, we should
476+
* still be able to load the properties.
477+
*/
478+
@Test
479+
public void testNoRulesInProperties() throws PropertiesException, RuleSetNotFoundException, CoreException {
480+
final IProjectPropertiesManager mgr = PMDPlugin.getDefault().getPropertiesManager();
481+
IProjectProperties model = mgr.loadProjectProperties(this.testProject);
482+
// remove PMD's cached project properties, so that we reload it from disk again
483+
mgr.removeProjectProperties(this.testProject);
484+
485+
// overwrite the properties file
486+
final IFile file = this.testProject.getFile(".pmd");
487+
String propertiesContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
488+
+ "<pmd>\n"
489+
+ " <useProjectRuleSet>false</useProjectRuleSet>\n"
490+
+ " <ruleSetFile>.ruleset</ruleSetFile>\n"
491+
+ " <includeDerivedFiles>false</includeDerivedFiles>\n"
492+
+ " <violationsAsErrors>true</violationsAsErrors>\n"
493+
+ " <fullBuildEnabled>true</fullBuildEnabled>\n"
494+
+ "</pmd>\n";
495+
InputStream propertiesStream = new ByteArrayInputStream(propertiesContent.getBytes(StandardCharsets.UTF_8));
496+
file.setContents(propertiesStream, 0, null);
497+
498+
// reload the project properties
499+
model = mgr.loadProjectProperties(this.testProject);
500+
Assert.assertFalse(model.isRuleSetStoredInProject());
501+
RuleSets projectRuleSets = model.getProjectRuleSets();
502+
Assert.assertNotNull(projectRuleSets);
503+
Assert.assertEquals(1, projectRuleSets.getAllRuleSets().length);
504+
Assert.assertEquals(0, projectRuleSets.getAllRules().size());
505+
}
506+
507+
/**
508+
* Bug: when the project properties are invalid, we should handle the error correctly
509+
* as a PropertiesException.
510+
*/
511+
@Test(expected = PropertiesException.class)
512+
public void testInvalidProperties() throws PropertiesException, RuleSetNotFoundException, CoreException {
513+
final IProjectPropertiesManager mgr = PMDPlugin.getDefault().getPropertiesManager();
514+
IProjectProperties model = mgr.loadProjectProperties(this.testProject);
515+
// remove PMD's cached project properties, so that we reload it from disk again
516+
mgr.removeProjectProperties(this.testProject);
517+
518+
// overwrite the properties file
519+
final IFile file = this.testProject.getFile(".pmd");
520+
String propertiesContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
521+
+ "<pmd>\n"
522+
+ " <invalid xml"
523+
+ "</pmd>\n";
524+
InputStream propertiesStream = new ByteArrayInputStream(propertiesContent.getBytes(StandardCharsets.UTF_8));
525+
file.setContents(propertiesStream, 0, null);
526+
527+
// reload the project properties
528+
model = mgr.loadProjectProperties(this.testProject);
529+
}
530+
471531
private void dumpRuleSet(final RuleSet ruleSet) {
472532
System.out.println("Dumping rule set:" + ruleSet.getName());
473533
for (final Rule rule : ruleSet.getRules()) {

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/impl/ProjectPropertiesManagerImpl.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.io.IOException;
4242
import java.io.StringReader;
4343
import java.io.StringWriter;
44+
import java.nio.charset.StandardCharsets;
4445
import java.util.ArrayList;
4546
import java.util.HashMap;
4647
import java.util.HashSet;
@@ -210,7 +211,7 @@ private ProjectPropertiesTO readProjectProperties(final IProject project) throws
210211

211212
final IFile propertiesFile = project.getFile(PROPERTIES_FILE);
212213
if (propertiesFile.exists() && propertiesFile.isAccessible()) {
213-
String properties = IOUtils.toString(propertiesFile.getContents());
214+
String properties = IOUtils.toString(propertiesFile.getContents(), StandardCharsets.UTF_8);
214215
projectProperties = convertProjectPropertiesFromString(properties);
215216
}
216217

@@ -220,6 +221,8 @@ private ProjectPropertiesTO readProjectProperties(final IProject project) throws
220221
throw new PropertiesException(e);
221222
} catch (CoreException e) {
222223
throw new PropertiesException(e);
224+
} catch (DataBindingException e) {
225+
throw new PropertiesException(e);
223226
}
224227
}
225228

@@ -268,11 +271,13 @@ private void setRuleSetFromProperties(IProjectProperties projectProperties, Rule
268271

269272
// de-duplicate rules
270273
Set<String> ruleNamesToAdd = new HashSet<>();
271-
for (RuleSpecTO rule : rules) {
272-
if (!ruleNamesToAdd.add(rule.getName())) {
273-
PMDPlugin.getDefault()
274-
.logInformation("Duplicated Rule found: " + rule.getName() + ". This rule will be ignored.");
275-
LOG.debug("Duplicated Rule found: " + rule.getName() + ". This rule will be ignored.");
274+
if (rules != null) {
275+
for (RuleSpecTO rule : rules) {
276+
if (!ruleNamesToAdd.add(rule.getName())) {
277+
PMDPlugin.getDefault()
278+
.logInformation("Duplicated Rule found: " + rule.getName() + ". This rule will be ignored.");
279+
LOG.debug("Duplicated Rule found: " + rule.getName() + ". This rule will be ignored.");
280+
}
276281
}
277282
}
278283

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/properties/PMDPropertyPageController.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@
5151
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
5252
import net.sourceforge.pmd.eclipse.runtime.cmd.BuildProjectCommand;
5353
import net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties;
54+
import net.sourceforge.pmd.eclipse.runtime.properties.IProjectPropertiesManager;
5455
import net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException;
56+
import net.sourceforge.pmd.eclipse.runtime.properties.impl.PropertiesFactoryImpl;
5557
import net.sourceforge.pmd.eclipse.ui.nls.StringKeys;
5658

5759
import name.herlin.command.CommandException;
@@ -109,9 +111,22 @@ public PMDPropertyPageBean getPropertyPageBean() {
109111

110112
if (this.propertyPageBean == null) {
111113
LOG.debug("Building a property page bean");
114+
IProjectProperties properties;
115+
112116
try {
113-
final IProjectProperties properties = PMDPlugin.getDefault().loadProjectProperties(this.project);
117+
properties = PMDPlugin.getDefault().loadProjectProperties(this.project);
118+
} catch (PropertiesException e) {
119+
PMDPlugin.getDefault().showError("Error loading project properties. Recreating empty properties.", e);
120+
IProjectPropertiesManager propertiesManager = PMDPlugin.getDefault().getPropertiesManager();
121+
properties = new PropertiesFactoryImpl().newProjectProperties(project, propertiesManager);
122+
try {
123+
propertiesManager.storeProjectProperties(properties);
124+
} catch (PropertiesException e1) {
125+
PMDPlugin.getDefault().showError(e.getMessage(), e);
126+
}
127+
}
114128

129+
try {
115130
propertyPageBean = new PMDPropertyPageBean();
116131
propertyPageBean.setPmdEnabled(properties.isPmdEnabled());
117132
propertyPageBean.setProjectWorkingSet(properties.getProjectWorkingSet());
@@ -122,7 +137,6 @@ public PMDPropertyPageBean getPropertyPageBean() {
122137
propertyPageBean.setFullBuildEnabled(properties.isFullBuildEnabled());
123138
propertyPageBean.setViolationsAsErrors(properties.violationsAsErrors());
124139
pmdAlreadyActivated = properties.isPmdEnabled();
125-
126140
} catch (PropertiesException e) {
127141
PMDPlugin.getDefault().showError(e.getMessage(), e);
128142
}

0 commit comments

Comments
 (0)