Fixed bugs 1060761 / 1433119 & RFE 1196954 - CloseResource now takes an optional parameter to identify closure methods

Using strings to identify the parameter, and not the property descriptor as the JUnit test did not pass using the property descriptor.


git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4688 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Allan Caplan
2006-10-17 00:13:16 +00:00
parent bb51e44aaf
commit 9c0eef920c
3 changed files with 82 additions and 17 deletions

View File

@ -9,6 +9,7 @@ Fixed bug 1573795 - PreserveStackTrace doesn't throw CastClassException on excep
Fixed bug 1573591 - NonThreadSafeSingleton doesn't throw NPE when using this keyword
Fixed bug 1371753 - UnnecessaryLocalBeforeReturn is now less aggressive in its reporting.
Fixed bug 1566547 - Annotations with an empty MemberValueArrayInitializer are now parsed properly.
Fixed bugs 1060761 / 1433119 & RFE 1196954 - CloseResource now takes an optional parameter to identify closure methods
Applied patch 1551189 - SingularField false + for initialization blocks
Applied patch 1573981 - false + in CloneMethodMustImplementCloneable
Applied patch 1574988 - false + in OverrideBothEqualsAndHashcode

View File

@ -12,15 +12,12 @@ import test.net.sourceforge.pmd.testframework.TestDescriptor;
public class CloseResourceTest extends SimpleAggregatorTst {
private Rule rule;
private Rule ruleParam;
public void setUp() throws RuleSetNotFoundException {
rule = findRule("design", "CloseResource");
rule.addProperty("types", "Connection,Statement,ResultSet");
ruleParam = findRule("design", "CloseResource");
ruleParam.addProperty("types", "ObjectInputStream");
}
public void testAll() {
@ -29,11 +26,29 @@ public class CloseResourceTest extends SimpleAggregatorTst {
new TestDescriptor(TEST2, "connection not closed, should have failed", 1, rule),
new TestDescriptor(TEST3, "ResultSet not closed, should have failed", 1, rule),
new TestDescriptor(TEST4, "Statement not closed, should have failed", 1, rule),
new TestDescriptor(TEST8, "Executing rule 8 to validate there are 2 problems before executing with param", 2, rule),
});
}
public void testTypes(){
rule.addProperty("types", "ObjectInputStream");
runTests(new TestDescriptor[]{
new TestDescriptor(TEST6, "Add type param", 1, ruleParam),
});
new TestDescriptor(TEST6, "Add type param", 1, rule),
});
}
public void testPropertySetter(){
rule.addProperty("closeTargets","MyHelper.close");
runTests(new TestDescriptor[]{
new TestDescriptor(TEST7, "OK", 0, rule),
});
}
public void testMultipleProperties(){
rule.addProperty("closeTargets","MyHelper.close, cleanup");
runTests(new TestDescriptor[]{
new TestDescriptor(TEST8, "OK", 0, rule),
});
}
private static final String TEST1 =
@ -91,4 +106,31 @@ public class CloseResourceTest extends SimpleAggregatorTst {
"readExternal(aStream); " + PMD.EOL +
"} " + PMD.EOL +
"}";
private static final String TEST7 =
"import java.sql.*;" + PMD.EOL +
"public class Foo {" + PMD.EOL +
" void bar() {" + PMD.EOL +
" Statement c = pool.getStmt();" + PMD.EOL +
" try {" + PMD.EOL +
" } finally {" + PMD.EOL +
" MyHelper.close(c);" + PMD.EOL +
"}" + PMD.EOL +
" }" + PMD.EOL +
"}";
private static final String TEST8 =
"import java.sql.*;" + PMD.EOL +
"public class Foo {" + PMD.EOL +
" void bar() {" + PMD.EOL +
" Statement c = pool.getStmt();" + PMD.EOL +
" Statement st = c.executeQuery(\"SELECT * FROM FOO\");" + PMD.EOL +
" try {" + PMD.EOL +
" } finally {" + PMD.EOL +
" MyHelper.close(c);" + PMD.EOL +
" cleanup(st);" + PMD.EOL +
"}" + PMD.EOL +
" }" + PMD.EOL +
"}";
}

View File

@ -3,14 +3,8 @@
*/
package net.sourceforge.pmd.rules;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.PropertyDescriptor;
import net.sourceforge.pmd.ast.ASTBlock;
import net.sourceforge.pmd.ast.ASTClassOrInterfaceType;
import net.sourceforge.pmd.ast.ASTCompilationUnit;
@ -22,6 +16,15 @@ import net.sourceforge.pmd.ast.ASTTryStatement;
import net.sourceforge.pmd.ast.ASTType;
import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.ast.Node;
import net.sourceforge.pmd.properties.StringProperty;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
/**
* Makes sure you close your database connections. It does this by
@ -38,10 +41,28 @@ import net.sourceforge.pmd.ast.Node;
public class CloseResource extends AbstractRule {
private Set types = new HashSet();
private Set closeTargets = new HashSet();
private static final PropertyDescriptor closeTargetsDescriptor = new StringProperty("closeTargets",
"Methods which may close this resource", "", 1.0f);
private static final PropertyDescriptor typesDescriptor = new StringProperty("types",
"Types that are affected by this rule", "", 2.0f);
private static final Map propertyDescriptorsByName = asFixedMap(new PropertyDescriptor[] { typesDescriptor, closeTargetsDescriptor });
protected Map propertiesByName() {
return propertyDescriptorsByName;
};
public Object visit(ASTCompilationUnit node, Object data) {
if (types.isEmpty()) {
if (closeTargets.isEmpty() && getStringProperty("closeTargets") != null) {
for (StringTokenizer st = new StringTokenizer(getStringProperty("closeTargets"), ","); st.hasMoreTokens();) {
closeTargets.add(st.nextToken());
}
}
if (types.isEmpty() && getStringProperty("types") != null) {
for (StringTokenizer st = new StringTokenizer(getStringProperty("types"), ","); st.hasMoreTokens();) {
types.add(st.nextToken());
}
@ -105,7 +126,8 @@ public class CloseResource extends AbstractRule {
List names = new ArrayList();
f.findChildrenOfType(ASTName.class, names, true);
for (Iterator it2 = names.iterator(); it2.hasNext();) {
if (((ASTName) it2.next()).getImage().equals(target)) {
String name = ((ASTName) it2.next()).getImage();
if (name.equals(target) || closeTargets.contains(name)) {
closed = true;
}
}