Implemented RFE 1188369 - UnnecessaryBooleanAssertion now checks for things like 'assertTrue(\!foo)'. These should be changed to 'assertFalse(foo)' for clarity.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@3493 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2005-05-11 23:38:47 +00:00
parent 53c121dfef
commit 6d2d4da866
5 changed files with 77 additions and 53 deletions

View File

@ -1,5 +1,6 @@
????, 2005 - 3.2:
Implemented RFE 1188604 - AvoidThrowingCertainExceptionTypes has been split into AvoidThrowingRawExceptionTypes and AvoidThrowingNullPointerException.
Implemented RFE 1188369 - UnnecessaryBooleanAssertion now checks for things like 'assertTrue(!foo)'. These should be changed to 'assertFalse(foo)' for clarity.
May 10, 2005 - 3.1:
New rules: SimplifyStartsWith, UnnecessaryParentheses, CollapsibleIfStatements, UseAssertEqualsInsteadOfAssertTrue, UseAssertSameInsteadOfAssertTrue, UseStringBufferForStringAppends, SimplifyConditional, SingularField

View File

@ -1,52 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package test.net.sourceforge.pmd.rules;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleSetNotFoundException;
import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
import test.net.sourceforge.pmd.testframework.TestDescriptor;
public class UnnecessaryBooleanAssertionTest extends SimpleAggregatorTst{
private Rule rule;
public void setUp() throws RuleSetNotFoundException {
rule = findRule("rulesets/junit.xml", "UnnecessaryBooleanAssertion");
}
public void testAll() {
runTests(new TestDescriptor[] {
new TestDescriptor(TEST1, "failure case", 1, rule),
new TestDescriptor(TEST2, "variations", 4, rule),
new TestDescriptor(TEST3, "nested boolean literal", 0, rule),
});
}
private static final String TEST1 =
"public class Foo extends TestCase {" + PMD.EOL +
" void bar() {" + PMD.EOL +
" assertTrue(true); " + PMD.EOL +
"}" + PMD.EOL +
"}";
private static final String TEST2 =
"public class Foo extends TestCase {" + PMD.EOL +
" void bar() {" + PMD.EOL +
" assertTrue(true); " + PMD.EOL +
" assertTrue(false); " + PMD.EOL +
" assertFalse(true); " + PMD.EOL +
" assertFalse(false); " + PMD.EOL +
"}" + PMD.EOL +
"}";
private static final String TEST3 =
"public class Foo extends TestCase {" + PMD.EOL +
" void bar() {" + PMD.EOL +
" assertTrue(foobar(fiddle, true)); " + PMD.EOL +
"}" + PMD.EOL +
"}";
}

View File

@ -0,0 +1,68 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package test.net.sourceforge.pmd.rules.junit;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleSetNotFoundException;
import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
import test.net.sourceforge.pmd.testframework.TestDescriptor;
public class UnnecessaryBooleanAssertionTest extends SimpleAggregatorTst{
private Rule rule;
public void setUp() throws RuleSetNotFoundException {
rule = findRule("rulesets/junit.xml", "UnnecessaryBooleanAssertion");
}
public void testAll() {
runTests(new TestDescriptor[] {
new TestDescriptor(TEST1, "failure case", 1, rule),
new TestDescriptor(TEST2, "variations", 4, rule),
new TestDescriptor(TEST3, "nested boolean literal", 0, rule),
new TestDescriptor(TEST4, "asserting true a !", 1, rule),
new TestDescriptor(TEST5, "asserting false a !", 1, rule),
});
}
private static final String TEST1 =
"public class Foo extends TestCase {" + PMD.EOL +
" void bar() {" + PMD.EOL +
" assertTrue(true); " + PMD.EOL +
" }" + PMD.EOL +
"}";
private static final String TEST2 =
"public class Foo extends TestCase {" + PMD.EOL +
" void bar() {" + PMD.EOL +
" assertTrue(true); " + PMD.EOL +
" assertTrue(false); " + PMD.EOL +
" assertFalse(true); " + PMD.EOL +
" assertFalse(false); " + PMD.EOL +
" }" + PMD.EOL +
"}";
private static final String TEST3 =
"public class Foo extends TestCase {" + PMD.EOL +
" void bar() {" + PMD.EOL +
" assertTrue(foobar(fiddle, true)); " + PMD.EOL +
" }" + PMD.EOL +
"}";
private static final String TEST4 =
"public class Foo extends TestCase {" + PMD.EOL +
" void bar() {" + PMD.EOL +
" assertTrue(!foo); " + PMD.EOL +
" }" + PMD.EOL +
"}";
private static final String TEST5 =
"public class Foo extends TestCase {" + PMD.EOL +
" void bar() {" + PMD.EOL +
" assertFalse(!foo); " + PMD.EOL +
" }" + PMD.EOL +
"}";
}

View File

@ -162,6 +162,13 @@ and
PrimaryExpression/PrimarySuffix/Arguments/ArgumentList
/Expression/PrimaryExpression/PrimaryPrefix
/Literal/BooleanLiteral
or
(
.//Name[@Image='assertTrue' or @Image='assertFalse']
and
PrimaryExpression/PrimarySuffix/Arguments/ArgumentList
/Expression/UnaryExpressionNotPlusMinus[@Image='!']
)
]
]]>
</value>

View File

@ -43,7 +43,7 @@
</subsection>
<subsection name="Contributors">
<ul>
<li>Tom Parker - Suggested splitting up AvoidThrowingCertainExceptionTypes, AvoidInstantiatingObjectsInLoops bug report, AtLeastOneConstructor bug report</li>
<li>Tom Parker - Suggested addition to UnnecessaryBooleanAssertion, suggested splitting up AvoidThrowingCertainExceptionTypes, AvoidInstantiatingObjectsInLoops bug report, AtLeastOneConstructor bug report</li>
<li>Oto 'tapik' Buchta - Patched XMLRenderer for UTF8 support</li>
<li>Arent-Jan Banck - Reported bug with JDK 1.5 annotation handling</li>
<li>Fred Hartman - Reported exact location of bug in TooManyFields, fixed bug in UnnecessaryBooleanAssertion</li>