    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 OverrideBothEqualsAndHashcodeTest extends SimpleAggregatorTst {
    13
    14      private Rule rule;
    15
    16      public void setUp() throws RuleSetNotFoundException {
    17          rule = findRule("basic", "OverrideBothEqualsAndHashcode");
    18      }
    19
    20      public void testAll() {
    21          runTests(new TestDescriptor[]{
    22              new TestDescriptor(TEST1, "hash code only", 1, rule),
    23              new TestDescriptor(TEST2, "equals only", 1, rule),
    24              new TestDescriptor(TEST3, "overrides both", 0, rule),
    25              new TestDescriptor(TEST4, "overrides neither", 0, rule),
    26              new TestDescriptor(TEST5, "equals sig uses String, not Object", 1, rule),
    27              new TestDescriptor(TEST6, "interface", 0, rule),
    28              new TestDescriptor(TEST7, "java.lang.Object", 0, rule),
    29              new TestDescriptor(TEST8, "skip Comparable implementations", 0, rule),
    30          });
    31      }
    32
    33      private static final String TEST1 =
    34              "public class Foo {" + PMD.EOL +
    35              " public int hashCode() {}" + PMD.EOL +
    36              "}";
    37
    38      private static final String TEST2 =
    39              "public class Foo {" + PMD.EOL +
    40              " public boolean equals(Object other) {}" + PMD.EOL +
    41              "}";
    42
    43      private static final String TEST3 =
    44              "public class Foo {" + PMD.EOL +
    45              " public boolean equals(Object other) {}" + PMD.EOL +
    46              " public int hashCode() {}" + PMD.EOL +
    47              "}";
    48
    49      private static final String TEST4 =
    50              "public class Foo {}";
    51
    52      private static final String TEST5 =
    53              "public class Foo {" + PMD.EOL +
    54              " public boolean equals(String o) {" + PMD.EOL +
    55              "  return true;" + PMD.EOL +
    56              " }" + PMD.EOL +
    57              " public int hashCode() {" + PMD.EOL +
    58              "  return 0;" + PMD.EOL +
    59              " }" + PMD.EOL +
    60              "}";
    61
    62      private static final String TEST6 =
    63              "public interface Foo {" + PMD.EOL +
    64              " public boolean equals(Object o);" + PMD.EOL +
    65              "}";
    66
    67      private static final String TEST7 =
    68              "public class Foo {" + PMD.EOL +
    69              " public boolean equals(java.lang.Object o) {" + PMD.EOL +
    70              "  return true;" + PMD.EOL +
    71              " }" + PMD.EOL +
    72              " public int hashCode() {" + PMD.EOL +
    73              "  return 0;" + PMD.EOL +
    74              " }" + PMD.EOL +
    75              "}";
    76
    77      private static final String TEST8 =
    78              "public class Foo implements Comparable {" + PMD.EOL +
    79              " public boolean equals(Object other) { return false; }" + PMD.EOL +
    80              " public int compareTo(Object other) { return 42; }" + PMD.EOL +
    81              "}";
    82
    83  }