1 /*** 2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html 3 */ 4 package test.net.sourceforge.pmd.rules; 5 6 import net.sourceforge.pmd.PMD; 7 import net.sourceforge.pmd.Rule; 8 import net.sourceforge.pmd.RuleSetNotFoundException; 9 import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst; 10 import test.net.sourceforge.pmd.testframework.TestDescriptor; 11 12 public class CloseConnectionTest extends SimpleAggregatorTst { 13 14 private Rule rule; 15 16 public void setUp() throws RuleSetNotFoundException { 17 rule = findRule("design", "CloseConnection"); 18 } 19 20 public void testAll() { 21 runTests(new TestDescriptor[] { 22 new TestDescriptor(TEST1, "connection is closed, ok", 0, rule), 23 new TestDescriptor(TEST2, "connection not closed, should have failed", 1, rule), 24 new TestDescriptor(TEST3, "java.sql.* not imported, ignore", 0, rule), 25 }); 26 } 27 28 private static final String TEST1 = 29 "public class Foo {" + PMD.EOL + 30 " void bar() {" + PMD.EOL + 31 " Connection c = pool.getConnection();" + PMD.EOL + 32 " try {" + PMD.EOL + 33 " } catch (Exception e) {" + PMD.EOL + 34 " } finally {" + PMD.EOL + 35 " c.close();" + PMD.EOL + 36 " }" + PMD.EOL + 37 " }" + PMD.EOL + 38 "}"; 39 40 private static final String TEST2 = 41 "import java.sql.*;" + PMD.EOL + 42 "public class Foo {" + PMD.EOL + 43 " void bar() {" + PMD.EOL + 44 " Connection c = pool.getConnection();" + PMD.EOL + 45 " try {" + PMD.EOL + 46 " } catch (Exception e) {" + PMD.EOL + 47 " }" + PMD.EOL + 48 " }" + PMD.EOL + 49 "}"; 50 51 private static final String TEST3 = 52 "import some.pckg.Connection;" + PMD.EOL + 53 "public class Foo {" + PMD.EOL + 54 " void bar() {" + PMD.EOL + 55 " Connection c = pool.getConnection();" + PMD.EOL + 56 " try {} catch (Exception e) {}" + PMD.EOL + 57 " }" + PMD.EOL + 58 "}"; 59 60 }