Skip to content

Commit 01c61ab

Browse files
committed
Merge branch 'pr-79'
2 parents a181c25 + 319c1df commit 01c61ab

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
@@ -17,6 +17,7 @@ Eclipse Update Site:
1717

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

2122
### External Contributions
2223

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
@@ -4,8 +4,11 @@
44

55
package net.sourceforge.pmd.eclipse.runtime.properties;
66

7+
import java.io.ByteArrayInputStream;
78
import java.io.ByteArrayOutputStream;
9+
import java.io.InputStream;
810
import java.io.PrintStream;
11+
import java.nio.charset.StandardCharsets;
912
import java.util.Collection;
1013
import java.util.Iterator;
1114

@@ -438,6 +441,63 @@ public void testRuleSetStoredInProjectTRUE() throws PropertiesException, RuleSet
438441
// projectRuleSet);
439442
}
440443

444+
/**
445+
* Bug: when the project properties don't contain any rules, we should
446+
* still be able to load the properties.
447+
*/
448+
@Test
449+
public void testNoRulesInProperties() throws PropertiesException, RuleSetNotFoundException, CoreException {
450+
final IProjectPropertiesManager mgr = PMDPlugin.getDefault().getPropertiesManager();
451+
IProjectProperties model = mgr.loadProjectProperties(this.testProject);
452+
// remove PMD's cached project properties, so that we reload it from disk again
453+
mgr.removeProjectProperties(this.testProject);
454+
455+
// overwrite the properties file
456+
final IFile file = this.testProject.getFile(".pmd");
457+
String propertiesContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
458+
+ "<pmd>\n"
459+
+ " <useProjectRuleSet>false</useProjectRuleSet>\n"
460+
+ " <ruleSetFile>.ruleset</ruleSetFile>\n"
461+
+ " <includeDerivedFiles>false</includeDerivedFiles>\n"
462+
+ " <violationsAsErrors>true</violationsAsErrors>\n"
463+
+ " <fullBuildEnabled>true</fullBuildEnabled>\n"
464+
+ "</pmd>\n";
465+
InputStream propertiesStream = new ByteArrayInputStream(propertiesContent.getBytes(StandardCharsets.UTF_8));
466+
file.setContents(propertiesStream, 0, null);
467+
468+
// reload the project properties
469+
model = mgr.loadProjectProperties(this.testProject);
470+
Assert.assertFalse(model.isRuleSetStoredInProject());
471+
RuleSets projectRuleSets = model.getProjectRuleSets();
472+
Assert.assertNotNull(projectRuleSets);
473+
Assert.assertEquals(1, projectRuleSets.getAllRuleSets().length);
474+
Assert.assertEquals(0, projectRuleSets.getAllRules().size());
475+
}
476+
477+
/**
478+
* Bug: when the project properties are invalid, we should handle the error correctly
479+
* as a PropertiesException.
480+
*/
481+
@Test(expected = PropertiesException.class)
482+
public void testInvalidProperties() throws PropertiesException, RuleSetNotFoundException, CoreException {
483+
final IProjectPropertiesManager mgr = PMDPlugin.getDefault().getPropertiesManager();
484+
IProjectProperties model = mgr.loadProjectProperties(this.testProject);
485+
// remove PMD's cached project properties, so that we reload it from disk again
486+
mgr.removeProjectProperties(this.testProject);
487+
488+
// overwrite the properties file
489+
final IFile file = this.testProject.getFile(".pmd");
490+
String propertiesContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
491+
+ "<pmd>\n"
492+
+ " <invalid xml"
493+
+ "</pmd>\n";
494+
InputStream propertiesStream = new ByteArrayInputStream(propertiesContent.getBytes(StandardCharsets.UTF_8));
495+
file.setContents(propertiesStream, 0, null);
496+
497+
// reload the project properties
498+
model = mgr.loadProjectProperties(this.testProject);
499+
}
500+
441501
private void dumpRuleSet(final RuleSet ruleSet) {
442502
System.out.println("Dumping rule set:" + ruleSet.getName());
443503
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
@@ -9,6 +9,7 @@
99
import java.io.IOException;
1010
import java.io.StringReader;
1111
import java.io.StringWriter;
12+
import java.nio.charset.StandardCharsets;
1213
import java.util.ArrayList;
1314
import java.util.HashMap;
1415
import java.util.HashSet;
@@ -178,7 +179,7 @@ private ProjectPropertiesTO readProjectProperties(final IProject project) throws
178179

179180
final IFile propertiesFile = project.getFile(PROPERTIES_FILE);
180181
if (propertiesFile.exists() && propertiesFile.isAccessible()) {
181-
String properties = IOUtils.toString(propertiesFile.getContents());
182+
String properties = IOUtils.toString(propertiesFile.getContents(), StandardCharsets.UTF_8);
182183
projectProperties = convertProjectPropertiesFromString(properties);
183184
}
184185

@@ -188,6 +189,8 @@ private ProjectPropertiesTO readProjectProperties(final IProject project) throws
188189
throw new PropertiesException(e);
189190
} catch (CoreException e) {
190191
throw new PropertiesException(e);
192+
} catch (DataBindingException e) {
193+
throw new PropertiesException(e);
191194
}
192195
}
193196

