    1   /*
    2    * Created on Jan 10, 2005
    3    *
    4    * $Id: LocalVariableCouldBeFinalTest.java,v 1.6 2006/02/10 14:26:26 tomcopeland Exp $
    5    */
    6   package test.net.sourceforge.pmd.rules.optimization;
    7
    8   import net.sourceforge.pmd.PMD;
    9   import net.sourceforge.pmd.Rule;
    10  import net.sourceforge.pmd.RuleSetNotFoundException;
    11  import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    12  import test.net.sourceforge.pmd.testframework.TestDescriptor;
    13
    14  /***
    15   * Tests for the rule LocalVariableCouldBeFinal
    16   *
    17   * @author mgriffa
    18   */
    19  public class LocalVariableCouldBeFinalTest extends SimpleAggregatorTst {
    20
    21      private Rule rule;
    22
    23      public void setUp() throws RuleSetNotFoundException {
    24          rule = findRule("optimizations", "LocalVariableCouldBeFinal");
    25      }
    26
    27      public void testAll() {
    28          runTests(new TestDescriptor[]{
    29              new TestDescriptor(TEST1, "TEST1", 1, rule),
    30              new TestDescriptor(TEST2, "TEST2", 0, rule),
    31              new TestDescriptor(TEST3, "TEST3", 0, rule),
    32              new TestDescriptor(TEST4, "TEST4", 0, rule),
    33              new TestDescriptor(TEST5, "TEST5", 2, rule),
    34              new TestDescriptor(TEST6, "TEST6", 0, rule),
    35              new TestDescriptor(TEST7, "TEST7", 0, rule),
    36              new TestDescriptor(TEST8, "TEST8", 0, rule),
    37              new TestDescriptor(TEST9, "TEST9", 1, rule),
    38          });
    39      }
    40
    41      private static final String TEST1 =
    42              "public class Foo {" + PMD.EOL +
    43              " public void test1() {" + PMD.EOL +
    44              "  int a = 0;" + PMD.EOL +
    45              " }" + PMD.EOL +
    46              "}";
    47
    48      private static final String TEST2 =
    49              "public class Foo {" + PMD.EOL +
    50              " public void test1() {" + PMD.EOL +
    51              "  final int a = 0;" + PMD.EOL +
    52              " }" + PMD.EOL +
    53              "}";
    54
    55      private static final String TEST3 =
    56              "public class Foo {" + PMD.EOL +
    57              " public void test1() {" + PMD.EOL +
    58              "  int a = 0;" + PMD.EOL +
    59              "  a = 100;" + PMD.EOL +
    60              " }" + PMD.EOL +
    61              "}";
    62
    63      private static final String TEST4 =
    64              "public class Foo {" + PMD.EOL +
    65              " public void test1() {" + PMD.EOL +
    66              "  int a = 0;" + PMD.EOL +
    67              "  a += 100;" + PMD.EOL +
    68              " }" + PMD.EOL +
    69              "}";
    70
    71      /*
    72       * It can be discussed if this is a violation or not,
    73       * not always the value of a, b is constant and he logic could of course be more complex
    74       * */
    75      private static final String TEST5 =
    76              "public class Foo {" + PMD.EOL +
    77              " public void test1() {" + PMD.EOL +
    78              "  int a = 0;" + PMD.EOL +
    79              "  int b = 0;" + PMD.EOL +
    80              "  int c ;" + PMD.EOL +
    81              "  c = a + b;" + PMD.EOL +
    82              " }" + PMD.EOL +
    83              "}";
    84
    85      private static final String TEST6 =
    86              "public class Foo {" + PMD.EOL +
    87              " public void test1() {" + PMD.EOL +
    88              "  int a = 0;" + PMD.EOL +
    89              "  ++a;" + PMD.EOL +
    90              " }" + PMD.EOL +
    91              "}";
    92
    93      private static final String TEST7 =
    94              "public class Foo {" + PMD.EOL +
    95              " public void test1() {" + PMD.EOL +
    96              "  int a = 0;" + PMD.EOL +
    97              "  a+=1;" + PMD.EOL +
    98              " }" + PMD.EOL +
    99              "}";
    100
    101     private static final String TEST8 =
    102             "public class Foo {" + PMD.EOL +
    103             " public void test1() {" + PMD.EOL +
    104             "  int a = 0;" + PMD.EOL +
    105             "  a++;" + PMD.EOL +
    106             " }" + PMD.EOL +
    107             "}";
    108
    109     private static final String TEST9 =
    110             "public class Foo {" + PMD.EOL +
    111             " public void test1() {" + PMD.EOL +
    112             "  int a = 0;" + PMD.EOL +
    113             "  int b = 0;" + PMD.EOL +
    114             "  a++;" + PMD.EOL +
    115             "  a+=b;" + PMD.EOL +
    116             " }" + PMD.EOL +
    117             "}";
    118
    119 }