    1   /*
    2    * Created on Jan 10, 2005
    3    *
    4    * $Id: MethodArgumentCouldBeFinalTest.java,v 1.10 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  public class MethodArgumentCouldBeFinalTest extends SimpleAggregatorTst {
    15
    16      private Rule rule;
    17
    18      public void setUp() throws RuleSetNotFoundException {
    19          rule = findRule("optimizations", "MethodArgumentCouldBeFinal");
    20      }
    21
    22      public void testAll() {
    23          runTests(new TestDescriptor[]{
    24              new TestDescriptor(TEST1, "TEST1", 1, rule),
    25              new TestDescriptor(TEST2, "TEST2", 2, rule),
    26              new TestDescriptor(TEST3, "TEST3", 2, rule),
    27              new TestDescriptor(TEST4, "TEST4", 1, rule),
    28              new TestDescriptor(TEST5, "TEST5", 1, rule),
    29              new TestDescriptor(TEST6, "TEST6", 0, rule),
    30              new TestDescriptor(TEST7, "Shouldn't trigger on try blocks", 0, rule),
    31              new TestDescriptor(TEST8, "Skip native methods", 0, rule),
    32              new TestDescriptor(TEST9, "Skip abstract methods", 0, rule),
    33              new TestDescriptor(TEST10, "self assignment of a method param means it can't be final", 0, rule),
    34              new TestDescriptor(TEST11, "same as above but prefix vs postfix", 0, rule),
    35          });
    36      }
    37
    38      private static final String TEST1 =
    39              "public class Foo {" + PMD.EOL +
    40              " public void bar(int a) {}" + PMD.EOL +
    41              "}";
    42
    43      private static final String TEST2 =
    44              "public class Foo {" + PMD.EOL +
    45              " public void bar(int a, Object o) {}" + PMD.EOL +
    46              "}";
    47
    48      private static final String TEST3 =
    49              "public class Foo {" + PMD.EOL +
    50              " public void bar(int a, Object o) {" + PMD.EOL +
    51              "  int z = a;" + PMD.EOL +
    52              "  Object x = o.clone();" + PMD.EOL +
    53              " }" + PMD.EOL +
    54              "}";
    55
    56      private static final String TEST4 =
    57              "public class Foo {" + PMD.EOL +
    58              " public void bar(final int a, Object o) {" + PMD.EOL +
    59              "  int z = a;" + PMD.EOL +
    60              "  Object x = o.clone();" + PMD.EOL +
    61              " }" + PMD.EOL +
    62              "}";
    63
    64      private static final String TEST5 =
    65              "public class Foo {" + PMD.EOL +
    66              " public void bar(int a, final Object o) {" + PMD.EOL +
    67              "  int z = a;" + PMD.EOL +
    68              "  Object x = o.clone();" + PMD.EOL +
    69              " }" + PMD.EOL +
    70              "}";
    71
    72      private static final String TEST6 =
    73              "public class Foo {" + PMD.EOL +
    74              " public void bar(final int a, final Object o) {" + PMD.EOL +
    75              "  int z = a;" + PMD.EOL +
    76              "  Object x = o.clone();" + PMD.EOL +
    77              " }" + PMD.EOL +
    78              "}";
    79
    80      private static final String TEST7 =
    81              "public class Foo {" + PMD.EOL +
    82              " public void bar(final List batch) {" + PMD.EOL +
    83              "   try {} catch (Exception e) {} " + PMD.EOL +
    84              "   try {} catch (Exception ee) {} " + PMD.EOL +
    85              " }" + PMD.EOL +
    86              "}";
    87
    88      private static final String TEST8 =
    89              "public class Foo {" + PMD.EOL +
    90              " public native void bar(Object x);" + PMD.EOL +
    91              "}";
    92
    93      private static final String TEST9 =
    94              "public class Foo {" + PMD.EOL +
    95              " public abstract void bar(Object x);" + PMD.EOL +
    96              "}";
    97
    98      private static final String TEST10 =
    99              "public class Foo {" + PMD.EOL +
    100             " public void bar(int a) {" + PMD.EOL +
    101             "  x[a++] = 1;" + PMD.EOL +
    102             " }" + PMD.EOL +
    103             "}";
    104
    105     private static final String TEST11 =
    106             "public class Foo {" + PMD.EOL +
    107             " public void bar(int a) {" + PMD.EOL +
    108             "  x[--a] = 1;" + PMD.EOL +
    109             " }" + PMD.EOL +
    110             "}";
    111 }