Added some JUnit tests, fixed comment in rule

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4639 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Allan Caplan
2006-10-13 20:54:08 +00:00
parent 0d2aec583c
commit 7e2877ec84
2 changed files with 25 additions and 3 deletions

View File

@ -23,7 +23,10 @@ public class StringInstantiationRuleTest extends SimpleAggregatorTst {
new TestDescriptor(TEST2, "new String array", 0, rule),
new TestDescriptor(TEST3, "using multiple parameter constructor", 0, rule),
new TestDescriptor(TEST4, "using 4 parameter constructor", 0, rule),
new TestDescriptor(TEST5, "byte array constructor is ok", 0, rule)
new TestDescriptor(TEST5, "byte array constructor is ok", 0, rule),
new TestDescriptor(TEST6, "Method returning new String", 1, rule),
new TestDescriptor(TEST7, "Not a new String", 0, rule),
new TestDescriptor(TEST8, "Returns new String(str)", 1, rule)
});
}
@ -62,5 +65,25 @@ public class StringInstantiationRuleTest extends SimpleAggregatorTst {
" }" + PMD.EOL +
"}";
private static final String TEST6 =
"public class Foo {" + PMD.EOL +
" String foo() {" + PMD.EOL +
" return new String(\"foo\");" + PMD.EOL +
" }" + PMD.EOL +
"}";
private static final String TEST7 =
"public class Foo {" + PMD.EOL +
" STRING foo() {" + PMD.EOL +
" return new STRING(\"foo\");" + PMD.EOL +
" }" + PMD.EOL +
"}";
private static final String TEST8 =
"public class Foo {" + PMD.EOL +
" String foo(String str) {" + PMD.EOL +
" return new String(str);" + PMD.EOL +
" }" + PMD.EOL +
"}";
}

View File

@ -46,10 +46,9 @@ public class StringInstantiation extends AbstractRule {
}
VariableNameDeclaration vnd = (VariableNameDeclaration) nd;
// nd == null in cases like: return new String("foo");
// nd == null in cases like: return new String(str);
if (vnd == null || vnd.getTypeImage().equals("String")) {
addViolation(data, node);
}
return data;
}