    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package test.net.sourceforge.pmd.rules.junit;
    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 UnnecessaryBooleanAssertionTest extends SimpleAggregatorTst {
    13
    14      private Rule rule;
    15
    16      public void setUp() throws RuleSetNotFoundException {
    17          rule = findRule("junit", "UnnecessaryBooleanAssertion");
    18      }
    19
    20      public void testAll() {
    21          runTests(new TestDescriptor[]{
    22              new TestDescriptor(TEST1, "failure case", 1, rule),
    23              new TestDescriptor(TEST2, "variations", 4, rule),
    24              new TestDescriptor(TEST3, "nested boolean literal", 0, rule),
    25              new TestDescriptor(TEST4, "asserting true a !", 1, rule),
    26              new TestDescriptor(TEST5, "asserting false a !", 1, rule),
    27              new TestDescriptor(TEST6, "buz", 0, rule),
    28          });
    29      }
    30
    31      private static final String TEST1 =
    32              "public class Foo extends TestCase {" + PMD.EOL +
    33              " void bar() {" + PMD.EOL +
    34              "  assertTrue(true); " + PMD.EOL +
    35              " }" + PMD.EOL +
    36              "}";
    37
    38      private static final String TEST2 =
    39              "public class Foo extends TestCase {" + PMD.EOL +
    40              " void bar() {" + PMD.EOL +
    41              "  assertTrue(true); " + PMD.EOL +
    42              "  assertTrue(false); " + PMD.EOL +
    43              "  assertFalse(true); " + PMD.EOL +
    44              "  assertFalse(false); " + PMD.EOL +
    45              " }" + PMD.EOL +
    46              "}";
    47
    48      private static final String TEST3 =
    49              "public class Foo extends TestCase {" + PMD.EOL +
    50              " void bar() {" + PMD.EOL +
    51              "  assertTrue(foobar(fiddle, true)); " + PMD.EOL +
    52              " }" + PMD.EOL +
    53              "}";
    54
    55      private static final String TEST4 =
    56              "public class Foo extends TestCase {" + PMD.EOL +
    57              " void bar() {" + PMD.EOL +
    58              "  assertTrue(!foo); " + PMD.EOL +
    59              " }" + PMD.EOL +
    60              "}";
    61
    62
    63      private static final String TEST5 =
    64              "public class Foo extends TestCase {" + PMD.EOL +
    65              " void bar() {" + PMD.EOL +
    66              "  assertFalse(!foo); " + PMD.EOL +
    67              " }" + PMD.EOL +
    68              "}";
    69
    70      private static final String TEST6 =
    71              "public class Foo extends TestCase {" + PMD.EOL +
    72              " void bar() {" + PMD.EOL +
    73              "  assertTrue(!s.contains(new NameOccurrence(node, node.getImage()))); " + PMD.EOL +
    74              " }" + PMD.EOL +
    75              "}";
    76
    77  }