11/*
22 * Created on 14 mai 2005
3- *
3+ *
44 * Copyright (c) 2005, PMD for Eclipse Development Team All rights reserved.
5- *
5+ *
66 * Redistribution and use in source and binary forms, with or without
77 * modification, are permitted provided that the following conditions are
88 * met:
9- *
9+ *
1010 * * Redistributions of source code must retain the above copyright notice,
1111 * this list of conditions and the following disclaimer. * Redistributions
1212 * in binary form must reproduce the above copyright notice, this list of
1818 * Neither the name of "PMD for Eclipse Development Team" nor the names of
1919 * its contributors may be used to endorse or promote products derived from
2020 * this software without specific prior written permission.
21- *
21+ *
2222 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
2323 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
2424 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4545import java .util .Map .Entry ;
4646import java .util .Set ;
4747import java .util .concurrent .CountDownLatch ;
48+ import java .util .regex .Pattern ;
4849
4950import org .eclipse .core .resources .IFile ;
5051import org .eclipse .core .resources .IFolder ;
6869
6970/**
7071 * This is a utility class for Eclipse various operations
71- *
72+ *
7273 * @author Philippe Herlin
7374 * @author Brian Remedios
7475 */
@@ -96,7 +97,7 @@ private EclipseUtils() {
9697
9798 /**
9899 * Test if 2 sets of rules are equals
99- *
100+ *
100101 * @param ruleSet1
101102 * @param ruleSet2
102103 * @return
@@ -126,7 +127,7 @@ public static boolean assertRuleSetEquals(final Collection<Rule> ruleSet1, final
126127
127128 /**
128129 * Create a new java project
129- *
130+ *
130131 * @param projectName
131132 * a project name
132133 * @return newProject a new project resource handle
@@ -161,7 +162,7 @@ public static IProject createProject(final String projectName) throws CoreExcept
161162
162163 /**
163164 * Create a test source file
164- *
165+ *
165166 * @param project
166167 * a project where to create that file; this project is expected to be empty
167168 */
@@ -186,7 +187,7 @@ public static IFile createTestSourceFile(final IProject project)
186187
187188 /**
188189 * Get the content of a project resource.
189- *
190+ *
190191 * @param project
191192 * a project reference
192193 * @param resourceName
@@ -202,7 +203,7 @@ public static InputStream getResourceStream(final IProject project, final String
202203
203204 /**
204205 * Remove the PMD Nature from a project
205- *
206+ *
206207 * @param project
207208 * a project to remove the PMD Nature
208209 * @param monitor
@@ -238,7 +239,7 @@ public static boolean removePMDNature(final IProject project) throws CoreExcepti
238239
239240 /**
240241 * Add a Java Nature to a project when creating
241- *
242+ *
242243 * @param project
243244 * @throws CoreException
244245 */
@@ -272,7 +273,7 @@ private static void addJavaNature(final IProject project) throws CoreException {
272273
273274 /**
274275 * Print rule details
275- *
276+ *
276277 * @param rule
277278 */
278279 private static void dumpRule (final Rule rule , final PrintStream out ) {
@@ -286,13 +287,50 @@ private static void dumpRule(final Rule rule, final PrintStream out) {
286287 }
287288
288289 private static boolean propertiesMatchFor (final Rule ruleA , final Rule ruleB ) {
290+ Map <PropertyDescriptor <?>, Object > ruleAProperties = ruleA .getPropertiesByPropertyDescriptor ();
289291
290- return ruleA .getPropertiesByPropertyDescriptor ().equals (ruleB .getPropertiesByPropertyDescriptor ());
292+ Map <PropertyDescriptor <?>, Object > ruleBProperties = ruleB .getPropertiesByPropertyDescriptor ();
293+
294+ // simple equals doesn't work for RegexProperties whose value type is java.util.regex.Pattern...
295+ //return ruleAProperties.equals(ruleBProperties);
296+
297+ if (ruleAProperties == ruleBProperties ) {
298+ return true ;
299+ }
300+
301+ if (ruleAProperties .size () != ruleBProperties .size ()) {
302+ return false ;
303+ }
304+
305+ for (Map .Entry <PropertyDescriptor <?>, Object > entry : ruleAProperties .entrySet ()) {
306+ if (entry .getValue () == null ) {
307+ Object valueB = ruleBProperties .get (entry .getKey ());
308+ if (!(valueB == null && ruleBProperties .containsKey (entry .getKey ()))) {
309+ return false ;
310+ }
311+ } else {
312+ Object valueB = ruleBProperties .get (entry .getKey ());
313+ if (entry .getValue () instanceof Pattern ) {
314+ if (!(valueB instanceof Pattern )) {
315+ return false ;
316+ }
317+ if (!entry .getValue ().toString ().equals (valueB .toString ())) {
318+ return false ;
319+ }
320+ } else {
321+ if (!entry .getValue ().equals (valueB )) {
322+ return false ;
323+ }
324+ }
325+ }
326+ }
327+
328+ return true ;
291329 }
292330
293331 /**
294332 * Search a rule in a set of rules
295- *
333+ *
296334 * @param rule
297335 * @param set
298336 * @return
0 commit comments