    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 net.sourceforge.pmd.RuleSetNotFoundException;
    9   import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
    10  import test.net.sourceforge.pmd.testframework.TestDescriptor;
    11
    12  public class BooleanInstantiationRuleTest extends SimpleAggregatorTst {
    13
    14      private Rule rule;
    15
    16      public void setUp() throws RuleSetNotFoundException {
    17          rule = findRule("basic", "BooleanInstantiation");
    18      }
    19
    20      public void testAll() {
    21          runTests(new TestDescriptor[]{
    22              new TestDescriptor(TEST1, "simple failure case", 1, rule),
    23              new TestDescriptor(TEST2, "new java.lang.Boolean", 1, rule),
    24              new TestDescriptor(TEST3, "ok", 0, rule),
    25              new TestDescriptor(TEST4, "don't use Boolean.valueOf() with literal", 2, rule),
    26              new TestDescriptor(TEST5, "valueOf() with variable is fine", 0, rule),
    27          });
    28      }
    29
    30      private static final String TEST1 =
    31              "public class Foo {" + PMD.EOL +
    32              " Boolean b = new Boolean(\"true\");" + PMD.EOL +
    33              "}";
    34
    35      private static final String TEST2 =
    36              "public class Foo {" + PMD.EOL +
    37              " Boolean b = new java.lang.Boolean(\"true\");" + PMD.EOL +
    38              "}";
    39
    40      private static final String TEST3 =
    41              "public class Foo {" + PMD.EOL +
    42              " Boolean b = Boolean.TRUE;" + PMD.EOL +
    43              "}";
    44
    45      private static final String TEST4 =
    46              "public class Foo {" + PMD.EOL +
    47              " Boolean b = Boolean.valueOf(true);" + PMD.EOL +
    48              " Boolean b1 = Boolean.valueOf(false);" + PMD.EOL +
    49              "}";
    50
    51      private static final String TEST5 =
    52              "public class Foo {" + PMD.EOL +
    53              " Boolean b = Boolean.valueOf(x);" + PMD.EOL +
    54              "}";
    55
    56  }