    1   package test.net.sourceforge.pmd.rules.design;
    2
    3   import net.sourceforge.pmd.PMD;
    4   import net.sourceforge.pmd.Rule;
    5   import net.sourceforge.pmd.RuleSetNotFoundException;
    6   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    7   import test.net.sourceforge.pmd.testframework.TestDescriptor;
    8
    9   public class ImmutableFieldTest extends SimpleAggregatorTst {
    10
    11      private Rule rule;
    12
    13      public void setUp() throws RuleSetNotFoundException {
    14          rule = findRule("design", "ImmutableField");
    15      }
    16
    17      public void testAll() {
    18          runTests(new TestDescriptor[]{
    19              new TestDescriptor(TEST1, "could be immutable, only assigned in constructor", 1, rule),
    20              new TestDescriptor(TEST2, "could be immutable, only assigned in decl", 1, rule),
    21              new TestDescriptor(TEST3, "ok, assigned twice", 0, rule),
    22              new TestDescriptor(TEST4, "ok, static field ", 0, rule), // TODO - probably should check these, not just discard them
    23              new TestDescriptor(TEST5, "ok, one constructor assigns, one doesn't", 0, rule),
    24              new TestDescriptor(TEST6, "ok, assignment via postfix expression", 0, rule),
    25              new TestDescriptor(TEST7, "postfix expressions imply mutability", 0, rule),
    26              new TestDescriptor(TEST8, "compound assignment", 0, rule),
    27              new TestDescriptor(TEST9, "preincrement", 0, rule),
    28              new TestDescriptor(TEST10, "predecrement", 0, rule),
    29              new TestDescriptor(TEST11, "compound assignment 2", 0, rule),
    30              new TestDescriptor(TEST12, "rhs 2", 0, rule),
    31              new TestDescriptor(TEST13, "assignment in constructor is in try block", 0, rule),
    32              new TestDescriptor(TEST14, "assignment in method is in try block", 0, rule),
    33              new TestDescriptor(TEST15, "assignment in constructor in loop is ok", 0, rule),
    34              new TestDescriptor(TEST16, "assignment in anonymous inner class method is OK", 0, rule),
    35          });
    36      }
    37
    38      private static final String TEST1 =
    39              "public class Foo {" + PMD.EOL +
    40              " private int x;" + PMD.EOL +
    41              " public Foo() {" + PMD.EOL +
    42              "  x = 2;" + PMD.EOL +
    43              " }" + PMD.EOL +
    44              "}";
    45
    46      private static final String TEST2 =
    47              "public class Foo {" + PMD.EOL +
    48              " private int x = 42;" + PMD.EOL +
    49              "}";
    50
    51      private static final String TEST3 =
    52              "public class Foo {" + PMD.EOL +
    53              " private int x;" + PMD.EOL +
    54              " public Foo() {" + PMD.EOL +
    55              "  x = 41;" + PMD.EOL +
    56              " }" + PMD.EOL +
    57              " public void bar() {" + PMD.EOL +
    58              "  x = 42;" + PMD.EOL +
    59              " }" + PMD.EOL +
    60              "}";
    61
    62      private static final String TEST4 =
    63              "public class Foo {" + PMD.EOL +
    64              " private static int x = 0;" + PMD.EOL +
    65              " private final int y;" + PMD.EOL +
    66              " public Foo() {" + PMD.EOL +
    67              "  y = x;" + PMD.EOL +
    68              "  x++;" + PMD.EOL +
    69              " }" + PMD.EOL +
    70              "}";
    71
    72      private static final String TEST5 =
    73              "public class Foo {" + PMD.EOL +
    74              " private int x;" + PMD.EOL +
    75              " public Foo(int y) {" + PMD.EOL +
    76              "  x = y;" + PMD.EOL +
    77              " }" + PMD.EOL +
    78              " public Foo() {}" + PMD.EOL +
    79              "}";
    80
    81      private static final String TEST6 =
    82              "public class Foo {" + PMD.EOL +
    83              " private int x;" + PMD.EOL +
    84              " public Foo() {}" + PMD.EOL +
    85              " private void bar() {x++;}" + PMD.EOL +
    86              "}";
    87
    88      private static final String TEST7 =
    89              "public class Foo {" + PMD.EOL +
    90              " private int x = 0;" + PMD.EOL +
    91              " private void bar() {" + PMD.EOL +
    92              "  x++;" + PMD.EOL +
    93              " }" + PMD.EOL +
    94              "}";
    95
    96      private static final String TEST8 =
    97              "public class Foo {" + PMD.EOL +
    98              " private int w;" + PMD.EOL +
    99              " private int z;" + PMD.EOL +
    100             " private void bar() {" + PMD.EOL +
    101             "  w = 2;" + PMD.EOL +
    102             "  z = 4;" + PMD.EOL +
    103             " }" + PMD.EOL +
    104             " private void gaz() {" + PMD.EOL +
    105             "  w += z++;" + PMD.EOL +
    106             " }" + PMD.EOL +
    107             "}";
    108
    109     private static final String TEST9 =
    110             "public class Foo {" + PMD.EOL +
    111             " private int x = 0;" + PMD.EOL +
    112             " public void bar() {" + PMD.EOL +
    113             "  ++x;" + PMD.EOL +
    114             " }" + PMD.EOL +
    115             "}";
    116
    117     private static final String TEST10 =
    118             "public class Foo {" + PMD.EOL +
    119             " private int x = 0;" + PMD.EOL +
    120             " public void bar() {" + PMD.EOL +
    121             "  --x;" + PMD.EOL +
    122             " }" + PMD.EOL +
    123             "}";
    124
    125     private static final String TEST11 =
    126             "public class Foo {" + PMD.EOL +
    127             " private int x = 0;" + PMD.EOL +
    128             " public void bar() {" + PMD.EOL +
    129             "  x += 1;" + PMD.EOL +
    130             " }" + PMD.EOL +
    131             "}";
    132
    133     private static final String TEST12 =
    134             "public class Foo {" + PMD.EOL +
    135             " private int x = 0;" + PMD.EOL +
    136             " public void bar() {" + PMD.EOL +
    137             "  Object y = new Bar(x++);" + PMD.EOL +
    138             " }" + PMD.EOL +
    139             "}";
    140
    141     private static final String TEST13 =
    142             "public class Foo {" + PMD.EOL +
    143             " private int x;" + PMD.EOL +
    144             " public Foo() {" + PMD.EOL +
    145             "  try {" + PMD.EOL +
    146             "   x = 2;" + PMD.EOL +
    147             "  } catch (Exception e) {}" + PMD.EOL +
    148             " }" + PMD.EOL +
    149             "}";
    150
    151     private static final String TEST14 =
    152             "public class Foo {" + PMD.EOL +
    153             " private int x;" + PMD.EOL +
    154             " public void bar() {" + PMD.EOL +
    155             "  try {" + PMD.EOL +
    156             "   x = 2;" + PMD.EOL +
    157             "  } catch (Exception e) {}" + PMD.EOL +
    158             " }" + PMD.EOL +
    159             "}";
    160
    161     private static final String TEST15 =
    162             "public class Foo {" + PMD.EOL +
    163             " private int x;" + PMD.EOL +
    164             " public Foo() {" + PMD.EOL +
    165             "  for (int i=0; i<10; i++) { x += 5; }" + PMD.EOL +
    166             " }" + PMD.EOL +
    167             "}";
    168
    169     private static final String TEST16 =
    170             "public class Foo {" + PMD.EOL +
    171             " private int x = 2 ;" + PMD.EOL +
    172             " public Foo() {" + PMD.EOL +
    173             "   mouseListener = new MouseAdapter() {" + PMD.EOL +
    174             "      public void mouseClicked(MouseEvent e) {" + PMD.EOL +
    175             "        x = e.getSource();" + PMD.EOL +
    176             "        super.mouseClicked(e);" + PMD.EOL +
    177             "      }" + PMD.EOL +
    178             "    };" + PMD.EOL +
    179             " }" + PMD.EOL +
    180             "}";
    181
    182 }