@@ -236,11 +239,13 @@ private void setRuleSetFromProperties(IProjectProperties projectProperties, Rule
236239

237240
// de-duplicate rules
238241
Set<String> ruleNamesToAdd = new HashSet<>();
239-
for (RuleSpecTO rule : rules) {
240-
if (!ruleNamesToAdd.add(rule.getName())) {
241-
PMDPlugin.getDefault()
242-
.logInformation("Duplicated Rule found: " + rule.getName() + ". This rule will be ignored.");
243-
LOG.debug("Duplicated Rule found: " + rule.getName() + ". This rule will be ignored.");
242+
if (rules != null) {
243+
for (RuleSpecTO rule : rules) {
244+
if (!ruleNamesToAdd.add(rule.getName())) {
245+
PMDPlugin.getDefault()
246+
.logInformation("Duplicated Rule found: " + rule.getName() + ". This rule will be ignored.");
247+
LOG.debug("Duplicated Rule found: " + rule.getName() + ". This rule will be ignored.");
248+
}
244249
}
245250
}
246251

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
@@ -19,7 +19,9 @@
1919
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
2020
import net.sourceforge.pmd.eclipse.runtime.cmd.BuildProjectCommand;
2121
import net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties;
22+
import net.sourceforge.pmd.eclipse.runtime.properties.IProjectPropertiesManager;
2223
import net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException;
24+
import net.sourceforge.pmd.eclipse.runtime.properties.impl.PropertiesFactoryImpl;
2325
import net.sourceforge.pmd.eclipse.ui.nls.StringKeys;
2426

2527
/**
@@ -75,9 +77,22 @@ public PMDPropertyPageBean getPropertyPageBean() {
7577

7678
if (this.propertyPageBean == null) {
7779
LOG.debug("Building a property page bean");
80+
IProjectProperties properties;
81+
7882
try {
79-
final IProjectProperties properties = PMDPlugin.getDefault().loadProjectProperties(this.project);
83+
properties = PMDPlugin.getDefault().loadProjectProperties(this.project);
84+
} catch (PropertiesException e) {
85+
PMDPlugin.getDefault().showError("Error loading project properties. Recreating empty properties.", e);
86+
IProjectPropertiesManager propertiesManager = PMDPlugin.getDefault().getPropertiesManager();
87+
properties = new PropertiesFactoryImpl().newProjectProperties(project, propertiesManager);
88+
try {
89+
propertiesManager.storeProjectProperties(properties);
90+
} catch (PropertiesException e1) {
91+
PMDPlugin.getDefault().showError(e.getMessage(), e);
92+
}
93+
}
8094

95+
try {
8196
propertyPageBean = new PMDPropertyPageBean();
8297
propertyPageBean.setPmdEnabled(properties.isPmdEnabled());
8398
propertyPageBean.setProjectWorkingSet(properties.getProjectWorkingSet());
@@ -88,7 +103,6 @@ public PMDPropertyPageBean getPropertyPageBean() {
88103
propertyPageBean.setFullBuildEnabled(properties.isFullBuildEnabled());
89104
propertyPageBean.setViolationsAsErrors(properties.violationsAsErrors());
90105
pmdAlreadyActivated = properties.isPmdEnabled();
91-
92106
} catch (PropertiesException e) {
93107
PMDPlugin.getDefault().showError(e.getMessage(), e);
94108
}

0 commit comments

Comments
 (0)