    1   package test.net.sourceforge.pmd.rules.design;
    2
    3   import net.sourceforge.pmd.PMD;
    4   import net.sourceforge.pmd.Rule;
    5   import net.sourceforge.pmd.RuleSetNotFoundException;
    6   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    7   import test.net.sourceforge.pmd.testframework.TestDescriptor;
    8
    9   public class CompareObjectsWithEqualsTest extends SimpleAggregatorTst {
    10
    11      private Rule rule;
    12
    13      public void setUp() throws RuleSetNotFoundException {
    14          rule = findRule("design", "CompareObjectsWithEquals");
    15      }
    16
    17      public void testAll() {
    18          runTests(new TestDescriptor[]{
    19              new TestDescriptor(TEST1, "simple failure with method params", 1, rule),
    20              new TestDescriptor(TEST2, "primitives are ok", 0, rule),
    21              new TestDescriptor(TEST3, "skip nulls", 0, rule),
    22              new TestDescriptor(TEST4, "missed hit - qualified names.  that's ok, we can't resolve the types yet, so better to skip this for now", 0, rule),
    23              new TestDescriptor(TEST5, "more qualified name skippage", 0, rule),
    24              new TestDescriptor(TEST6, "locals", 1, rule),
    25              new TestDescriptor(TEST7, "2 locals declared on one line", 1, rule),
    26              new TestDescriptor(TEST8, "array element comparison", 0, rule),
    27          });
    28      }
    29
    30      private static final String TEST1 =
    31              "public class Foo {" + PMD.EOL +
    32              " boolean bar(String a, String b) {" + PMD.EOL +
    33              "  return a == b;" + PMD.EOL +
    34              " }" + PMD.EOL +
    35              "}";
    36
    37      private static final String TEST2 =
    38              "public class Foo {" + PMD.EOL +
    39              " boolean bar(int a, int b) {" + PMD.EOL +
    40              "  return a == b;" + PMD.EOL +
    41              " }" + PMD.EOL +
    42              "}";
    43
    44      private static final String TEST3 =
    45              "public class Foo {" + PMD.EOL +
    46              " boolean bar(int a, int b) {" + PMD.EOL +
    47              "  return a == null || null == b;" + PMD.EOL +
    48              " }" + PMD.EOL +
    49              "}";
    50
    51      private static final String TEST4 =
    52              "public class Foo {" + PMD.EOL +
    53              " boolean bar(Foo b) {" + PMD.EOL +
    54              "  return this.b == b.foo;" + PMD.EOL +
    55              " }" + PMD.EOL +
    56              "}";
    57
    58      private static final String TEST5 =
    59              "public class Foo {" + PMD.EOL +
    60              " boolean bar(String a, String b) {" + PMD.EOL +
    61              "  return a.charAt(0) == b.charAt(0);" + PMD.EOL +
    62              " }" + PMD.EOL +
    63              "}";
    64
    65      private static final String TEST6 =
    66              "public class Foo {" + PMD.EOL +
    67              " boolean bar() {" + PMD.EOL +
    68              "  String a = \"foo\";" + PMD.EOL +
    69              "  String b = \"bar\";" + PMD.EOL +
    70              "  return a == b;" + PMD.EOL +
    71              " }" + PMD.EOL +
    72              "}";
    73
    74      private static final String TEST7 =
    75              "public class Foo {" + PMD.EOL +
    76              " void bar() {" + PMD.EOL +
    77              "  String a,b;" + PMD.EOL +
    78              "  a = \"foo\";" + PMD.EOL +
    79              "  b = \"bar\";" + PMD.EOL +
    80              "  if (a == b) {}" + PMD.EOL +
    81              " }" + PMD.EOL +
    82              "}";
    83
    84      private static final String TEST8 =
    85              "public class Foo {" + PMD.EOL +
    86              " void bar(int[] a, String[] b) {" + PMD.EOL +
    87              "  if (a[1] == b[1]) {}" + PMD.EOL +
    88              " }" + PMD.EOL +
    89              "}";
    90
    91  }