    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package test.net.sourceforge.pmd.rules.design;
    5
    6   import net.sourceforge.pmd.PMD;
    7   import net.sourceforge.pmd.Rule;
    8   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    9   import test.net.sourceforge.pmd.testframework.TestDescriptor;
    10
    11  public class LooseCouplingTest extends SimpleAggregatorTst {
    12
    13      private Rule rule;
    14
    15      public void setUp() {
    16          rule = findRule("coupling", "LooseCoupling");
    17      }
    18
    19      public void testAll() {
    20          runTests(new TestDescriptor[]{
    21              new TestDescriptor(TEST1, "returning a HashSet, bad", 1, rule),
    22              new TestDescriptor(TEST2, "returning a Map, OK", 0, rule),
    23              new TestDescriptor(TEST3, "no problemo", 0, rule),
    24              new TestDescriptor(TEST4, "returning a set", 0, rule),
    25              new TestDescriptor(TEST5, "field declared of type HashSet", 1, rule),
    26              new TestDescriptor(TEST6, "field, return type both HashSet", 2, rule),
    27              new TestDescriptor(TEST7, "two fields", 2, rule),
    28              new TestDescriptor(TEST8, "method param is HashMap", 1, rule),
    29              new TestDescriptor(TEST9, "Vector could be List", 1, rule),
    30          });
    31      }
    32
    33      private static final String TEST1 =
    34              "public class Foo {" + PMD.EOL +
    35              " HashSet foo() {" + PMD.EOL +
    36              "  return new HashSet();" + PMD.EOL +
    37              " }" + PMD.EOL +
    38              "}";
    39
    40      private static final String TEST2 =
    41              "public class Foo {" + PMD.EOL +
    42              " Map getFoo() {" + PMD.EOL +
    43              "  return new HashMap();" + PMD.EOL +
    44              " }" + PMD.EOL +
    45              "}";
    46
    47      private static final String TEST3 =
    48              "public class Foo {" + PMD.EOL +
    49              " void foo() {}" + PMD.EOL +
    50              "}";
    51
    52      private static final String TEST4 =
    53              "import java.util.*;" + PMD.EOL +
    54              "public class Foo {" + PMD.EOL +
    55              " Set fooSet = new HashSet(); // OK" + PMD.EOL +
    56              " Set foo() {" + PMD.EOL +
    57              "  return fooSet;" + PMD.EOL +
    58              " }" + PMD.EOL +
    59              "}";
    60
    61      private static final String TEST5 =
    62              "public class Foo {" + PMD.EOL +
    63              " HashSet fooSet = new HashSet(); // NOT OK" + PMD.EOL +
    64              "}";
    65
    66      private static final String TEST6 =
    67              "public class Foo {" + PMD.EOL +
    68              " HashSet fooSet = new HashSet(); // NOT OK" + PMD.EOL +
    69              " HashSet foo() { // NOT OK" + PMD.EOL +
    70              "  return fooSet;" + PMD.EOL +
    71              " }" + PMD.EOL +
    72              "}";
    73
    74      private static final String TEST7 =
    75              "public class Foo {" + PMD.EOL +
    76              " HashSet fooSet = new HashSet();" + PMD.EOL +
    77              " HashMap fooMap = new HashMap();" + PMD.EOL +
    78              "}";
    79
    80      private static final String TEST8 =
    81              "public class Foo {" + PMD.EOL +
    82              " void foo(HashMap bar) {}" + PMD.EOL +
    83              "}";
    84
    85      private static final String TEST9 =
    86              "import java.util.*;" + PMD.EOL +
    87              "public class Foo {" + PMD.EOL +
    88              " public void foo(Vector bar) {}" + PMD.EOL +
    89              "}";
    90
    91  }