    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 test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    9   import test.net.sourceforge.pmd.testframework.TestDescriptor;
    10
    11  public class AvoidDeeplyNestedIfStmtsRuleTest extends SimpleAggregatorTst {
    12
    13      private Rule rule;
    14
    15      public void setUp() {
    16          rule = findRule("design", "AvoidDeeplyNestedIfStmts");
    17          rule.addProperty("problemDepth", "3");
    18      }
    19
    20      public void testAll() {
    21          runTests(new TestDescriptor[]{
    22              new TestDescriptor(TEST1, "Bad, very deep", 1, rule),
    23              new TestDescriptor(TEST2, "OK, not so deep", 0, rule),
    24          });
    25      }
    26
    27      public static final String TEST1 =
    28              "public class Foo {" + PMD.EOL +
    29              " public void bar() { " + PMD.EOL +
    30              "  int x=2; " + PMD.EOL +
    31              "  int y=3; " + PMD.EOL +
    32              "  int z=4; " + PMD.EOL +
    33              "  if (x>y) { " + PMD.EOL +
    34              "   if (y>z) { " + PMD.EOL +
    35              "    if (z==x) { " + PMD.EOL +
    36              "     // this is officially out of control now " + PMD.EOL +
    37              "    } " + PMD.EOL +
    38              "   } " + PMD.EOL +
    39              "  }" + PMD.EOL +
    40              " }" + PMD.EOL +
    41              "}";
    42
    43      public static final String TEST2 =
    44              "public class Foo {" + PMD.EOL +
    45              " public void bar() { " + PMD.EOL +
    46              "  if (true) {" + PMD.EOL +
    47              "  } else if (true) {" + PMD.EOL +
    48              "  } else if (true) {" + PMD.EOL +
    49              "  } else {" + PMD.EOL +
    50              "    // this ain't good code, but it shouldn't trigger this rule" + PMD.EOL +
    51              "  }" + PMD.EOL +
    52              " }" + PMD.EOL +
    53              "}";
    54
    55  }