Skip to content

Commit e7ee9ee

Browse files
committed
Refactor ruleset comparisons
1 parent 699e477 commit e7ee9ee

File tree

2 files changed

+91
-58
lines changed

2 files changed

+91
-58
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3+
*/
4+
5+
6+
package net.sourceforge.pmd.eclipse.internal;
7+
8+
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertSame;
10+
11+
import java.util.Comparator;
12+
import java.util.Iterator;
13+
import java.util.Map;
14+
import java.util.Map.Entry;
15+
import java.util.Objects;
16+
import java.util.TreeMap;
17+
import java.util.regex.Pattern;
18+
19+
import net.sourceforge.pmd.Rule;
20+
import net.sourceforge.pmd.RuleSet;
21+
import net.sourceforge.pmd.properties.PropertyDescriptor;
22+
23+
public final class RuleSetAssertUtil {
24+
private static final Comparator<PropertyDescriptor<?>> BY_NAME = (a, b) -> a.name().compareTo(b.name());
25+
26+
private RuleSetAssertUtil() {
27+
// utility
28+
}
29+
30+
public static void assertEqualsRules(RuleSet expected, RuleSet actual) {
31+
assertEquals(expected.getRules().size(), actual.getRules().size());
32+
33+
Iterator<Rule> expectedIterator = expected.getRules().iterator();
34+
Iterator<Rule> actualIterator = actual.getRules().iterator();
35+
36+
for (int i = 0; i < actual.getRules().size(); i++) {
37+
Rule expectedRule = expectedIterator.next();
38+
Rule actualRule = actualIterator.next();
39+
assertSame("Wrong rule class", expectedRule.getClass(), actualRule.getClass());
40+
assertEquals("Wrong rule name", expectedRule.getName(), actualRule.getName());
41+
assertEquals("Wrong priority", expectedRule.getPriority(), actualRule.getPriority());
42+
43+
// sort properties by name
44+
Map<PropertyDescriptor<?>, Object> expectedProperties = new TreeMap<>(BY_NAME);
45+
expectedProperties.putAll(expectedRule.getPropertiesByPropertyDescriptor());
46+
Map<PropertyDescriptor<?>, Object> actualProperties = new TreeMap<>(BY_NAME);
47+
actualProperties.putAll(actualRule.getPropertiesByPropertyDescriptor());
48+
assertEquals(expectedProperties.size(), actualProperties.size());
49+
50+
Iterator<Entry<PropertyDescriptor<?>, Object>> expectedPropertiesIterator = expectedProperties.entrySet().iterator();
51+
Iterator<Entry<PropertyDescriptor<?>, Object>> actualPropertiesIterator = actualProperties.entrySet().iterator();
52+
for (int j = 0; j < expectedProperties.size(); j++) {
53+
Entry<PropertyDescriptor<?>, Object> expectedProperty = expectedPropertiesIterator.next();
54+
Entry<PropertyDescriptor<?>, Object> actualProperty = actualPropertiesIterator.next();
55+
assertEquals(expectedProperty.getKey(), actualProperty.getKey());
56+
// java.util.regex.Pattern can't be compared using equals...
57+
if (expectedProperty.getValue() instanceof Pattern) {
58+
Pattern expectedPattern = (Pattern) expectedProperty.getValue();
59+
Pattern actualPattern = (Pattern) actualProperty.getValue();
60+
assertEquals(expectedPattern.pattern(), actualPattern.pattern());
61+
} else {
62+
assertEquals(expectedProperty.getValue(), actualProperty.getValue());
63+
}
64+
}
65+
}
66+
}
67+
68+
public static void compareTwoRuleSets(RuleSet ruleSet1, RuleSet ruleSet2) {
69+
if (!ruleSet1.getRules().equals(ruleSet2.getRules())) {
70+
System.out.println("###################################################");
71+
System.out.println("RuleSet1: " + ruleSet1 + " (count " + ruleSet1.size() + ") RuleSet2: " + ruleSet2 + " (count " + ruleSet2.size() + ")");
72+
Iterator<Rule> it1 = ruleSet1.getRules().iterator();
73+
Iterator<Rule> it2 = ruleSet2.getRules().iterator();
74+
for (int i = 0; i < ruleSet2.getRules().size(); i++) {
75+
Rule pluginRule = it1.next();
76+
Rule projectRule = it2.next();
77+
78+
if (!Objects.equals(pluginRule, projectRule)) {
79+
System.out.println("i=" + i + ": pluginRule=" + pluginRule + " projectRule=" + projectRule);
80+
System.out.println("plugin: " + pluginRule.getName() + " (" + pluginRule.getLanguage() + ")");
81+
System.out.println("project: " + projectRule.getName() + " (" + projectRule.getLanguage() + ")");
82+
}
83+
}
84+
System.out.println("###################################################");
85+
}
86+
}
87+
}

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

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,10 @@
1616
import java.nio.file.Files;
1717
import java.util.Collection;
1818
import java.util.HashSet;
19-
import java.util.Iterator;
2019
import java.util.LinkedList;
2120
import java.util.List;
22-
import java.util.Map;
23-
import java.util.Map.Entry;
2421
import java.util.Objects;
2522
import java.util.Set;
26-
import java.util.TreeMap;
27-
import java.util.regex.Pattern;
2823

2924
import org.apache.commons.io.IOUtils;
3025
import org.eclipse.core.resources.IFile;
@@ -48,13 +43,13 @@
4843
import net.sourceforge.pmd.RuleSetLoadException;
4944
import net.sourceforge.pmd.RuleSetLoader;
5045
import net.sourceforge.pmd.eclipse.EclipseUtils;
46+
import net.sourceforge.pmd.eclipse.internal.RuleSetAssertUtil;
5147
import net.sourceforge.pmd.eclipse.plugin.PMDPlugin;
5248
import net.sourceforge.pmd.eclipse.runtime.builder.PMDNature;
5349
import net.sourceforge.pmd.eclipse.runtime.preferences.IPreferencesManager;
5450
import net.sourceforge.pmd.eclipse.ui.actions.RuleSetUtil;
5551
import net.sourceforge.pmd.lang.LanguageRegistry;
5652
import 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

Comments
 (0)