Doing a JUnit review - checking tests with low coverage for what they're missing

ArrayIsStoredDirectly is documented as finding constructors, as well as methods, but only methods were tested. Adding a constructor to the test revealed a failed JUnit test - fixed rule.


git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4632 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Allan Caplan
2006-10-13 00:44:12 +00:00
parent 394556e9a5
commit 1aa75f9540
3 changed files with 34 additions and 4 deletions

View File

@ -12,6 +12,7 @@ Fixed bug 1573591 - NonThreadSafeSingleton doesn't throw NPE when using this key
CloseResource rule now checks code without java.sql import.
Applied patch 1573981 - false + in CloneMethodMustImplementCloneable
Applied patch 1574988 - false + in OverrideBothEqualsAndHashcode
ArrayIsStoredDirectly rule now checks Constructors
October 4, 2006 - 3.8:
New rules:

View File

@ -27,6 +27,9 @@ public class ArrayIsStoredDirectlyTest extends SimpleAggregatorTst {
new TestDescriptor(TEST6, "skip abstract, native", 0, rule),
new TestDescriptor(TEST7, "equality expression, not assignment", 0, rule),
new TestDescriptor(TEST8, "assignment of array element", 0, rule),
new TestDescriptor(TEST9, "Constructor clear violation", 1, rule),
new TestDescriptor(TEST10, "Constructor no violation", 0, rule),
new TestDescriptor(TEST11, "No reassignment", 0, rule),
});
}
@ -80,4 +83,23 @@ public class ArrayIsStoredDirectlyTest extends SimpleAggregatorTst {
"}";
private static final String TEST9 =
"public class Foo {" + PMD.EOL +
" String [] arr;" + PMD.EOL +
" Foo (String[] x) {arr = x;}" + PMD.EOL +
"}";
private static final String TEST10 =
"public class Foo {" + PMD.EOL +
" String [] arr;" + PMD.EOL +
" Foo (String[] x) {arr = x.clone();}" + PMD.EOL +
"}";
private static final String TEST11 =
"public class Foo {" + PMD.EOL +
" String [] arr;" + PMD.EOL +
" void foo() {String[] bar = {\"\"};arr = bar;}" + PMD.EOL +
"}";
}

View File

@ -18,6 +18,7 @@ import net.sourceforge.pmd.ast.ASTPrimaryExpression;
import net.sourceforge.pmd.ast.ASTPrimarySuffix;
import net.sourceforge.pmd.ast.ASTStatementExpression;
import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.ast.SimpleNode;
import java.util.Iterator;
import java.util.List;
@ -79,10 +80,13 @@ public class ArrayIsStoredDirectly extends AbstractSunSecureRule {
assignedVar = ((ASTPrimarySuffix) se.getFirstChildOfType(ASTPrimarySuffix.class)).getImage();
}
ASTMethodDeclaration n = (ASTMethodDeclaration) pe.getFirstParentOfType(ASTMethodDeclaration.class);
SimpleNode n = (ASTMethodDeclaration) pe.getFirstParentOfType(ASTMethodDeclaration.class);
if (n == null) {
continue;
}
n = (ASTConstructorDeclaration) pe.getFirstParentOfType(ASTConstructorDeclaration.class);
if (n == null) {
continue;
}
}
if (!isLocalVariable(assignedVar, n)) {
// TODO could this be more clumsy? We really
// need to build out the PMD internal framework more
@ -111,7 +115,10 @@ public class ArrayIsStoredDirectly extends AbstractSunSecureRule {
}
if (val.equals(varName)) {
ASTMethodDeclaration md = (ASTMethodDeclaration) parameter.getFirstParentOfType(ASTMethodDeclaration.class);
SimpleNode md = (ASTMethodDeclaration) parameter.getFirstParentOfType(ASTMethodDeclaration.class);
if (md == null) {
md = (ASTConstructorDeclaration) pe.getFirstParentOfType(ASTConstructorDeclaration.class);
}
if (!isLocalVariable(varName, md)) {
addViolation(ctx, parameter, varName);
}