3535
3636import java .io .ByteArrayOutputStream ;
3737import java .io .PrintStream ;
38- import java .util .Set ;
38+ import java .util .Collection ;
39+ import java .util .Iterator ;
3940
4041import org .eclipse .core .resources .IFile ;
4142import org .eclipse .core .resources .IProject ;
5455import net .sourceforge .pmd .eclipse .plugin .PMDPlugin ;
5556import net .sourceforge .pmd .eclipse .runtime .builder .PMDNature ;
5657import net .sourceforge .pmd .eclipse .runtime .preferences .IPreferencesManager ;
58+ import net .sourceforge .pmd .eclipse .ui .actions .RuleSetUtil ;
5759import 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