    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package test.net.sourceforge.pmd.rules.design;
    5
    6   import net.sourceforge.pmd.PMD;
    7   import net.sourceforge.pmd.Rule;
    8   import net.sourceforge.pmd.RuleSetNotFoundException;
    9   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    10  import test.net.sourceforge.pmd.testframework.TestDescriptor;
    11
    12  public class NullAssignmentRuleTest extends SimpleAggregatorTst {
    13
    14      private Rule rule;
    15
    16      public void setUp() throws RuleSetNotFoundException {
    17          rule = findRule("controversial", "NullAssignment");
    18      }
    19
    20      public void testAll() {
    21          runTests(new TestDescriptor[]{
    22              new TestDescriptor(TEST1, "initial assignment", 0, rule),
    23              new TestDescriptor(TEST2, "bad assignment", 1, rule),
    24              new TestDescriptor(TEST3, "check test", 0, rule),
    25              new TestDescriptor(TEST4, "null param on right hand sidel", 0, rule),
    26              new TestDescriptor(TEST5, "null assignment in ternary", 1, rule),
    27              new TestDescriptor(TEST6, "null assignment in ternary, part deux", 1, rule),
    28              new TestDescriptor(TEST7, "comparison is not assignment", 0, rule),
    29              new TestDescriptor(TEST8, "final fields must be assigned", 0, rule),
    30          });
    31      }
    32
    33      private static final String TEST1 =
    34              "public class Foo {" + PMD.EOL +
    35              " public Object foo() {" + PMD.EOL +
    36              "  Object x = null; // OK" + PMD.EOL +
    37              "  return x;" + PMD.EOL +
    38              " }       " + PMD.EOL +
    39              "}";
    40
    41      private static final String TEST2 =
    42              "public class Foo {" + PMD.EOL +
    43              " public void foo() {" + PMD.EOL +
    44              "  Object x;" + PMD.EOL +
    45              "  x = new Object();" + PMD.EOL +
    46              "  bar(x);  " + PMD.EOL +
    47              "  x = null; // This is bad" + PMD.EOL +
    48              " }" + PMD.EOL +
    49              "}";
    50
    51      private static final String TEST3 =
    52              "public class Foo {" + PMD.EOL +
    53              " public void foo() {" + PMD.EOL +
    54              "  Object x;" + PMD.EOL +
    55              "  if (x == null) { // This is OK" + PMD.EOL +
    56              "   return;" + PMD.EOL +
    57              "  }" + PMD.EOL +
    58              " }" + PMD.EOL +
    59              "}";
    60
    61      private static final String TEST4 =
    62              "public class Foo {" + PMD.EOL +
    63              " public void foo() {" + PMD.EOL +
    64              "  String x = null;" + PMD.EOL +
    65              "  x = new String(null);" + PMD.EOL +
    66              " }" + PMD.EOL +
    67              "}";
    68
    69      private static final String TEST5 =
    70              "public class Foo {" + PMD.EOL +
    71              " public void foo() {" + PMD.EOL +
    72              "  String x = bar() ? \"fiz\" : null;" + PMD.EOL +
    73              " }" + PMD.EOL +
    74              "}";
    75
    76      private static final String TEST6 =
    77              "public class Foo {" + PMD.EOL +
    78              " public void foo() {" + PMD.EOL +
    79              "  String x = bar() ? null : \"fiz\";" + PMD.EOL +
    80              " }" + PMD.EOL +
    81              "}";
    82
    83      private static final String TEST7 =
    84              "public class Foo {" + PMD.EOL +
    85              " public String foo() {" + PMD.EOL +
    86              "  return x == null ? \"42\" : x;" + PMD.EOL +
    87              " }" + PMD.EOL +
    88              "}";
    89
    90      private static final String TEST8 =
    91              "public class Foo {" + PMD.EOL +
    92              " private final String x;" + PMD.EOL +
    93              " public Foo(String y) {" + PMD.EOL +
    94              "  if (y == \"\") x = null;" + PMD.EOL +
    95              " }" + PMD.EOL +
    96              "}";
    97
    98  }