    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 CloseResourceTest extends SimpleAggregatorTst {
    13
    14      private Rule rule;
    15
    16      public void setUp() throws RuleSetNotFoundException {
    17          rule = findRule("design", "CloseResource");
    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, "ResultSet not closed, should have failed", 1, rule),
    25              new TestDescriptor(TEST4, "Statement not closed, should have failed", 1, rule),
    26              new TestDescriptor(TEST5, "java.sql.* not imported, ignore", 0, rule),
    27          });
    28      }
    29
    30      private static final String TEST1 =
    31              "public class Foo {" + PMD.EOL +
    32              " void bar() {" + PMD.EOL +
    33              "  Connection c = pool.getConnection();" + PMD.EOL +
    34              "  try {" + PMD.EOL +
    35              "  } catch (Exception e) {" + PMD.EOL +
    36              "  } finally {" + PMD.EOL +
    37              "   c.close();" + PMD.EOL +
    38              "  }" + PMD.EOL +
    39              " }" + PMD.EOL +
    40              "}";
    41
    42      private static final String TEST2 =
    43              "import java.sql.*;" + PMD.EOL +
    44              "public class Foo {" + PMD.EOL +
    45              " void bar() {" + PMD.EOL +
    46              "  Connection c = pool.getConnection();" + PMD.EOL +
    47              "  try {" + PMD.EOL +
    48              "  } catch (Exception e) {" + PMD.EOL +
    49              "  }" + PMD.EOL +
    50              " }" + PMD.EOL +
    51              "}";
    52
    53      private static final String TEST3 =
    54              "import java.sql.*;" + PMD.EOL +
    55              "public class Foo {" + PMD.EOL +
    56              " void bar() {" + PMD.EOL +
    57              "  ResultSet c = pool.getRS();" + PMD.EOL +
    58              "  try {" + PMD.EOL +
    59              "  } catch (Exception e) {}" + PMD.EOL +
    60              " }" + PMD.EOL +
    61              "}";
    62
    63      private static final String TEST4 =
    64              "import java.sql.*;" + PMD.EOL +
    65              "public class Foo {" + PMD.EOL +
    66              " void bar() {" + PMD.EOL +
    67              "  Statement c = pool.getStmt();" + PMD.EOL +
    68              "  try {" + PMD.EOL +
    69              "  } catch (Exception e) {}" + PMD.EOL +
    70              " }" + PMD.EOL +
    71              "}";
    72
    73      private static final String TEST5 =
    74              "import some.pckg.Connection;" + PMD.EOL +
    75              "public class Foo {" + PMD.EOL +
    76              " void bar() {" + PMD.EOL +
    77              "  Connection c = pool.getConnection();" + PMD.EOL +
    78              "  try {} catch (Exception e) {}" + PMD.EOL +
    79              " }" + PMD.EOL +
    80              "}";
    81
    82  }