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         });
26      }
27  
28      private static final String TEST1 =
29      "public class Foo {" + PMD.EOL +
30      " boolean bar(String a, String b) {" + PMD.EOL +
31      "  return a == b;" + PMD.EOL +
32      " }" + PMD.EOL +
33      "}";
34  
35      private static final String TEST2 =
36      "public class Foo {" + PMD.EOL +
37      " boolean bar(int a, int b) {" + PMD.EOL +
38      "  return a == b;" + PMD.EOL +
39      " }" + PMD.EOL +
40      "}";
41  
42      private static final String TEST3 =
43      "public class Foo {" + PMD.EOL +
44      " boolean bar(int a, int b) {" + PMD.EOL +
45      "  return a == null || null == b;" + PMD.EOL +
46      " }" + PMD.EOL +
47      "}";
48  
49      private static final String TEST4 =
50      "public class Foo {" + PMD.EOL +
51      " boolean bar(Foo b) {" + PMD.EOL +
52      "  return this.b == b.foo;" + PMD.EOL +
53      " }" + PMD.EOL +
54      "}";
55  
56      private static final String TEST5 =
57      "public class Foo {" + PMD.EOL +
58      " boolean bar(String a, String b) {" + PMD.EOL +
59      "  return a.charAt(0) == b.charAt(0);" + PMD.EOL +
60      " }" + PMD.EOL +
61      "}";
62  
63      private static final String TEST6 =
64      "public class Foo {" + PMD.EOL +
65      " boolean bar() {" + PMD.EOL +
66      "  String a = \"foo\";" + PMD.EOL +
67      "  String b = \"bar\";" + PMD.EOL +
68      "  return a == b;" + PMD.EOL +
69      " }" + PMD.EOL +
70      "}";
71  
72  }