    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package test.net.sourceforge.pmd.rules.braces;
    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 IfElseStmtsMustUseBracesTest extends SimpleAggregatorTst {
    12
    13      private Rule rule;
    14
    15      public void setUp() throws Exception {
    16          rule = findRule("braces", "IfElseStmtsMustUseBraces");
    17      }
    18
    19      public void testAll() {
    20          runTests(new TestDescriptor[]{
    21              new TestDescriptor(TEST1, "else without braces", 1, rule),
    22              new TestDescriptor(TEST2, "ok", 0, rule),
    23              new TestDescriptor(TEST3, "two sets of missing braces", 2, rule),
    24              new TestDescriptor(TEST4, "elseif with missing braces", 1, rule),
    25              new TestDescriptor(TEST5, "elseif with braces after else", 0, rule),
    26              new TestDescriptor(TEST6, "elseif with missing braces, first braces on separate line", 1, rule),
    27              new TestDescriptor(TEST7, "bug 976643 - nested ifs without braces", 0, rule),
    28          });
    29      }
    30
    31      private static final String TEST1 =
    32              "public class Foo {" + PMD.EOL +
    33              " void foo() {     " + PMD.EOL +
    34              "  if (true) {" + PMD.EOL +
    35              "   x=2;" + PMD.EOL +
    36              "  } else " + PMD.EOL +
    37              "   y=4;" + PMD.EOL +
    38              "  " + PMD.EOL +
    39              " }" + PMD.EOL +
    40              "}";
    41
    42      private static final String TEST2 =
    43              "public class Foo {" + PMD.EOL +
    44              " void foo() {     " + PMD.EOL +
    45              "  if (true) {" + PMD.EOL +
    46              "   x=2;" + PMD.EOL +
    47              "  } else {" + PMD.EOL +
    48              "   x=4;" + PMD.EOL +
    49              "  }" + PMD.EOL +
    50              " }" + PMD.EOL +
    51              "}";
    52
    53      private static final String TEST3 =
    54              "public class Foo {" + PMD.EOL +
    55              " void foo() {     " + PMD.EOL +
    56              "  if (true) " + PMD.EOL +
    57              "   y=2;" + PMD.EOL +
    58              "  else " + PMD.EOL +
    59              "   x=4;" + PMD.EOL +
    60              " }" + PMD.EOL +
    61              "}";
    62
    63      private static final String TEST4 =
    64              "public class Foo {" + PMD.EOL +
    65              " void foo() {     " + PMD.EOL +
    66              "  if (true) {" + PMD.EOL +
    67              "   x=2;" + PMD.EOL +
    68              "  } else if (true) { " + PMD.EOL +
    69              "   x = 3; " + PMD.EOL +
    70              "  } else " + PMD.EOL +
    71              "   y=4;" + PMD.EOL +
    72              "  " + PMD.EOL +
    73              " }" + PMD.EOL +
    74              "}";
    75
    76      private static final String TEST5 =
    77              "public class Foo {" + PMD.EOL +
    78              " void foo() {     " + PMD.EOL +
    79              "  if (true) {" + PMD.EOL +
    80              "   x=2;" + PMD.EOL +
    81              "  } else if (true) { " + PMD.EOL +
    82              "   x = 3; " + PMD.EOL +
    83              "  } else { " + PMD.EOL +
    84              "   y=4;" + PMD.EOL +
    85              "  }" + PMD.EOL +
    86              " }" + PMD.EOL +
    87              "}";
    88
    89      private static final String TEST6 =
    90              "public class Foo {" + PMD.EOL +
    91              " void foo() {     " + PMD.EOL +
    92              "  if (true) " + PMD.EOL +
    93              "  {" + PMD.EOL +
    94              "   x=2;" + PMD.EOL +
    95              "  } " + PMD.EOL +
    96              "  else " + PMD.EOL +
    97              "   y=4;" + PMD.EOL +
    98              "  " + PMD.EOL +
    99              " }" + PMD.EOL +
    100             "}";
    101
    102     private static final String TEST7 =
    103             "public class Foo {" + PMD.EOL +
    104             " void foo() {" + PMD.EOL +
    105             "  if (true) " + PMD.EOL +
    106             "   if (false) " + PMD.EOL +
    107             "    x=2;" + PMD.EOL +
    108             " }" + PMD.EOL +
    109             "}";
    110
    111 }