    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 AvoidReassigningParametersTest extends SimpleAggregatorTst {
    12
    13      private Rule rule;
    14
    15      public void setUp() throws Exception {
    16          rule = findRule("design", "AvoidReassigningParameters");
    17      }
    18
    19      public void testAll() {
    20          runTests(new TestDescriptor[]{
    21              new TestDescriptor(TEST1, "reassigned parameter, bad", 1, rule),
    22              new TestDescriptor(TEST2, "one parameter, not reassigned, good", 0, rule),
    23              new TestDescriptor(TEST3, "instance variable and parameter have same name", 1, rule),
    24              new TestDescriptor(TEST4, "qualified instance variable same name as parameter", 0, rule),
    25              new TestDescriptor(TEST5, "qualified name same as parameter", 0, rule),
    26              new TestDescriptor(TEST6, "assignment to parameter public field", 0, rule),
    27              new TestDescriptor(TEST7, "assignment to array parameter slot", 0, rule),
    28              new TestDescriptor(TEST8, "throws a stacktrace", 1, rule),
    29              new TestDescriptor(TEST9, "postfix increment in array dereference is bad", 1, rule),
    30              // FIXME new TestDescriptor(TEST10, "assignment to array slot", 0, rule),
    31          });
    32      }
    33
    34      public static final String TEST1 =
    35              "public class Foo {" + PMD.EOL +
    36              " void foo(int bar) {" + PMD.EOL +
    37              "  bar = 2;" + PMD.EOL +
    38              " }" + PMD.EOL +
    39              "}";
    40
    41      public static final String TEST2 =
    42              "public class Foo {" + PMD.EOL +
    43              " void foo(int bar) {}" + PMD.EOL +
    44              "}";
    45
    46      public static final String TEST3 =
    47              "public class Foo {" + PMD.EOL +
    48              " private int bar;" + PMD.EOL +
    49              " void foo(float bar) {" + PMD.EOL +
    50              "  bar = 2.2;" + PMD.EOL +
    51              " }" + PMD.EOL +
    52              "}";
    53
    54      public static final String TEST4 =
    55              "public class Foo {" + PMD.EOL +
    56              " private int bar;" + PMD.EOL +
    57              " void foo(float bar) {" + PMD.EOL +
    58              "  this.bar = 2;" + PMD.EOL +
    59              " }" + PMD.EOL +
    60              "}";
    61
    62      public static final String TEST5 =
    63              "public class Faz {" + PMD.EOL +
    64              " private class Foo {" + PMD.EOL +
    65              "  public String bar;" + PMD.EOL +
    66              " }" + PMD.EOL +
    67              " void foo(String bar) {" + PMD.EOL +
    68              "  Foo f = new Foo();" + PMD.EOL +
    69              "  f.bar = bar;" + PMD.EOL +
    70              " }" + PMD.EOL +
    71              "}";
    72
    73      public static final String TEST6 =
    74              "public class Foo {" + PMD.EOL +
    75              " void foo(Bar bar) {" + PMD.EOL +
    76              "  bar.buz = 2;" + PMD.EOL +
    77              " }" + PMD.EOL +
    78              "}";
    79
    80      public static final String TEST7 =
    81              "public class Foo {" + PMD.EOL +
    82              " void foo(Bar[] bar) {" + PMD.EOL +
    83              "  bar[0] = 2;" + PMD.EOL +
    84              " }" + PMD.EOL +
    85              "}";
    86
    87      public static final String TEST8 =
    88              "public class Foo {" + PMD.EOL +
    89              " void foo(int x) {" + PMD.EOL +
    90              "   try {" + PMD.EOL +
    91              "     x = 2;" + PMD.EOL +
    92              "   } catch (Throwable t) { " + PMD.EOL +
    93              "   } " + PMD.EOL +
    94              " }" + PMD.EOL +
    95              "}";
    96
    97      public static final String TEST9 =
    98              "public class Foo {" + PMD.EOL +
    99              " void foo(int x) {" + PMD.EOL +
    100             "  y[x++] = 2;" + PMD.EOL +
    101             " }" + PMD.EOL +
    102             "}";
    103
    104     public static final String TEST10 =
    105             "public class Foo {" + PMD.EOL +
    106             " void foo(int x[]) {" + PMD.EOL +
    107             "  x[2] = 2;" + PMD.EOL +
    108             " }" + PMD.EOL +
    109             "}";
    110 }