Skip to content

Commit 3b1e4f4

Browse files
committed
RuleSetFactory changes
* The access to rulesets is now through RuleSetUtil only * The rules and rulesets are in ordered collections now
1 parent 5020ef1 commit 3b1e4f4

28 files changed

+576
-476
lines changed

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/core/PMDCorePluginTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@
3333
*/
3434
package net.sourceforge.pmd.eclipse.core;
3535

36+
import java.util.Collection;
3637
import java.util.Iterator;
37-
import java.util.Set;
38+
39+
import org.junit.Assert;
40+
import org.junit.Test;
3841

3942
import net.sourceforge.pmd.RuleSet;
4043
import net.sourceforge.pmd.RuleSetFactory;
4144
import net.sourceforge.pmd.RuleSetNotFoundException;
4245
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
4346

44-
import org.junit.Assert;
45-
import org.junit.Test;
46-
4747
/**
4848
* Test the PMD Core plugin
4949
*
@@ -59,7 +59,7 @@ public class PMDCorePluginTest {
5959
*/
6060
@Test
6161
public void testDefaultPMDRuleSetsRegistered() throws RuleSetNotFoundException {
62-
final Set<RuleSet> defaultRuleSets = PMDPlugin.getDefault().getRuleSetManager().getRegisteredRuleSets();
62+
final Collection<RuleSet> defaultRuleSets = PMDPlugin.getDefault().getRuleSetManager().getRegisteredRuleSets();
6363
Assert.assertFalse("No registered default rulesets!", defaultRuleSets.isEmpty());
6464

6565
final RuleSetFactory factory = new RuleSetFactory();
@@ -95,7 +95,7 @@ public void testRuleSetManagerNotNull() {
9595
*/
9696
@Test
9797
public void testStandardPMDRuleSetsRegistered() throws RuleSetNotFoundException {
98-
final Set<RuleSet> registeredRuleSets = PMDPlugin.getDefault().getRuleSetManager().getRegisteredRuleSets();
98+
final Collection<RuleSet> registeredRuleSets = PMDPlugin.getDefault().getRuleSetManager().getRegisteredRuleSets();
9999
Assert.assertFalse("No registered rulesets!", registeredRuleSets.isEmpty());
100100

101101
final RuleSetFactory factory = new RuleSetFactory();
@@ -113,7 +113,7 @@ public void testStandardPMDRuleSetsRegistered() throws RuleSetNotFoundException
113113
* @param set
114114
* @return true if OK
115115
*/
116-
private boolean ruleSetRegistered(final RuleSet ruleSet, final Set<RuleSet> set) {
116+
private boolean ruleSetRegistered(final RuleSet ruleSet, final Collection<RuleSet> set) {
117117
boolean registered = false;
118118

119119
final Iterator<RuleSet> i = set.iterator();

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/core/RuleSetManagerTest.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@
3333
*/
3434
package net.sourceforge.pmd.eclipse.core;
3535

36-
import net.sourceforge.pmd.RuleSet;
37-
import net.sourceforge.pmd.eclipse.core.impl.RuleSetManagerImpl;
38-
3936
import org.junit.Assert;
4037
import org.junit.Before;
4138
import org.junit.Test;
4239

40+
import net.sourceforge.pmd.RuleSet;
41+
import net.sourceforge.pmd.eclipse.core.impl.RuleSetManagerImpl;
42+
import net.sourceforge.pmd.eclipse.ui.actions.RuleSetUtil;
43+
4344
/**
4445
* Test the ruleset manager.
4546
*
@@ -63,7 +64,7 @@ public void setUp() throws Exception {
6364
*/
6465
@Test
6566
public void testDuplicateRegister() {
66-
final RuleSet ruleSet = new RuleSet();
67+
final RuleSet ruleSet = RuleSetUtil.newEmpty();
6768
this.ruleSetManager.registerRuleSet(ruleSet);
6869
this.ruleSetManager.registerRuleSet(ruleSet);
6970
Assert.assertEquals("Only one rule set should have been registered", 1, this.ruleSetManager.getRegisteredRuleSets().size());
@@ -75,7 +76,7 @@ public void testDuplicateRegister() {
7576
*/
7677
@Test
7778
public void testDuplicateRegisterDefault() {
78-
final RuleSet ruleSet = new RuleSet();
79+
final RuleSet ruleSet = RuleSetUtil.newEmpty();
7980
this.ruleSetManager.registerDefaultRuleSet(ruleSet);
8081
this.ruleSetManager.registerDefaultRuleSet(ruleSet);
8182
Assert.assertEquals("Only one rule set should have been registered", 1, this.ruleSetManager.getDefaultRuleSets().size());
@@ -87,7 +88,7 @@ public void testDuplicateRegisterDefault() {
8788
*/
8889
@Test
8990
public void testDuplicateUnregister() {
90-
final RuleSet ruleSet = new RuleSet();
91+
final RuleSet ruleSet = RuleSetUtil.newEmpty();
9192
this.ruleSetManager.registerRuleSet(ruleSet);
9293

9394
this.ruleSetManager.unregisterRuleSet(ruleSet);
@@ -101,7 +102,7 @@ public void testDuplicateUnregister() {
101102
*/
102103
@Test
103104
public void testDuplicateUnregisterDefault() {
104-
final RuleSet ruleSet = new RuleSet();
105+
final RuleSet ruleSet = RuleSetUtil.newEmpty();
105106
this.ruleSetManager.registerRuleSet(ruleSet);
106107

107108
this.ruleSetManager.unregisterDefaultRuleSet(ruleSet);
@@ -115,7 +116,7 @@ public void testDuplicateUnregisterDefault() {
115116
*/
116117
@Test
117118
public void testRegisterDefaultRuleSet() {
118-
final RuleSet ruleSet = new RuleSet();
119+
final RuleSet ruleSet = RuleSetUtil.newEmpty();
119120
this.ruleSetManager.registerDefaultRuleSet(ruleSet);
120121
Assert.assertEquals("Default RuleSet not registrered!", 1, this.ruleSetManager.getDefaultRuleSets().size());
121122
}
@@ -156,7 +157,7 @@ public void testRegisterNullRuleSet() {
156157
*/
157158
@Test
158159
public void testRegisterRuleSet() {
159-
final RuleSet ruleSet = new RuleSet();
160+
final RuleSet ruleSet = RuleSetUtil.newEmpty();
160161
this.ruleSetManager.registerRuleSet(ruleSet);
161162
Assert.assertEquals("RuleSet not registrered!", 1, this.ruleSetManager.getRegisteredRuleSets().size());
162163
}
@@ -167,7 +168,7 @@ public void testRegisterRuleSet() {
167168
*/
168169
@Test
169170
public void testUnregisterDefaultRuleSet() {
170-
final RuleSet ruleSet = new RuleSet();
171+
final RuleSet ruleSet = RuleSetUtil.newEmpty();
171172
this.ruleSetManager.registerDefaultRuleSet(ruleSet);
172173
Assert.assertEquals("Default RuleSet not registered!", 1, this.ruleSetManager.getDefaultRuleSets().size());
173174

@@ -211,7 +212,7 @@ public void testUnregisterNullRuleSet() {
211212
*/
212213
@Test
213214
public void testUnregisterRuleSet() {
214-
final RuleSet ruleSet = new RuleSet();
215+
final RuleSet ruleSet = RuleSetUtil.newEmpty();
215216
this.ruleSetManager.registerRuleSet(ruleSet);
216217
Assert.assertEquals("RuleSet not registered!", 1, this.ruleSetManager.getRegisteredRuleSets().size());
217218

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/core/ext/RuleSetsExtensionProcessorTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@
3333
*/
3434
package net.sourceforge.pmd.eclipse.core.ext;
3535

36+
import java.util.Collection;
3637
import java.util.Iterator;
37-
import java.util.Set;
38+
39+
import org.junit.Assert;
40+
import org.junit.Test;
3841

3942
import net.sourceforge.pmd.RuleSet;
4043
import net.sourceforge.pmd.RuleSetFactory;
4144
import net.sourceforge.pmd.RuleSetNotFoundException;
4245
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
4346

44-
import org.junit.Assert;
45-
import org.junit.Test;
46-
4747
/**
4848
* Test the ruleset extension
4949
*
@@ -59,7 +59,7 @@ public class RuleSetsExtensionProcessorTest {
5959
*/
6060
@Test
6161
public void testAdditionalDefaultRuleSetsRegistered() throws RuleSetNotFoundException {
62-
final Set<RuleSet> registeredRuleSets = PMDPlugin.getDefault().getRuleSetManager().getDefaultRuleSets();
62+
final Collection<RuleSet> registeredRuleSets = PMDPlugin.getDefault().getRuleSetManager().getDefaultRuleSets();
6363
Assert.assertFalse("No registered default rulesets!", registeredRuleSets.isEmpty());
6464

6565
final RuleSetFactory factory = new RuleSetFactory();
@@ -77,7 +77,7 @@ public void testAdditionalDefaultRuleSetsRegistered() throws RuleSetNotFoundExce
7777
*/
7878
@Test
7979
public void testAdditionalRuleSetsRegistered() throws RuleSetNotFoundException {
80-
final Set<RuleSet> registeredRuleSets = PMDPlugin.getDefault().getRuleSetManager().getRegisteredRuleSets();
80+
final Collection<RuleSet> registeredRuleSets = PMDPlugin.getDefault().getRuleSetManager().getRegisteredRuleSets();
8181
Assert.assertFalse("No registered rulesets!", registeredRuleSets.isEmpty());
8282

8383
final RuleSetFactory factory = new RuleSetFactory();
@@ -95,7 +95,7 @@ public void testAdditionalRuleSetsRegistered() throws RuleSetNotFoundException {
9595
* @param set
9696
* @return true if OK
9797
*/
98-
private boolean ruleSetRegistered(final RuleSet ruleSet, final Set<RuleSet> set) {
98+
private boolean ruleSetRegistered(final RuleSet ruleSet, final Collection<RuleSet> set) {
9999
boolean registered = false;
100100

101101
final Iterator<RuleSet> i = set.iterator();

net.sourceforge.pmd.eclipse.plugin.test/src/main/java/net/sourceforge/pmd/eclipse/runtime/cmd/ReviewCmdTest.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,26 @@
3737
import java.util.Map;
3838
import java.util.Set;
3939

40-
import name.herlin.command.CommandException;
41-
import name.herlin.command.UnsetInputPropertiesException;
42-
import net.sourceforge.pmd.Rule;
43-
import net.sourceforge.pmd.RuleSet;
44-
import net.sourceforge.pmd.eclipse.EclipseUtils;
45-
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
46-
import net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties;
47-
4840
import org.eclipse.core.resources.IFile;
4941
import org.eclipse.core.resources.IProject;
5042
import org.eclipse.core.resources.IResource;
5143
import org.eclipse.core.resources.IncrementalProjectBuilder;
5244
import org.eclipse.core.runtime.CoreException;
53-
import org.eclipse.jdt.core.JavaCore;
54-
import org.eclipse.jdt.launching.IVMInstall;
55-
import org.eclipse.jdt.launching.JavaRuntime;
5645
import org.junit.After;
5746
import org.junit.Assert;
5847
import org.junit.Before;
5948
import org.junit.Test;
6049

50+
import net.sourceforge.pmd.Rule;
51+
import net.sourceforge.pmd.RuleSet;
52+
import net.sourceforge.pmd.eclipse.EclipseUtils;
53+
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
54+
import net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties;
55+
import net.sourceforge.pmd.eclipse.ui.actions.RuleSetUtil;
56+
57+
import name.herlin.command.CommandException;
58+
import name.herlin.command.UnsetInputPropertiesException;
59+
6160
/**
6261
* This tests the PMD Processor command
6362
*
@@ -133,8 +132,7 @@ public void testReviewCmdBasic() throws CommandException, CoreException {
133132
public void testProjectBuildPath() throws Exception {
134133
IProjectProperties properties = PMDPlugin.getDefault().getPropertiesManager().loadProjectProperties(testProject);
135134
Rule compareObjectsWithEquals = properties.getProjectRuleSet().getRuleByName("CompareObjectsWithEquals");
136-
RuleSet projectRuleSet = new RuleSet();
137-
projectRuleSet.addRule(compareObjectsWithEquals);
135+
RuleSet projectRuleSet = RuleSetUtil.newSingle(compareObjectsWithEquals);
138136
properties.setProjectRuleSet(projectRuleSet);
139137
boolean oldSetting = PMDPlugin.getDefault().getPreferencesManager().loadPreferences().isProjectBuildPathEnabled();
140138

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

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535

3636
import java.io.ByteArrayOutputStream;
3737
import java.io.PrintStream;
38-
import java.util.Set;
38+
import java.util.Collection;
39+
import java.util.Iterator;
3940

4041
import org.eclipse.core.resources.IFile;
4142
import org.eclipse.core.resources.IProject;
@@ -54,6 +55,7 @@
5455
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
5556
import net.sourceforge.pmd.eclipse.runtime.builder.PMDNature;
5657
import net.sourceforge.pmd.eclipse.runtime.preferences.IPreferencesManager;
58+
import net.sourceforge.pmd.eclipse.ui.actions.RuleSetUtil;
5759
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
5860

5961
/**
@@ -79,11 +81,23 @@ public void setUp() throws Exception {
7981

8082
// 2. Keep the plugin ruleset
8183
this.initialPluginRuleSet = PMDPlugin.getDefault().getPreferencesManager().getRuleSet();
82-
this.initialPluginRuleSet.getRules().clear();
83-
final Set<RuleSet> defaultRuleSets = PMDPlugin.getDefault().getRuleSetManager().getDefaultRuleSets();
84+
this.initialPluginRuleSet = RuleSetUtil.clearRules(this.initialPluginRuleSet);
85+
final Collection<RuleSet> defaultRuleSets = PMDPlugin.getDefault().getRuleSetManager().getDefaultRuleSets();
86+
Assert.assertEquals(0, this.initialPluginRuleSet.getRules().size());
87+
int ruleCount = 0;
8488
for (final RuleSet ruleSet : defaultRuleSets) {
85-
this.initialPluginRuleSet.addRuleSet(ruleSet);
89+
int countBefore = this.initialPluginRuleSet.getRules().size();
90+
int expectedCount = countBefore + ruleSet.getRules().size();
91+
ruleCount = ruleCount + ruleSet.getRules().size();
92+
this.initialPluginRuleSet = RuleSetUtil.addRules(this.initialPluginRuleSet, ruleSet.getRules());
93+
Assert.assertEquals(expectedCount, this.initialPluginRuleSet.getRules().size());
8694
}
95+
Assert.assertEquals(ruleCount, this.initialPluginRuleSet.getRules().size());
96+
RuleSet cloned = RuleSetUtil.newCopyOf(this.initialPluginRuleSet);
97+
Assert.assertEquals(cloned.getRules(), this.initialPluginRuleSet.getRules());
98+
99+
PMDPlugin.getDefault().getPreferencesManager().setRuleSet(this.initialPluginRuleSet);
100+
87101
}
88102

89103
/**
@@ -103,35 +117,56 @@ public void tearDown() throws Exception {
103117
PMDPlugin.getDefault().getPreferencesManager().setRuleSet(this.initialPluginRuleSet);
104118
}
105119

120+
public static void compareTwoRuleSets(RuleSet ruleSet1, RuleSet ruleSet2) {
121+
if (!ruleSet1.getRules().equals(ruleSet2.getRules())) {
122+
System.out.println("###################################################");
123+
System.out.println("RuleSet1: " + ruleSet1 + " RuleSet2: " + ruleSet2);
124+
Iterator<Rule> it1 = ruleSet1.getRules().iterator();
125+
Iterator<Rule> it2 = ruleSet2.getRules().iterator();
126+
for (int i = 0; i < ruleSet2.getRules().size(); i++) {
127+
Rule pluginRule = it1.next();
128+
Rule projectRule = it2.next();
129+
130+
if (pluginRule != projectRule) {
131+
System.out.println("i=" + i + ": pluginRule=" + pluginRule + " projectRule=" + projectRule);
132+
System.out.println("plugin: " + pluginRule.getName() + " (" + pluginRule.getLanguage() + ")");
133+
System.out.println("project: " + projectRule.getName() + " (" + projectRule.getLanguage() + ")");
134+
}
135+
}
136+
System.out.println("###################################################");
137+
}
138+
}
139+
106140
/**
107141
* Bug: when a user deselect a project rule it is not saved
108142
*/
109143
@Test
110144
public void testBug() throws PropertiesException, RuleSetNotFoundException, CoreException {
111-
final RuleSetFactory factory = new RuleSetFactory();
112-
113145
// First ensure that the plugin initial ruleset is equal to the project
114146
// ruleset
115147
final IProjectPropertiesManager mgr = PMDPlugin.getDefault().getPropertiesManager();
116148
final IProjectProperties model = mgr.loadProjectProperties(this.testProject);
117149

118150
RuleSet projectRuleSet = model.getProjectRuleSet();
151+
Assert.assertEquals(this.initialPluginRuleSet.getRules().size(), projectRuleSet.getRules().size());
152+
compareTwoRuleSets(initialPluginRuleSet, projectRuleSet);
119153
Assert.assertEquals("The project ruleset is not equal to the plugin ruleset", this.initialPluginRuleSet.getRules(),
120154
projectRuleSet.getRules());
121155
int ruleCountBefore = projectRuleSet.getRules().size();
122156

123157
// 2. remove a rule (keep its name for assertion)
124-
final RuleSet newRuleSet = new RuleSet();
125-
newRuleSet.addRuleSet(projectRuleSet);
158+
RuleSet newRuleSet = RuleSetUtil.newEmpty();
159+
newRuleSet = RuleSetUtil.addRules(newRuleSet, projectRuleSet.getRules());
126160
final Rule removedRule = newRuleSet.getRuleByName("UnnecessaryParentheses");
127-
newRuleSet.getRules().remove(removedRule);
161+
newRuleSet = RuleSetUtil.removeRule(newRuleSet, removedRule);
162+
Assert.assertEquals("No rule has been removed - test problem", newRuleSet.getRules().size(), ruleCountBefore - 1);
128163

129164
model.setProjectRuleSet(newRuleSet);
130165
model.sync();
131166

132167
// 3. test the rule has correctly been removed
133168
projectRuleSet = model.getProjectRuleSet();
134-
Assert.assertEquals("The rule count should be 1 less", ruleCountBefore - 1, projectRuleSet.getRules().size());
169+
Assert.assertEquals("The rule count should 1 less", ruleCountBefore - 1, projectRuleSet.getRules().size());
135170
for (Rule r : projectRuleSet.getRules()) {
136171
if (r.getName().equals(removedRule.getName()) && r.getLanguage() == removedRule.getLanguage()) {
137172
Assert.fail("The rule has not been removed!");
@@ -275,10 +310,9 @@ public String getName() {
275310
}
276311
};
277312

278-
final RuleSet newRuleSet = new RuleSet();
279-
newRuleSet.setName("foo");
280-
newRuleSet.addRuleSet(this.initialPluginRuleSet);
281-
newRuleSet.addRule(myRule);
313+
RuleSet newRuleSet = RuleSetUtil.newEmpty("foo", "bar");
314+
newRuleSet = RuleSetUtil.addRules(newRuleSet, this.initialPluginRuleSet.getRules());
315+
newRuleSet = RuleSetUtil.addRule(newRuleSet, myRule);
282316
PMDPlugin.getDefault().getPreferencesManager().setRuleSet(newRuleSet);
283317

284318
// Test that the project rule set should still be the same as the plugin
@@ -363,11 +397,8 @@ public void testRebuild3() throws PropertiesException {
363397
model.setPmdEnabled(true);
364398

365399
final RuleSet pmdRuleSet = PMDPlugin.getDefault().getPreferencesManager().getRuleSet();
366-
final RuleSet fooRuleSet = new RuleSet();
367-
368400
final Rule rule1 = pmdRuleSet.getRuleByName("EmptyCatchBlock");
369-
370-
fooRuleSet.addRule(rule1);
401+
final RuleSet fooRuleSet = RuleSetUtil.newSingle(rule1);
371402

372403
model.setProjectRuleSet(fooRuleSet);
373404
Assert.assertTrue(model.isNeedRebuild());

0 commit comments

Comments
 (0)