forked from phoedos/pmd
CloseResource was looking for import java.sql.* - removed this check, since java.io checks might might have java.sql imports git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4625 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -9,6 +9,7 @@ Fixed CSVRenderer - had flipped line and priority columns
|
||||
Applied patch 1551189 - SingularField false + for initialization blocks
|
||||
Fixed bug 1573795 - PreserveStackTrace doesn't throw CastClassException on exception with 0 args
|
||||
Fixed bug 1573591 - NonThreadSafeSingleton doesn't throw NPE when using this keyword
|
||||
CloseResource rule now checks code without java.sql import.
|
||||
|
||||
October 4, 2006 - 3.8:
|
||||
New rules:
|
||||
|
@ -12,19 +12,28 @@ 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() {
|
||||
runTests(new TestDescriptor[]{
|
||||
new TestDescriptor(TEST1, "connection is closed, ok", 0, rule),
|
||||
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(TEST5, "java.sql.* not imported, ignore", 0, rule),
|
||||
});
|
||||
new TestDescriptor(TEST1, "connection is closed, ok", 0, rule),
|
||||
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),
|
||||
});
|
||||
|
||||
runTests(new TestDescriptor[]{
|
||||
new TestDescriptor(TEST6, "Add type param", 1, ruleParam),
|
||||
});
|
||||
}
|
||||
|
||||
private static final String TEST1 =
|
||||
@ -70,13 +79,16 @@ public class CloseResourceTest extends SimpleAggregatorTst {
|
||||
" }" + PMD.EOL +
|
||||
"}";
|
||||
|
||||
private static final String TEST5 =
|
||||
"import some.pckg.Connection;" + PMD.EOL +
|
||||
"public class Foo {" + PMD.EOL +
|
||||
" void bar() {" + PMD.EOL +
|
||||
" Connection c = pool.getConnection();" + PMD.EOL +
|
||||
" try {} catch (Exception e) {}" + PMD.EOL +
|
||||
" }" + PMD.EOL +
|
||||
"}";
|
||||
|
||||
private static final String TEST6 =
|
||||
"import java.io.*;" + PMD.EOL +
|
||||
"public class BadClose {" + PMD.EOL +
|
||||
"private void readData() { " + PMD.EOL +
|
||||
"File aFile = new File(FileName); " + PMD.EOL +
|
||||
"FileInputStream anInput = new FileInputStream(aFile); " + PMD.EOL +
|
||||
"ObjectInputStream aStream = new ObjectInputStream(anInput); " + PMD.EOL +
|
||||
" " + PMD.EOL +
|
||||
"readExternal(aStream); " + PMD.EOL +
|
||||
"} " + PMD.EOL +
|
||||
"}";
|
||||
}
|
||||
|
@ -23,8 +23,6 @@ import net.sourceforge.pmd.ast.ASTType;
|
||||
import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
|
||||
import net.sourceforge.pmd.ast.Node;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Makes sure you close your database connections. It does this by
|
||||
* looking for code patterned like this:
|
||||
@ -42,9 +40,7 @@ public class CloseResource extends AbstractRule {
|
||||
private Set types = new HashSet();
|
||||
|
||||
public Object visit(ASTCompilationUnit node, Object data) {
|
||||
if (!importsJavaSqlPackage(node)) {
|
||||
return data;
|
||||
}
|
||||
|
||||
if (types.isEmpty()) {
|
||||
for (StringTokenizer st = new StringTokenizer(getStringProperty("types"), ","); st.hasMoreTokens();) {
|
||||
types.add(st.nextToken());
|
||||
@ -124,9 +120,4 @@ public class CloseResource extends AbstractRule {
|
||||
addViolation(data, id, clazz.getImage());
|
||||
}
|
||||
}
|
||||
|
||||
private boolean importsJavaSqlPackage(ASTCompilationUnit node) {
|
||||
return importsPackage(node, "java.sql");
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user