1616import java .nio .file .Files ;
1717import java .util .Collection ;
1818import java .util .HashSet ;
19- import java .util .Iterator ;
2019import java .util .LinkedList ;
2120import java .util .List ;
22- import java .util .Map ;
23- import java .util .Map .Entry ;
2421import java .util .Objects ;
2522import java .util .Set ;
26- import java .util .TreeMap ;
27- import java .util .regex .Pattern ;
2823
2924import org .apache .commons .io .IOUtils ;
3025import org .eclipse .core .resources .IFile ;
4843import net .sourceforge .pmd .RuleSetLoadException ;
4944import net .sourceforge .pmd .RuleSetLoader ;
5045import net .sourceforge .pmd .eclipse .EclipseUtils ;
46+ import net .sourceforge .pmd .eclipse .internal .RuleSetAssertUtil ;
5147import net .sourceforge .pmd .eclipse .plugin .PMDPlugin ;
5248import net .sourceforge .pmd .eclipse .runtime .builder .PMDNature ;
5349import net .sourceforge .pmd .eclipse .runtime .preferences .IPreferencesManager ;
5450import net .sourceforge .pmd .eclipse .ui .actions .RuleSetUtil ;
5551import net .sourceforge .pmd .lang .LanguageRegistry ;
5652import net .sourceforge .pmd .lang .java .rule .AbstractJavaRule ;
57- import net .sourceforge .pmd .properties .PropertyDescriptor ;
5853
5954/**
6055 * Test the project properties model.
@@ -89,40 +84,11 @@ public void setUp() throws Exception {
8984 Assert .assertEquals (expectedCount , this .initialPluginRuleSet .getRules ().size ());
9085 }
9186 Assert .assertEquals (ruleCount , this .initialPluginRuleSet .getRules ().size ());
87+
9288 RuleSet cloned = RuleSetUtil .newCopyOf (this .initialPluginRuleSet );
93- Assert .assertEquals (cloned .getRules ().size (), this .initialPluginRuleSet .getRules ().size ());
94- Iterator <Rule > clonedIterator = cloned .getRules ().iterator ();
95- Iterator <Rule > initialIterator = this .initialPluginRuleSet .getRules ().iterator ();
96- for (int i = 0 ; i < cloned .getRules ().size (); i ++) {
97- System .out .println ("Comparing rule " + i );
98- Rule clonedRule = clonedIterator .next ();
99- Rule initialRule = initialIterator .next ();
100- Assert .assertSame (clonedRule .getClass (), initialRule .getClass ());
101- Assert .assertEquals (clonedRule .getName (), initialRule .getName ());
102- Assert .assertEquals (clonedRule .getPriority (), initialRule .getPriority ());
103- Map <PropertyDescriptor <?>, Object > clonedProperties = new TreeMap <>((a , b ) -> a .name ().compareTo (b .name ()));
104- clonedProperties .putAll (clonedRule .getPropertiesByPropertyDescriptor ());
105- Map <PropertyDescriptor <?>, Object > initialProperties = new TreeMap <>((a , b ) -> a .name ().compareTo (b .name ()));
106- initialProperties .putAll (initialRule .getPropertiesByPropertyDescriptor ());
107- Assert .assertEquals (clonedProperties .size (), initialProperties .size ());
108- Iterator <Entry <PropertyDescriptor <?>, Object >> clonedPropertiesIterator = clonedProperties .entrySet ().iterator ();
109- Iterator <Entry <PropertyDescriptor <?>, Object >> initialPropertiesIterator = initialProperties .entrySet ().iterator ();
110- for (int j = 0 ; j < clonedProperties .size (); j ++) {
111- Entry <PropertyDescriptor <?>, Object > clonedProperty = clonedPropertiesIterator .next ();
112- Entry <PropertyDescriptor <?>, Object > initialProperty = initialPropertiesIterator .next ();
113- Assert .assertEquals (clonedProperty .getKey (), initialProperty .getKey ());
114- if (clonedProperty .getValue () instanceof Pattern ) {
115- Pattern clonedPattern = (Pattern ) clonedProperty .getValue ();
116- Pattern initialPattern = (Pattern ) initialProperty .getValue ();
117- Assert .assertEquals (clonedPattern .pattern (), initialPattern .pattern ());
118- } else {
119- Assert .assertEquals (clonedProperty .getValue (), initialProperty .getValue ());
120- }
121- }
122- }
89+ RuleSetAssertUtil .assertEqualsRules (cloned , initialPluginRuleSet );
12390
12491 PMDPlugin .getDefault ().getPreferencesManager ().setRuleSet (this .initialPluginRuleSet );
125-
12692 }
12793
12894 @ After
@@ -150,26 +116,6 @@ public void tearDown() throws Exception {
150116 }
151117 }
152118
153- public static void compareTwoRuleSets (RuleSet ruleSet1 , RuleSet ruleSet2 ) {
154- if (!ruleSet1 .getRules ().equals (ruleSet2 .getRules ())) {
155- System .out .println ("###################################################" );
156- System .out .println ("RuleSet1: " + ruleSet1 + " (count " + ruleSet1 .size () + ") RuleSet2: " + ruleSet2 + " (count " + ruleSet2 .size () + ")" );
157- Iterator <Rule > it1 = ruleSet1 .getRules ().iterator ();
158- Iterator <Rule > it2 = ruleSet2 .getRules ().iterator ();
159- for (int i = 0 ; i < ruleSet2 .getRules ().size (); i ++) {
160- Rule pluginRule = it1 .next ();
161- Rule projectRule = it2 .next ();
162-
163- if (!Objects .equals (pluginRule , projectRule )) {
164- System .out .println ("i=" + i + ": pluginRule=" + pluginRule + " projectRule=" + projectRule );
165- System .out .println ("plugin: " + pluginRule .getName () + " (" + pluginRule .getLanguage () + ")" );
166- System .out .println ("project: " + projectRule .getName () + " (" + projectRule .getLanguage () + ")" );
167- }
168- }
169- System .out .println ("###################################################" );
170- }
171- }
172-
173119 /**
174120 * Bug: when a user deselect a project rule it is not saved
175121 */
@@ -187,7 +133,7 @@ public void testBug() throws PropertiesException, RuleSetLoadException, CoreExce
187133
188134 RuleSet projectRuleSet = model .getProjectRuleSet ();
189135 Assert .assertEquals (this .initialPluginRuleSet .getRules ().size (), projectRuleSet .getRules ().size ());
190- compareTwoRuleSets (initialPluginRuleSet , projectRuleSet );
136+ RuleSetAssertUtil . compareTwoRuleSets (initialPluginRuleSet , projectRuleSet );
191137 Assert .assertEquals ("The project ruleset is not equal to the plugin ruleset" ,
192138 this .initialPluginRuleSet .getRules (), projectRuleSet .getRules ());
193139 int ruleCountBefore = projectRuleSet .getRules ().size ();
0 commit comments