1   package test.net.sourceforge.pmd.rules;
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 SimplifyConditionalTest extends SimpleAggregatorTst {
10      private Rule rule;
11  
12      public void setUp() throws RuleSetNotFoundException {
13          rule = findRule("design", "SimplifyConditional");
14      }
15  
16      public void testAll() {
17          runTests(new TestDescriptor[]{
18              new TestDescriptor(TEST1, "failure case", 1, rule),
19              new TestDescriptor(TEST2, "ok", 0, rule),
20              new TestDescriptor(TEST3, "transpose x and null, still bad", 1, rule),
21              new TestDescriptor(TEST4, "conditional or and !(instanceof)", 1, rule),
22              new TestDescriptor(TEST5, "indexing into array is ok", 0, rule),
23          });
24      }
25  
26      private static final String TEST1 =
27              "public class Foo {" + PMD.EOL +
28              " void bar(Object x) {" + PMD.EOL +
29              "  if (x != null && x instanceof String) {}" + PMD.EOL +
30              " }" + PMD.EOL +
31              "}";
32  
33      private static final String TEST2 =
34              "public class Foo {" + PMD.EOL +
35              " void bar(Object x) {" + PMD.EOL +
36              "  if (x instanceof String) {}" + PMD.EOL +
37              " }" + PMD.EOL +
38              "}";
39  
40      private static final String TEST3 =
41              "public class Foo {" + PMD.EOL +
42              " void bar(Object x) {" + PMD.EOL +
43              "  if (null != x && x instanceof String) {}" + PMD.EOL +
44              " }" + PMD.EOL +
45              "}";
46  
47      private static final String TEST4 =
48              "public class Foo {" + PMD.EOL +
49              " void bar(Object x) {" + PMD.EOL +
50              "  if (x == null || !(x instanceof String)) {}" + PMD.EOL +
51              " }" + PMD.EOL +
52              "}";
53  
54      private static final String TEST5 =
55              "public class Foo {" + PMD.EOL +
56              " void bar(Object x) {" + PMD.EOL +
57              "  if (x != null && x[0] instanceof String) {}" + PMD.EOL +
58              " }" + PMD.EOL +
59              "}";
60  
61  }