Menu

#871 false positive with UseAssertEqualsInsteadOfAssertTrue rule

open
pmd (543)
5
2012-10-07
2009-05-14
No

when an equals(...) method is used within an assertTrue call, UseAssertEqualsInsteadOfAssertTrue doesn't care if this is the Object.equals(Object o) which is used.

In the following example, the last assert should'nt be considered as an error:

public class MyUtilsTest extends TestCase {
public void testEquals() {
String[] foo = new String[] {""};
String[] bar = new String[] {""};
assertTrue(foo.equals(bar)); // Bad, should use assertEquals(...)
assertTrue(MyUtils.equals(foo, bar)); // OK, because this is not Object.equals(Object o) which is used
}
}

I suggest the replacement of the existing Xpath rule with this one (I've just added an argument count verification, which is not precise but adequate in my use case, because the SuspiciousEqualsMethodName is also activated):
//PrimaryExpression
PrimaryPrefix/Name[@Image = 'assertTrue'

]
PrimarySuffix/Arguments/ArgumentList/Expression/PrimaryExpression
[PrimaryPrefix/Name[ends-with(@Image, '.equals')
and PrimarySuffix/Arguments/ArgumentList[count(Expression)=1]]
]
[ancestor::ClassOrInterfaceDeclaration[//ClassOrInterfaceType[typeof(@Image, 'junit.framework.TestCase','TestCase')] or //MarkerAnnotation/Name[typeof(@Image, 'org.junit.Test', 'Test')]]]

Discussion


Log in to post a comment.