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.rules.CloseConnectionRule; 8 import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst; 9 import test.net.sourceforge.pmd.testframework.TestDescriptor; 10 11 public class CloseConnectionRuleTest extends SimpleAggregatorTst { 12 13 public void testAll() { 14 runTests(new TestDescriptor[] { 15 new TestDescriptor(TEST1, "connection is closed, ok", 0, new CloseConnectionRule()), 16 new TestDescriptor(TEST2, "connection not closed, should have failed", 1, new CloseConnectionRule()), 17 }); 18 } 19 20 private static final String TEST1 = 21 "public class Foo {" + PMD.EOL + 22 " void bar() {" + PMD.EOL + 23 " Connection c = pool.getConnection();" + PMD.EOL + 24 " try {" + PMD.EOL + 25 " } catch (Exception e) {" + PMD.EOL + 26 " } finally {" + PMD.EOL + 27 " c.close();" + PMD.EOL + 28 " }" + PMD.EOL + 29 " }" + PMD.EOL + 30 "}"; 31 32 private static final String TEST2 = 33 "public class Foo {" + PMD.EOL + 34 " void bar() {" + PMD.EOL + 35 " Connection c = pool.getConnection();" + PMD.EOL + 36 " try {" + PMD.EOL + 37 " } catch (Exception e) {" + PMD.EOL + 38 " }" + PMD.EOL + 39 " }" + PMD.EOL + 40 "}"; 41 42 }